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 added Pavlov_Dmitry_cw/Pavlov_Dmitry_cw.pdf
Binary file not shown.
89 changes: 89 additions & 0 deletions Pavlov_Dmitry_cw/src/HashTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef COURSEWORK_HASHTABLE_H
#define COURSEWORK_HASHTABLE_H
#include <iostream>
#include <list>


using namespace std;

//Creating a hashtable class
class HashTable{
private:
list<int> *table;
int total_elements;

[[nodiscard]] int getHash(int key) const{
return key % total_elements;
}

public:
//Contructor
explicit HashTable(int n){
total_elements = n;
table = new list<int>[total_elements];
}
//Insertion method
int insertElement(int key){
int operation_counter = 1;
unsigned hash = getHash(abs(key));
list<int>& valueList = table[hash];
for(auto& elem : valueList){
if (elem == key){
elem = key;
return operation_counter++;
}
operation_counter++;
}
int newElem = key;
table[hash].push_back(newElem);
return operation_counter;
}
//Removal method
void removeElement(int key){
int x = getHash(key);

list<int>::iterator i;
for (i = table[x].begin(); i != table[x].end(); i++) {
if (*i == key)
break;
}


if (i != table[x].end())
table[x].erase(i);
else{
cout << "[WARNING] Key not found!\n";
}

}
//Method to print all hash table
void printAll(){
for(int i = 0; i < total_elements; i++){
cout << "Index " << i << ": ";
for(int j : table[i]) {
cout << j << " => ";
}

cout << endl;
}
}
//Method to print list of values by key
void printSearch(int key){
for(int i = 0; i < total_elements; i++){
if (i == key){
for(int j : table[i]) {
cout << j << " => ";
}
}

}
}
//Clear hash table values
void clear(){
delete [] table;
table = new list<int>[this->total_elements];
}
};


#endif //COURSEWORK_HASHTABLE_H
63 changes: 63 additions & 0 deletions Pavlov_Dmitry_cw/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include "HashTable.h"
#include <ctime>
#include <random>

using namespace std;


int main() {
std::mt19937 rndGen(static_cast<unsigned>(time(nullptr))); // Generator Mersenne twister
int testCount = 512; // Start test data
for (int i = 0; i < 7; ++i) {
int operCounter = 0; // Operation counter
HashTable avrHT(testCount);

clock_t clocks;
float time = 0;
float avrTime;
avrHT.clear();
//
for (int j = 0; j < testCount; ++j) {
int value = static_cast<int>(rndGen()); // Generating a value
clocks = clock();
operCounter += avrHT.insertElement(value);

clocks = clock() - clocks;
time += static_cast<float>(clocks) / CLOCKS_PER_SEC;
}

if(i == 1){
avrHT.printAll();
}
avrTime = time / (float )testCount;
operCounter /= testCount;
std::cout << "Elements - " << testCount << " Average case insertion - " <<fixed<< avrTime << " sec " <<"Operation Counts - "<< operCounter << std::endl;
time = 0;



avrHT.clear();
long long int base = 1;
while (base <= testCount){
base <<= 1;
}
operCounter = 0;
for (int j = 0; j < testCount; ++j) {
long long int key = (j+1)*base;
clocks = clock();
operCounter += avrHT.insertElement(key);
clocks = clock() - clocks;
time += static_cast<float>(clocks) / CLOCKS_PER_SEC;
}



avrTime = time / (float )testCount;
operCounter /= testCount;
std::cout << "Elements - " << testCount << " Bad case insertion - " <<fixed<< avrTime << " sec" <<" Average operation counts - "<< operCounter << std::endl << std::endl;
testCount *= 2;

}
return 0;
}