SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
frag_vector.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
25 
26 #ifndef SHORE_CONTAINER_FRAG_VECTOR_HPP__
27 #define SHORE_CONTAINER_FRAG_VECTOR_HPP__
28 
29 #include <algorithm>
30 #include <iterator>
31 #include <vector>
32 
33 #include <boost/iterator/iterator_facade.hpp>
34 
35 namespace shore {
36 
38 template<typename Iterator>
40 {
41  public:
42 
43  typedef typename std::iterator_traits<Iterator>::value_type value_type;
44  typedef typename std::iterator_traits<Iterator>::reference reference;
45  typedef typename std::iterator_traits<Iterator>::pointer pointer;
46 
47  private:
48 
49  std::vector<Iterator> m_begins;
50  std::vector<Iterator> m_ends;
51  std::vector<size_t> m_begofs;
52  std::vector<size_t> m_endofs;
53  size_t m_size;
54 
55  public:
56 
59  :m_size(0)
60  {}
61 
66  void add_fragment(Iterator beg,Iterator end)
67  {
68  m_begins.push_back(beg);
69  m_ends.push_back(end);
70  m_begofs.push_back(m_size);
71  m_size+=(end-beg);
72  m_endofs.push_back(m_size);
73  }
74 
76  size_t size() const
77  {
78  return m_size;
79  }
80 
82  bool empty() const
83  {
84  return m_size==0;
85  }
86 
88  void clear()
89  {
90  m_begins.clear();
91  m_ends.clear();
92  m_begofs.clear();
93  m_endofs.clear();
94  m_size=0;
95  }
96 
100  reference operator[](const size_t idx)
101  {
102  const std::vector<size_t>::const_iterator ub=
103  std::upper_bound(m_endofs.begin(),m_endofs.end(),idx);
104 
105  const size_t blocknum=ub-m_endofs.begin();
106  return m_begins[blocknum][idx-m_begofs[blocknum]];
107  }
108 
110  reference front()
111  {
112  return *m_begins.front();
113  }
114 
116  reference back()
117  {
118  return (*(m_ends.back()-1));
119  }
120 };
121 
122 } // namespace
123 
124 #endif // SHORE_CONTAINER_FRAG_VECTOR_HPP__
125