Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
StlMultiMap.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_STLMULTIMAP_HXX_
35#define _UTILS_STLMULTIMAP_HXX_
36
37#include <map>
38
39#include "utils/Allocator.hxx"
40#include "utils/macros.h"
41
49template <typename Key, typename Value> class StlMultiMap
50{
51public:
55 : mappingAllocator(NULL),
56 mapping(new Mapping())
57 {
58 }
59
63 StlMultiMap(size_t entries)
64 : mappingAllocator(new MappingAllocator(std::less<Key>(), Allocator<std::pair<const Key, Value>>(entries))),
65 mapping(NULL)
66 {
67 }
68
72 {
74 {
75 delete mappingAllocator;
76 }
77 if (mapping)
78 {
79 delete mapping;
80 }
81 }
82
84 typedef std::pair<Key, Value> Pair;
85
87 typedef typename std::multimap<Key, Value>::iterator Iterator;
88
93 size_t erase(Key key)
94 {
95 return mapping ? mapping->erase(key) : mappingAllocator->erase(key);
96 }
97
102 {
103 mapping ? mapping->erase(it) : mappingAllocator->erase(it);
104 }
105
111 Iterator insert(const Iterator pos, const Pair& val)
112 {
113 return mapping ? mapping->insert(pos, val) : mappingAllocator->insert(pos, val);
114 }
115
120 Iterator insert(const Pair& val)
121 {
122 return mapping ? mapping->insert(val) : mappingAllocator->insert(val);
123 }
124
128 size_t size()
129 {
130 return mapping ? mapping->size() : mappingAllocator->size();
131 }
132
136 size_t max_size()
137 {
138 return mapping ? mapping->max_size() : mappingAllocator->max_size();
139 }
140
145 Iterator find( const Key &key )
146 {
147 return mapping ? mapping->find(key) : mappingAllocator->find(key);
148 }
149
155 Iterator lower_bound(const Key& key)
156 {
157 return mapping ? mapping->lower_bound(key) : mappingAllocator->lower_bound(key);
158 }
159
165 Iterator upper_bound(const Key& key)
166 {
167 return mapping ? mapping->upper_bound(key) : mappingAllocator->upper_bound(key);
168 }
169
174 {
175 return mapping ? mapping->end() : mappingAllocator->end();
176 }
177
182 {
183 return mapping ? mapping->begin() : mappingAllocator->begin();
184 }
185
190 size_t count(const Key& key) const
191 {
192 return mapping ? mapping->count(key) : mappingAllocator->count(key);
193 }
194
195private:
197 typedef std::multimap<Key, Value, std::less<Key>, Allocator<std::pair<const Key, Value>>> MappingAllocator;
198
200 typedef std::multimap<Key, Value> Mapping;
201
204
207
209};
210
211#endif /* _UTILS_STLMULTIMAP_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::multimap,...
size_t max_size()
Maximum theoretical number of elements in the map.
size_t erase(Key key)
Remove an element from the tree.
Iterator end()
Get an iterator index pointing one past the last element in mapping.
Iterator upper_bound(const Key &key)
Get an iterator pointing the the first element in the map after the provided key.
Iterator lower_bound(const Key &key)
Get an iterator pointing the the first element in the map with the provided key, else iterator end() ...
std::multimap< Key, Value > Mapping
short hand for the default allocator std::map type
~StlMultiMap()
Destructor.
void erase(Iterator it)
Remove a node from the tree.
Iterator insert(const Iterator pos, const Pair &val)
Insert (create) a new key value pair.
Mapping * mapping
pointer to an std::map instance with a default allocator
size_t size()
Number of elements currently in the map.
Iterator find(const Key &key)
Find an element matching the given key.
std::pair< Key, Value > Pair
This translation is done for consistency with SysMap and LinearMap.
StlMultiMap()
Default Constructor which with no mapping entry limit.
size_t count(const Key &key) const
Get the count of elements with a givin Key.
Iterator begin()
Get an iterator index pointing one past the last element in mapping.
std::multimap< Key, Value >::iterator Iterator
Short hand for the iterator type of a given instance.
MappingAllocator * mappingAllocator
pointer to an std::map instance with a custom allocator
Iterator insert(const Pair &val)
Insert (create) a new key value pair.
std::multimap< Key, Value, std::less< Key >, Allocator< std::pair< const Key, Value > > > MappingAllocator
short hand for the custom allocator std::map type
StlMultiMap(size_t entries)
Constructor that limits the number of mappings to a static pool.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171