From 4ade45a655c622f60f0073fa9ca0011dda1cc21e Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Fri, 11 Oct 2024 03:33:29 +0200 Subject: [PATCH] added unordered map with api and benchmarks --- main.cpp | 7 ++++++- unomap.hpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 unomap.hpp diff --git a/main.cpp b/main.cpp index b67d4f3..89a4098 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include "amap.h" #include "simap.h" #include "mapmap.hpp" +#include "unomap.hpp" /** * Creates keys or returns the ith key. Used for performance tests. @@ -114,13 +115,17 @@ int main() { mapmap_instance mi = mapmap_create(); test_basics(mapmap, &mi); + unomap_instance umi = unomap_create(); + test_basics(unomap, &umi); + /* Performance tests */ - int i = 1000; + int i = 100; keystore(i, true); datastore(i, true); test_perf(mapmap, &mi, i, "std::map"); test_perf(simap, &si, i, "simap"); + test_perf(unomap, &umi, i, "std::unordered_map"); return 0; } diff --git a/unomap.hpp b/unomap.hpp new file mode 100644 index 0000000..03fa7c8 --- /dev/null +++ b/unomap.hpp @@ -0,0 +1,39 @@ +#ifndef UNOMAP_HPP +#define UNOMAP_HPP + +#include +#include +#include + +struct unomap_instance { + std::unordered_map m; +}; + +static inline unomap_instance unomap_create() { + unomap_instance ret; + return ret; +} + +static inline void* unomap(void *amap_instance, AMAP_OP op, const char *key, void *ptr) { + unomap_instance *map = (unomap_instance *) amap_instance; + if(op == AMAP_GET) { + try { + return map->m[key]; + } catch(...) { + return ptr; + } + } else if(op == AMAP_SET) { + try { + map->m[key] = ptr; + return map; // non-null + } catch(...) { + return NULL; + } + } else { // if(op == AMAP_ERASE) { + assert(op == AMAP_ERASE); + map->m = std::move(std::unordered_map()); + return (void *)((uint8_t)(NULL) - 1L); + } +} + +#endif // MAPMAP_HPP