added unordered map with api and benchmarks
This commit is contained in:
parent
312c6f14ca
commit
4ade45a655
7
main.cpp
7
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;
|
||||
}
|
||||
|
39
unomap.hpp
Normal file
39
unomap.hpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user