Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
LinearMap.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_LINEARMAP_HXX_
35#define _UTILS_LINEARMAP_HXX_
36
37#include <stdio.h> // for ssize_t
38#include <sys/tree.hxx>
39
40#include "utils/macros.h"
41
45template <typename Key, typename Value> class LinearMap
46{
47public:
51 : entries(0),
52 used(0),
53 list(NULL)
54 {
55 /* dynamic allocation not supported */
56 HASSERT(0);
57 }
58
64 used(0),
65 list(new Element[entries])
66 {
67 }
68
72 {
73 delete [] list;
74 }
75
77 struct Pair
78 {
79 Key first;
80 Value second;
81 };
82
83private:
85 struct Element
86 {
87 union
88 {
90 struct
91 {
92 Key key;
93 Value value;
94 };
95 };
96 };
97
98public:
99
103 {
104 public:
107 Iterator() : index(0), m(nullptr)
108 {
109 }
110
114 : index(it.index),
115 m(it.m)
116 {
117 }
118
123 Iterator(LinearMap* context, size_t index)
124 : index(index),
125 m(context)
126 {
127 }
128
131 {
132 }
133
137 {
138 return m->list[index].p;
139 }
140
144 {
145 return &m->list[index].p;
146 }
147
150 {
151 if (index < m->used)
152 {
153 ++index;
154 }
155 return *this;
156 }
157
160 bool operator != (const Iterator& it)
161 {
162 return m != it.m || index != it.index;
163 }
164
167 bool operator == (const Iterator& it)
168 {
169 return m == it.m && index == it.index;
170 }
171
172 private:
174 size_t index;
175
178
180 friend class LinearMap;
181 };
182
184 void clear()
185 {
186 used = 0;
187 }
188
193 size_t erase(Key key)
194 {
195 for (size_t i = 0; i < used; ++i)
196 {
197 if (list[i].key == key)
198 {
199 /* scrunch up the list if we are able */
200 if (i != --used)
201 {
202 list[i].key = list[used].key;
203 list[i].value = list[used].value;
204 }
205 return 1;
206 }
207 }
208 return 0;
209 }
210
215 {
216 ssize_t i = it.index;
217 HASSERT(i < (ssize_t)used && i >= 0);
218
219 /* scrunch up the list if we are able */
220 if (i != (ssize_t)--used)
221 {
222 list[i].key = list[used].key;
223 list[i].value = list[used].value;
224 }
225 }
226
231 Value &operator[](const Key &key)
232 {
233 for (size_t i = 0; i < used; ++i)
234 {
235 if (list[i].key == key)
236 {
237 return list[i].value;
238 }
239 }
240
242
243 list[used].key = key;
244 list[used].value = 0;
245
246 return list[used++].value;
247 }
248
252 size_t size()
253 {
254 return used;
255 }
256
260 size_t max_size()
261 {
262 return entries;
263 }
264
269 Iterator find(const Key &key)
270 {
271 for (size_t i = 0; i < used; ++i)
272 {
273 if (list[i].key == key)
274 {
275 return Iterator(this, i);
276 }
277 }
278 return end();
279 }
280
285 {
286 return Iterator(this, size());
287 }
288
294 {
295 return Iterator(this, 0);
296 }
297
298private:
300 size_t entries;
301
303 size_t used;
304
307
309};
310
311#endif /* _UTILS_LINEARMAP_HXX_ */
This mimics an std::Iterator.
Iterator(LinearMap *context, size_t index)
Constructor.
~Iterator()
Destructor.
bool operator==(const Iterator &it)
Overloaded equals operator.
bool operator!=(const Iterator &it)
Overloaded not equals operator.
Pair & operator*() const
Overloaded reference operator.
Iterator(const Iterator &it)
Copy constructor.
Iterator & operator++()
Overloaded pre-increement operator.
Pair * operator->() const
Overloaded pointer operator.
size_t index
index this iteration is currently indexed to
Iterator()
Default constructor.
LinearMap * m
Context this iteration lives in.
This is an abstraction of BSD sys/tree.h that is meant to mimic the semantics of std::map without hav...
Definition LinearMap.hxx:46
Element * list
list of entries
size_t max_size()
Maximum theoretical number of elements in the map.
LinearMap(size_t entries)
Constructor that limits the number of mappings to a static pool.
Definition LinearMap.hxx:62
Iterator find(const Key &key)
Find an element matching the given key.
void clear()
Removes all elements.
size_t entries
total number of entries for this instance
size_t size()
Number of elements currently in the map.
size_t erase(Key key)
Remove a node from the tree.
Iterator begin()
Get an Iterator index pointing to the first element in the mapping.
~LinearMap()
Destructor.
Definition LinearMap.hxx:71
Value & operator[](const Key &key)
Find the index associated with the key and create it does not exist.
LinearMap()
Default Constructor which with no mapping entry limit.
Definition LinearMap.hxx:50
void erase(Iterator it)
Remove a node from the tree.
size_t used
total number of entries in use for this instance
Iterator end()
Get an Iterator index pointing one past the last element in mapping.
#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
Entry element in mapping.
Definition LinearMap.hxx:86
Key key
key by which to sort the node
Definition LinearMap.hxx:92
Pair p
pair of element
Definition LinearMap.hxx:89
Value value
value of the node
Definition LinearMap.hxx:93
list entry.
Definition LinearMap.hxx:78
Key first
mimic first element in an std::pair
Definition LinearMap.hxx:79
Value second
mimic second element in an std::pair
Definition LinearMap.hxx:80