-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathHashMap.h
More file actions
236 lines (220 loc) · 4.89 KB
/
HashMap.h
File metadata and controls
236 lines (220 loc) · 4.89 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* $Id$
||
|| @author Alexander Brevig <abrevig@wiring.org.co>
|| @url http://wiring.org.co/
|| @url http://alexanderbrevig.com/
|| @contribution Brett Hagman <bhagman@wiring.org.co>
||
|| @description
|| | Implementation of a HashMap data structure.
|| |
|| | Wiring Cross-platform Library
|| #
||
|| @license Please see cores/Common/License.txt.
||
*/
#ifndef HASHMAP_H
#define HASHMAP_H
//for convenience
#define CreateHashMap(hashM, ktype, vtype, capacity) HashMap<ktype,vtype,capacity> hashM
#define CreateComplexHashMap(hashM, ktype, vtype, capacity, comparator) HashMap<ktype,vtype,capacity> hashM(comparator)
template<typename K, typename V, unsigned int capacity>
class HashMap
{
public:
typedef bool (*comparator)(K, K);
/*
|| @constructor
|| | Initialize this HashMap
|| #
||
|| @parameter compare optional function for comparing a key against another (for complex types)
*/
HashMap(comparator compare = 0)
{
cb_comparator = compare;
currentIndex = 0;
}
/*
|| @description
|| | Get the size of this HashMap
|| #
||
|| @return The size of this HashMap
*/
unsigned int size() const
{
return currentIndex;
}
/*
|| @description
|| | Get a key at a specified index
|| #
||
|| @parameter idx the index to get the key at
||
|| @return The key at index idx
*/
const K& keyAt(unsigned int idx)
{
return keys[idx];
}
/*
|| @description
|| | Get a value at a specified index
|| #
||
|| @parameter idx the index to get the value at
||
|| @return The value at index idx
*/
V valueAt(unsigned int idx)
{
return values[idx];
}
/*
|| @description
|| | Check if a new assignment will overflow this HashMap
|| #
||
|| @return true if next assignment will overflow this HashMap
*/
bool willOverflow()
{
return (currentIndex + 1 > capacity);
}
/*
|| @description
|| | An indexer for accessing and assigning a value to a key
|| | If a key is used that exists, it returns the value for that key
|| | If there exists no value for that key, the key is added
|| #
||
|| @parameter key the key to get the value for
||
|| @return The const value for key
*/
const V& operator[](const K& key) const
{
return operator[](key);
}
/*
|| @description
|| | An indexer for accessing and assigning a value to a key
|| | If a key is used that exists, it returns the value for that key
|| | If there exists no value for that key, the key is added
|| #
||
|| @parameter key the key to get the value for
||
|| @return The value for key
*/
V& operator[](const K& key)
{
if (contains(key))
{
return values[indexOf(key)];
}
else if (currentIndex < capacity)
{
keys[currentIndex] = key;
values[currentIndex] = nil;
currentIndex++;
return values[currentIndex - 1];
}
return nil;
}
/*
|| @description
|| | Get the index of a key
|| #
||
|| @parameter key the key to get the index for
||
|| @return The index of the key, or -1 if key does not exist
*/
unsigned int indexOf(const K& key)
{
for (int i = 0; i < currentIndex; i++)
{
if (cb_comparator)
{
if (cb_comparator(key, keys[i]))
{
return i;
}
}
else
{
if (key == keys[i])
{
return i;
}
}
}
return -1;
}
/*
|| @description
|| | Check if a key is contained within this HashMap
|| #
||
|| @parameter key the key to check if is contained within this HashMap
||
|| @return true if it is contained in this HashMap
*/
bool contains(const K& key)
{
for (int i = 0; i < currentIndex; i++)
{
if (cb_comparator)
{
if (cb_comparator(key, keys[i]))
{
return true;
}
}
else
{
if (key == keys[i])
{
return true;
}
}
}
return false;
}
/*
|| @description
|| | Check if a key is contained within this HashMap
|| #
||
|| @parameter key the key to remove from this HashMap
*/
void remove(const K& key)
{
int index = indexOf(key);
if (contains(key))
{
for (int i = index; i < capacity - 1; i++)
{
keys[i] = keys[i + 1];
values[i] = values[i + 1];
}
currentIndex--;
}
}
void setNullValue(V nullv)
{
nil = nullv;
}
protected:
K keys[capacity];
V values[capacity];
V nil;
int currentIndex;
comparator cb_comparator;
};
#endif
// HASHMAP_H