Kaynağa Gözat

ncd: NCDValue: port to SAvl

ambrop7 13 yıl önce
ebeveyn
işleme
cf40e1671e
3 değiştirilmiş dosya ile 48 ekleme ve 56 silme
  1. 28 28
      ncd/NCDValue.c
  2. 11 13
      ncd/NCDValue.h
  3. 9 15
      ncd/NCDValue_maptree.h

+ 28 - 28
ncd/NCDValue.c

@@ -38,7 +38,7 @@
 #include <ncd/NCDValue.h>
 
 #include "NCDValue_maptree.h"
-#include <structure/CAvl_impl.h>
+#include <structure/SAvl_impl.h>
 
 static int ncdvalue_comparator (void *unused, void *vv1, void *vv2)
 {
@@ -154,12 +154,12 @@ void NCDValue_Free (NCDValue *o)
         } break;
         
         case NCDVALUE_MAP: {
-            NCDValue__MapTreeRef ref;
-            while (NCDValue__MapTreeIsValidRef(ref = NCDValue__MapTree_GetFirst(&o->map_tree, 0))) {
-                NCDValue__MapTree_Remove(&o->map_tree, 0, ref);
-                NCDValue_Free(&ref.ptr->key);
-                NCDValue_Free(&ref.ptr->val);
-                free(ref.ptr);
+            NCDMapElement *e;
+            while (e = NCDValue__MapTree_GetFirst(&o->map_tree, 0)) {
+                NCDValue__MapTree_Remove(&o->map_tree, 0, e);
+                NCDValue_Free(&e->key);
+                NCDValue_Free(&e->val);
+                free(e);
             }
         } break;
         
@@ -508,15 +508,15 @@ NCDValue * NCDValue_MapFirstKey (NCDValue *o)
     value_assert(o);
     ASSERT(o->type == NCDVALUE_MAP)
     
-    NCDValue__MapTreeRef ref = NCDValue__MapTree_GetFirst(&o->map_tree, 0);
-    if (NCDValue__MapTreeIsNullRef(ref)) {
+    NCDMapElement *e = NCDValue__MapTree_GetFirst(&o->map_tree, 0);
+    if (!e) {
         return NULL;
     }
     
-    value_assert(&ref.ptr->key);
-    value_assert(&ref.ptr->val);
+    value_assert(&e->key);
+    value_assert(&e->val);
     
-    return &ref.ptr->key;
+    return &e->key;
 }
 
 NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey)
@@ -524,19 +524,19 @@ NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey)
     value_assert(o);
     ASSERT(o->type == NCDVALUE_MAP)
     
-    NCDMapElement *e = UPPER_OBJECT(ekey, NCDMapElement, key);
-    value_assert(&e->key);
-    value_assert(&e->val);
+    NCDMapElement *e0 = UPPER_OBJECT(ekey, NCDMapElement, key);
+    value_assert(&e0->key);
+    value_assert(&e0->val);
     
-    NCDValue__MapTreeRef ref = NCDValue__MapTree_GetNext(&o->map_tree, 0, NCDValue__MapTreeDeref(0, e));
-    if (NCDValue__MapTreeIsNullRef(ref)) {
+    NCDMapElement *e = NCDValue__MapTree_GetNext(&o->map_tree, 0, e0);
+    if (!e) {
         return NULL;
     }
     
-    value_assert(&ref.ptr->key);
-    value_assert(&ref.ptr->val);
+    value_assert(&e->key);
+    value_assert(&e->val);
     
-    return &ref.ptr->key;
+    return &e->key;
 }
 
 NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey)
@@ -557,16 +557,16 @@ NCDValue * NCDValue_MapFindKey (NCDValue *o, NCDValue *key)
     ASSERT(o->type == NCDVALUE_MAP)
     value_assert(key);
     
-    NCDValue__MapTreeRef ref = NCDValue__MapTree_LookupExact(&o->map_tree, 0, key);
-    if (NCDValue__MapTreeIsNullRef(ref)) {
+    NCDMapElement *e = NCDValue__MapTree_LookupExact(&o->map_tree, 0, key);
+    if (!e) {
         return NULL;
     }
     
-    value_assert(&ref.ptr->key);
-    value_assert(&ref.ptr->val);
-    ASSERT(!NCDValue_Compare(&ref.ptr->key, key))
+    value_assert(&e->key);
+    value_assert(&e->val);
+    ASSERT(!NCDValue_Compare(&e->key, key))
     
-    return &ref.ptr->key;
+    return &e->key;
 }
 
 NCDValue * NCDValue_MapInsert (NCDValue *o, NCDValue key, NCDValue val)
@@ -588,7 +588,7 @@ NCDValue * NCDValue_MapInsert (NCDValue *o, NCDValue key, NCDValue val)
     
     e->key = key;
     e->val = val;
