Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SysMap.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_SYSMAP_HXX_
35#define _UTILS_SYSMAP_HXX_
36
37#include <sys/tree.hxx>
38#include <cstdint>
39
40#include "utils/macros.h"
41
45template <typename Key, typename Value> class SysMap
46{
47public:
51 : entries(0),
52 used(0),
53 freeList(NULL),
54 nodes(NULL)
55
56 {
57 clear();
58 }
59
65 used(0),
66 freeList(NULL),
67 nodes(NULL)
68 {
69 if (entries) {
70 nodes = new Node[entries];
71 }
72 clear();
73 }
74
78 {
79 delete nodes;
80 }
81
83 struct Pair
84 {
85 Key first;
86 Value second;
87 };
88
89private:
91 struct Node
92 {
93 public:
95 RB_ENTRY(Node) entry;
96 union
97 {
99 struct
100 {
101 Key key;
102 Value value;
103 };
104 };
105
108 Node() = default;
109 };
110
111public:
115 {
116 public:
120 : node(NULL),
121 m(NULL)
122 {
123 }
124
128 : node(it.node),
129 m(it.m)
130 {
131 }
132
138 : node(node),
139 m(context)
140
141 {
142 }
143
146 {
147 }
148
152 {
153 return node->p;
154 }
155
159 {
160 return &node->p;
161 }
162
165 {
166 if (node)
167 {
168 node = RB_NEXT(m->tree, &(m->head), node);
169 }
170 return *this;
171 }
172
174 bool operator != (const Iterator &it)
175 {
176 return node != it.node;
177 }
178
180 bool operator == (const Iterator &it)
181 {
182 return node == it.node;
183 }
184
185 private:
188
191
193 friend class SysMap;
194 };
195
200 Value &operator[](const Key &key)
201 {
202 Iterator it = find(key);
203 if (it == end())
204 {
205 Node *node = alloc();
206 HASSERT(node);
207 node->key = key;
208 node->value = (Value)0;
209 HASSERT(RB_INSERT(tree, &head, node) == NULL);
210 it = Iterator(this, node);
211 }
212 return (*it).second;
213 }
214
218 size_t size()
219 {
220 return used;
221 }
222
226 size_t max_size()
227 {
228 return entries ? entries : UINTPTR_MAX / sizeof(Pair);
229 }
230
232 void clear()
233 {
234 RB_INIT(&head);
235 used = 0;
236 if (entries)
237 {
238 nodes->entry.rbe_left = NULL;
239
240 for (size_t i = 1; i < entries; ++i)
241 {
242 nodes[i].entry.rbe_left = nodes + (i - 1);
243 }
244 freeList = nodes + (entries - 1);
245 }
246 }
247
252 Iterator find(const Key &key)
253 {
254 Node lookup;
255 lookup.key = key;
256 Node *node = RB_FIND(tree, &head, &lookup);
257
258 return node ? Iterator(this, node) : Iterator();
259 }
260
265 {
266 return Iterator();
267 }
268
274 {
275 Node *node = RB_MIN(tree, &head);
276
277 return node ? Iterator(this, node) : Iterator();
278 }
279
284 size_t erase(Key key)
285 {
286 Node lookup;
287 lookup.key = key;
288 Node *node = RB_FIND(tree, &head, &lookup);
289
290 if (node)
291 {
292 RB_REMOVE(tree, &head, node);
293 free(node);
294 return 1;
295 }
296 return 0;
297 }
298
303 {
304 Node *node = it.node;
305 if (node)
306 {
307 RB_REMOVE(tree, &head, node);
308 free(node);
309 }
310 }
311
312private:
317 {
318 if (nodes)
319 {
320 if (freeList != NULL)
321 {
322 Node *node = freeList;
323 freeList = freeList->entry.rbe_left;
324 memset(node, 0, sizeof(Node));
325 ++used;
326 return node;
327 }
328 return NULL;
329 }
330 else
331 {
332 Node *node = new Node;
333 memset(node, 0, sizeof(Node));
334 ++used;
335 return node;
336 }
337
338 }
339
343 void free(Node *node)
344 {
345 if (nodes)
346 {
347 node->entry.rbe_left = freeList;
348 freeList = node;
349 --used;
350 }
351 else
352 {
353 --used;
354 delete node;
355 }
356 }
357
364 int64_t compare(Node *a, Node *b)
365 {
366 union comp
367 {
368 int64_t s;
369 Key k;
370 };
371
372 comp ca, cb;
373 ca.s = 0;
374 cb.s = 0;
375 ca.k = a->key;
376 cb.k = b->key;
377
378 return ca.s - cb.s;
379 }
380
382 size_t entries;
383
385 size_t used;
386
389
392
394 RB_HEAD(tree, Node);
395
397 RB_GENERATE(tree, Node, entry, compare);
398
400 struct tree head;
401
403};
404
405#endif /* _UTILS_SYSMAP_HXX_ */
This mimics an std::iterator.
Definition SysMap.hxx:115
Iterator(const Iterator &it)
Copy constructor.
Definition SysMap.hxx:127
Node * node
node this iteration is currently indexed to
Definition SysMap.hxx:187
Iterator(SysMap *context, Node *node)
Constructor.
Definition SysMap.hxx:137
~Iterator()
Destructor.
Definition SysMap.hxx:145
Iterator & operator++()
Overloaded pre-increement operator.
Definition SysMap.hxx:164
bool operator!=(const Iterator &it)
Overloaded not equals operator.
Definition SysMap.hxx:174
Iterator()
Default constructor.
Definition SysMap.hxx:119
SysMap * m
Context this iteration lives in.
Definition SysMap.hxx:190
Pair & operator*() const
Overloaded reference operator.
Definition SysMap.hxx:151
Pair * operator->() const
Overloaded pointer operator.
Definition SysMap.hxx:158
bool operator==(const Iterator &it)
Overloaded equals operator.
Definition SysMap.hxx:180
This is an abstraction of BSD sys/tree.h that is meant to mimic the semantics of std::map without hav...
Definition SysMap.hxx:46
size_t erase(Key key)
Remove a node from the tree.
Definition SysMap.hxx:284
void free(Node *node)
free a node to the free list if it exists.
Definition SysMap.hxx:343
Iterator find(const Key &key)
Find an element matching the given key.
Definition SysMap.hxx:252
SysMap()
Default Constructor which with no mapping entry limit.
Definition SysMap.hxx:50
size_t max_size()
Maximum theoretical number of elements in the map.
Definition SysMap.hxx:226
size_t size()
Number of elements currently in the map.
Definition SysMap.hxx:218
int64_t compare(Node *a, Node *b)
Compare two nodes.
Definition SysMap.hxx:364
RB_HEAD(tree, Node)
The datagram tree type.
SysMap(size_t entries)
Constructor that limits the number of mappings to a static pool.
Definition SysMap.hxx:63
size_t entries
total number of entries for this instance
Definition SysMap.hxx:382
Value & operator[](const Key &key)
Find the index associated with the key and create it if does not exist.
Definition SysMap.hxx:200
size_t used
total number of entries in use for this instance
Definition SysMap.hxx:385
Iterator end()
Get an iterator index pointing one past the last element in mapping.
Definition SysMap.hxx:264
void clear()
Removes all elements in the map.
Definition SysMap.hxx:232
Iterator begin()
Get an iterator index pointing to the first element in the mapping.
Definition SysMap.hxx:273
void erase(Iterator it)
Remove a node from the tree.
Definition SysMap.hxx:302
Node * freeList
list of free nodes
Definition SysMap.hxx:388
Node * alloc()
Allocate a node from the free list.
Definition SysMap.hxx:316
~SysMap()
Destructor.
Definition SysMap.hxx:77
Node * nodes
location of pre-allocated Node memory
Definition SysMap.hxx:391
struct tree head
tree instance
Definition SysMap.hxx:400
RB_GENERATE(tree, Node, entry, compare)
The datagram tree methods.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
The metadata for a tree node.
Definition SysMap.hxx:92
Key key
key by which to sort the node
Definition SysMap.hxx:101
Value value
value of the node
Definition SysMap.hxx:102
RB_ENTRY(Node) entry
Pointer structure for the tree.
Pair p
pair of element
Definition SysMap.hxx:98
Node()=default
Default constructor.
mimic std::pair
Definition SysMap.hxx:84
Key first
mimic first element in an std::pair
Definition SysMap.hxx:85
Value second
mimic second element in an std::pair
Definition SysMap.hxx:86