Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
111 changes: 111 additions & 0 deletions Skvorchevsky_Bogdan_cw/src/HashMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <ctime>

#define DValue -1

using namespace std;

template<typename K, typename V>
// Класс элемента хеш-таблицы
class HashNode {
public:
V value;
K key;

// Конструктор класса
HashNode(K key, V value){
this->value = value;
this->key = key;
}
};

template<typename K, typename V>
// Класс хеш-таблицы
class HashMap {
private:
HashNode<K,V> **arr;
int capacity;
int size;
HashNode<K,V> *undef;

public:
// Конструктор класса
HashMap(int cap){
this->capacity = cap;
this->size = 0;
arr = new HashNode<K,V>*[capacity];

for(int i = 0; i < capacity; i++)
arr[i] = NULL;

undef = new HashNode<K,V>(-1, DValue);
}

int hashCode(K key) { // Метод для получения хэша элемента таблицы по ключу
return key % capacity;
}

int insertNode(K key, V value) { // Метод вставки элемента в хеш-таблицу
auto *temp = new HashNode<K,V>(key, value);

int hashIndex = hashCode(key);
int counter = 0;

while(arr[hashIndex] != NULL && arr[hashIndex]->key != -1) {
hashIndex++;
hashIndex %= capacity;
counter++;
}

if(arr[hashIndex] == NULL || arr[hashIndex]->key == -1) {
size++;
arr[hashIndex] = temp;
}
return counter;
}

V deleteNode(int key) { // Метод удаления элемента из хеш-таблицы по ключу
int hashIndex = hashCode(key);

while(arr[hashIndex] != NULL) {
if(arr[hashIndex]->key == key) {
HashNode<K,V> *temp = arr[hashIndex];

arr[hashIndex] = undef;

size--;
return temp->value;
}
hashIndex++;
hashIndex %= capacity;
}

return DValue;
}

void display() { // Метод для вывода хеш-таблицы
cout << "Hash table is:\n";
for (int i = 0; i < capacity; i++) {
if(arr[i] != NULL && arr[i]->key != -1)
cout << "key = " << arr[i]->key
<<" value = "<< arr[i]->value << endl;
}
}

void resize(int new_capacity) { // Метод для изменения размера хеш-таблицы и её очистки
this->capacity = new_capacity;
delete [] arr;
arr = new HashNode<K,V>*[capacity];
for(int i = 0; i < capacity; i++)
arr[i] = NULL;
size = 0;
}

int sizeofMap() { // Метод для вывода размера хеш-таблицы
cout << "Size is " << size << endl;
return size;
}
};
52 changes: 52 additions & 0 deletions Skvorchevsky_Bogdan_cw/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "HashMap.h"

int main() {
int capacity = 512;
auto *h = new HashMap<int, int>(capacity);
unsigned int start_time;
double used_time = 0;
unsigned int bad_key;
int r_value;
int r_key;
int operations_count = 0;
for (int k = 0; k < 7; k++) {
printf("Number of elements is %d\n", capacity);
// Вставка случайных чисел со случайным ключом в хеш-таблицу (средний случай)
for (int i = 0; i < capacity; i++) {
srand((int) clock());
r_key = rand() % 1000000; // Генерация ключа
r_value = rand() % 1000000 + 1; // Генерация значения
start_time = clock();
operations_count += h->insertNode(r_key, r_value);
used_time += ((double) (clock() - start_time)) / CLOCKS_PER_SEC;
}
used_time *= 1000;
used_time /= capacity;
operations_count /= capacity;
printf("Average insertion time and operations count in the average case is %lf ms and %d pieces\n", used_time, operations_count);
used_time = 0;
operations_count = 0;
srand((int) clock());
bad_key = rand() % 10000; // Генерация ключа для худшего случая
h->resize(capacity);
// Вставка случайных чисел с одним и тем же ключом в хеш-таблицу (худший случай)
for (int i = 0; i < capacity; i++) {
srand((int) clock());
r_value = rand() % 1000000 + 1; // Генерация значения
start_time = clock();
operations_count += h->insertNode(bad_key, r_value); // Вызов функции для вставки в хеш-таблицу
used_time += ((double) (clock() - start_time)) / CLOCKS_PER_SEC;
}
used_time *= 1000;
used_time /= capacity;
operations_count /= capacity;
printf("Average insertion time and operations count in the bad case is %lf ms and %d pieces\n", used_time, operations_count);
printf("---------------------------------------------------------\n");
capacity *= 2;
h->resize(capacity);
used_time = 0;
operations_count = 0;
}

return 0;
}