From 54f35ff396849351def8f055d87857eb3ebd1494 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 16 Jun 2024 10:49:32 +0800 Subject: [PATCH] some fix --- include/pocketpy/common/smallmap.h | 4 ++-- include/pocketpy/xmacros/smallmap.h | 21 +++++++++++++++------ src/common/smallmap.c | 8 +++----- src/objects/namedict.c | 6 +----- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/pocketpy/common/smallmap.h b/include/pocketpy/common/smallmap.h index c7b6cb6d..0a903cd8 100644 --- a/include/pocketpy/common/smallmap.h +++ b/include/pocketpy/common/smallmap.h @@ -20,8 +20,8 @@ extern "C" { #define K c11_string #define V uint16_t #define TAG s2n -#define less(a, b) (c11_string__cmp((a.key), (b)) < 0) -#define equal(a, b) (c11_string__cmp((a), (b)) == 0) +#define less(a, b) (c11_string__cmp((a), (b)) < 0) +#define equal(a, b) (c11_string__cmp((a), (b)) == 0) #include "pocketpy/xmacros/smallmap.h" #undef SMALLMAP_T__HEADER diff --git a/include/pocketpy/xmacros/smallmap.h b/include/pocketpy/xmacros/smallmap.h index 5b24ba46..35cbf7e3 100644 --- a/include/pocketpy/xmacros/smallmap.h +++ b/include/pocketpy/xmacros/smallmap.h @@ -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 diff --git a/src/common/smallmap.c b/src/common/smallmap.c index 3def6cf3..a1866b50 100644 --- a/src/common/smallmap.c +++ b/src/common/smallmap.c @@ -1,6 +1,4 @@ -#include "pocketpy/common/vector.h" -#include "pocketpy/common/str.h" -#include +#include "pocketpy/common/smallmap.h" #define SMALLMAP_T__SOURCE #define K uint16_t @@ -14,7 +12,7 @@ #define K c11_string #define V uint16_t #define TAG s2n -#define less(a, b) (c11_string__cmp((a.key), (b)) < 0) -#define equal(a, b) (c11_string__cmp((a), (b)) == 0) +#define less(a, b) (c11_string__cmp((a), (b)) < 0) +#define equal(a, b) (c11_string__cmp((a), (b)) == 0) #include "pocketpy/xmacros/smallmap.h" #undef SMALLMAP_T__SOURCE diff --git a/src/objects/namedict.c b/src/objects/namedict.c index 313330b8..1ace2f85 100644 --- a/src/objects/namedict.c +++ b/src/objects/namedict.c @@ -1,8 +1,4 @@ -#include "pocketpy/common/vector.h" -#include "pocketpy/common/str.h" -#include - -#include "pocketpy/objects/pyvar.h" +#include "pocketpy/objects/namedict.h" #define SMALLMAP_T__SOURCE #define K uint16_t