-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.h
More file actions
89 lines (87 loc) · 1.66 KB
/
HashTable.h
File metadata and controls
89 lines (87 loc) · 1.66 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
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include"Alloctor.h"
#include"HashFunc.h"
#include"Vector.h"
#include"List.h"
#define init_size 5
#include<iostream>
template<class T>
class t_node{
public:
t_node(int k,const T& e):key(k),elem(e){
}
t_node(const t_node& init):key(init.key),elem(init.elem){
};
t_node(){
};
int key;
T elem;
};
template<class K,class E >
class HashTable{
public:
HashTable(){
init();
size_num=0;
};
int size(){
return size_num;
}
void put(K,E);
E get(K);
private:
void init();
int vaild_size=init_size;
int size_num;
Alloctor<List<t_node<E>>> alloc;
void extend();
Vector<List<t_node<E>>*> v;
inline int posit(int input){
return input%vaild_size;
}
};
template<class K,class E>
void HashTable<K,E>::extend(){
t_node<E> temp[size_num];
for(int i=0,k=0;i<v.size();++i){
for(int j=0;j<(*v.get(i)).size();++j){
temp[k++]=(*v.get(i)).get(j);
}
}
v.clear();
vaild_size*=3;
init();
for( t_node<E> node:temp){
put(node.key,node.elem);
size_num--;
}
}
template<class K,class E>
void HashTable<K,E>::init(){
for(int i=0;i<vaild_size;++i){
auto p=alloc.allocate(1);
v.push_back(p);
*(p)=List<t_node<E>>();
}
}
template<class K,class E>
void HashTable<K,E>::put(K key,E elem){
int p=posit(hash_func(key));
t_node<E> node=t_node<E>(key,elem);
(*v.get(p)).add(node);
size_num++;
if(size_num>2*vaild_size) extend();
}
template<class K,class E>
E HashTable<K,E>::get(K key){
int hash_code=hash_func(key);
int p=posit(hash_code);
List<t_node<E>> list=*v.get(p);
for(int i=0;i<list.size();++i){
if(list.get(i).key==hash_code)
return list.get(i).elem;
}
return E{};
}
#endif