restructuring

This commit is contained in:
Nicholas Orlowsky 2023-10-18 14:09:26 -04:00
parent ac669ba504
commit 6c9f7f7c49
No known key found for this signature in database
GPG key ID: BE7DF0188A405E2B
13 changed files with 573 additions and 496 deletions

View file

@ -0,0 +1,80 @@
#include <unordered_map>
#include <vector>
#include <string>
#include <optional>
#include <chrono>
#include <algorithm>
#include <bits/stdc++.h>
constexpr int benchmark_loops = 1000000;
template <typename KeyType, typename ValueType>
class smart_map {
private:
bool use_hmap = false;
std::unordered_map<KeyType, ValueType> hmap;
double assess_hmap(const std::vector<std::pair<KeyType, ValueType>> check) {
const auto& start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < benchmark_loops; i++) {
for(const auto& check_item : check) {
assert(check_item.second == hmap[check_item.first]);
}
}
const auto& end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration<double, std::nano>(end - start).count();
return duration;
}
double assess_vmap(const std::vector<std::pair<KeyType, ValueType>> check) {
const auto& start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < benchmark_loops; i++) {
for(const auto& check_item : check) {
for(const auto& item : hmap) {
if(check_item.first == item.first) {
assert(check_item.second == item.second);
}
}
}
}
const auto& end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration<double, std::nano>(end - start).count();
return duration;
}
public:
smart_map () = default;
void assess_datastructure() {
std::vector<std::pair<KeyType, ValueType>> vals(hmap.begin(), hmap.end());
std::shuffle(vals.begin(), vals.end(), std::default_random_engine(570));
use_hmap = assess_hmap(vals) > assess_vmap(vals);
}
bool will_use_hmap() {
return use_hmap;
}
ValueType* get(const KeyType& key) {
if(use_hmap) {
if(hmap.find(key) != hmap.end()) {
return &hmap[key];
} else {
return NULL;
}
}
for(auto& item : hmap) {
if(item.first == key) {
std::string& ref = item.second;
return &ref;
}
}
return NULL;
}
void insert(const KeyType key, const ValueType value) {
hmap[key] = value;
}
};