diff --git a/Pavlov_Dmitry_cw/Pavlov_Dmitry_cw.pdf b/Pavlov_Dmitry_cw/Pavlov_Dmitry_cw.pdf new file mode 100644 index 0000000..46cab82 Binary files /dev/null and b/Pavlov_Dmitry_cw/Pavlov_Dmitry_cw.pdf differ diff --git a/Pavlov_Dmitry_cw/src/HashTable.h b/Pavlov_Dmitry_cw/src/HashTable.h new file mode 100644 index 0000000..bb5ce32 --- /dev/null +++ b/Pavlov_Dmitry_cw/src/HashTable.h @@ -0,0 +1,89 @@ +#ifndef COURSEWORK_HASHTABLE_H +#define COURSEWORK_HASHTABLE_H +#include +#include + + +using namespace std; + +//Creating a hashtable class +class HashTable{ +private: + list *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[total_elements]; + } + //Insertion method + int insertElement(int key){ + int operation_counter = 1; + unsigned hash = getHash(abs(key)); + list& 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::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[this->total_elements]; + } +}; + + +#endif //COURSEWORK_HASHTABLE_H diff --git a/Pavlov_Dmitry_cw/src/main.cpp b/Pavlov_Dmitry_cw/src/main.cpp new file mode 100644 index 0000000..f04465d --- /dev/null +++ b/Pavlov_Dmitry_cw/src/main.cpp @@ -0,0 +1,63 @@ +#include +#include "HashTable.h" +#include +#include + +using namespace std; + + +int main() { + std::mt19937 rndGen(static_cast(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(rndGen()); // Generating a value + clocks = clock(); + operCounter += avrHT.insertElement(value); + + clocks = clock() - clocks; + time += static_cast(clocks) / CLOCKS_PER_SEC; + } + + if(i == 1){ + avrHT.printAll(); + } + avrTime = time / (float )testCount; + operCounter /= testCount; + std::cout << "Elements - " << testCount << " Average case insertion - " <(clocks) / CLOCKS_PER_SEC; + } + + + + avrTime = time / (float )testCount; + operCounter /= testCount; + std::cout << "Elements - " << testCount << " Bad case insertion - " <