added mapmap.hpp so maybe future benchmarks can be done
This commit is contained in:
parent
c1f0e5f1a9
commit
70f9b24669
6
main.cpp
6
main.cpp
@ -2,6 +2,7 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include "amap.h"
|
#include "amap.h"
|
||||||
#include "simap.h"
|
#include "simap.h"
|
||||||
|
#include "mapmap.hpp"
|
||||||
|
|
||||||
void test_basics(amap mapdo, void *map) {
|
void test_basics(amap mapdo, void *map) {
|
||||||
assert(NULL == mapdo(map, AMAP_GET, "asdf", NULL));
|
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, "meaningless1", &i));
|
||||||
assert(NULL != mapdo(map, AMAP_SET, "meaning2", &i));
|
assert(NULL != mapdo(map, AMAP_SET, "meaning2", &i));
|
||||||
const char *helloworld = "Hello world!";
|
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(NULL != (chptr = (const char *)mapdo(map, AMAP_GET, "hello", NULL)));
|
||||||
assert(strlen(chptr) == strlen(helloworld));
|
assert(strlen(chptr) == strlen(helloworld));
|
||||||
@ -48,5 +49,8 @@ int main() {
|
|||||||
simap_instance si = simap_create();
|
simap_instance si = simap_create();
|
||||||
test_basics(simap, &si);
|
test_basics(simap, &si);
|
||||||
|
|
||||||
|
mapmap_instance mi = mapmap_create();
|
||||||
|
test_basics(mapmap, &mi);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
39
mapmap.hpp
Normal file
39
mapmap.hpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#ifndef MAPMAP_HPP
|
||||||
|
#define MAPMAP_HPP
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
struct mapmap_instance {
|
||||||
|
std::map<const char *, void *> 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<const char *, void *>());
|
||||||
|
return (void *)((uint8_t)(NULL) - 1L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // MAPMAP_HPP
|
Loading…
x
Reference in New Issue
Block a user