diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-01-13 18:33:41 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-01-13 18:33:41 +0100 |
commit | 9eef7cb7d5a2cd510de0cf17e729dc702fc19128 (patch) | |
tree | 9ffe6b0b2f998146d8ea0b2550b3d381cfce7581 /Year_2 | |
parent | 9718f03886b597292c007d215cc86d5b40009a8a (diff) |
Adds for hash table
Diffstat (limited to 'Year_2')
-rw-r--r-- | Year_2/Algorithms/hash-chain-div.cc | 64 | ||||
-rw-r--r-- | Year_2/Algorithms/hash-chain-multiply.cc | 66 |
2 files changed, 130 insertions, 0 deletions
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 <fstream> +#include <iostream> + +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<int>(k) % bucketsize]++; + } + } else if (t == "char") { + char k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast<int>(k) % bucketsize]++; + } + } else if (t == "bool") { + bool k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast<int>(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 <fstream> +#include <iostream> +#include <math.h> + +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<int>(floor(bucketsize * fmod(k * A, 1)))]++; + } + } else if (t == "double") { + double k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast<int>(floor(bucketsize * fmod(k * A, 1)))]++; + } + } else if (t == "char") { + char k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast<int>(floor(bucketsize * fmod(k * A, 1)))]++; + } + } else if (t == "bool") { + bool k; + + for (j = 0; j < bucketnum; ++j) { + fin >> k; + h[static_cast<int>(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; +} |