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
writedatasetfile.args
args
Definition: writedatasetfile.py:18
RandomServiceHelper.t2
t2
Definition: RandomServiceHelper.py:257
cmsutils::bqueue::m_size
size_type m_size
Definition: bqueue.h:241
mps_fire.i
i
Definition: mps_fire.py:428
cmsutils::bqueue::bqueue
bqueue()
Definition: bqueue.h:131
cmsutils::_bqueue_itr::operator++
_bqueue_itr< T > & operator++()
Definition: bqueue.h:98
cmsutils::bqueue::push_back
void push_back(T &&val)
Definition: bqueue.h:168
cmsutils::_bqueue_item::back
boost::intrusive_ptr< _bqueue_item< T > > back
Definition: bqueue.h:71
cmsutils
Definition: bqueue.h:36
cmsutils::bqueue::bqueue
bqueue(bqueue< T > &&cp) noexcept
Definition: bqueue.h:137
cmsutils::bqueue::swap
void swap(bqueue< T > &cp)
Definition: bqueue.h:150
cmsutils::_bqueue_item
Definition: bqueue.h:43
cmsutils::bqueue::iterator
_bqueue_itr< value_type > iterator
Definition: bqueue.h:128
cmsutils::_bqueue_item::delRef
void delRef()
Definition: bqueue.h:56
cms::cuda::assert
assert(be >=bs)
mathSSE::lh
bool int lh
Definition: SIMDVec.h:20
hgcal_conditions::parameters
Definition: HGCConditions.h:86
cmsutils::_bqueue_item::refCount
unsigned int refCount
Definition: bqueue.h:73
cmsutils::_bqueue_item::addRef
void addRef()
Definition: bqueue.h:55
cmsutils::_bqueue_item::_bqueue_item
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > tail, T &&val)
Definition: bqueue.h:65
cmsutils::bqueue::itemptr
boost::intrusive_ptr< _bqueue_item< value_type > > itemptr
Definition: bqueue.h:127
cmsutils::bqueue::operator=
bqueue & operator=(bqueue< T > &&cp) noexcept
Definition: bqueue.h:142
cmsutils::bqueue::operator[]
const T & operator[](size_type i) const
Definition: bqueue.h:203
cmsutils::_bqueue_item::_bqueue_item
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > tail, Args &&...args)
Definition: bqueue.h:69
cmsutils::bqueue::clear
void clear()
Definition: bqueue.h:235
cmsutils::bqueue::fork
bqueue< T > fork() const
Definition: bqueue.h:157
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
cmsutils::bqueue::back
const T & back() const
Definition: bqueue.h:195
cmsutils::_bqueue_itr::it
const _bqueue_item< T > * it
Definition: bqueue.h:118
cmsutils::bqueue::begin
const_iterator begin() const
Definition: bqueue.h:199
cmsutils::bqueue::size
size_type size() const
Definition: bqueue.h:201
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
cmsutils::intrusive_ptr_add_ref
void intrusive_ptr_add_ref(_bqueue_item< T > *it)
Definition: bqueue.h:77
trackingPlots.other
other
Definition: trackingPlots.py:1464
cmsutils::bqueue
Definition: bqueue.h:41
cmsutils::bqueue::front
const T & front() const
Definition: bqueue.h:193
cmsutils::bqueue::rbegin
const_iterator rbegin() const
Definition: bqueue.h:197
cmsutils::bqueue::shared
bool shared()
Definition: bqueue.h:211
cmsutils::bqueue::~bqueue
~bqueue()
Definition: bqueue.h:132
cmsutils::bqueue::item
_bqueue_item< value_type > item
Definition: bqueue.h:126
cmsutils::bqueue::size_type
unsigned short int size_type
Definition: bqueue.h:125
cmsutils::bqueue::join
void join(bqueue< T > &other)
Definition: bqueue.h:220
cmsutils::_bqueue_itr
Definition: bqueue.h:39
cmsutils::bqueue::empty
bool empty() const
Definition: bqueue.h:202
cmsutils::_bqueue_item::_bqueue_item
_bqueue_item(boost::intrusive_ptr< _bqueue_item< T > > tail, const T &val)
Definition: bqueue.h:63
cmsutils::bqueue::push_back
void push_back(const T &val)
Definition: bqueue.h:160
value
Definition: value.py:1
cmsutils::bqueue::value_type
T value_type
Definition: bqueue.h:124
cmsutils::_bqueue_itr::operator==
bool operator==(const _bqueue_itr< T > &t2) const
Definition: bqueue.h:106
cmsutils::intrusive_ptr_release
void intrusive_ptr_release(_bqueue_item< T > *it)
Definition: bqueue.h:81
cmsutils::swap
void swap(bqueue< T > &rh, bqueue< T > &lh)
Definition: bqueue.h:246
cmsutils::bqueue::pop_back
void pop_back()
Definition: bqueue.h:184
cmsutils::bqueue::emplace_back
void emplace_back(Args &&...args)
Definition: bqueue.h:177
heppy_batch.val
val
Definition: heppy_batch.py:351
eostools.move
def move(src, dest)
Definition: eostools.py:511
cmsutils::bqueue::operator=
bqueue & operator=(bqueue< T > const &)=default
std
Definition: JetResolutionObject.h:76
cmsutils::_bqueue_itr::operator--
const _bqueue_itr< T > & operator--() const
Definition: bqueue.h:102
T
long double T
Definition: Basic3DVectorLD.h:48
cmsutils::bqueue::m_tail
itemptr m_tail
Definition: bqueue.h:242
cmsutils::bqueue::rend
const_iterator rend() const
Definition: bqueue.h:198
cmsutils::_bqueue_item::_bqueue_item
_bqueue_item()
Definition: bqueue.h:62
cmsutils::_bqueue_itr::operator!=
bool operator!=(const _bqueue_itr< T > &t2) const
Definition: bqueue.h:107
cmsutils::_bqueue_itr::operator*
const T & operator*() const
Definition: bqueue.h:93
cmsutils::_bqueue_itr::operator--
_bqueue_itr< T > & operator--()
Definition: bqueue.h:94
cmsutils::_bqueue_itr::operator->
const T * operator->() const
Definition: bqueue.h:92
cmsutils::_bqueue_itr::_bqueue_itr
_bqueue_itr(const _bqueue_item< T > *t)
Definition: bqueue.h:117
cmsutils::bqueue::bqueue
bqueue(const bqueue< T > &cp)
Definition: bqueue.h:134
cmsutils::_bqueue_item::value
const T value
Definition: bqueue.h:72
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
cmsutils::bqueue::m_head
itemptr m_head
Definition: bqueue.h:242
cmsutils::bqueue::end
const_iterator end() const
Definition: bqueue.h:200
cmsutils::bqueue::const_iterator
_bqueue_itr< value_type > const_iterator
Definition: bqueue.h:129
cmsutils::_bqueue_itr::operator=
const _bqueue_itr< T > & operator=(const _bqueue_itr< T > &t2)
Definition: bqueue.h:109