-    int res = NCDValue__MapTree_Insert(&o->map_tree, 0, NCDValue__MapTreeDeref(0, e), NULL);
+    int res = NCDValue__MapTree_Insert(&o->map_tree, 0, e, NULL);
     ASSERT(res)
     
     o->map_count++;
@@ -608,7 +608,7 @@ void NCDValue_MapRemove (NCDValue *o, NCDValue *ekey, NCDValue *out_key, NCDValu
     value_assert(&e->key);
     value_assert(&e->val);
     
-    NCDValue__MapTree_Remove(&o->map_tree, 0, NCDValue__MapTreeDeref(0, e));
+    NCDValue__MapTree_Remove(&o->map_tree, 0, e);
     
     *out_key = e->key;
     *out_val = e->val;

+ 11 - 13
ncd/NCDValue.h

@@ -35,20 +35,20 @@
 
 #include <misc/debug.h>
 #include <structure/LinkedList1.h>
-#include <structure/CAvl.h>
+#include <structure/SAvl.h>
 
 #define NCDVALUE_STRING 1
 #define NCDVALUE_LIST 2
 #define NCDVALUE_MAP 3
 #define NCDVALUE_VAR 4
 
-typedef struct NCDValue_s NCDValue;
-typedef struct NCDMapElement_s NCDMapElement;
-typedef NCDMapElement *NCDValue__maptree_link;
-typedef NCDValue *NCDValue__maptree_key;
+struct NCDValue_s;
+struct NCDMapElement_s;
+
+typedef struct NCDValue_s *NCDValue__maptree_key;
 
 #include "NCDValue_maptree.h"
-#include <structure/CAvl_decl.h>
+#include <structure/SAvl_decl.h>
 
 /**
  * Holds an NCD "value", which is used in the NCD programming when passing arguments to
@@ -66,7 +66,7 @@ typedef NCDValue *NCDValue__maptree_key;
  * which proceeds to take ownership of the value), all the structures become invalid.
  * Similarly, if the value is modified via one structure, the others become invalid.
  */
-struct NCDValue_s {
+typedef struct NCDValue_s {
     int type;
     union {
         struct {
@@ -85,20 +85,18 @@ struct NCDValue_s {
             char *var_name;
         };
     };
-};
+} NCDValue;
 
 typedef struct {
     LinkedList1Node list_node;
     NCDValue v;
 } NCDListElement;
 
-struct NCDMapElement_s {
-    NCDMapElement *tree_child[2];
-    NCDMapElement *tree_parent;
-    int8_t tree_balance;
+typedef struct NCDMapElement_s {
+    NCDValue__MapTreeNode tree_node;
     NCDValue key;
     NCDValue val;
-};
+} NCDMapElement;
 
 /**
  * Initializes a value by copying an existing value.

+ 9 - 15
ncd/NCDValue_maptree.h

@@ -1,15 +1,9 @@
-#define CAVL_PARAM_NAME NCDValue__MapTree
-#define CAVL_PARAM_FEATURE_COUNTS 0
-#define CAVL_PARAM_FEATURE_KEYS_ARE_INDICES 0
-#define CAVL_PARAM_FEATURE_NOKEYS 0
-#define CAVL_PARAM_TYPE_ENTRY NCDMapElement
-#define CAVL_PARAM_TYPE_LINK NCDValue__maptree_link
-#define CAVL_PARAM_TYPE_KEY NCDValue__maptree_key
-#define CAVL_PARAM_TYPE_ARG int
-#define CAVL_PARAM_VALUE_NULL ((NCDValue__maptree_link)NULL)
-#define CAVL_PARAM_FUN_DEREF(arg, link) (link)
-#define CAVL_PARAM_FUN_COMPARE_ENTRIES(arg, entry1, entry2) NCDValue_Compare(&(entry1).ptr->key, &(entry2).ptr->key)
-#define CAVL_PARAM_FUN_COMPARE_KEY_ENTRY(arg, key1, entry2) NCDValue_Compare((key1), &(entry2).ptr->key)
-#define CAVL_PARAM_MEMBER_CHILD tree_child
-#define CAVL_PARAM_MEMBER_BALANCE tree_balance
-#define CAVL_PARAM_MEMBER_PARENT tree_parent
+#define SAVL_PARAM_NAME NCDValue__MapTree
+#define SAVL_PARAM_FEATURE_COUNTS 0
+#define SAVL_PARAM_FEATURE_NOKEYS 0
+#define SAVL_PARAM_TYPE_ENTRY struct NCDMapElement_s
+#define SAVL_PARAM_TYPE_KEY NCDValue__maptree_key
+#define SAVL_PARAM_TYPE_ARG int
+#define SAVL_PARAM_FUN_COMPARE_ENTRIES(arg, entry1, entry2) NCDValue_Compare(&(entry1)->key, &(entry2)->key)
+#define SAVL_PARAM_FUN_COMPARE_KEY_ENTRY(arg, key1, entry2) NCDValue_Compare((key1), &(entry2)->key)
+#define SAVL_PARAM_MEMBER_NODE tree_node