|
|
@@ -1,6 +1,8 @@
|
|
|
#if !defined(SMALLMAP_T__HEADER) && !defined(SMALLMAP_T__SOURCE)
|
|
|
#include "pocketpy/common/vector.h"
|
|
|
|
|
|
+ #define SMALLMAP_T__HEADER
|
|
|
+ #define SMALLMAP_T__SOURCE
|
|
|
/* Input */
|
|
|
#define K int
|
|
|
#define V float
|
|
|
@@ -9,7 +11,7 @@
|
|
|
|
|
|
/* Optional Input */
|
|
|
#ifndef less
|
|
|
- #define less(a, b) ((a.key) < (b))
|
|
|
+ #define less(a, b) ((a) < (b))
|
|
|
#endif
|
|
|
|
|
|
#ifndef equal
|
|
|
@@ -17,13 +19,16 @@
|
|
|
#endif
|
|
|
|
|
|
/* Temprary macros */
|
|
|
-#define CONCAT(A, B) CONCAT_(A, B)
|
|
|
-#define CONCAT_(A, B) A##B
|
|
|
+#define partial_less(a, b) less((a).key, (b))
|
|
|
+#define CONCAT(A, B) CONCAT_(A, B)
|
|
|
+#define CONCAT_(A, B) A##B
|
|
|
|
|
|
#define KV CONCAT(c11_smallmap_entry_, TAG)
|
|
|
#define SMALLMAP CONCAT(c11_smallmap_, TAG)
|
|
|
#define SMALLMAP_METHOD(name) CONCAT(SMALLMAP, CONCAT(__, name))
|
|
|
|
|
|
+#ifdef SMALLMAP_T__HEADER
|
|
|
+/* Declaration */
|
|
|
typedef struct {
|
|
|
K key;
|
|
|
V value;
|
|
|
@@ -40,6 +45,8 @@ bool SMALLMAP_METHOD(contains)(const SMALLMAP* self, K key);
|
|
|
bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key);
|
|
|
void SMALLMAP_METHOD(clear)(SMALLMAP* self);
|
|
|
|
|
|
+#endif
|
|
|
+
|
|
|
#ifdef SMALLMAP_T__SOURCE
|
|
|
/* Implementation */
|
|
|
|
|
|
@@ -54,7 +61,7 @@ void SMALLMAP_METHOD(dtor)(SMALLMAP* self) {
|
|
|
|
|
|
void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value) {
|
|
|
int index;
|
|
|
- c11__lower_bound(KV, self->data, self->count, key, less, &index);
|
|
|
+ c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
|
|
|
KV* it = c11__at(KV, self, index);
|
|
|
if(index != self->count && equal(it->key, key)) {
|
|
|
it->value = value;
|
|
|
@@ -73,7 +80,7 @@ V* SMALLMAP_METHOD(try_get)(const SMALLMAP* self, K key) {
|
|
|
int mid = (low + high) / 2;
|
|
|
if(equal(a[mid].key, key)){
|
|
|
return &a[mid].value;
|
|
|
- } else if(less(a[mid], key)){
|
|
|
+ } else if(less(a[mid].key, key)){
|
|
|
low = mid + 1;
|
|
|
} else {
|
|
|
high = mid - 1;
|
|
|
@@ -93,7 +100,7 @@ bool SMALLMAP_METHOD(contains)(const SMALLMAP* self, K key) {
|
|
|
|
|
|
bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key) {
|
|
|
int index;
|
|
|
- c11__lower_bound(KV, self->data, self->count, key, less, &index);
|
|
|
+ c11__lower_bound(KV, self->data, self->count, key, partial_less, &index);
|
|
|
KV* it = c11__at(KV, self, index);
|
|
|
if(index != self->count && equal(it->key, key)) {
|
|
|
c11_vector__erase(KV, self, index);
|
|
|
@@ -108,6 +115,7 @@ void SMALLMAP_METHOD(clear)(SMALLMAP* self) {
|
|
|
|
|
|
#endif
|
|
|
|
|
|
+/* Undefine all macros */
|
|
|
#undef KV
|
|
|
#undef SMALLMAP
|
|
|
#undef SMALLMAP_METHOD
|
|
|
@@ -118,4 +126,5 @@ void SMALLMAP_METHOD(clear)(SMALLMAP* self) {
|
|
|
#undef V
|
|
|
#undef TAG
|
|
|
#undef less
|
|
|
+#undef partial_less
|
|
|
#undef equal
|