-
Notifications
You must be signed in to change notification settings - Fork 2
/
HashMap.h
112 lines (99 loc) · 2.42 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
/*
||
|| @file HashMap.h
|| @version 1.0 Beta
|| @author Alexander Brevig
|| @contact [email protected]
||
|| @description
|| | This library provides a simple interface for storing data with an associate key
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/
#ifndef HASHMAP_H
#define HASHMAP_H
#include <WProgram.h>
/* Handle association */
template<typename hash,typename map>
class HashType {
public:
HashType(){ reset(); }
HashType(hash code,map value):hashCode(code),mappedValue(value){}
void reset(){ hashCode = 0; mappedValue = 0; }
hash getHash(){ return hashCode; }
void setHash(hash code){ hashCode = code; }
map getValue(){ return mappedValue; }
void setValue(map value){ mappedValue = value; }
HashType& operator()(hash code, map value){
setHash( code );
setValue( value );
}
private:
hash hashCode;
map mappedValue;
};
/*
Handle indexing and searches
TODO - extend API
*/
template<typename hash,typename map>
class HashMap {
public:
HashMap(HashType<hash,map>* newMap,byte newSize){
hashMap = newMap;
size = newSize;
for (byte i=0; i<size; i++){
hashMap[i].reset();
}
}
HashType<hash,map>& operator[](int x){
//TODO - bounds
return hashMap[x];
}
byte getIndexOf( hash key ){
for (byte i=0; i<size; i++){
if (hashMap[i].getHash()==key){
return i;
}
}
}
map getValueOf( hash key ){
for (byte i=0; i<size; i++){
if (hashMap[i].getHash()==key){
return hashMap[i].getValue();
}
}
}
void debug(){
for (byte i=0; i<size; i++){
Serial.print(hashMap[i].getHash());
Serial.print(" - ");
Serial.println(hashMap[i].getValue());
}
}
private:
HashType<hash,map>* hashMap;
byte size;
};
#endif
/*
|| @changelog
|| | 1.0 2009-07-13 - Alexander Brevig : Initial Release
|| #
*/