added unordered map with api and benchmarks

This commit is contained in:
Richard Thier 2024-10-11 03:33:29 +02:00
parent 312c6f14ca
commit 4ade45a655
2 changed files with 45 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include "amap.h" #include "amap.h"
#include "simap.h" #include "simap.h"
#include "mapmap.hpp" #include "mapmap.hpp"
#include "unomap.hpp"
/** /**
* Creates keys or returns the ith key. Used for performance tests. * Creates keys or returns the ith key. Used for performance tests.
@ -114,13 +115,17 @@ int main() {
mapmap_instance mi = mapmap_create(); mapmap_instance mi = mapmap_create();
test_basics(mapmap, &mi); test_basics(mapmap, &mi);
unomap_instance umi = unomap_create();
test_basics(unomap, &umi);
/* Performance tests */ /* Performance tests */
int i = 1000; int i = 100;
keystore(i, true); keystore(i, true);
datastore(i, true); datastore(i, true);
test_perf(mapmap, &mi, i, "std::map"); test_perf(mapmap, &mi, i, "std::map");
test_perf(simap, &si, i, "simap"); test_perf(simap, &si, i, "simap");
test_perf(unomap, &umi, i, "std::unordered_map");
return 0; return 0;
} }

39
unomap.hpp Normal file
View File

@ -0,0 +1,39 @@
#ifndef UNOMAP_HPP
#define UNOMAP_HPP
#include <unordered_map>
#include <cassert>
#include <memory>
struct unomap_instance {
std::unordered_map<const char *, void *> 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<const char *, void *>());
return (void *)((uint8_t)(NULL) - 1L);
}
}
#endif // MAPMAP_HPP