9#ifndef MAXOS_COMMON_MAP_H
10#define MAXOS_COMMON_MAP_H
16namespace MaxOS::common {
30 virtual void on_read(Key, Value);
41 template<
class Key,
class Value>
class Map {
76 void iterate(
void (* callback)(Key&, Value&));
120 return find(key)->second;
131 return m_elements.
begin();
142 return m_elements.
end();
156 for(
iterator it = begin(); it != end(); it++)
157 if(it->first == element)
221 return m_elements.
empty();
231 return m_elements.
size();
281 m_elements.
erase(it);
294 m_elements.
erase(position);
329 for(
auto& it : m_elements) {
332 handler->
on_read(it.first, it.second);
350 for(
auto& it : m_elements) {
351 callback(it.first, it.second);
Handles iteration of a map.
virtual void on_read(Key, Value)
Called when a key-value pair is read.
MapIterationHandler()
______________ TEMPLATE IMPLEMENTATION ______________
virtual void on_end_of_stream()
Called when the end of the stream is reached.
A list of key-value pairs.
void erase(iterator position)
Removes an element from the map at the specified position.
bool empty()
Returns whether the map is empty.
iterator find(Key)
Finds an element in the map based on the key.
Pair< Key, Value > pop_front()
Removes the first key-value pair from the map and returns it.
Vector< Pair< Key, Value > >::iterator iterator
The iterator type for the map.
Pair< Key, Value > pop_back()
Removes the last key-value pair from the map and returns it.
iterator end()
Returns the end of the map.
iterator push_front(Key, Value)
Adds a new key-value pair to the front of the map.
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Value & operator[](Key)
Overloads the [] operator to return the value of the key.
void reserve(size_t amount)
Reserves space in the map for a certain amount of elements to avoid reallocations.
void iterate(MapIterationHandler< Key, Value > *handler)
Iterates through the map and calls the handler.
void erase(Key)
Removes an element from the map.
void increase_size()
Doubles the size of the map.
iterator begin()
Returns the beginning of the map.
iterator push_back(Key, Value)
Adds a new key-value pair to the end of the map.
void clear()
Removes all elements from the map.
Vector< Pair< Key, Value > > m_elements
The internal storage of the map, a vector of key-value pairs.
void iterate(void(*callback)(Key &, Value &))
Iterates through the map and calls the callback.
int size()
The number of elements in the map.
Second second
The second object (often the value)
Dynamically stores an array of elements.
iterator end() const
Returns the last element of the Vector.
uint32_t size() const
Returns the number of elements in the Vector.
void reserve(size_t amount)
Reserves space in the Vector for a certain amount of elements.
iterator push_front(Type)
Adds an element to the front of the Vector and returns the iterator of the element.
void clear()
Removes all elements from the Vector.
void increase_size()
Increases the size of the Vector by doubling the capacity.
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Type pop_back()
Removes the last element from the Vector.
iterator begin() const
Returns the first element of the Vector.
Type pop_front()
Removes the m_first_memory_chunk element from the Vector.
bool empty() const
Checks if the Vector is empty.
void erase(Type)
Removes all elements from the Vector that are equal to the element.
Defines a Pair class for storing two related objects together.
Defines a Vector class for dynamically storing an array of elements.