forked from Turupawn/MapDelete-Exists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.cpp
65 lines (54 loc) · 1.09 KB
/
Map.cpp
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
#include "Map.h"
int Map::hash(string key)
{
int sum = 0;
for(int i=0;i < (int)key.length();i++)
sum+=key[i];
return sum;
}
Map::Map()
{
for(int i=0;i<999999;i++)
{
array.push_back(new vector< pair<string,int>* >);
}
}
void Map::put(string key, int value)
{
vector< pair<string, int>* > * v = array[hash(key)];
v->push_back(new pair<string, int>(key, value));
}
int Map::get(string key)
{
vector< pair<string, int>* > * v = array[hash(key)];
for(int i=0; i< (int)v->size(); i++)
{
pair<string, int>* current_pair = (*v)[i];
if(current_pair->first == key)
{
return current_pair->second;
}
}
return -1;
}
bool Map::exists(string key)
{
vector< pair<string, int>* > * v = array[hash(key)];
return get(key) != -1;
}
void Map::remove(string key)
{
if(exists(key))
{
vector< pair<string, int>* > * v = array[hash(key)];
for(unsigned int i = 0; i < v->size(); ++i)
{
pair<string, int>* current_pair = (*v)[i];
if(current_pair->first == key)
{
v->erase(v->begin() + i);
return;
}
}
}
}