ATLAS Offline Software
Loading...
Searching...
No Matches
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*/
12
13
14#ifndef CXXUTILS_POINTER_LIST_H
15#define CXXUTILS_POINTER_LIST_H
16
17
18#include <type_traits>
19#include <iterator>
20
21
22namespace CxxUtils {
23
24
55{
56public:
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
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
189protected:
192
195
197 size_t m_size;
198
201
203 void firstblock ();
204
207 void nextblock ();
208
211};
212
213
239template <size_t NELT = 15>
241 : public pointer_list_base
242{
243public:
246
247
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
281 {
282 public:
283 using iterator_category = std::forward_iterator_tag;
285 using difference_type = std::ptrdiff_t;
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
335
336
337#endif // not CXXUTILS_POINTER_LIST_H
Allocator for pointer_list, specialized for NELT.
static const unsigned long END_OFFS
Verify that NELT is one less than a power of two.
allocator(size_t nblock=100)
Constructor.
static bool at_end_static(const void *p)
Test if P is pointing at the end-pointer of a block.
static const unsigned long END_MASK
Forward iterator over the list.
iterator & operator++()
Advance (pre-increment).
std::forward_iterator_tag iterator_category
iterator operator++(int)
Advance (post-increment).
iterator(value_type *p)
Constructor, from a pointer into a pointer_list.
value_type * m_p
Current iteration position.
pointer_list::value_type value_type
bool operator==(const iterator &other) const
Equality comparison.
bool operator!=(const iterator &other) const
Inequality comparison.
reference operator*()
Dereference.
Very simple allocator for use with pointer_list.
unsigned long m_end_mask
Mask for testing for an end pointer.
allocator(size_t nelt, size_t nblock, unsigned long end_mask, unsigned long end_offs)
Constructor.
bool at_end(const void *p) const
Test if P is pointing at the end-pointer of a block.
void refill()
Allocate a new chunk of blocks.
size_t m_nblock
Number of blocks per chunk.
~allocator()
Destructor. Deletes all blocks from this allocator.
chunk * m_chunks
Most recent chunk allocated.
size_t m_nelt
Number of elements per block (excluding the end-pointer).
size_t m_nchunks
Current number of allocated chunks.
unsigned long m_end_offs
Offset for testing for an end pointer.
size_t nelt() const
Return the number of pointers per block (excluding the end-pointer).
size_t nchunks() const
Return the current number of allocated chunks.
size_t m_nthis
Number of blocks allocated so far from that chunk.
list_block * allocate()
Allocate a new block.
list_block::value_type value_type
The stored element type.
void push_back(value_type p)
Add a new element to the end of the container. O(1)
list_block * m_head
The first block in the list.
void nextblock()
Extend the list with another block.
value_type * m_insert
The current insertion point in the list.
void clear()
Erase the container.
bool empty() const
Test to see if the container is empty.
void firstblock()
Allocate the first block of the list.
size_t m_size
The current list size.
pointer_list_base(pool_type &pool)
Constructor. pool gives the allocator for this container.
allocator pool_type
Alias for allocator.
allocator & m_pool
The list allocator.
list_block * getblock()
Allocate a new block.
size_t size() const
The current size of the container. O(1).
iterator end()
Iterator at the end of the container.
iterator begin()
Iterator at the beginning of the container.
pointer_list(pool_type &pool)
Constructor. pool gives the allocator for this container.
void erase(iterator it)
Erase one element. O(n)
pointer_list_base::value_type value_type
Stored value type.
pool namespace
Definition libname.h:15
list_block * m_blocks
Pointer to the first block contained within this chunk.
chunk * m_next
Link to the next chunk.
A single block in the list.
static size_t size(size_t nelt)
Size in bytes of a block holding nelt elements (excluding the end-pointer).
value_type m_data[1]
The elements.
void * value_type
The element type we store.