Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
StlMap.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_STLMAP_HXX_
35#define _UTILS_STLMAP_HXX_
36
37#include <map>
38
39#include "utils/Allocator.hxx"
40#include "utils/macros.h"
41
49template <typename Key, typename Value> class StlMap
50{
51public:
55 : mappingAllocator(NULL),
56 mapping(new Mapping())
57 {
58 }
59
63 StlMap(size_t entries)
64 : mappingAllocator(entries > 0 ? new MappingAllocator(std::less<Key>(), Allocator<std::pair<const Key, Value>>(entries)) : NULL),
65 mapping(entries > 0 ? NULL : new Mapping())
66 {
67 }
68
72 {
74 {
75 delete mappingAllocator;
76 }
77 if (mapping)
78 {
79 delete mapping;
80 }
81 }
82
84 typedef pair<Key, Value> Pair;
85
87 typedef typename std::map<Key, Value>::iterator Iterator;
88
90 void clear() {
91 if (mapping) {
92 mapping->clear();
93 } else {
94 mappingAllocator->clear();
95 }
96 }
97
102 size_t erase(Key key)
103 {
104 return mapping ? mapping->erase(key) : mappingAllocator->erase(key);
105 }
106
111 {
112 mapping ? mapping->erase(it) : mappingAllocator->erase(it);
113 }
114
119 Value& operator[](const Key &key)
120 {
121 return mapping ? (*mapping)[key] : (*mappingAllocator)[key];
122 }
123
127 size_t size()
128 {
129 return mapping ? mapping->size() : mappingAllocator->size();
130 }
131
135 size_t max_size()
136 {
137 return mapping ? mapping->max_size() : mappingAllocator->max_size();
138 }
139
144 Iterator find( const Key &key )
145 {
146 return mapping ? mapping->find(key) : mappingAllocator->find(key);
147 }
148
153 {
154 return mapping ? mapping->end() : mappingAllocator->end();
155 }
156
161 {
162 return mapping ? mapping->begin() : mappingAllocator->begin();
163 }
164
165private:
167 typedef std::map<Key, Value, std::less<Key>, Allocator<std::pair<const Key, Value>>> MappingAllocator;
168
170 typedef std::map<Key, Value> Mapping;
171
174
177
179};
180
181#endif /* _UTILS_STLMAP_HXX_ */
This is a custom allocator that limits the number of mappings.
Definition Allocator.hxx:47
Though at the surface, this may seem like an unnecessary abstraction of std::map, it has the purpose ...
Definition StlMap.hxx:50
StlMap(size_t entries)
Constructor that limits the number of mappings to a static pool.
Definition StlMap.hxx:63
~StlMap()
Destructor.
Definition StlMap.hxx:71
std::map< Key, Value > Mapping
short hand for the default allocator std::map type
Definition StlMap.hxx:170
Mapping * mapping
pointer to an std::map instance with a default allocator
Definition StlMap.hxx:176
MappingAllocator * mappingAllocator
pointer to an std::map instance with a custom allocator
Definition StlMap.hxx:173
std::map< Key, Value >::iterator Iterator
Short hand for the iterator type of a given instance.
Definition StlMap.hxx:87
size_t max_size()
Maximum theoretical number of elements in the map.
Definition StlMap.hxx:135
std::map< Key, Value, std::less< Key >, Allocator< std::pair< const Key, Value > > > MappingAllocator
short hand for the custom allocator std::map type
Definition StlMap.hxx:167
Value & operator[](const Key &key)
Find the index associated with the key and create it if does not exist.
Definition StlMap.hxx:119
Iterator end()
Get an iterator index pointing one past the last element in mapping.
Definition StlMap.hxx:152
void erase(Iterator it)
Remove a node from the tree.
Definition StlMap.hxx:110
StlMap()
Default Constructor which with no mapping entry limit.
Definition StlMap.hxx:54
size_t size()
Number of elements currently in the map.
Definition StlMap.hxx:127
void clear()
Removes all elements in the map.
Definition StlMap.hxx:90
size_t erase(Key key)
Remove an element from the tree.
Definition StlMap.hxx:102
Iterator begin()
Get an iterator index pointing one past the last element in mapping.
Definition StlMap.hxx:160
Iterator find(const Key &key)
Find an element matching the given key.
Definition StlMap.hxx:144
pair< Key, Value > Pair
This translation is done for consistency with SysMap and LinearMap.
Definition StlMap.hxx:84
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171