-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleHashTable.h
164 lines (140 loc) · 3.42 KB
/
DoubleHashTable.h
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
#ifndef DOUBLE_HASH_TABLE_H
#define DOUBLE_HASH_TABLE_H
#include "Exception.h"
#include "ece250.h"
enum state { EMPTY, OCCUPIED, DELETED };
template<typename T>
class DoubleHashTable {
private:
int count;
int power;
int array_size;
T *array;
state *array_state;
int h1( T const & ) const; // first hash function
int h2( T const & ) const; // second hash function
public:
DoubleHashTable( int = 5 );
~DoubleHashTable();
int size() const;
int capacity() const;
bool empty() const;
bool member( T const & ) const;
T bin( int ) const;
void print() const;
void insert( T const & );
bool remove( T const & );
void clear();
};
template<typename T >
DoubleHashTable<T >::DoubleHashTable( int m ):
count( 0 ), power( m ),
array_size( 1 << power ),
array( new T [array_size] ),
array_state( new state[array_size] ) {
for ( int i = 0; i < array_size; ++i ) {
array_state[i] = EMPTY;
}
}
template<typename T >
DoubleHashTable<T >::~DoubleHashTable() {
delete [] array;
delete [] array_state;
}
template<typename T >
int DoubleHashTable<T >::size() const {
return count;
}
template<typename T >
int DoubleHashTable<T >::capacity() const {
return array_size;
}
template<typename T >
bool DoubleHashTable<T >::empty() const {
return (count == 0);
}
template<typename T >
int DoubleHashTable<T >::h1( T const &obj ) const {
int index1 = 0;
index1 = static_cast<int>(obj) % array_size;
if (index1 < 0){
index1 += array_size;
}
return index1;
}
template<typename T >
int DoubleHashTable<T >::h2( T const &obj ) const {
int index2 = 0;
index2 = (static_cast<int>(obj)/array_size) % array_size;
if(index2%2==0){
++index2;
}
if(index2 < 0){
index2 += array_size;
}
return index2;
}
template<typename T >
bool DoubleHashTable<T >::member( T const &obj ) const {
int probe = h1(obj);
int offset = h2(obj);
if ( !empty() ){
while (array_state[probe] == OCCUPIED && array[probe] != obj){
probe = (probe + offset)%array_size;
}
}
return( array[probe] == obj);
}
template<typename T >
T DoubleHashTable<T >::bin( int n ) const {
if (array_state[n] = OCCUPIED)
return array[n];
}
template<typename T >
void DoubleHashTable<T >::insert( T const &obj ) {
int probe = h1(obj);
int offset = h2(obj);
if ( count == array_size ){
throw overflow();
}
while (array_state[probe] == OCCUPIED){
probe = (probe + offset)%array_size;
}
array[probe] = obj;
array_state[probe] = OCCUPIED;
++count;
}
template<typename T >
bool DoubleHashTable<T >::remove( T const &obj ) {
int probe = h1(obj);
int offset = h2(obj);
if ( empty() ){
throw overflow();
}
if (member(obj)){
while (array_state[probe] == OCCUPIED && array[probe] != obj){
probe = (probe + offset)%array_size;
}
array_state[probe] = DELETED;
return true;
}
return false;
}
template<typename T >
void DoubleHashTable<T >::clear() {
for (int i = 0; i < array_size; ++i){
//array[i] = 0;
array_state[i] = EMPTY;
}
count = 0;
}
template<typename T >
void DoubleHashTable<T >::print() const {
for (int i = 0; i < array_size; ++i){
std::cout << "INDEX: " << i <<" ";
std::cout << "KEY: " << array[i] <<" ";
std::cout << "STATE: "<< array_state[i] << std::endl;
}
return;
}
#endif