-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashMap.h
162 lines (136 loc) · 3.66 KB
/
HashMap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef _HASHMAP_H
#define _HASHMAP_H
#include "Arduino.h"
template<typename K, typename V, unsigned int capacity>
class HashMap {
public:
HashMap();
~HashMap();
unsigned int Size();
void Put(K key, V value);
K GetKeyAt(int pos);
V GetValueAt(int pos);
V *GetValuePointerAt(int pos);
V Get(K key);
V *GetPointer(K key);
V Get(K key, V defaultValue);
bool ContainsKey(K key);
unsigned int GetIndexOfKey(K key);
void Remove(K key);
void Dump();
void DumpKeys();
void Clear();
int GetCapacity();
protected:
K m_keys[capacity];
V m_values[capacity];
int m_position;
int m_capacity;
};
template<typename K, typename V, unsigned int capacity>
int HashMap<K, V, capacity>::GetCapacity() {
return m_capacity;
}
template<typename K, typename V, unsigned int capacity>
HashMap<K, V, capacity>::HashMap() {
m_capacity = capacity;
Clear();
}
template<typename K, typename V, unsigned int capacity>
void HashMap<K, V, capacity>::Clear() {
m_position = 0;
}
template<typename K, typename V, unsigned int capacity>
HashMap<K, V, capacity>::~HashMap() {
}
template<typename K, typename V, unsigned int capacity>
unsigned int HashMap<K, V, capacity>::Size() {
return m_position;
}
template<typename K, typename V, unsigned int capacity>
void HashMap<K, V, capacity>::Put(K key, V value) {
m_keys[m_position] = key;
m_values[m_position] = value;
m_position++;
}
template<typename K, typename V, unsigned int capacity>
V HashMap<K, V, capacity>::Get(K key) {
unsigned int index = GetIndexOfKey(key);
if (index != -1) {
return m_values[index];
}
return nullptr;
}
template<typename K, typename V, unsigned int capacity>
V *HashMap<K, V, capacity>::GetPointer(K key) {
unsigned int index = GetIndexOfKey(key);
if (index != -1) {
return &m_values[index];
}
return nullptr;
}
template<typename K, typename V, unsigned int capacity>
K HashMap<K, V, capacity>::GetKeyAt(int pos) {
return m_keys[pos];
}
template<typename K, typename V, unsigned int capacity>
V HashMap<K, V, capacity>::GetValueAt(int pos) {
return m_values[pos];
}
template<typename K, typename V, unsigned int capacity>
V *HashMap<K, V, capacity>::GetValuePointerAt(int pos) {
return &m_values[pos];
}
template<typename K, typename V, unsigned int capacity>
V HashMap<K, V, capacity>::Get(K key, V defaultValue) {
unsigned int index = GetIndexOfKey(key);
if (index != -1) {
return m_values[index];
}
else {
return defaultValue;
}
}
template<typename K, typename V, unsigned int capacity>
unsigned int HashMap<K, V, capacity>::GetIndexOfKey(K key) {
for (int i = 0; i < m_position; i++) {
if (key == m_keys[i]) {
return i;
}
}
return -1;
}
template<typename K, typename V, unsigned int capacity>
bool HashMap<K, V, capacity>::ContainsKey(K key) {
return GetIndexOfKey(key) != -1;
}
template<typename K, typename V, unsigned int capacity>
void HashMap<K, V, capacity>::Remove(K key) {
int index = GetIndexOfKey(key);
if (index != -1) {
for (int i = index; i < capacity - 1; i++) {
m_keys[i] = m_keys[i + 1];
m_values[i] = m_values[i + 1];
}
m_position--;
}
}
template<typename K, typename V, unsigned int capacity>
void HashMap<K, V, capacity>::Dump() {
for (unsigned int i = 0; i < Size(); i++) {
Serial.print("Key:");
Serial.print(m_keys[i]);
Serial.print(" Val:");
Serial.print(m_values[i]);
Serial.println();
}
}
template<typename K, typename V, unsigned int capacity>
void HashMap<K, V, capacity>::DumpKeys() {
for (unsigned int i = 0; i < Size(); i++) {
Serial.print("Key:");
Serial.print(m_keys[i]);
Serial.println();
}
}
#endif