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 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

View File

@ -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

View File

@ -1,6 +1,4 @@
#include "pocketpy/common/vector.h"
#include "pocketpy/common/str.h"
#include <stdint.h>
#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

View File

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