src/hash_map.c

changeset 856
6bbbf219251d
parent 855
35bcb3216c0d
     1.1 --- a/src/hash_map.c	Thu May 23 20:31:37 2024 +0200
     1.2 +++ b/src/hash_map.c	Thu May 23 20:43:04 2024 +0200
     1.3 @@ -53,9 +53,9 @@
     1.4                  // invoke the destructor
     1.5                  cx_invoke_destructor(map, elem->data);
     1.6                  // free the key data
     1.7 -                cxFree(map->base.allocator, (void *) elem->key.data);
     1.8 +                cxFree(map->collection.allocator, (void *) elem->key.data);
     1.9                  // free the node
    1.10 -                cxFree(map->base.allocator, elem);
    1.11 +                cxFree(map->collection.allocator, elem);
    1.12                  // proceed
    1.13                  elem = next;
    1.14              } while (elem != NULL);
    1.15 @@ -64,7 +64,7 @@
    1.16              hash_map->buckets[i] = NULL;
    1.17          }
    1.18      }
    1.19 -    map->base.size = 0;
    1.20 +    map->collection.size = 0;
    1.21  }
    1.22  
    1.23  static void cx_hash_map_destructor(struct cx_map_s *map) {
    1.24 @@ -72,10 +72,10 @@
    1.25  
    1.26      // free the buckets
    1.27      cx_hash_map_clear(map);
    1.28 -    cxFree(map->base.allocator, hash_map->buckets);
    1.29 +    cxFree(map->collection.allocator, hash_map->buckets);
    1.30  
    1.31      // free the map structure
    1.32 -    cxFree(map->base.allocator, map);
    1.33 +    cxFree(map->collection.allocator, map);
    1.34  }
    1.35  
    1.36  static int cx_hash_map_put(
    1.37 @@ -84,7 +84,7 @@
    1.38          void *value
    1.39  ) {
    1.40      struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
    1.41 -    CxAllocator const *allocator = map->base.allocator;
    1.42 +    CxAllocator const *allocator = map->collection.allocator;
    1.43  
    1.44      unsigned hash = key.hash;
    1.45      if (hash == 0) {
    1.46 @@ -104,26 +104,26 @@
    1.47      if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len &&
    1.48          memcmp(elm->key.data, key.data, key.len) == 0) {
    1.49          // overwrite existing element
    1.50 -        if (map->base.store_pointer) {
    1.51 +        if (map->collection.store_pointer) {
    1.52              memcpy(elm->data, &value, sizeof(void *));
    1.53          } else {
    1.54 -            memcpy(elm->data, value, map->base.elem_size);
    1.55 +            memcpy(elm->data, value, map->collection.elem_size);
    1.56          }
    1.57      } else {
    1.58          // allocate new element
    1.59          struct cx_hash_map_element_s *e = cxMalloc(
    1.60                  allocator,
    1.61 -                sizeof(struct cx_hash_map_element_s) + map->base.elem_size
    1.62 +                sizeof(struct cx_hash_map_element_s) + map->collection.elem_size
    1.63          );
    1.64          if (e == NULL) {
    1.65              return -1;
    1.66          }
    1.67  
    1.68          // write the value
    1.69 -        if (map->base.store_pointer) {
    1.70 +        if (map->collection.store_pointer) {
    1.71              memcpy(e->data, &value, sizeof(void *));
    1.72          } else {
    1.73 -            memcpy(e->data, value, map->base.elem_size);
    1.74 +            memcpy(e->data, value, map->collection.elem_size);
    1.75          }
    1.76  
    1.77          // copy the key
    1.78 @@ -145,7 +145,7 @@
    1.79          e->next = elm;
    1.80  
    1.81          // increase the size
    1.82 -        map->base.size++;
    1.83 +        map->collection.size++;
    1.84      }
    1.85  
    1.86      return 0;
    1.87 @@ -164,10 +164,10 @@
    1.88          prev->next = elm->next;
    1.89      }
    1.90      // free element
    1.91 -    cxFree(hash_map->base.base.allocator, (void *) elm->key.data);
    1.92 -    cxFree(hash_map->base.base.allocator, elm);
    1.93 +    cxFree(hash_map->base.collection.allocator, (void *) elm->key.data);
    1.94 +    cxFree(hash_map->base.collection.allocator, elm);
    1.95      // decrease size
    1.96 -    hash_map->base.base.size--;
    1.97 +    hash_map->base.collection.size--;
    1.98  }
    1.99  
   1.100  /**
   1.101 @@ -203,7 +203,7 @@
   1.102                  if (destroy) {
   1.103                      cx_invoke_destructor(map, elm->data);
   1.104                  } else {
   1.105 -                    if (map->base.store_pointer) {
   1.106 +                    if (map->collection.store_pointer) {
   1.107                          data = *(void **) elm->data;
   1.108                      } else {
   1.109                          data = elm->data;
   1.110 @@ -254,7 +254,7 @@
   1.111      struct cx_iterator_s const *iter = it;
   1.112      struct cx_hash_map_s const *map = iter->src_handle.c;
   1.113      struct cx_hash_map_element_s *elm = iter->elem_handle;
   1.114 -    if (map->base.base.store_pointer) {
   1.115 +    if (map->base.collection.store_pointer) {
   1.116          return *(void **) elm->data;
   1.117      } else {
   1.118          return elm->data;
   1.119 @@ -315,7 +315,7 @@
   1.120          iter->kv_data.value = NULL;
   1.121      } else {
   1.122          iter->kv_data.key = &elm->key;
   1.123 -        if (map->base.base.store_pointer) {
   1.124 +        if (map->base.collection.store_pointer) {
   1.125              iter->kv_data.value = *(void **) elm->data;
   1.126          } else {
   1.127              iter->kv_data.value = elm->data;
   1.128 @@ -330,7 +330,7 @@
   1.129      CxIterator iter;
   1.130  
   1.131      iter.src_handle.c = map;
   1.132 -    iter.elem_count = map->base.size;
   1.133 +    iter.elem_count = map->collection.size;
   1.134  
   1.135      switch (type) {
   1.136          case CX_MAP_ITERATOR_PAIRS:
   1.137 @@ -342,7 +342,7 @@
   1.138              iter.base.current = cx_hash_map_iter_current_key;
   1.139              break;
   1.140          case CX_MAP_ITERATOR_VALUES:
   1.141 -            iter.elem_size = map->base.elem_size;
   1.142 +            iter.elem_size = map->collection.elem_size;
   1.143              iter.base.current = cx_hash_map_iter_current_value;
   1.144              break;
   1.145          default:
   1.146 @@ -357,7 +357,7 @@
   1.147      iter.slot = 0;
   1.148      iter.index = 0;
   1.149  
   1.150 -    if (map->base.size > 0) {
   1.151 +    if (map->collection.size > 0) {
   1.152          struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
   1.153          struct cx_hash_map_element_s *elm = hash_map->buckets[0];
   1.154          while (elm == NULL) {
   1.155 @@ -365,7 +365,7 @@
   1.156          }
   1.157          iter.elem_handle = elm;
   1.158          iter.kv_data.key = &elm->key;
   1.159 -        if (map->base.store_pointer) {
   1.160 +        if (map->collection.store_pointer) {
   1.161              iter.kv_data.value = *(void **) elm->data;
   1.162          } else {
   1.163              iter.kv_data.value = elm->data;
   1.164 @@ -413,14 +413,14 @@
   1.165  
   1.166      // initialize base members
   1.167      map->base.cl = &cx_hash_map_class;
   1.168 -    map->base.base.allocator = allocator;
   1.169 +    map->base.collection.allocator = allocator;
   1.170  
   1.171      if (itemsize > 0) {
   1.172 -        map->base.base.store_pointer = false;
   1.173 -        map->base.base.elem_size = itemsize;
   1.174 +        map->base.collection.store_pointer = false;
   1.175 +        map->base.collection.elem_size = itemsize;
   1.176      } else {
   1.177 -        map->base.base.store_pointer = true;
   1.178 -        map->base.base.elem_size = sizeof(void *);
   1.179 +        map->base.collection.store_pointer = true;
   1.180 +        map->base.collection.elem_size = sizeof(void *);
   1.181      }
   1.182  
   1.183      return (CxMap *) map;
   1.184 @@ -428,11 +428,11 @@
   1.185  
   1.186  int cxMapRehash(CxMap *map) {
   1.187      struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
   1.188 -    if (map->base.size > ((hash_map->bucket_count * 3) >> 2)) {
   1.189 +    if (map->collection.size > ((hash_map->bucket_count * 3) >> 2)) {
   1.190  
   1.191 -        size_t new_bucket_count = (map->base.size * 5) >> 1;
   1.192 +        size_t new_bucket_count = (map->collection.size * 5) >> 1;
   1.193          struct cx_hash_map_element_s **new_buckets = cxCalloc(
   1.194 -                map->base.allocator,
   1.195 +                map->collection.allocator,
   1.196                  new_bucket_count, sizeof(struct cx_hash_map_element_s *)
   1.197          );
   1.198  
   1.199 @@ -472,7 +472,7 @@
   1.200  
   1.201          // assign result to the map
   1.202          hash_map->bucket_count = new_bucket_count;
   1.203 -        cxFree(map->base.allocator, hash_map->buckets);
   1.204 +        cxFree(map->collection.allocator, hash_map->buckets);
   1.205          hash_map->buckets = new_buckets;
   1.206      }
   1.207      return 0;

mercurial