Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct HashNode {
- long long key = 0;
- int offset = 0;
- };
- class HashTable
- {
- private:
- int size;
- int countOfNotes = 0;
- int countOfAdd = 0;
- list<HashNode>* table;
- public:
- HashTable(int size)
- {
- this->size = size;
- table = new list<HashNode>[size];
- }
- void insert(long long key, int offset)
- {
- HashNode node;
- node.key = key;
- node.offset = offset;
- int index = hashFunction(key);
- table[index].push_back(node);
- countOfNotes++;
- countOfAdd++;
- if (countOfNotes / size > 0.75) {
- rehash();
- }
- }
- int hashFunction(int key)
- {
- return key % size;
- }
- void rehash() {
- HashTable newHT(size * 2);
- for (int i = 0; i < size; i++) {
- for (HashNode node : table[i]) {
- newHT.insert(node.key, node.offset);
- }
- }
- size = size * 2;
- table = newHT.table;
- }
- };
- template<typename T>
- bool insertFromFile(T& container) {
- ifstream file(fileName, ios::binary);
- if (file) {
- for (size_t i = 0; i < fileSize; i++) {
- Car car;
- file.read((char*)&car, sizeof(Car));
- container.insert(car.num, i);
- }
- }
- else {
- return false;
- }
- file.close();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment