Max OS 0.3
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1
9#ifndef MAXOS_COMMON_MAP_H
10#define MAXOS_COMMON_MAP_H
11
12#include <common/vector.h>
13#include <common/pair.h>
14
15
16namespace MaxOS::common {
17
25 template<class Key, class Value> class MapIterationHandler {
26 public:
28 ~MapIterationHandler() = default;
29
30 virtual void on_read(Key, Value);
31 virtual void on_end_of_stream();
32 };
33
41 template<class Key, class Value> class Map {
42 protected:
44
45 public:
47
48 Map();
49 ~Map();
50
51 Value& operator [](Key);
52
53 bool empty();
54 int size();
55
59
60 iterator push_back(Key, Value);
62
63 iterator push_front(Key, Value);
65
66 void insert(Key, Value);
67
68 void erase(Key);
69 void erase(iterator position);
70 void clear();
71
72 void reserve(size_t amount);
74
76 void iterate(void (* callback)(Key&, Value&));
77
78 };
79
81 template<class Key, class Value> MapIterationHandler<Key, Value>::MapIterationHandler() = default;
82
83
84
91 template<class Key, class Value> void MapIterationHandler<Key, Value>::on_end_of_stream() {
92
93 }
94
101 template<class Key, class Value> void MapIterationHandler<Key, Value>::on_read(Key, Value) {
102
103 }
104
105 template<class Key, class Value> Map<Key, Value>::Map() = default;
106
107 template<class Key, class Value> Map<Key, Value>::~Map() = default;
108
117 template<class Key, class Value> Value& Map<Key, Value>::operator [](Key key) {
118
119 // Return the value of the key (second item in the pair)
120 return find(key)->second;
121 }
122
130 template<class Key, class Value> typename Map<Key, Value>::iterator Map<Key, Value>::begin() {
131 return m_elements.begin();
132 }
133
141 template<class Key, class Value> typename Map<Key, Value>::iterator Map<Key, Value>::end() {
142 return m_elements.end();
143 }
144
153 template<class Key, class Value> typename Map<Key, Value>::iterator Map<Key, Value>::find(Key element) {
154
155 // Search for the element
156 for(iterator it = begin(); it != end(); it++)
157 if(it->first == element)
158 return it;
159
160 // Item not found
161 return end();
162
163 }
164
174 template<class Key, class Value> Map<Key, Value>::iterator Map<Key, Value>::push_back(Key key, Value value) {
175 return m_elements.push_back(Pair<Key, Value>(key, value));
176 }
177
185 template<class Key, class Value> Pair<Key, Value> Map<Key, Value>::pop_back() {
186 return m_elements.pop_back();
187 }
188
198 template<class Key, class Value> Map<Key, Value>::iterator Map<Key, Value>::push_front(Key key, Value value) {
199 return m_elements.push_front({ key, value });
200 }
201
209 template<class Key, class Value> Pair<Key, Value> Map<Key, Value>::pop_front() {
210 return m_elements.pop_front();
211 }
212
220 template<class Key, class Value> bool Map<Key, Value>::empty() {
221 return m_elements.empty();
222 }
223
229 template<class Key, class Value> int Map<Key, Value>::size() {
230 // Return the size of the vector
231 return m_elements.size();
232
233 }
234
241 template<class Key, class Value> void Map<Key, Value>::clear() {
242 m_elements.clear();
243 }
244
253 template<class Key, class Value> void Map<Key, Value>::insert(Key key, Value value) {
254
255 // Find the element
256 iterator it = find(key);
257
258 // If the element is found then update the value
259 if(it != end()) {
260 it->second = value;
261 } else {
262 // Otherwise, add a new element
263 m_elements.push_back(Pair<Key, Value>(key, value));
264 }
265 }
266
274 template<class Key, class Value> void Map<Key, Value>::erase(Key key) {
275
276 // Find the element
277 iterator it = find(key);
278
279 // If the element is found then remove it
280 if(it != end()) {
281 m_elements.erase(it);
282 }
283
284 }
285
293 template<class Key, class Value> void Map<Key, Value>::erase(Map::iterator position) {
294 m_elements.erase(position);
295
296 }
297
305 template<class Key, class Value> void Map<Key, Value>::reserve(size_t amount) {
306 m_elements.reserve(amount);
307 }
308
315 template<class Key, class Value> void Map<Key, Value>::increase_size() {
316 m_elements.increase_size();
317 }
318
326 template<class Key, class Value> void Map<Key, Value>::iterate(MapIterationHandler<Key, Value>* handler) {
327
328 // Loop through the elements
329 for(auto& it : m_elements) {
330
331 // Call the handler
332 handler->on_read(it.first, it.second);
333 }
334
335 // Call the handler
336 handler->on_end_of_stream();
337
338 }
339
347 template<class Key, class Value> void Map<Key, Value>::iterate(void (* callback)(Key&, Value&)) {
348
349 // Call the callback for each element
350 for(auto& it : m_elements) {
351 callback(it.first, it.second);
352 }
353 }
354}
355
356
357#endif //MAXOS_COMMON_MAP_H
Handles iteration of a map.
Definition map.h:25
virtual void on_read(Key, Value)
Called when a key-value pair is read.
Definition map.h:101
MapIterationHandler()
______________ TEMPLATE IMPLEMENTATION ______________
virtual void on_end_of_stream()
Called when the end of the stream is reached.
Definition map.h:91
A list of key-value pairs.
Definition map.h:41
void erase(iterator position)
Removes an element from the map at the specified position.
Definition map.h:293
bool empty()
Returns whether the map is empty.
Definition map.h:220
iterator find(Key)
Finds an element in the map based on the key.
Definition map.h:153
Pair< Key, Value > pop_front()
Removes the first key-value pair from the map and returns it.
Definition map.h:209
Vector< Pair< Key, Value > >::iterator iterator
The iterator type for the map.
Definition map.h:46
Pair< Key, Value > pop_back()
Removes the last key-value pair from the map and returns it.
Definition map.h:185
iterator end()
Returns the end of the map.
Definition map.h:141
iterator push_front(Key, Value)
Adds a new key-value pair to the front of the map.
Definition map.h:198
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Definition map.h:253
Value & operator[](Key)
Overloads the [] operator to return the value of the key.
Definition map.h:117
void reserve(size_t amount)
Reserves space in the map for a certain amount of elements to avoid reallocations.
Definition map.h:305
void iterate(MapIterationHandler< Key, Value > *handler)
Iterates through the map and calls the handler.
Definition map.h:326
void erase(Key)
Removes an element from the map.
Definition map.h:274
void increase_size()
Doubles the size of the map.
Definition map.h:315
iterator begin()
Returns the beginning of the map.
Definition map.h:130
iterator push_back(Key, Value)
Adds a new key-value pair to the end of the map.
Definition map.h:174
void clear()
Removes all elements from the map.
Definition map.h:241
Vector< Pair< Key, Value > > m_elements
The internal storage of the map, a vector of key-value pairs.
Definition map.h:43
void iterate(void(*callback)(Key &, Value &))
Iterates through the map and calls the callback.
Definition map.h:347
int size()
The number of elements in the map.
Definition map.h:229
Second second
The second object (often the value)
Definition pair.h:26
Dynamically stores an array of elements.
Definition vector.h:39
iterator end() const
Returns the last element of the Vector.
Definition vector.h:293
uint32_t size() const
Returns the number of elements in the Vector.
Definition vector.h:273
void reserve(size_t amount)
Reserves space in the Vector for a certain amount of elements.
Definition vector.h:174
iterator push_front(Type)
Adds an element to the front of the Vector and returns the iterator of the element.
Definition vector.h:366
void clear()
Removes all elements from the Vector.
Definition vector.h:461
void increase_size()
Increases the size of the Vector by doubling the capacity.
Definition vector.h:164
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333
Type pop_back()
Removes the last element from the Vector.
Definition vector.h:350
iterator begin() const
Returns the first element of the Vector.
Definition vector.h:283
Type pop_front()
Removes the m_first_memory_chunk element from the Vector.
Definition vector.h:390
bool empty() const
Checks if the Vector is empty.
Definition vector.h:321
void erase(Type)
Removes all elements from the Vector that are equal to the element.
Definition vector.h:415
Defines a Pair class for storing two related objects together.
Defines a Vector class for dynamically storing an array of elements.