From 9eef7cb7d5a2cd510de0cf17e729dc702fc19128 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 13 Jan 2023 18:33:41 +0100 Subject: Adds for hash table --- Year_2/Algorithms/hash-chain-div.cc | 64 +++++++++++++++++++++++++++++++ Year_2/Algorithms/hash-chain-multiply.cc | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Year_2/Algorithms/hash-chain-div.cc create mode 100644 Year_2/Algorithms/hash-chain-multiply.cc (limited to 'Year_2/Algorithms') diff --git a/Year_2/Algorithms/hash-chain-div.cc b/Year_2/Algorithms/hash-chain-div.cc new file mode 100644 index 0000000..cac5c2c --- /dev/null +++ b/Year_2/Algorithms/hash-chain-div.cc @@ -0,0 +1,64 @@ +#include +#include + +using namespace std; + +int +main(int argc, char** argv) +{ + int n = (argc > 1) ? atoi(argv[1]) : 100; + unsigned i, j, bucketsize, bucketnum; + string t; + + ifstream fin("input.txt"); + ofstream fout("output.txt"); + + for (i = 0; i < n; ++i) { + fin >> t >> bucketsize >> bucketnum; + + int* h = new int[bucketsize]; + for (j = 0; j < bucketsize; ++j) + h[j] = 0; + + if (t == "int") { + int k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[k % bucketsize]++; + } + } else if (t == "double") { + double k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(k) % bucketsize]++; + } + } else if (t == "char") { + char k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(k) % bucketsize]++; + } + } else if (t == "bool") { + bool k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(k) % bucketsize]++; + } + } + + for (j = 0; j < bucketsize; ++j) + fout << h[j] << ' '; + + fout << endl; + + delete[] h; + } + + fin.close(); + fout.close(); + return 0; +} diff --git a/Year_2/Algorithms/hash-chain-multiply.cc b/Year_2/Algorithms/hash-chain-multiply.cc new file mode 100644 index 0000000..55c9fa5 --- /dev/null +++ b/Year_2/Algorithms/hash-chain-multiply.cc @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; + +int +main(int argc, char** argv) +{ + int n = (argc > 1) ? atoi(argv[1]) : 100; + unsigned i, j, bucketsize, bucketnum; + double A { 0.61803 }; + string t; + + ifstream fin("input.txt"); + ofstream fout("output.txt"); + + for (i = 0; i < n; ++i) { + fin >> t >> bucketsize >> bucketnum; + + int* h = new int[bucketsize]; + for (j = 0; j < bucketsize; ++j) + h[j] = 0; + + if (t == "int") { + int k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(floor(bucketsize * fmod(k * A, 1)))]++; + } + } else if (t == "double") { + double k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(floor(bucketsize * fmod(k * A, 1)))]++; + } + } else if (t == "char") { + char k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(floor(bucketsize * fmod(k * A, 1)))]++; + } + } else if (t == "bool") { + bool k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast(floor(bucketsize * fmod(k * A, 1)))]++; + } + } + + for (j = 0; j < bucketsize; ++j) + fout << h[j] << ' '; + + fout << endl; + + delete[] h; + } + + fin.close(); + fout.close(); + return 0; +} -- cgit v1.2.3-18-g5258