-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.hpp
More file actions
120 lines (118 loc) · 2.2 KB
/
Copy pathHashMap.hpp
File metadata and controls
120 lines (118 loc) · 2.2 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
#pragma once
#include"ExpandableLinkedHashTable.hpp"
#include"DbListNode.hpp"
#include"DbLinkedList.hpp"
#include<set>
#include <utility>
#include<unordered_set>
template<typename K,typename V>
class HashMap
{
public:
//int BucketSize;
//int MaxLoadFactor;
//int Default_BucketSize=16;
//int Default_MaxLoadFactor=0.7;
//struct Element
//{
// K key;
// V value;
// bool operator==(const Element &other)const
// {
// return(key == other.key) && (value == other.value);
// }
//};
ExpandableLinkedHashTable<K, std::pair<K,V>>* table = nullptr;
HashMap()
{
/*BucketSize = Default_BucketSize;
MaxLoadFactor = Default_MaxLoadFactor;*/
table = new ExpandableLinkedHashTable<K, std::pair<K, V>>();
}
HashMap(int initialSize)
{
/*BucketSize = initialSize;
MaxLoadFactor = Default_MaxLoadFactor;*/
table = new ExpandableLinkedHashTable<K, std::pair<K, V>>(initialSize);
}
HashMap(int initialSize, double MaxLoadFactor)
{
/* BucketSize = initialSize;
this->MaxLoadFactor = MaxLoadFactor;*/
table = new ExpandableLinkedHashTable<K, std::pair<K, V>>(initialSize,MaxLoadFactor);
}
~HashMap()
{
table->Clear();
delete table;
table = nullptr;
}
V getValue(const K& key)
{
int bucket;
V v;
DbListNode<std::pair<K,V>>* node = table->findPos(key, bucket);
if (bucket != -1)
{
return node->data.second;
}
return V();
}
std::pair<K,V> getFront()
{
DbListNode<std::pair<K, V>>* node = table->get_front();
if (node != nullptr)
{
std::pair<K, V> pair = std::make_pair(node->data.first, node->data.second);
return pair;
}
else
{
return std::make_pair(K(), V());
}
}
//std::set<K> keySet()
//{
//
//
//}
bool containsKey(const K& key)
{
return table->Search(key);
}
void Insert(const std::pair<K, V> &k_v)
{
table->Insert(k_v);
}
V Remove(const K& key)
{
std::pair<K, V> e;
if (table->Remove(key, e) != 0)
{
return e.second;
}
return V();
}
V Remove(const K& key, const V& val)
{
std::pair<K,V> e;
if (val==getValue(key))
{
table->Remove(key, e);
return e.second;
}
return V();
}
void Clear()
{
table->Clear();
}
int getSize()
{
return table->getCapcity();
}
void resizeTable()
{
table->resizeTable();
}
};