blueloveTH 1 년 전
부모
커밋
0ffacbbbe8
2개의 변경된 파일32개의 추가작업 그리고 17개의 파일을 삭제
  1. 17 0
      include/pocketpy/common/smallmap.h
  2. 15 17
      include/pocketpy/xmacros/smallmap.h

+ 17 - 0
include/pocketpy/common/smallmap.h

@@ -0,0 +1,17 @@
+#include "pocketpy/common/vector.h"
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define SMALLMAP_T__HEADER
+#define K uint16_t
+#define V int
+#include "pocketpy/xmacros/smallmap.h"
+#undef SMALLMAP_T__HEADER
+
+
+#ifdef __cplusplus
+}
+#endif

+ 15 - 17
include/pocketpy/xmacros/smallmap.h

@@ -26,9 +26,7 @@ typedef struct {
     V value;
 } KV;
 
-typedef struct{
-    c11_vector entries;
-} SMALLMAP;
+typedef c11_vector SMALLMAP;
 
 void SMALLMAP_METHOD(ctor)(SMALLMAP* self);
 void SMALLMAP_METHOD(dtor)(SMALLMAP* self);
@@ -43,30 +41,30 @@ void SMALLMAP_METHOD(clear)(SMALLMAP* self);
 /* Implementation */
 
 void SMALLMAP_METHOD(ctor)(SMALLMAP* self) {
-    c11_vector__ctor(&self->entries, sizeof(KV));
+    c11_vector__ctor(self, sizeof(KV));
 }
 
 void SMALLMAP_METHOD(dtor)(SMALLMAP* self) {
-    c11_vector__dtor(&self->entries);
+    c11_vector__dtor(self);
 }
 
 void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value) {
     int index;
-    c11__lower_bound(KV, self->entries.data, self->entries.count, key, less, &index);
-    KV* it = c11__at(KV, &self->entries, index);
-    if(index != self->entries.count && it->key == key) {
+    c11__lower_bound(KV, self->data, self->count, key, less, &index);
+    KV* it = c11__at(KV, self, index);
+    if(index != self->count && it->key == key) {
         it->value = value;
     } else {
         KV kv = {key, value};
-        c11_vector__insert(KV, &self->entries, index, kv);
+        c11_vector__insert(KV, self, index, kv);
     }
 }
 
 V* SMALLMAP_METHOD(try_get)(SMALLMAP* self, K key) {
     int index;
-    c11__lower_bound(KV, self->entries.data, self->entries.count, key, less, &index);
-    KV* it = c11__at(KV, &self->entries, index);
-    if(index != self->entries.count && it->key == key) {
+    c11__lower_bound(KV, self->data, self->count, key, less, &index);
+    KV* it = c11__at(KV, self, index);
+    if(index != self->count && it->key == key) {
         return &it->value;
     } else {
         return NULL;
@@ -84,17 +82,17 @@ bool SMALLMAP_METHOD(contains)(SMALLMAP* self, K key) {
 
 bool SMALLMAP_METHOD(del)(SMALLMAP* self, K key) {
     int index;
-    c11__lower_bound(KV, self->entries.data, self->entries.count, key, less, &index);
-    KV* it = c11__at(KV, &self->entries, index);
-    if(index != self->entries.count && it->key == key) {
-        c11_vector__erase(KV, &self->entries, index);
+    c11__lower_bound(KV, self->data, self->count, key, less, &index);
+    KV* it = c11__at(KV, self, index);
+    if(index != self->count && it->key == key) {
+        c11_vector__erase(KV, self, index);
         return true;
     }
     return false;
 }
 
 void SMALLMAP_METHOD(clear)(SMALLMAP* self) {
-    c11_vector__clear(&self->entries);
+    c11_vector__clear(self);
 }
 
 #endif