Bachelors_Thesis_Code/helper_code/fagprojekt_code/hash_table.cpp
Knyffen 03ec008d0d A bit of cleanup
Move some helper files to other folders.

Actually remove the .sage files from the main directory, as I wrote I
would in the readme.
2021-11-14 14:48:58 +01:00

57 lines
1.3 KiB
C++

#include "hash_table.hpp"
#include "hashed_string.hpp"
#include <stdint.h>
#include <string.h>
Hash_table::Hash_table(Hash_function * hash_function) : hash_function(hash_function) {
m = hash_function->get_m();
words = new Hashed_string[m];
}
Hash_table::~Hash_table() {
delete hash_function;
delete[] words;
}
size_t Hash_table::get_distict_words() {
return distinct_words;
}
void Hash_table::hash(My_string * word) {
uint64_t hash = hash_function->hash(word);
distinct_words += words[hash].append(new Hashed_string(word));
}
void Hash_table::hash(Hashed_string * hs) {
uint64_t hash = hash_function->hash( &(hs->string) );
distinct_words += words[hash].append(hs);
}
void Hash_table::rehash(Hash_function * hash_function) {
delete this->hash_function;
this->hash_function = hash_function;
size_t old_m = m;
m = this->hash_function->get_m();
Hashed_string * old_words = this->words;
words = new Hashed_string[m];
distinct_words = 0; // reset, because we are hashing all the words once more
for (size_t i = 0; i < old_m; i++) {
Hashed_string * hs = &(old_words[i]);
// skip the initializer Hashed_string
while (hs->next != NULL) {
hs = hs->next;
hash(new Hashed_string(hs));
}
}
// delete the old stuff
delete[] old_words;
}