SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rle_queue.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_RLE_QUEUE_HPP__
27 #define SHORE_CONTAINER_RLE_QUEUE_HPP__
28 
29 #include <algorithm>
30 #include <deque>
31 
32 namespace shore {
33 
35 template<typename T>
36 class rle_queue
37 {
38  public:
39 
40  typedef T value_type;
41 
42  private:
43 
45  struct compare
46  {
47  bool operator()(const size_t ofs,
48  const std::pair<size_t,value_type> &p)
49  {
50  return ofs<p.first;
51  }
52  bool operator()(const std::pair<size_t,value_type> &p,
53  const size_t ofs)
54  {
55  return p.first<ofs;
56  }
57  bool operator()(const std::pair<size_t,value_type> &p1,
58  const std::pair<size_t,value_type> &p2)
59  {
60  return p1.first<p2.first;
61  }
62  };
63 
65  std::deque<std::pair<size_t,value_type> > m_endofs_and_values;
66 
68  uint64_t m_off_offset;
69 
71  compare m_cmp;
72 
73  public:
74 
77  :m_off_offset(0)
78  {}
79 
81  size_t size() const
82  {
83  return empty()?0:(m_endofs_and_values.back().first-m_off_offset);
84  }
85 
87  bool empty() const
88  {
89  return m_endofs_and_values.empty();
90  }
91 
93  void clear()
94  {
95  m_endofs_and_values.clear();
96  m_off_offset=0;
97  }
98 
102  const value_type &operator[](const size_t idx) const
103  {
104  const std::deque<size_t>::const_iterator ub=
105  std::upper_bound(m_endofs_and_values.begin(),m_endofs_and_values.end(),
106  idx+m_off_offset,m_cmp);
107 
108  return ub->second;
109  }
110 
112  const value_type &front() const
113  {
114  return m_endofs_and_values.front().second;
115  }
116 
118  const value_type &back() const
119  {
120  return m_endofs_and_values.back().second;
121  }
122 
124  void push_back(const value_type &v)
125  {
126  if(empty())
127  {
128  m_endofs_and_values.push_back(std::make_pair(1,v));
129  }
130  else if(m_endofs_and_values.back()==v)
131  {
132  m_endofs_and_values.back().first+=1;
133  }
134  else
135  {
136  m_endofs_and_values.push_back(std::make_pair(m_endofs_and_values.back().first+1,v));
137  }
138  }
139 
141  void pop_front()
142  {
143  ++m_off_offset;
144  if(m_off_offset==m_endofs_and_values.front().first)
145  {
146  m_endofs_and_values.pop_front();
147  if(empty())
148  m_off_offset=0;
149  }
150  }
151 
153  void pop_back()
154  {
155  --m_endofs_and_values.back().first;
156  if(m_endofs_and_values.size()==1)
157  {
158  if(m_endofs_and_values.back().first==m_off_offset)
159  clear();
160  }
161  else
162  {
163  if(m_endofs_and_values.back().first
164  ==m_endofs_and_values[m_endofs_and_values.size()-2].first)
165  {
166  m_endofs_and_values.pop_back();
167  }
168  }
169  }
170 };
171 
172 } // namespace
173 
174 #endif // SHORE_CONTAINER_RLE_QUEUE_HPP__
175