std::vector<Entry> table;
60 return1 + (7 - (hash(key) % 7));
72 inthash =
static_cast<int>(
hashFxn(key));
77 static_cast<int>(hash + (i *
otherHashFxn(key))) % totalSize;
80 if(entry.
key== notPresent) {
84std::cout <<
"Found key!"<< std::endl;
87std::cout <<
"Found tombstone or equal hash, checking next" 93std::cout <<
"Spot found!"<< std::endl;
98std::cout <<
"Spot taken, looking at next (next index:" 106 if(i == totalSize * 100) {
107std::cout <<
"DoubleHash probe failed"<< std::endl;
110}
while(entry.
key!= notPresent);
121 if(entry.
key== notPresent || entry.
key== tomb) {
134 if(entry.
key== key) {
144 for(
inti = 0; i < totalSize; i++) {
145 if(table[i].key == notPresent) {
146std::cout <<
" Empty ";
147}
else if(table[i].key == tomb) {
148std::cout <<
" Tomb ";
151std::cout << table[i].key;
155std::cout << std::endl;
164 intoldSize = totalSize;
165std::vector<Entry> oldTable(table);
167table = std::vector<Entry>(totalSize * 2);
169 for(
inti = 0; i < oldSize; i++) {
170 if(oldTable[i].key != -1 && oldTable[i].key != notPresent) {
172 add(oldTable[i].key);
179std::cout <<
"Table was rehashed, new size is: "<< totalSize << std::endl;
189table[index].key = key;
191 if(++size /
static_cast<double>(totalSize) >= 0.5) {
201 if(index == notPresent) {
202std::cout <<
"key not found"<< std::endl;
204table[index].key = tomb;
205std::cout <<
"Removal successful, leaving tombstone"<< std::endl;
213std::cout <<
"Initial table: ";
215std::cout << std::endl;
216std::cout <<
"hash of "<< key <<
" is "<<
hashFxn(key) <<
" % " 217<< totalSize <<
" == "<<
hashFxn(key) % totalSize;
218std::cout << std::endl;
220std::cout <<
"New table: ";
228std::cout <<
"Initial table: ";
230std::cout << std::endl;
231std::cout <<
"hash of "<< key <<
" is "<<
hashFxn(key) <<
" % " 232<< totalSize <<
" == "<<
hashFxn(key) % totalSize;
233std::cout << std::endl;
235std::cout <<
"New table: ";
244usingdouble_hashing::table;
245usingdouble_hashing::totalSize;
251 intcmd = 0, key = 0;
252std::cout <<
"Enter the initial size of Hash Table. = ";
253std::cin >> totalSize;
254table = std::vector<Entry>(totalSize);
257std::cout << std::endl;
258std::cout <<
"PLEASE CHOOSE -"<< std::endl;
259std::cout <<
"1. Add key. (Numeric only)"<< std::endl;
260std::cout <<
"2. Remove key."<< std::endl;
261std::cout <<
"3. Find key."<< std::endl;
262std::cout <<
"4. Generate Hash. (Numeric only)"<< std::endl;
263std::cout <<
"5. Display Hash table."<< std::endl;
264std::cout <<
"6. Exit."<< std::endl;
268std::cout <<
"Enter key to add = ";
273std::cout <<
"Enter key to remove = ";
278std::cout <<
"Enter key to search = ";
281 if(entry.
key== double_hashing::notPresent) {
282std::cout <<
"Key not present";
287std::cout <<
"Enter element to generate hash = ";
289std::cout <<
"Hash of "<< key
300std::cout << std::endl;
An implementation of hash table using double hashing algorithm.
size_t hashFxn(int key)
Hash a key. Uses the STL library's std::hash() function.
bool searchingProber(const Entry &entry, int key)
size_t otherHashFxn(int key)
Used for second hash function.
void removalInfo(int key)
int doubleHash(int key, bool searching)
Performs double hashing to resolve collisions.
bool putProber(const Entry &entry, int key)
Entry(int key=notPresent)
constructor
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4