-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.hpp
More file actions
90 lines (81 loc) · 2.89 KB
/
HashMap.hpp
File metadata and controls
90 lines (81 loc) · 2.89 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
/*
Name: Nicholas Swisher
Purpose: Define a template for a creating a HashMap data structure
*/
#include <iostream>
#include <functional>
#include <stdexcept>
template<typename T>
class HashMap {
public:
HashMap(){
num_buckets_ = 1000;
std::vector<std::vector<T>> new_store(num_buckets_);
store_ = new_store;
}
HashMap(std::vector<T> vals, int size){
num_buckets_ = 1000;
std::vector<std::vector<T>> new_store(num_buckets_);
store_ = new_store;
for(auto val : vals ){
insertElement(val);
}
}
// Input: a value to be put into the hashtable
// Result: an integer with the bucket index
int hashKey(T val) const {
std::hash<T> func;
return func(val) % num_buckets_;
}
// Input: value to be inserted
// Result: calls the hash function to get the index to insert the value into
// if bucket reaches max size the hashtable is expanded and values are redistributed
void insertElement(T val){
int key = hashKey(val);
size_t size = store_[key].size();
if(size >= max_size_){
Hashmap_Resize();
key = hashKey(val);
}
store_[key].push_back(val);
}
// Input: value to be searched for
// Result: gets the hash key and check the values in that bucket if found returns true else returns false
bool search(T val) const {
std::size_t idx = hashKey(val);
for(auto i : store_[idx]){
if(i == val){
return true;
}
}
return false;
}
// Input: the value to be deleted
// Result: finds the val and pops it from the internal vector
void deleteElement(T val){
std::size_t idx = hashKey(val);
auto it = find(store_[idx].begin(), store_[idx].end(), val);
if(it != store_[idx].end()){
store_[idx].erase(it);
}
}
// copies values in store into temp container, resizes and reinserts all values
void Hashmap_Resize(){
std::vector<std::vector<T>> new_store = store_;
store_.clear();
num_buckets_ *= 2;
store_.resize(num_buckets_);
for(auto i = new_store.begin(); i != new_store.end(); i++){
for(auto j = i->begin(); j != i->end(); j++){
int key = hashKey(*j);
store_[key].push_back(*j);
}
}
}
// template<typename U>
// friend std::ostream& operator<<(std::ostream& os, const HashMap<U> &m);
private:
int num_buckets_;
std::vector<std::vector<T>> store_;
const std::size_t max_size_ = 100;
};