This commit is contained in:
blueloveTH 2024-06-16 10:49:32 +08:00
parent b63ae3741d
commit 54f35ff396
4 changed files with 21 additions and 18 deletions

View File

@ -20,8 +20,8 @@ extern "C" {
#define K c11_string #define K c11_string
#define V uint16_t #define V uint16_t
#define TAG s2n #define TAG s2n
#define less(a, b) (c11_string__cmp((a.key), (b)) < 0) #define less(a, b) (c11_string__cmp((a), (b)) < 0)
#define equal(a, b) (c11_string__cmp((a), (b)) == 0) #define equal(a, b) (c11_string__cmp((a), (b)) == 0)
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__HEADER #undef SMALLMAP_T__HEADER

View File

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

View File

@ -1,6 +1,4 @@
#include "pocketpy/common/vector.h" #include "pocketpy/common/smallmap.h"
#include "pocketpy/common/str.h"
#include <stdint.h>
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K uint16_t #define K uint16_t
@ -14,7 +12,7 @@
#define K c11_string #define K c11_string
#define V uint16_t #define V uint16_t
#define TAG s2n #define TAG s2n
#define less(a, b) (c11_string__cmp((a.key), (b)) < 0) #define less(a, b) (c11_string__cmp((a), (b)) < 0)
#define equal(a, b) (c11_string__cmp((a), (b)) == 0) #define equal(a, b) (c11_string__cmp((a), (b)) == 0)
#include "pocketpy/xmacros/smallmap.h" #include "pocketpy/xmacros/smallmap.h"
#undef SMALLMAP_T__SOURCE #undef SMALLMAP_T__SOURCE

View File

@ -1,8 +1,4 @@
#include "pocketpy/common/vector.h" #include "pocketpy/objects/namedict.h"
#include "pocketpy/common/str.h"
#include <stdint.h>
#include "pocketpy/objects/pyvar.h"
#define SMALLMAP_T__SOURCE #define SMALLMAP_T__SOURCE
#define K uint16_t #define K uint16_t