-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashtable.h
More file actions
executable file
·166 lines (154 loc) · 3.26 KB
/
Hashtable.h
File metadata and controls
executable file
·166 lines (154 loc) · 3.26 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
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include "Bintree.h"
#ifndef NULL
#define NULL 0
#endif
#define DEBUG std::cout << "fuck" << std::endl;
#define HASH_TABLE_SIZE 4096
template <typename Key,typename Value>
struct Key_Value_couple
{
Key key;
Value value;
Key_Value_couple(){}
bool operator == (Key_Value_couple ano){
return this->key == ano.key;
}
void operator = (Key_Value_couple ano){
this -> key = ano.key;
this -> value = ano.value;
}
};
template <typename Key,typename Value>
struct HashLinkNode
{
HashLinkNode *next;
Key_Value_couple<Key,Value> data;
HashLinkNode(){
this -> next = NULL;
}
};
template <typename Key,typename Value>
class HashLinkList
{
private:
HashLinkNode<Key,Value> *head;
HashLinkNode<Key,Value> *tail;
int _size;
public:
HashLinkList(){
this -> head = new HashLinkNode<Key,Value>();
this -> tail = new HashLinkNode<Key,Value>();
this -> head -> next = this -> tail;
this -> tail -> next = NULL;
this -> _size = 0;
}
~HashLinkList(){
HashLinkNode<Key,Value> *tra = head;
HashLinkNode<Key,Value> *tmp = head -> next;
while(tra){
tmp = tra -> next;
delete tra;
tra = tmp;
}
}
void insert(Key_Value_couple<Key,Value> data){
if(_size == 0){
head -> data = data;
_size++;
}
HashLinkNode<Key,Value> *new_node = new HashLinkNode<Key,Value>();
new_node -> data.key = data.key;
new_node -> data.value = data.value;
new_node -> next = this -> head;
this -> head = new_node;
this -> _size++;
}
void update(Key target,Value value){
HashLinkNode<Key,Value> *tra = this -> head;
while(tra != tail){
if(tra -> data.key == target){
tra -> data.value = value;
return;
}
tra = tra -> next;
}
}
bool found(Key target){
HashLinkNode<Key,Value> *tra = this -> head;
while(tra != tail){
std::cout << tra -> data.key<< std::endl;
if(tra -> data.key == target){
return true;
}
tra = tra -> next;
}
return false;
}
Value find(Key target){
HashLinkNode<Key,Value> *tra = this -> head;
while(tra != tail){
if(tra -> data.key == target){
return tra -> data.value;
}
tra = tra -> next;
}
}
int size(){
return this -> _size;
}
};
template <typename Key,typename Value>
class HashTable
{
private:
HashLinkList<Key,Value> *table;
int _size;
protected:
int get_hash_code(Key key){
int k = (int)key;
return k % HASH_TABLE_SIZE;
}
public:
HashTable(){
_size = 0;
table = new HashLinkList<Key,Value>[HASH_TABLE_SIZE];
}
~HashTable(){
delete[] table;
}
int size(){
return this -> _size;
}
void insert(Key key,Value value){
int hashcode = get_hash_code(key);
Key_Value_couple<Key,Value> data;
data.key = key;
data.value = value;
if(table[hashcode].size() == 0 || (!table[hashcode].found(key))){
table[hashcode].insert(data);
_size++;
}else{
table[hashcode].update(data.key,data.value);
}
}
Value find(Key key){
int hashcode = get_hash_code(key);
return table[hashcode].find(key);
}
//many bugs
// Value& operator[](Key key){
// int hashcode = get_hash_code(key);
// if(table[hashcode].find(key,value)){
// return value;
// }else{
// Key_Value_couple<Key,Value> data;
// data.key = key;
// table[hashcode].insert(data);
// DEBUG
// table[hashcode].find(key,value);
// _size++;
// return value;
// }
// }
};