Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::common::Map< Key, Value > Class Template Reference

A list of key-value pairs. More...

#include <map.h>

Public Types

typedef Vector< Pair< Key, Value > >::iterator iterator
 The iterator type for the map.
 

Public Member Functions

Valueoperator[] (Key)
 Overloads the [] operator to return the value of the key.
 
bool empty ()
 Returns whether the map is empty.
 
int size ()
 The number of elements in the map.
 
iterator begin ()
 Returns the beginning of the map.
 
iterator end ()
 Returns the end of the map.
 
iterator find (Key)
 Finds an element in the map based on the key.
 
iterator push_back (Key, Value)
 Adds a new key-value pair to the end of the map.
 
Pair< Key, Valuepop_back ()
 Removes the last key-value pair from the map and returns it.
 
iterator push_front (Key, Value)
 Adds a new key-value pair to the front of the map.
 
Pair< Key, Valuepop_front ()
 Removes the first key-value pair from the map and returns it.
 
void insert (Key, Value)
 Updates the value of an element, or adds a new element if it does not exist.
 
void erase (Key)
 Removes an element from the map.
 
void erase (iterator position)
 Removes an element from the map at the specified position.
 
void clear ()
 Removes all elements from the map.
 
void reserve (size_t amount)
 Reserves space in the map for a certain amount of elements to avoid reallocations.
 
void increase_size ()
 Doubles the size of the map.
 
void iterate (MapIterationHandler< Key, Value > *handler)
 Iterates through the map and calls the handler.
 
void iterate (void(*callback)(Key &, Value &))
 Iterates through the map and calls the callback.
 

Protected Attributes

Vector< Pair< Key, Value > > m_elements
 The internal storage of the map, a vector of key-value pairs.
 

Detailed Description

template<class Key, class Value>
class MaxOS::common::Map< Key, Value >

A list of key-value pairs.

Template Parameters
KeyThe key type
ValueThe value type

Definition at line 41 of file map.h.

Member Typedef Documentation

◆ iterator

template<class Key , class Value >
typedef Vector<Pair<Key,Value>>::iterator MaxOS::common::Map< Key, Value >::iterator

The iterator type for the map.

Definition at line 46 of file map.h.

Member Function Documentation

◆ begin()

Returns the beginning of the map.

Template Parameters
KeyThe key type
ValueThe value type
Returns
The m_first_memory_chunk element in the map

Definition at line 130 of file map.h.

130 {
131 return m_elements.begin();
132 }
Vector< Pair< Key, Value > > m_elements
The internal storage of the map, a vector of key-value pairs.
Definition map.h:43

References MaxOS::common::Vector< Type >::begin().

◆ clear()

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::clear ( )

Removes all elements from the map.

Template Parameters
KeyThe key type
ValueThe value type

Definition at line 241 of file map.h.

241 {
242 m_elements.clear();
243 }

References MaxOS::common::Vector< Type >::clear().

◆ empty()

template<class Key , class Value >
bool MaxOS::common::Map< Key, Value >::empty ( )

Returns whether the map is empty.

Template Parameters
KeyThe key type
ValueThe value type
Returns
Whether the map is empty

Definition at line 220 of file map.h.

220 {
221 return m_elements.empty();
222 }

References MaxOS::common::Vector< Type >::empty().

◆ end()

Returns the end of the map.

Template Parameters
KeyThe key type
ValueThe value type
Returns
The last element in the map

Definition at line 141 of file map.h.

141 {
142 return m_elements.end();
143 }

References MaxOS::common::Vector< Type >::end().

Referenced by MaxOS::processes::SharedMemory::open(), MaxOS::processes::SharedMemory::read(), and MaxOS::net::AddressResolutionProtocol::resolve().

◆ erase() [1/2]

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::erase ( Map< Key, Value >::iterator  position)

Removes an element from the map at the specified position.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
positionThe iterator of the element to remove

Definition at line 293 of file map.h.

293 {
294 m_elements.erase(position);
295
296 }

References MaxOS::common::Vector< Type >::erase().

◆ erase() [2/2]

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::erase ( Key  key)

Removes an element from the map.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
keyThe key of the element to remove

Definition at line 274 of file map.h.

274 {
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 }
iterator find(Key)
Finds an element in the map based on the key.
Definition map.h:153
Vector< Pair< Key, Value > >::iterator iterator
The iterator type for the map.
Definition map.h:46
iterator end()
Returns the end of the map.
Definition map.h:141

References MaxOS::common::Vector< Type >::erase().

Referenced by MaxOS::processes::BaseResourceRegistry::close_resource().

◆ find()

template<class Key , class Value >
Map< Key, Value >::iterator MaxOS::common::Map< Key, Value >::find ( Key  element)

Finds an element in the map based on the key.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
elementThe key to search for
Returns
The iterator of the element, or the end iterator if not found

Definition at line 153 of file map.h.

153 {
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 }
iterator begin()
Returns the beginning of the map.
Definition map.h:130

Referenced by MaxOS::processes::SharedMemory::open(), MaxOS::processes::SharedMemory::read(), and MaxOS::net::AddressResolutionProtocol::resolve().

