CMS 3D CMS Logo

bqueue.h
Go to the documentation of this file.
1 #ifndef CMSUTILS_BEUEUE_H
2 #define CMSUTILS_BEUEUE_H
3 #include <boost/intrusive_ptr.hpp>
4 #include <cassert>
5 
36 namespace cmsutils {
37 
38  template <class T>
39  class _bqueue_itr;
40  template <class T>
41  class bqueue;
42  template <class T>
43  class _bqueue_item;
44  template <class T>
46  template <class T>
48 
49  template <class T>
50  class _bqueue_item {
51  friend class bqueue<T>;
52  friend class _bqueue_itr<T>;
53  friend void intrusive_ptr_add_ref<T>(_bqueue_item<T> *it);
54  friend void intrusive_ptr_release<T>(_bqueue_item<T> *it);
55  void addRef() { ++refCount; }
56  void delRef() {
57  if ((--refCount) == 0)
58  delete this;
59  }
60 
61  private:
62  _bqueue_item() : back(0), value(), refCount(0) {}
63  _bqueue_item(boost::intrusive_ptr<_bqueue_item<T> > tail, const T &val) : back(tail), value(val), refCount(0) {}
64  // move
65  _bqueue_item(boost::intrusive_ptr<_bqueue_item<T> > tail, T &&val)
66  : back(tail), value(std::move(val)), refCount(0) {}
67  // emplace
68  template <typename... Args>
69  _bqueue_item(boost::intrusive_ptr<_bqueue_item<T> > tail, Args &&...args)
70  : back(tail), value(std::forward<Args>(args)...), refCount(0) {}
71  boost::intrusive_ptr<_bqueue_item<T> > back;
72  T const value;
73  unsigned int refCount;
74  };
75 
76  template <class T>
78  it->addRef();
79  }
80  template <class T>
82  it->delRef();
83  }
84  //inline void intrusive_ptr_add_ref(const _bqueue_item *it) { it->addRef(); }
85  //inline void intrusive_ptr_release(const _bqueue_item *it) { it->delRef(); }
86 
87  template <class T>
88  class _bqueue_itr {
89  public:
90  // T* operator->() { return &it->value; }
91  // T& operator*() { return it->value; }
92  const T *operator->() const { return &it->value; }
93  const T &operator*() const { return it->value; }
95  it = it->back.get();
96  return *this;
97  }
99  it = it->back.get();
100  return *this;
101  }
102  const _bqueue_itr<T> &operator--() const {
103  it = it->back.get();
104  return *this;
105  }
106  bool operator==(const _bqueue_itr<T> &t2) const { return t2.it == it; }
107  bool operator!=(const _bqueue_itr<T> &t2) const { return t2.it != it; }
108  // so that I can assign a const_iterator to a const_iterator
110  it = t2.it;
111  return *this;
112  }
113  friend class bqueue<T>;
114 
115  private:
116  // _bqueue_itr(_bqueue_item<T> *t) : it(t) { }
119  };
120 
121  template <class T>
122  class bqueue {
123  public:
124  typedef T value_type;
125  typedef unsigned short int size_type;
127  typedef boost::intrusive_ptr<_bqueue_item<value_type> > itemptr;
130 
131  bqueue() : m_size(0), m_head(), m_tail() {}
132  ~bqueue() {}
133 
135 
136  // move
137  bqueue(bqueue<T> &&cp) noexcept : m_size(cp.m_size), m_head(std::move(cp.m_head)), m_tail(std::move(cp.m_tail)) {
138  cp.m_size = 0;
139  }
140 
141  bqueue &operator=(bqueue<T> const &) = default;
142  bqueue &operator=(bqueue<T> &&cp) noexcept {
143  using std::swap;
144  swap(m_size, cp.m_size);
145  swap(m_head, cp.m_head);
146  swap(m_tail, cp.m_tail);
147  return *this;
148  }
149 
150  void swap(bqueue<T> &cp) {
151  using std::swap;
152  swap(m_size, cp.m_size);
153  swap(m_head, cp.m_head);
154  swap(m_tail, cp.m_tail);
155  }
156 
157  bqueue<T> fork() const { return *this; }
158 
159  // copy
160  void push_back(const T &val) {
161  m_tail = itemptr(new item(this->m_tail, val));
162  if ((++m_size) == 1) {
163  m_head = m_tail;
164  };
165  }
166 
167  //move
168  void push_back(T &&val) {
169  m_tail = itemptr(new item(this->m_tail, std::forward<T>(val)));
170  if ((++m_size) == 1) {
171  m_head = m_tail;
172  };
173  }
174 
175  // emplace
176  template <typename... Args>
177  void emplace_back(Args &&...args) {
178  m_tail = itemptr(new item(this->m_tail, std::forward<Args>(args)...));
179  if ((++m_size) == 1) {
180  m_head = m_tail;
181  };
182  }
183 
184  void pop_back() {
185  assert(m_size > 0);
186  --m_size;
187  m_tail = m_tail->back;
188  if (m_size == 0)
189  m_head = nullptr;
190  }
191 
192  // T & front() { return m_head->value; }
193  const T &front() const { return m_head->value; }
194  //vT & back() { return m_tail->value; }
195  const T &back() const { return m_tail->value; }
196  // iterator rbegin() { return m_tail.get(); }
197  const_iterator rbegin() const { return m_tail.get(); }
198  const_iterator rend() const { return nullptr; }
199  const_iterator begin() const { return m_tail.get(); }
200  const_iterator end() const { return nullptr; }
201  size_type size() const { return m_size; }
202  bool empty() const { return m_size == 0; }
203  const T &operator[](size_type i) const {
204  int idx = m_size - i - 1;
205  const_iterator it = rbegin();
206  while (idx-- > 0)
207  --it;
208  return *it;
209  }
210 
211  bool shared() {
212  // size = 0: never shared
213  // size = 1: shared if head->refCount > 2 (m_head and m_tail)
214  // size > 1: shared if head->refCount > 2 (m_head and second_hit->back)
215  return (m_size > 0) && (m_head->refCount > 2);
216  }
217 
218  // connect 'other' at the tail of this. will reset 'other' to an empty sequence
219  // other better not to be shared!
221  assert(!other.shared());
222  using std::swap;
223  if (m_size == 0) {
224  swap(m_head, other.m_head);
225  swap(m_tail, other.m_tail);
226  swap(m_size, other.m_size);
227  } else {
228  other.m_head->back = this->m_tail;
229  m_tail = other.m_tail;
230  m_size += other.m_size;
231  other.clear();
232  }
233  }
234 
235  void clear() {
236  m_head = m_tail = nullptr;
237  m_size = 0;
238  }
239 
240  private:
243  };
244 
245  template <typename T>
246  void swap(bqueue<T> &rh, bqueue<T> &lh) {
247  rh.swap(lh);
248  }
249 
250 } // namespace cmsutils
251 
252 #endif
bqueue< T > fork() const
Definition: bqueue.h:157
bqueue & operator=(bqueue< T > &&cp) noexcept
Definition: bqueue.h:142
const_iterator rbegin() const
Definition: bqueue.h:197
_bqueue_itr< value_type > iterator
Definition: bqueue.h:128
const T & back() const
Definition: bqueue.h:195
void swap(bqueue< T > &cp)
Definition: bqueue.h:150
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > tail, const T &val)
Definition: bqueue.h:63
bqueue & operator=(bqueue< T > const &)=default
_bqueue_itr< value_type > const_iterator
Definition: bqueue.h:129
bqueue(bqueue< T > &&cp) noexcept
Definition: bqueue.h:137
unsigned short int size_type
Definition: bqueue.h:125
_bqueue_itr< T > & operator++()
Definition: bqueue.h:98
const T & front() const
Definition: bqueue.h:193
void swap(bqueue< T > &rh, bqueue< T > &lh)
Definition: bqueue.h:246
bool int lh
Definition: SIMDVec.h:20
bool operator==(const _bqueue_itr< T > &t2) const
Definition: bqueue.h:106
void emplace_back(Args &&...args)
Definition: bqueue.h:177
const_iterator end() const
Definition: bqueue.h:200
assert(be >=bs)
itemptr m_head
Definition: bqueue.h:242
_bqueue_item< T > const * it
Definition: bqueue.h:118
_bqueue_itr< T > & operator--()
Definition: bqueue.h:94
const_iterator begin() const
Definition: bqueue.h:199
_bqueue_item< value_type > item
Definition: bqueue.h:126
bqueue(const bqueue< T > &cp)
Definition: bqueue.h:134
const T & operator*() const
Definition: bqueue.h:93
bool shared()
Definition: bqueue.h:211
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > tail, T &&val)
Definition: bqueue.h:65
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > tail, Args &&...args)
Definition: bqueue.h:69
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
boost::intrusive_ptr< _bqueue_item< value_type > > itemptr
Definition: bqueue.h:127
_bqueue_itr(const _bqueue_item< T > *t)
Definition: bqueue.h:117
itemptr m_tail
Definition: bqueue.h:242
void clear()
Definition: bqueue.h:235
Definition: value.py:1
void intrusive_ptr_add_ref(_bqueue_item< T > *it)
Definition: bqueue.h:77
unsigned int refCount
Definition: bqueue.h:73
void intrusive_ptr_release(_bqueue_item< T > *it)
Definition: bqueue.h:81
const T & operator[](size_type i) const
Definition: bqueue.h:203
boost::intrusive_ptr< _bqueue_item< T > > back
Definition: bqueue.h:71
size_type size() const
Definition: bqueue.h:201
const T * operator->() const
Definition: bqueue.h:92
bool empty() const
Definition: bqueue.h:202
size_type m_size
Definition: bqueue.h:241
void pop_back()
Definition: bqueue.h:184
const_iterator rend() const
Definition: bqueue.h:198
void join(bqueue< T > &other)
Definition: bqueue.h:220
const _bqueue_itr< T > & operator=(const _bqueue_itr< T > &t2)
Definition: bqueue.h:109
void push_back(T &&val)
Definition: bqueue.h:168
const _bqueue_itr< T > & operator--() const
Definition: bqueue.h:102
long double T
def move(src, dest)
Definition: eostools.py:511
bool operator!=(const _bqueue_itr< T > &t2) const
Definition: bqueue.h:107
void push_back(const T &val)
Definition: bqueue.h:160