-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashMap.h
81 lines (69 loc) · 1.12 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
// HashTable.h
#ifndef _HASHMAPE_h
#define _HASHMAPE_h
#include <WString.h>
#include "LinkedList.h"
#include <WString.h>
template <class K, class V>
struct Entry
{
K key;
V value;
};
template <class V>
class HashMap
{
public:
HashMap();
~HashMap();
V put(String key, V value);
V get(String key);
bool remove(String key);
protected:
LinkedList<Entry<String, V> >* list;
};
template <class V>
HashMap<V>::HashMap()
{
list = new LinkedList<Entry<String, V> >();
}
template <class V>
HashMap<V>::~HashMap()
{
delete list;
}
template <class V>
V HashMap<V>::put(String key, V value)
{
Entry<String, V>* entry = new Entry<String, V>();
entry->key = key;
entry->value = value;
list->add(*entry);
return value;
}
template <class V>
V HashMap<V>::get(String key)
{
for (int i = 0; i < list->size(); i++)
{
if (list->get(i).key.equalsIgnoreCase(key))
{
return list->get(i).value;
}
}
return V();
}
template <class V>
bool HashMap<V>::remove(String key)
{
for (int i = 0; i < list->size(); i++)
{
if (list->get(i).key.equalsIgnoreCase(key))
{
list->remove(i);
return true;
}
}
return false;
}
#endif