Pico-Arduino
Map.h
1 #pragma once
2 
3 #include "Vector.h"
4 
5 namespace pico_arduino {
6 
13 template <class K, class V>
14 class Map {
15  protected:
16 
23  template <class KM, class VM>
24  struct MapEntry {
25  VM value;
26  KM key;
27  };
28  V* empty_ptr;
30 
31  V &find_entry(K key){
32  for ( auto it = data.begin(); it != data.end(); ++it){
33  if ((*it).key==key){
34  return (*it).value;
35  }
36  }
37  return *empty_ptr;
38  }
39 
40 
41  public:
47  Map(const V &empty){
48  this->empty_ptr = (V*) &empty;
49  }
50 
52  V &get(K key){
53  return find_entry(key);
54  }
55 
57  void put(K key,V value){
58  V value_found = find_entry(key);
59  // check if enty exists
60  if (&value_found == empty_ptr ){
61  // add new entry
62  MapEntry<K,V> new_entry = {value, key};
63  data.push_back(new_entry);
64  }
65  }
66 
67 };
68 
69 }
A simple key value map collection.
Definition: Map.h:14
V & get(K key)
Gets an element by key.
Definition: Map.h:52
void put(K key, V value)
Adds an element with a key.
Definition: Map.h:57
Map(const V &empty)
Construct a new Map object.
Definition: Map.h:47
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:15
Pico Arduino Framework.
Definition: Arduino.cpp:26
Key/Value.
Definition: Map.h:24