add base collection members to map interface

Tue, 18 Apr 2023 18:01:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 18 Apr 2023 18:01:41 +0200
changeset 685
2dd841e364af
parent 684
380bd45bc94a
child 686
64919f63f059

add base collection members to map interface

src/cx/map.h file | annotate | diff | comparison | revisions
src/hash_map.c file | annotate | diff | comparison | revisions
tests/test_map.cpp file | annotate | diff | comparison | revisions
--- a/src/cx/map.h	Mon Apr 17 21:47:34 2023 +0200
+++ b/src/cx/map.h	Tue Apr 18 18:01:41 2023 +0200
@@ -56,21 +56,9 @@
 
 /** Structure for the UCX map. */
 struct cx_map_s {
+    CX_COLLECTION_MEMBERS
     /** The map class definition. */
     cx_map_class *cl;
-    /** An allocator that is used for the map elements. */
-    CxAllocator *allocator;
-    /** The number of elements currently stored. */
-    size_t size;
-    /**
-     * The size of an element.
-     */
-    size_t item_size;
-    /**
-     * True, if this map shall store pointers instead
-     * of copies of objects.
-     */
-    bool store_pointers;
 };
 
 /**
@@ -179,7 +167,7 @@
  */
 __attribute__((__nonnull__))
 static inline void cxMapStoreObjects(CxMap *map) {
-    map->store_pointers = false;
+    map->store_pointer = false;
 }
 
 /**
@@ -196,7 +184,7 @@
  */
 __attribute__((__nonnull__))
 static inline void cxMapStorePointers(CxMap *map) {
-    map->store_pointers = true;
+    map->store_pointer = true;
     map->item_size = sizeof(void *);
 }
 
--- a/src/hash_map.c	Mon Apr 17 21:47:34 2023 +0200
+++ b/src/hash_map.c	Tue Apr 18 18:01:41 2023 +0200
@@ -100,7 +100,7 @@
     if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len &&
         memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) {
         // overwrite existing element
-        if (map->store_pointers) {
+        if (map->store_pointer) {
             memcpy(elm->data, &value, sizeof(void *));
         } else {
             memcpy(elm->data, value, map->item_size);
@@ -116,7 +116,7 @@
         }
 
         // write the value
-        if (map->store_pointers) {
+        if (map->store_pointer) {
             memcpy(e->data, &value, sizeof(void *));
         } else {
             memcpy(e->data, value, map->item_size);
@@ -194,7 +194,7 @@
         if (elm->key.hash == hash && elm->key.len == key.len) {
             if (memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) {
                 void *data = NULL;
-                if (map->store_pointers) {
+                if (map->store_pointer) {
                     data = *(void **) elm->data;
                 } else if (!remove) {
                     data = elm->data;
@@ -243,7 +243,7 @@
     struct cx_iterator_s const *iter = it;
     struct cx_hash_map_s const *map = iter->src_handle;
     struct cx_hash_map_element_s *elm = iter->elem_handle;
-    if (map->base.store_pointers) {
+    if (map->base.store_pointer) {
         return *(void **) elm->data;
     } else {
         return elm->data;
@@ -304,7 +304,7 @@
         iter->kv_data.value = NULL;
     } else {
         iter->kv_data.key = &elm->key;
-        if (map->base.store_pointers) {
+        if (map->base.store_pointer) {
             iter->kv_data.value = *(void **) elm->data;
         } else {
             iter->kv_data.value = elm->data;
@@ -344,7 +344,7 @@
         }
         iter.elem_handle = elm;
         iter.kv_data.key = &elm->key;
-        if (map->store_pointers) {
+        if (map->store_pointer) {
             iter.kv_data.value = *(void **) elm->data;
         } else {
             iter.kv_data.value = elm->data;
@@ -416,7 +416,7 @@
         buckets = 16;
     }
 
-    struct cx_hash_map_s *map = cxMalloc(allocator, sizeof(struct cx_hash_map_s));
+    struct cx_hash_map_s *map = cxCalloc(allocator, 1, sizeof(struct cx_hash_map_s));
     if (map == NULL) return NULL;
 
     // initialize hash map members
@@ -430,13 +430,12 @@
     // initialize base members
     map->base.cl = &cx_hash_map_class;
     map->base.allocator = allocator;
-    map->base.size = 0;
 
     if (itemsize > 0) {
-        map->base.store_pointers = false;
+        map->base.store_pointer = false;
         map->base.item_size = itemsize;
     } else {
-        map->base.store_pointers = true;
+        map->base.store_pointer = true;
         map->base.item_size = sizeof(void *);
     }
 
--- a/tests/test_map.cpp	Mon Apr 17 21:47:34 2023 +0200
+++ b/tests/test_map.cpp	Tue Apr 18 18:01:41 2023 +0200
@@ -124,12 +124,16 @@
     EXPECT_EQ(map->item_size, 1);
     EXPECT_EQ(map->size, 0);
     EXPECT_EQ(map->allocator, &allocator);
-    EXPECT_FALSE(map->store_pointers);
+    EXPECT_FALSE(map->store_pointer);
+    EXPECT_EQ(map->cmpfunc, nullptr);
+    EXPECT_EQ(map->simple_destructor, nullptr);
+    EXPECT_EQ(map->advanced_destructor, nullptr);
+    EXPECT_EQ(map->destructor_data, nullptr);
     cxMapStorePointers(map);
-    EXPECT_TRUE(map->store_pointers);
+    EXPECT_TRUE(map->store_pointer);
     EXPECT_EQ(map->item_size, sizeof(void *));
     cxMapStoreObjects(map);
-    EXPECT_FALSE(map->store_pointers);
+    EXPECT_FALSE(map->store_pointer);
 
     cxMapDestroy(map);
     EXPECT_TRUE(allocator.verify());
@@ -145,7 +149,7 @@
     }
     EXPECT_EQ(map->size, 0);
     EXPECT_EQ(map->allocator, &allocator);
-    EXPECT_TRUE(map->store_pointers);
+    EXPECT_TRUE(map->store_pointer);
     EXPECT_EQ(map->item_size, sizeof(void *));
 
     cxMapDestroy(map);
@@ -302,11 +306,11 @@
     auto map = cxHashMapCreate(&allocator, sizeof(cxstring), 8);
 
     // define some strings
-    cxstring s1 = CX_STR("this");
-    cxstring s2 = CX_STR("is");
-    cxstring s3 = CX_STR("a");
-    cxstring s4 = CX_STR("test");
-    cxstring s5 = CX_STR("setup");
+    auto s1 = CX_STR("this");
+    auto s2 = CX_STR("is");
+    auto s3 = CX_STR("a");
+    auto s4 = CX_STR("test");
+    auto s5 = CX_STR("setup");
 
     // put them into the map
     cxMapPut(map, cx_hash_key_str("s1"), &s1);
@@ -339,3 +343,4 @@
     cxMapDestroy(map);
     EXPECT_TRUE(allocator.verify());
 }
+

mercurial