◆ increase_size()

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::increase_size ( )

Doubles the size of the map.

Template Parameters
KeyThe key type
ValueThe value type

Definition at line 315 of file map.h.

315 {
316 m_elements.increase_size();
317 }

References MaxOS::common::Vector< Type >::increase_size().

◆ insert()

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::insert ( Key  key,
Value  value 
)

Updates the value of an element, or adds a new element if it does not exist.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
keyThe key of the new element
valueThe value of the new element

Definition at line 253 of file map.h.

253 {
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 }

References MaxOS::common::Vector< Type >::push_back(), and MaxOS::common::Pair< First, Second >::second.

Referenced by MaxOS::processes::GlobalScheduler::add_process(), MaxOS::processes::GlobalScheduler::add_thread(), MaxOS::net::AddressResolutionProtocol::handle_ethernetframe_payload(), MaxOS::processes::SharedMemory::open(), MaxOS::processes::BaseResourceRegistry::register_resource(), and MaxOS::net::AddressResolutionProtocol::store().

◆ iterate() [1/2]

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::iterate ( MapIterationHandler< Key, Value > *  handler)

Iterates through the map and calls the handler.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
handlerThe handler to call

Definition at line 326 of file map.h.

326 {
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 }

References MaxOS::common::MapIterationHandler< Key, Value >::on_end_of_stream(), and MaxOS::common::MapIterationHandler< Key, Value >::on_read().

◆ iterate() [2/2]

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::iterate ( void(*)(Key &, Value &)  callback)

Iterates through the map and calls the callback.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
callbackThe callback to call

Definition at line 347 of file map.h.

347 {
348
349 // Call the callback for each element
350 for(auto& it : m_elements) {
351 callback(it.first, it.second);
352 }
353 }

◆ operator[]()

template<class Key , class Value >
Value & MaxOS::common::Map< Key, Value >::operator[] ( Key  key)

Overloads the [] operator to return the value of the key.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
keyThe key to search for
Returns
The value of the key

Definition at line 117 of file map.h.

117 {
118
119 // Return the value of the key (second item in the pair)
120 return find(key)->second;
121 }
Second second
The second object (often the value)
Definition pair.h:26

◆ pop_back()

template<class Key , class Value >
Pair< Key, Value > MaxOS::common::Map< Key, Value >::pop_back ( )

Removes the last key-value pair from the map and returns it.

Template Parameters
KeyThe key type
ValueThe value type
Returns

Definition at line 185 of file map.h.

185 {
186 return m_elements.pop_back();
187 }

References MaxOS::common::Vector< Type >::pop_back().

◆ pop_front()

template<class Key , class Value >
Pair< Key, Value > MaxOS::common::Map< Key, Value >::pop_front ( )

Removes the first key-value pair from the map and returns it.

Template Parameters
KeyThe key type
ValueThe value type
Returns
The removed key-value pair

Definition at line 209 of file map.h.

209 {
210 return m_elements.pop_front();
211 }

References MaxOS::common::Vector< Type >::pop_front().

◆ push_back()

template<class Key , class Value >
Map< Key, Value >::iterator MaxOS::common::Map< Key, Value >::push_back ( Key  key,
Value  value 
)

Adds a new key-value pair to the end of the map.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
keyThe key
valueThe value
Returns
The iterator of the new element

Definition at line 174 of file map.h.

174 {
175 return m_elements.push_back(Pair<Key, Value>(key, value));
176 }

References MaxOS::common::Vector< Type >::push_back().

◆ push_front()

template<class Key , class Value >
Map< Key, Value >::iterator MaxOS::common::Map< Key, Value >::push_front ( Key  key,
Value  value 
)

Adds a new key-value pair to the front of the map.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
keyThe key
valueThe value
Returns

Definition at line 198 of file map.h.

198 {
199 return m_elements.push_front({ key, value });
200 }

References MaxOS::common::Vector< Type >::push_front().

◆ reserve()

template<class Key , class Value >
void MaxOS::common::Map< Key, Value >::reserve ( size_t  amount)

Reserves space in the map for a certain amount of elements to avoid reallocations.

Template Parameters
KeyThe key type
ValueThe value type
Parameters
amountThe amount of elements to reserve space for

Definition at line 305 of file map.h.

305 {
306 m_elements.reserve(amount);
307 }

References MaxOS::common::Vector< Type >::reserve().

◆ size()

template<class Key , class Value >
int MaxOS::common::Map< Key, Value >::size ( )

The number of elements in the map.

Returns
The number of elements in the Map

Definition at line 229 of file map.h.

229 {
230 // Return the size of the vector
231 return m_elements.size();
232
233 }

References MaxOS::common::Vector< Type >::size().

Member Data Documentation

◆ m_elements

template<class Key , class Value >
Vector<Pair<Key, Value> > MaxOS::common::Map< Key, Value >::m_elements
protected

The internal storage of the map, a vector of key-value pairs.

Definition at line 43 of file map.h.


The documentation for this class was generated from the following file: