ATLAS Offline Software
pointer_list.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 */
14 #ifndef CXXUTILS_POINTER_LIST_H
15 #define CXXUTILS_POINTER_LIST_H
16 
17 
18 #include <type_traits>
19 #include <iterator>
20 
21 
22 namespace CxxUtils {
23 
24 
55 {
56 public:
65  struct list_block
66  {
67  public:
68  typedef unsigned long ulong;
69 
71  typedef void* value_type;
72 
76 
79  static size_t size (size_t nelt);
80  };
81 
82 
94  class allocator
95  {
96  public:
106  allocator (size_t nelt,
107  size_t nblock,
108  unsigned long end_mask,
109  unsigned long end_offs);
110 
112  ~allocator();
113 
116 
119  size_t nelt() const;
120 
122  size_t nchunks() const;
123 
125  bool at_end (const void* p) const;
126 
127 
128  private:
130  void refill();
131 
133  struct chunk
134  {
137 
140  };
141 
143  size_t m_nelt;
144 
146  size_t m_nblock;
147 
150 
152  size_t m_nthis;
153 
155  size_t m_nchunks;
156 
158  unsigned long m_end_mask;
159 
161  unsigned long m_end_offs;
162  };
163 
164 
167 
170 
173 
176 
178  size_t size() const;
179 
183  void clear();
184 
186  bool empty() const;
187 
188 
189 protected:
192 
195 
197  size_t m_size;
198 
201 
203  void firstblock ();
204 
207  void nextblock ();
208 
210  list_block* getblock();
211 };
212 
213 
239 template <size_t NELT = 15>
241  : public pointer_list_base
242 {
243 public:
246 
247 
254  class allocator
256  {
257  public:
259  static_assert (((NELT+1) & NELT) == 0);
260 
262  static const unsigned long END_OFFS = NELT * sizeof(value_type);
263  static const unsigned long END_MASK = END_OFFS | (sizeof(value_type)-1);
264 
269  allocator (size_t nblock = 100);
270 
271 
273  static bool at_end_static (const void* p);
274  };
275 
276 
280  class iterator
281  {
282  public:
283  using iterator_category = std::forward_iterator_tag;
285  using difference_type = std::ptrdiff_t;
286  using pointer = value_type*;
288 
290  bool operator== (const iterator& other) const;
291 
293  bool operator!= (const iterator& other) const;
294 
297 
300 
303 
304 
305  private:
308 
311 
312  friend class pointer_list;
313  };
314 
316 
319 
322 
325 
327  void erase (iterator it);
328 };
329 
330 
331 } // namespace CxxUtils
332 
333 
334 #include "CxxUtils/pointer_list.icc"
335 
336 
337 #endif // not CXXUTILS_POINTER_LIST_H
CxxUtils::pointer_list_base::m_head
list_block * m_head
The first block in the list.
Definition: pointer_list.h:191
CxxUtils::pointer_list_base::allocator::m_nelt
size_t m_nelt
Number of elements per block (excluding the end-pointer).
Definition: pointer_list.h:143
CxxUtils::pointer_list::iterator::operator*
reference operator*()
Dereference.
CxxUtils::pointer_list_base::clear
void clear()
Erase the container.
Definition: pointer_list.cxx:89
CxxUtils::pointer_list_base::list_block::m_data
value_type m_data[1]
The elements.
Definition: pointer_list.h:75
CxxUtils::pointer_list::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: pointer_list.h:283
CxxUtils::pointer_list_base::list_block
A single block in the list.
Definition: pointer_list.h:66
CxxUtils::pointer_list_base::allocator::m_end_mask
unsigned long m_end_mask
Mask for testing for an end pointer.
Definition: pointer_list.h:158
CxxUtils::pointer_list::iterator::operator==
bool operator==(const iterator &other) const
Equality comparison.
CxxUtils::pointer_list_base::m_insert
value_type * m_insert
The current insertion point in the list.
Definition: pointer_list.h:194
pool
pool namespace
Definition: libname.h:15
skel.it
it
Definition: skel.GENtoEVGEN.py:396
CxxUtils::pointer_list_base::allocator::refill
void refill()
Allocate a new chunk of blocks.
Definition: pointer_list.cxx:62
CxxUtils::pointer_list::value_type
pointer_list_base::value_type value_type
Stored value type.
Definition: pointer_list.h:245
CxxUtils::pointer_list_base::allocator::m_nblock
size_t m_nblock
Number of blocks per chunk.
Definition: pointer_list.h:146
CxxUtils::pointer_list_base::list_block::value_type
void * value_type
The element type we store.
Definition: pointer_list.h:71
CxxUtils::pointer_list::pointer_list
pointer_list(pool_type &pool)
Constructor. pool gives the allocator for this container.
CxxUtils::pointer_list::allocator::at_end_static
static bool at_end_static(const void *p)
Test if P is pointing at the end-pointer of a block.
CxxUtils::pointer_list_base::allocator
Very simple allocator for use with pointer_list.
Definition: pointer_list.h:95
CxxUtils::pointer_list_base::allocator::at_end
bool at_end(const void *p) const
Test if P is pointing at the end-pointer of a block.
reference
Definition: hcg.cxx:437
CxxUtils::pointer_list_base::nextblock
void nextblock()
Extend the list with another block.
Definition: pointer_list.cxx:111
CxxUtils::pointer_list_base::getblock
list_block * getblock()
Allocate a new block.
Definition: pointer_list.cxx:127
CxxUtils::pointer_list::end
iterator end()
Iterator at the end of the container.
CxxUtils::pointer_list::allocator::END_OFFS
static const unsigned long END_OFFS
Verify that NELT is one less than a power of two.
Definition: pointer_list.h:262
CxxUtils::pointer_list::iterator::operator++
iterator operator++(int)
Advance (post-increment).
CxxUtils::pointer_list::allocator::allocator
allocator(size_t nblock=100)
Constructor.
CxxUtils::pointer_list_base::m_pool
allocator & m_pool
The list allocator.
Definition: pointer_list.h:200
CxxUtils::pointer_list_base::m_size
size_t m_size
The current list size.
Definition: pointer_list.h:197
CxxUtils::pointer_list_base::pointer_list_base
pointer_list_base(pool_type &pool)
Constructor. pool gives the allocator for this container.
CxxUtils::pointer_list_base::list_block::ulong
unsigned long ulong
Definition: pointer_list.h:68
CxxUtils::pointer_list_base::value_type
list_block::value_type value_type
The stored element type.
Definition: pointer_list.h:169
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
CxxUtils::pointer_list::begin
iterator begin()
Iterator at the beginning of the container.
CxxUtils::pointer_list_base::allocator::m_end_offs
unsigned long m_end_offs
Offset for testing for an end pointer.
Definition: pointer_list.h:161
CxxUtils::pointer_list_base::allocator::nelt
size_t nelt() const
Return the number of pointers per block (excluding the end-pointer).
CxxUtils::pointer_list::iterator::m_p
value_type * m_p
Current iteration position.
Definition: pointer_list.h:310
CxxUtils::pointer_list_base::allocator::allocator
allocator(size_t nelt, size_t nblock, unsigned long end_mask, unsigned long end_offs)
Constructor.
Definition: pointer_list.cxx:29
CxxUtils::pointer_list_base::allocator::allocate
list_block * allocate()
Allocate a new block.
CxxUtils::pointer_list::iterator::operator!=
bool operator!=(const iterator &other) const
Inequality comparison.
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::pointer_list_base::allocator::m_chunks
chunk * m_chunks
Most recent chunk allocated.
Definition: pointer_list.h:149
CxxUtils::pointer_list_base::allocator::m_nchunks
size_t m_nchunks
Current number of allocated chunks.
Definition: pointer_list.h:155
CxxUtils::pointer_list_base
A fast way to store a variable-sized collection of pointers.
Definition: pointer_list.h:55
CxxUtils::pointer_list_base::push_back
void push_back(value_type p)
Add a new element to the end of the container. O(1)
CxxUtils::pointer_list_base::pool_type
allocator pool_type
Alias for allocator.
Definition: pointer_list.h:166
CxxUtils::pointer_list
A fast way to store a variable-sized collection of pointers.
Definition: pointer_list.h:242
CxxUtils::pointer_list_base::allocator::chunk
One memory allocation chunk.
Definition: pointer_list.h:134
CxxUtils::pointer_list::erase
void erase(iterator it)
Erase one element. O(n)
CxxUtils::pointer_list::allocator
Allocator for pointer_list, specialized for NELT.
Definition: pointer_list.h:256
CxxUtils::pointer_list::iterator::iterator
iterator(value_type *p)
Constructor, from a pointer into a pointer_list.
pointer_list.icc
CxxUtils::pointer_list_base::size
size_t size() const
The current size of the container. O(1).
CxxUtils::pointer_list::iterator
Forward iterator over the list.
Definition: pointer_list.h:281
CxxUtils::pointer_list_base::allocator::m_nthis
size_t m_nthis
Number of blocks allocated so far from that chunk.
Definition: pointer_list.h:152
CxxUtils::pointer_list_base::allocator::nchunks
size_t nchunks() const
Return the current number of allocated chunks.
CxxUtils::pointer_list_base::firstblock
void firstblock()
Allocate the first block of the list.
Definition: pointer_list.cxx:100
CxxUtils::pointer_list::pool_type
allocator pool_type
Definition: pointer_list.h:315
CxxUtils::pointer_list::allocator::END_MASK
static const unsigned long END_MASK
Definition: pointer_list.h:263
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
CxxUtils::pointer_list_base::allocator::chunk::m_blocks
list_block * m_blocks
Pointer to the first block contained within this chunk.
Definition: pointer_list.h:139
CxxUtils::pointer_list_base::empty
bool empty() const
Test to see if the container is empty.
CxxUtils::pointer_list::iterator::difference_type
std::ptrdiff_t difference_type
Definition: pointer_list.h:285
CxxUtils::pointer_list::iterator::pointer
value_type * pointer
Definition: pointer_list.h:286
CxxUtils::pointer_list_base::allocator::chunk::m_next
chunk * m_next
Link to the next chunk.
Definition: pointer_list.h:136
value_type
Definition: EDM_MasterSearch.h:11
CxxUtils::pointer_list::iterator::operator++
iterator & operator++()
Advance (pre-increment).
CxxUtils::pointer_list_base::allocator::~allocator
~allocator()
Destructor. Deletes all blocks from this allocator.
Definition: pointer_list.cxx:47
CxxUtils::pointer_list_base::list_block::size
static size_t size(size_t nelt)
Size in bytes of a block holding nelt elements (excluding the end-pointer).