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  _bqueue_itr(const _bqueue_itr<T> &t2) = default;
114  friend class bqueue<T>;
115 
116  private:
117  // _bqueue_itr(_bqueue_item<T> *t) : it(t) { }
120  };
121 
122  template <class T>
123  class bqueue {
124  public:
125  typedef T value_type;
126  typedef unsigned short int size_type;
128  typedef boost::intrusive_ptr<_bqueue_item<value_type> > itemptr;
131 
132  bqueue() : m_size(0), m_head(), m_tail() {}
133  ~bqueue() {}
134 
136 
137  // move
138  bqueue(bqueue<T> &&cp) noexcept : m_size(cp.m_size), m_head(std::move(cp.m_head)), m_tail(std::move(cp.m_tail)) {
139  cp.m_size = 0;
140  }
141 
142  bqueue &operator=(bqueue<T> const &) = default;
143  bqueue &operator=(bqueue<T> &&cp) noexcept {
144  using std::swap;
145  swap(m_size, cp.m_size);
146  swap(m_head, cp.m_head);
147  swap(m_tail, cp.m_tail);
148  return *this;
149  }
150 
151  void swap(bqueue<T> &cp) {
152  using std::swap;
153  swap(m_size, cp.m_size);
154  swap(m_head, cp.m_head);
155  swap(m_tail, cp.m_tail);
156  }
157 
158  bqueue<T> fork() const { return *this; }
159 
160  // copy
161  void push_back(const T &val) {
162  m_tail = itemptr(new item(this->m_tail, val));
163  if ((++m_size) == 1) {
164  m_head = m_tail;
165  };
166  }
167 
168  //move
169  void push_back(T &&val) {
170  m_tail = itemptr(new item(this->m_tail, std::forward<T>(val)));
171  if ((++m_size) == 1) {
172  m_head = m_tail;
173  };
174  }
175 
176  // emplace
177  template <typename... Args>
178  void emplace_back(Args &&...args) {
179  m_tail = itemptr(new item(this->m_tail, std::forward<Args>(args)...));
180  if ((++m_size) == 1) {
181  m_head = m_tail;
182  };
183  }
184 
185  void pop_back() {
186  assert(m_size > 0);
187  --m_size;
188  m_tail = m_tail->back;
189  if (m_size == 0)
190  m_head = nullptr;
191  }
192 
193  // T & front() { return m_head->value; }
194  const T &front() const { return m_head->value; }
195  //vT & back() { return m_tail->value; }
196  const T &back() const { return m_tail->value; }
197  // iterator rbegin() { return m_tail.get(); }
198  const_iterator rbegin() const { return m_tail.get(); }
199  const_iterator rend() const { return nullptr; }
200  const_iterator begin() const { return m_tail.get(); }
201  const_iterator end() const { return nullptr; }
202  size_type size() const { return m_size; }
203  bool empty() const { return m_size == 0; }
204  const T &operator[](size_type i) const {
205  int idx = m_size - i - 1;
207  while (idx-- > 0)
208  --it;
209  return *it;
210  }
211 
212  bool shared() {
213  // size = 0: never shared
214  // size = 1: shared if head->refCount > 2 (m_head and m_tail)
215  // size > 1: shared if head->refCount > 2 (m_head and second_hit->back)
216  return (m_size > 0) && (m_head->refCount > 2);
217  }
218 
219  // connect 'other' at the tail of this. will reset 'other' to an empty sequence
220  // other better not to be shared!
222  assert(!other.shared());
223  using std::swap;
224  if (m_size == 0) {
225  swap(m_head, other.m_head);
226  swap(m_tail, other.m_tail);
227  swap(m_size, other.m_size);
228  } else {
229  other.m_head->back = this->m_tail;
230  m_tail = other.m_tail;
231  m_size += other.m_size;
232  other.clear();
233  }
234  }
235 
236  void clear() {
237  m_head = m_tail = nullptr;
238  m_size = 0;
239  }
240 
241  private:
244  };
245 
246  template <typename T>
247  void swap(bqueue<T> &rh, bqueue<T> &lh) {
248  rh.swap(lh);
249  }
250 
251 } // namespace cmsutils
252 
253 #endif
bqueue< T > fork() const
Definition: bqueue.h:158
bqueue & operator=(bqueue< T > &&cp) noexcept
Definition: bqueue.h:143
const_iterator rbegin() const
Definition: bqueue.h:198
_bqueue_itr< value_type > iterator
Definition: bqueue.h:129
const T & back() const
Definition: bqueue.h:196
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > &tail, const T &val)
Definition: bqueue.h:63
void swap(bqueue< T > &cp)
Definition: bqueue.h:151
bqueue & operator=(bqueue< T > const &)=default
_bqueue_itr< value_type > const_iterator
Definition: bqueue.h:130
bqueue(bqueue< T > &&cp) noexcept
Definition: bqueue.h:138
unsigned short int size_type
Definition: bqueue.h:126
_bqueue_itr< T > & operator++()
Definition: bqueue.h:98
const T & front() const
Definition: bqueue.h:194
void swap(bqueue< T > &rh, bqueue< T > &lh)
Definition: bqueue.h:247
bool int lh
Definition: SIMDVec.h:27
bool operator==(const _bqueue_itr< T > &t2) const
Definition: bqueue.h:106
void emplace_back(Args &&...args)
Definition: bqueue.h:178
const_iterator end() const
Definition: bqueue.h:201
assert(be >=bs)
itemptr m_head
Definition: bqueue.h:243
_bqueue_item< T > const * it
Definition: bqueue.h:119
_bqueue_itr< T > & operator--()
Definition: bqueue.h:94
const_iterator begin() const
Definition: bqueue.h:200
_bqueue_item< value_type > item
Definition: bqueue.h:127
bqueue(const bqueue< T > &cp)
Definition: bqueue.h:135
const T & operator*() const
Definition: bqueue.h:93
bool shared()
Definition: bqueue.h:212
boost::intrusive_ptr< _bqueue_item< value_type > > itemptr
Definition: bqueue.h:128
_bqueue_itr(const _bqueue_item< T > *t)
Definition: bqueue.h:118
itemptr m_tail
Definition: bqueue.h:243
void clear()
Definition: bqueue.h:236
Definition: value.py:1
void intrusive_ptr_add_ref(_bqueue_item< T > *it)
Definition: bqueue.h:77
unsigned int refCount
Definition: bqueue.h:73
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > &tail, Args &&...args)
Definition: bqueue.h:69
void intrusive_ptr_release(_bqueue_item< T > *it)
Definition: bqueue.h:81
const T & operator[](size_type i) const
Definition: bqueue.h:204
boost::intrusive_ptr< _bqueue_item< T > > back
Definition: bqueue.h:71
size_type size() const
Definition: bqueue.h:202
const T * operator->() const
Definition: bqueue.h:92
bool empty() const
Definition: bqueue.h:203
size_type m_size
Definition: bqueue.h:242
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > &tail, T &&val)
Definition: bqueue.h:65
void pop_back()
Definition: bqueue.h:185
const_iterator rend() const
Definition: bqueue.h:199
void join(bqueue< T > &other)
Definition: bqueue.h:221
const _bqueue_itr< T > & operator=(const _bqueue_itr< T > &t2)
Definition: bqueue.h:109
void push_back(T &&val)
Definition: bqueue.h:169
const _bqueue_itr< T > & operator--() const
Definition: bqueue.h:102
_bqueue_itr(const _bqueue_itr< T > &t2)=default
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:161