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