-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.cpp
More file actions
154 lines (129 loc) · 3.16 KB
/
Copy pathHashMap.cpp
File metadata and controls
154 lines (129 loc) · 3.16 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
#include "HashMap.h";
#include "Move.h";
#include "Pokemon.h";
//method to perform seperate chaining and insert Node
void HashMap::insertNode(Node* k, Move move) {
//iterates until it can place the new Node
while (!(k->next == nullptr)) {
k = k->next;
}
if (k->next == nullptr) {
k->next = new Node(move);
}
}
//return the move based on Name
Move HashMap::get(string key) {
int hashNum = hashFunction(key);
if (!arrayList[hashNum].hasValue) {
throw std::invalid_argument("Move does not exist in hash Map");
}
else {
return searchLinkedList(&arrayList[hashNum], key);
}
};
//returns the hash value based on name
int HashMap::hashFunction(string name) {
int key = 0;
for (int i = 0; i < name.size(); i++) {
key += name[i];
}
vector<Node> arrayList(loadFactor);
key = key % size;
return key;
}
//removes the value based on the name
void HashMap::remove(string key) {
int hashNum = hashFunction(key);
if (!arrayList[hashNum].hasValue) {
throw std::invalid_argument("Move does not exist in hash Map");
}
else {
if (arrayList[hashNum].val.getMoveName() == key) {
arrayList.erase(arrayList.begin() + hashNum);
numelements--;
}
else {
bool removed = removeNode(&arrayList[hashNum], key);
if (!removed) {
throw std::invalid_argument("Move does not exist in hash Map");
}
else {
numelements--;
}
}
}
};
//searches for the bestMove Possible and reurns a subset of 4 of the strongest moves for the type
vector<Move> HashMap::searchBestMove(Pokemon poke) {
vector<Move> s;
for (int i = 0; i < arrayList.size(); i++) {
Node* k = &arrayList[i];
while (k != nullptr) {
if (k->val.getMoveType() == poke.getType1() || ((k->val.getMoveType() == poke.getType2()) && poke.getType2() != ""))
{
if (s.size() < 4) {
s.push_back(k->val);
}
else {
int min = 0;
for (int j = 0; j < 4; j++) {
if (s[min].getPower() > s[j].getPower()) {
min = j;
}
}
if(s[min].getPower() < k->val.getPower()){
s[min] = k->val;
}
}
}
k = k->next;
}
}
return s;
}
//searches the LinkedList for the name
Move HashMap::searchLinkedList(Node* k, string name) {
while (k != nullptr) {
if (k->val.getMoveName() == name) {
return k->val;
}
else {
k = k->next;
}
}
throw std::invalid_argument("Move does not exist in hash Map");
}
//removes the Node from the index from the Linked List
bool HashMap::removeNode(Node* k, string name) {
while (k->next != nullptr) {
Node* child = k->next;
if (child->val.getMoveName() == name) {
k->next = child->next;
delete child;
return true;
}
else {
k = child;
}
}
return false;
}
//inserts the value in the HashMap
void HashMap::insert(Move value) {
//returns the hash from the Move
int key = hashFunction(value.getMoveName());
//inserts the Node into the array
if (arrayList[key].hasValue) {
insertNode(&arrayList[key], value);
}
else {
arrayList[key] = Node(value);
}
//increases the num of elements
numelements++;
//resizes the array based on the load factor
if ((double(numelements) / double(arrayList.size())) > loadFactor) {
size = (arrayList.size() / loadFactor);
arrayList.resize(size);
}
};