From 70f9b246698f846beac7e639c47e16e4bb23c3d6 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Thu, 10 Oct 2024 17:16:41 +0200 Subject: [PATCH] added mapmap.hpp so maybe future benchmarks can be done --- main.cpp | 6 +++++- mapmap.hpp | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 mapmap.hpp diff --git a/main.cpp b/main.cpp index ca6a6be..20aac2c 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include "amap.h" #include "simap.h" +#include "mapmap.hpp" void test_basics(amap mapdo, void *map) { assert(NULL == mapdo(map, AMAP_GET, "asdf", NULL)); @@ -30,7 +31,7 @@ void test_basics(amap mapdo, void *map) { assert(NULL != mapdo(map, AMAP_SET, "meaningless1", &i)); assert(NULL != mapdo(map, AMAP_SET, "meaning2", &i)); const char *helloworld = "Hello world!"; - assert(NULL != mapdo(map, AMAP_SET, "hello", (char *)helloworld)); /* TODO: ugly cast... */ + assert(NULL != mapdo(map, AMAP_SET, "hello", (char *)helloworld)); /* ugly cast... */ assert(NULL != (chptr = (const char *)mapdo(map, AMAP_GET, "hello", NULL))); assert(strlen(chptr) == strlen(helloworld)); @@ -48,5 +49,8 @@ int main() { simap_instance si = simap_create(); test_basics(simap, &si); + mapmap_instance mi = mapmap_create(); + test_basics(mapmap, &mi); + return 0; } diff --git a/mapmap.hpp b/mapmap.hpp new file mode 100644 index 0000000..7afbb40 --- /dev/null +++ b/mapmap.hpp @@ -0,0 +1,39 @@ +#ifndef MAPMAP_HPP +#define MAPMAP_HPP + +#include +#include +#include + +struct mapmap_instance { + std::map m; +}; + +static inline mapmap_instance mapmap_create() { + mapmap_instance ret; + return ret; +} + +static inline void* mapmap(void *amap_instance, AMAP_OP op, const char *key, void *ptr) { + mapmap_instance *map = (mapmap_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::map()); + return (void *)((uint8_t)(NULL) - 1L); + } +} + +#endif // MAPMAP_HPP