src/hash_map.c

Wed, 08 Jan 2025 20:06:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Jan 2025 20:06:37 +0100
changeset 1115
6db21dee4929
parent 1114
ad5eeb256242
permissions
-rw-r--r--

create specialized map iterators - fixes #550

549
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/hash_map.h"
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
31 #include <string.h>
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
32 #include <assert.h>
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 994
diff changeset
33 #include <errno.h>
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
34
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
35 struct cx_hash_map_element_s {
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
36 /** A pointer to the next element in the current bucket. */
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
37 struct cx_hash_map_element_s *next;
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
38
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
39 /** The corresponding key. */
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
40 CxHashKey key;
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
41
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
42 /** The value data. */
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
43 char data[];
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
44 };
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
45
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
46 static void cx_hash_map_clear(struct cx_map_s *map) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
47 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
48 for (size_t i = 0; i < hash_map->bucket_count; i++) {
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
49 struct cx_hash_map_element_s *elem = hash_map->buckets[i];
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
50 if (elem != NULL) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
51 do {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
52 struct cx_hash_map_element_s *next = elem->next;
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
53 // invoke the destructor
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
54 cx_invoke_destructor(map, elem->data);
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
55 // free the key data
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
56 cxFree(map->collection.allocator, (void *) elem->key.data);
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
57 // free the node
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
58 cxFree(map->collection.allocator, elem);
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
59 // proceed
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
60 elem = next;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
61 } while (elem != NULL);
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
62
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
63 // do not leave a dangling pointer
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
64 hash_map->buckets[i] = NULL;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
65 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
66 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
67 map->collection.size = 0;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
68 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
69
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
70 static void cx_hash_map_destructor(struct cx_map_s *map) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
71 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
72
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
73 // free the buckets
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
74 cx_hash_map_clear(map);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
75 cxFree(map->collection.allocator, hash_map->buckets);
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
76
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
77 // free the map structure
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
78 cxFree(map->collection.allocator, map);
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
79 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
80
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
81 static int cx_hash_map_put(
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
82 CxMap *map,
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
83 CxHashKey key,
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
84 void *value
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
85 ) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
86 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
87 const CxAllocator *allocator = map->collection.allocator;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
88
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
89 unsigned hash = key.hash;
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
90 if (hash == 0) {
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
91 cx_hash_murmur(&key);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
92 hash = key.hash;
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
93 }
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
94
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
95 size_t slot = hash % hash_map->bucket_count;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
96 struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
97 struct cx_hash_map_element_s *prev = NULL;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
98
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
99 while (elm != NULL && elm->key.hash < hash) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
100 prev = elm;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
101 elm = elm->next;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
102 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
103
575
b05935945637 fix #200 - key contents not compared in cx_hash_map_put()
Mike Becker <universe@uap-core.de>
parents: 574
diff changeset
104 if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len &&
690
2c2304622981 simplify CxHashKey
Mike Becker <universe@uap-core.de>
parents: 688
diff changeset
105 memcmp(elm->key.data, key.data, key.len) == 0) {
1114
ad5eeb256242 fix that overwriting a map element did not call any destructor function
Mike Becker <universe@uap-core.de>
parents: 1111
diff changeset
106 // overwrite existing element, but call destructors first
ad5eeb256242 fix that overwriting a map element did not call any destructor function
Mike Becker <universe@uap-core.de>
parents: 1111
diff changeset
107 cx_invoke_destructor(map, elm->data);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
108 if (map->collection.store_pointer) {
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
109 memcpy(elm->data, &value, sizeof(void *));
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
110 } else {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
111 memcpy(elm->data, value, map->collection.elem_size);
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
112 }
574
d566bd3e6af8 invert if-condition in preparation for the next bugfix
Mike Becker <universe@uap-core.de>
parents: 573
diff changeset
113 } else {
575
b05935945637 fix #200 - key contents not compared in cx_hash_map_put()
Mike Becker <universe@uap-core.de>
parents: 574
diff changeset
114 // allocate new element
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
115 struct cx_hash_map_element_s *e = cxMalloc(
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
116 allocator,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
117 sizeof(struct cx_hash_map_element_s) + map->collection.elem_size
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
118 );
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
119 if (e == NULL) return -1;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
120
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
121 // write the value
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
122 if (map->collection.store_pointer) {
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
123 memcpy(e->data, &value, sizeof(void *));
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
124 } else {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
125 memcpy(e->data, value, map->collection.elem_size);
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
126 }
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
127
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
128 // copy the key
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
129 void *kd = cxMalloc(allocator, key.len);
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
130 if (kd == NULL) return -1;
690
2c2304622981 simplify CxHashKey
Mike Becker <universe@uap-core.de>
parents: 688
diff changeset
131 memcpy(kd, key.data, key.len);
2c2304622981 simplify CxHashKey
Mike Becker <universe@uap-core.de>
parents: 688
diff changeset
132 e->key.data = kd;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
133 e->key.len = key.len;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
134 e->key.hash = hash;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
135
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
136 // insert the element into the linked list
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
137 if (prev == NULL) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
138 hash_map->buckets[slot] = e;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
139 } else {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
140 prev->next = e;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
141 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
142 e->next = elm;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
143
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
144 // increase the size
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
145 map->collection.size++;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
146 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
147
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
148 return 0;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
149 }
549
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
151 static void cx_hash_map_unlink(
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
152 struct cx_hash_map_s *hash_map,
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
153 size_t slot,
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
154 struct cx_hash_map_element_s *prev,
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
155 struct cx_hash_map_element_s *elm
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
156 ) {
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
157 // unlink
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
158 if (prev == NULL) {
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
159 hash_map->buckets[slot] = elm->next;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
160 } else {
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
161 prev->next = elm->next;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
162 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
163 // free element
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
164 cxFree(hash_map->base.collection.allocator, (void *) elm->key.data);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
165 cxFree(hash_map->base.collection.allocator, elm);
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
166 // decrease size
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
167 hash_map->base.collection.size--;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
168 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
169
549
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 /**
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
171 * Helper function to avoid code duplication.
549
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 *
1088
881994685681 do not mixup Doxygen command syntax
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
173 * If @p remove is true, and @p targetbuf is @c NULL, the element
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
174 * will be destroyed when found.
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
175 *
1088
881994685681 do not mixup Doxygen command syntax
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
176 * If @p remove is true, and @p targetbuf is set, the element will
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
177 * be copied to that buffer and no destructor function is called.
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
178 *
1088
881994685681 do not mixup Doxygen command syntax
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
179 * If @p remove is false, @p targetbuf must not be non-null and
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
180 * either the pointer, when the map is storing pointers, is copied
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
181 * to the target buffer, or a pointer to the stored object will
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
182 * be copied to the target buffer.
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
183 *
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
184 * @param map the map
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
185 * @param key the key to look up
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
186 * @param targetbuf see description
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
187 * @param remove flag indicating whether the looked up entry shall be removed
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
188 * @return zero, if the key was found, non-zero otherwise
549
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 */
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
190 static int cx_hash_map_get_remove(
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
191 CxMap *map,
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
192 CxHashKey key,
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
193 void *targetbuf,
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
194 bool remove
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
195 ) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
196 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
197
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
198 unsigned hash = key.hash;
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
199 if (hash == 0) {
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
200 cx_hash_murmur(&key);
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
201 hash = key.hash;
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
202 }
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
203
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
204 size_t slot = hash % hash_map->bucket_count;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
205 struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
206 struct cx_hash_map_element_s *prev = NULL;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
207 while (elm && elm->key.hash <= hash) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
208 if (elm->key.hash == hash && elm->key.len == key.len) {
690
2c2304622981 simplify CxHashKey
Mike Becker <universe@uap-core.de>
parents: 688
diff changeset
209 if (memcmp(elm->key.data, key.data, key.len) == 0) {
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
210 if (remove) {
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
211 if (targetbuf == NULL) {
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
212 cx_invoke_destructor(map, elm->data);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
213 } else {
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
214 memcpy(targetbuf, elm->data, map->collection.elem_size);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
215 }
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
216 cx_hash_map_unlink(hash_map, slot, prev, elm);
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
217 } else {
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
218 assert(targetbuf != NULL);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
219 void *data = NULL;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
220 if (map->collection.store_pointer) {
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
221 data = *(void **) elm->data;
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
222 } else {
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
223 data = elm->data;
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
224 }
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
225 memcpy(targetbuf, &data, sizeof(void *));
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
226 }
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
227 return 0;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
228 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
229 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
230 prev = elm;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
231 elm = prev->next;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
232 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
233
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
234 return 1;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
235 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
236
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
237 static void *cx_hash_map_get(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
238 const CxMap *map,
563
69a83fad8a35 improve hash key handling
Mike Becker <universe@uap-core.de>
parents: 562
diff changeset
239 CxHashKey key
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
240 ) {
688
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
241 // we can safely cast, because we know the map stays untouched
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
242 void *ptr = NULL;
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
243 int found = cx_hash_map_get_remove((CxMap *) map, key, &ptr, false);
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
244 return found == 0 ? ptr : NULL;
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
245 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
246
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
247 static int cx_hash_map_remove(
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
248 CxMap *map,
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
249 CxHashKey key,
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
250 void *targetbuf
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
251 ) {
994
3603bdf4a78b remove map detach function - fixes #487
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
252 return cx_hash_map_get_remove(map, key, targetbuf, true);
549
d7f0b5a9a985 #189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 }
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
254
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
255 static void *cx_hash_map_iter_current_entry(const void *it) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
256 const CxMapIterator *iter = it;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
257 // we have to cast away const, because of the signature
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
258 return (void*) &iter->entry;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
259 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
260
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
261 static void *cx_hash_map_iter_current_key(const void *it) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
262 const CxMapIterator *iter = it;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
263 struct cx_hash_map_element_s *elm = iter->elem;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
264 return &elm->key;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
265 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
266
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
267 static void *cx_hash_map_iter_current_value(const void *it) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
268 const CxMapIterator *iter = it;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
269 const CxMap *map = iter->map.c;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
270 struct cx_hash_map_element_s *elm = iter->elem;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
271 if (map->collection.store_pointer) {
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
272 return *(void **) elm->data;
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
273 } else {
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
274 return elm->data;
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
275 }
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
276 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
277
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
278 static bool cx_hash_map_iter_valid(const void *it) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
279 const CxMapIterator *iter = it;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
280 return iter->elem != NULL;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
281 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
282
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 575
diff changeset
283 static void cx_hash_map_iter_next(void *it) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
284 CxMapIterator *iter = it;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
285 CxMap *map = iter->map.m;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
286 struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
287 struct cx_hash_map_element_s *elm = iter->elem;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
288
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
289 // remove current element, if asked
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
290 if (iter->base.remove) {
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 575
diff changeset
291
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
292 // clear the flag
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
293 iter->base.remove = false;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
294
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
295 // determine the next element
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
296 struct cx_hash_map_element_s *next = elm->next;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
297
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
298 // search the previous element
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
299 struct cx_hash_map_element_s *prev = NULL;
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
300 if (hmap->buckets[iter->slot] != elm) {
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
301 prev = hmap->buckets[iter->slot];
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
302 while (prev->next != elm) {
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
303 prev = prev->next;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
304 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
305 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
306
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
307 // destroy
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
308 cx_invoke_destructor(map, elm->data);
686
64919f63f059 add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents: 685
diff changeset
309
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
310 // unlink
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
311 cx_hash_map_unlink(hmap, iter->slot, prev, elm);
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
312
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
313 // advance
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
314 elm = next;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
315 } else {
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
316 // just advance
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
317 elm = elm->next;
560
2d6a3e2dc8ff fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents: 557
diff changeset
318 iter->index++;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
319 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
320
560
2d6a3e2dc8ff fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents: 557
diff changeset
321 // search the next bucket, if required
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
322 while (elm == NULL && ++iter->slot < hmap->bucket_count) {
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
323 elm = hmap->buckets[iter->slot];
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
324 }
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
325 iter->elem = elm;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
326
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
327 // copy data to a location where the iterator can point to
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
328 // we need to do it here, because the iterator function call
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
329 // must not modify the iterator (the parameter is const)
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
330 if (elm != NULL) {
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
331 iter->entry.key = &elm->key;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
332 if (iter->map.c->collection.store_pointer) {
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
333 iter->entry.value = *(void **) elm->data;
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
334 } else {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
335 iter->entry.value = elm->data;
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
336 }
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
337 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
338 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
339
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
340 static CxMapIterator cx_hash_map_iterator(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
341 const CxMap *map,
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
342 enum cx_map_iterator_type type
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
343 ) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
344 CxMapIterator iter;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
345
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
346 iter.map.c = map;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
347 iter.elem_count = map->collection.size;
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
348
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
349 switch (type) {
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
350 case CX_MAP_ITERATOR_PAIRS:
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
351 iter.elem_size = sizeof(CxMapEntry);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
352 iter.base.current = cx_hash_map_iter_current_entry;
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
353 break;
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
354 case CX_MAP_ITERATOR_KEYS:
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents: 829
diff changeset
355 iter.elem_size = sizeof(CxHashKey);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
356 iter.base.current = cx_hash_map_iter_current_key;
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
357 break;
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
358 case CX_MAP_ITERATOR_VALUES:
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
359 iter.elem_size = map->collection.elem_size;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
360 iter.base.current = cx_hash_map_iter_current_value;
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
361 break;
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
362 default:
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
363 assert(false); // LCOV_EXCL_LINE
709
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
364 }
1e8ba59e7911 simplify map class structure
Mike Becker <universe@uap-core.de>
parents: 691
diff changeset
365
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
366 iter.base.valid = cx_hash_map_iter_valid;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
367 iter.base.next = cx_hash_map_iter_next;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
368 iter.base.remove = false;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
369 iter.base.mutating = false;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
370
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
371 iter.slot = 0;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
372 iter.index = 0;
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
373
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
374 if (map->collection.size > 0) {
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
375 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
560
2d6a3e2dc8ff fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents: 557
diff changeset
376 struct cx_hash_map_element_s *elm = hash_map->buckets[0];
665
c4041b07165e fix hashmap iterator skipping the second element in some cases
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 658
diff changeset
377 while (elm == NULL) {
c4041b07165e fix hashmap iterator skipping the second element in some cases
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 658
diff changeset
378 elm = hash_map->buckets[++iter.slot];
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
379 }
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
380 iter.elem = elm;
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
381 iter.entry.key = &elm->key;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
382 if (map->collection.store_pointer) {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
383 iter.entry.value = *(void **) elm->data;
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
384 } else {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
385 iter.entry.value = elm->data;
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
386 }
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
387 } else {
1115
6db21dee4929 create specialized map iterators - fixes #550
Mike Becker <universe@uap-core.de>
parents: 1114
diff changeset
388 iter.elem = NULL;
551
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
389 }
2946e13c89a4 #189 implement map iterators
Mike Becker <universe@uap-core.de>
parents: 550
diff changeset
390
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
391 return iter;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
392 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
393
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
394 static cx_map_class cx_hash_map_class = {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
395 cx_hash_map_destructor,
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
396 cx_hash_map_clear,
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
397 cx_hash_map_put,
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
398 cx_hash_map_get,
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
399 cx_hash_map_remove,
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
400 cx_hash_map_iterator,
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
401 };
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
402
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
403 CxMap *cxHashMapCreate(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
404 const CxAllocator *allocator,
658
56c62780582e make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
405 size_t itemsize,
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
406 size_t buckets
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
407 ) {
989
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 962
diff changeset
408 if (allocator == NULL) {
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 962
diff changeset
409 allocator = cxDefaultAllocator;
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 962
diff changeset
410 }
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 962
diff changeset
411
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
412 if (buckets == 0) {
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
413 // implementation defined default
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
414 buckets = 16;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
415 }
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
416
688
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
417 struct cx_hash_map_s *map = cxCalloc(allocator, 1,
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
418 sizeof(struct cx_hash_map_s));
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
419 if (map == NULL) return NULL;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
420
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
421 // initialize hash map members
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
422 map->bucket_count = buckets;
688
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
423 map->buckets = cxCalloc(allocator, buckets,
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
424 sizeof(struct cx_hash_map_element_s *));
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
425 if (map->buckets == NULL) { // LCOV_EXCL_START
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
426 cxFree(allocator, map);
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
427 return NULL;
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
428 } // LCOV_EXCL_STOP
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
429
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
430 // initialize base members
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
431 map->base.cl = &cx_hash_map_class;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
432 map->base.collection.allocator = allocator;
668
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 665
diff changeset
433
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 665
diff changeset
434 if (itemsize > 0) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
435 map->base.collection.elem_size = itemsize;
668
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 665
diff changeset
436 } else {
1111
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1088
diff changeset
437 map->base.collection.elem_size = sizeof(void *);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
438 map->base.collection.store_pointer = true;
668
d7129285ac32 add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents: 665
diff changeset
439 }
550
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
440
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
441 return (CxMap *) map;
89b2a83728b1 #189 basic map implementation
Mike Becker <universe@uap-core.de>
parents: 549
diff changeset
442 }
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
443
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
444 int cxMapRehash(CxMap *map) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
445 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
446 if (map->collection.size > ((hash_map->bucket_count * 3) >> 2)) {
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
447
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
448 size_t new_bucket_count = (map->collection.size * 5) >> 1;
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 994
diff changeset
449 if (new_bucket_count < hash_map->bucket_count) {
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 994
diff changeset
450 errno = EOVERFLOW;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 994
diff changeset
451 return 1;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 994
diff changeset
452 }
688
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
453 struct cx_hash_map_element_s **new_buckets = cxCalloc(
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
454 map->collection.allocator,
688
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
455 new_bucket_count, sizeof(struct cx_hash_map_element_s *)
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
456 );
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
457
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
458 if (new_buckets == NULL) return 1;
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
459
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
460 // iterate through the elements and assign them to their new slots
962
cd418898af5c remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
461 for (size_t slot = 0; slot < hash_map->bucket_count; slot++) {
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
462 struct cx_hash_map_element_s *elm = hash_map->buckets[slot];
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
463 while (elm != NULL) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
464 struct cx_hash_map_element_s *next = elm->next;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
465 size_t new_slot = elm->key.hash % new_bucket_count;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
466
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
467 // find position where to insert
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
468 struct cx_hash_map_element_s *bucket_next = new_buckets[new_slot];
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
469 struct cx_hash_map_element_s *bucket_prev = NULL;
688
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
470 while (bucket_next != NULL &&
c27fa8b67286 serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents: 686
diff changeset
471 bucket_next->key.hash < elm->key.hash) {
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
472 bucket_prev = bucket_next;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
473 bucket_next = bucket_next->next;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
474 }
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
475
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
476 // insert
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
477 if (bucket_prev == NULL) {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
478 elm->next = new_buckets[new_slot];
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
479 new_buckets[new_slot] = elm;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
480 } else {
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
481 bucket_prev->next = elm;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
482 elm->next = bucket_next;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
483 }
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
484
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
485 // advance
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
486 elm = next;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
487 }
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
488 }
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
489
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
490 // assign result to the map
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
491 hash_map->bucket_count = new_bucket_count;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
492 cxFree(map->collection.allocator, hash_map->buckets);
562
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
493 hash_map->buckets = new_buckets;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
494 }
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
495 return 0;
fd3368c20413 #189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents: 560
diff changeset
496 }

mercurial