-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtHashTable.cpp
More file actions
142 lines (118 loc) · 3.21 KB
/
ExtHashTable.cpp
File metadata and controls
142 lines (118 loc) · 3.21 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
#include <iostream>
#include <cstdlib>
#include <string.h>
#include "ExtHashTable.h"
ExtHashTable::ExtHashTable( double upper_bound_ratio, double lower_bound_ratio, int size) :
HashTable(size),
upper_bound_ratio(upper_bound_ratio),
lower_bound_ratio(lower_bound_ratio){}
ExtHashTable::ExtHashTable(const ExtHashTable &t) :
HashTable(t),
upper_bound_ratio (t.upper_bound_ratio),
lower_bound_ratio (t.lower_bound_ratio){}
void ExtHashTable::rehash() {
if(size==0){return;}
if(((double)size/capacity)>upper_bound_ratio){
rehashing(capacity*2);
}
else if(((double)size/capacity)<lower_bound_ratio){
rehashing(capacity/2);
}
}
void ExtHashTable::rehashing(int newcapacity){
HashTable newHashTable (newcapacity);
for(int i=0; i<this->capacity; i++){
if(!this->isAvailable(i)){
newHashTable.add(this->table[i]);
}
}
this->size = newHashTable.getSize();
this->capacity = newHashTable.getCapacity();
delete []this->table;
this->table = new string[this->capacity];
for(int i =0; i<newHashTable.getCapacity(); i++){
this->table[i].assign(newHashTable.table[i]);
}
cout<<"--> Size: "<<newHashTable.getSize()<<", New capacity: "<<newcapacity<<"\n";
}
bool ExtHashTable::add(const string &str) {
return add(str.c_str());
}
bool ExtHashTable::add(const char *s) {
if(this->HashTable::add(s)){
rehash();
return true;
}
return false;
}
bool ExtHashTable::remove(const string &str) {
return remove(str.c_str());
}
bool ExtHashTable::remove(const char *s) {
if(this->HashTable::remove(s)){
rehash();
return true;
}
return false;
}
ExtHashTable ExtHashTable::operator+(const string &str) const{
ExtHashTable newHashTable (*this);
newHashTable.add(str);
return newHashTable;
}
ExtHashTable ExtHashTable::operator+(const char* s) const{
ExtHashTable newHashTable (*this);
newHashTable.add(s);
return newHashTable;
}
ExtHashTable ExtHashTable::operator-(const string &str) const{
ExtHashTable newHashTable (*this);
newHashTable.remove(str);
return newHashTable;
}
ExtHashTable ExtHashTable::operator-(const char *s) const{
ExtHashTable newHashTable (*this);
newHashTable.remove(s);
return newHashTable;
}
bool ExtHashTable::operator += (const string &str) {
return this->add(str);
}
bool ExtHashTable::operator += (const char* s) {
return this->add(s);
}
bool ExtHashTable::operator -= (const string &str) {
return this->remove(str);
}
bool ExtHashTable::operator -= (const char *s) {
return this->add(s);
}
ExtHashTable ExtHashTable::operator+(const ExtHashTable &table) const {
ExtHashTable newHashTable (*this);
for(int i=0; i<table.capacity; i++){
if(!table.isAvailable(i)){
newHashTable.add(table.table[i]);
}
}
return newHashTable;
}
ExtHashTable & ExtHashTable::operator+=(const ExtHashTable &table) {
for(int i=0; i<table.capacity; i++){
if(!table.isAvailable(i)){
this->add(table.table[i]);
}
}
return *this;
}
ExtHashTable & ExtHashTable::operator=(const ExtHashTable &t) {
this->size = t.size;
this->capacity = t.capacity;
this->upper_bound_ratio = t.upper_bound_ratio;
this->lower_bound_ratio = t.lower_bound_ratio;
delete []this->table;
this->table = new string[t.capacity];
for(int i =0; i<t.getCapacity(); i++){
this->table[i].assign(t.table[i]);
}
return *this;
}