Fri, 21 Apr 2023 20:28:55 +0200
fix missing const qualifier for allocator
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 <string.h> |
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include "cx/hash_map.h" |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
31 | #include "cx/utils.h" |
549
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
33 | 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
|
34 | /** 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
|
35 | 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
|
36 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
37 | /** 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
|
38 | CxHashKey key; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
39 | |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
40 | /** 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
|
41 | char data[]; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
42 | }; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
43 | |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
44 | 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
|
45 | 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
|
46 | cx_for_n(i, hash_map->bucket_count) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
47 | 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
|
48 | if (elem != NULL) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
49 | do { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
50 | 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
|
51 | // invoke the destructor |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
52 | cx_invoke_destructor(map, elem->data); |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
53 | // free the key data |
690 | 54 | cxFree(map->allocator, (void *) elem->key.data); |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
55 | // free the node |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
56 | cxFree(map->allocator, elem); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
57 | // proceed |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
58 | elem = next; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
59 | } while (elem != NULL); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
60 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
61 | // do not leave a dangling pointer |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
62 | hash_map->buckets[i] = NULL; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
63 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
64 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
65 | map->size = 0; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
66 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
67 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
68 | 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
|
69 | 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
|
70 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
71 | // free the buckets |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
72 | cx_hash_map_clear(map); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
73 | cxFree(map->allocator, hash_map->buckets); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
74 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
75 | // free the map structure |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
76 | cxFree(map->allocator, map); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
77 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
78 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
79 | static int cx_hash_map_put( |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
80 | CxMap *map, |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
81 | CxHashKey key, |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
82 | void *value |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
83 | ) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
84 | struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map; |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
85 | CxAllocator const *allocator = map->allocator; |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
86 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
87 | unsigned hash = key.hash; |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
88 | if (hash == 0) { |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
89 | cx_hash_murmur(&key); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
90 | hash = key.hash; |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
91 | } |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
92 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
93 | size_t slot = hash % hash_map->bucket_count; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
94 | 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
|
95 | struct cx_hash_map_element_s *prev = NULL; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
96 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
97 | while (elm != NULL && elm->key.hash < hash) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
98 | prev = elm; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
99 | elm = elm->next; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
100 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
101 | |
575
b05935945637
fix #200 - key contents not compared in cx_hash_map_put()
Mike Becker <universe@uap-core.de>
parents:
574
diff
changeset
|
102 | if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len && |
690 | 103 | memcmp(elm->key.data, key.data, key.len) == 0) { |
574
d566bd3e6af8
invert if-condition in preparation for the next bugfix
Mike Becker <universe@uap-core.de>
parents:
573
diff
changeset
|
104 | // overwrite existing element |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
105 | if (map->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
|
106 | 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
|
107 | } else { |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
108 | memcpy(elm->data, value, map->item_size); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
109 | } |
574
d566bd3e6af8
invert if-condition in preparation for the next bugfix
Mike Becker <universe@uap-core.de>
parents:
573
diff
changeset
|
110 | } else { |
575
b05935945637
fix #200 - key contents not compared in cx_hash_map_put()
Mike Becker <universe@uap-core.de>
parents:
574
diff
changeset
|
111 | // 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
|
112 | 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
|
113 | allocator, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
114 | sizeof(struct cx_hash_map_element_s) + map->item_size |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
115 | ); |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
116 | if (e == NULL) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
117 | return -1; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
118 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
119 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
120 | // write the value |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
121 | if (map->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
|
122 | 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
|
123 | } else { |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
124 | memcpy(e->data, value, map->item_size); |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
125 | } |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
126 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
127 | // copy the key |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
128 | void *kd = cxMalloc(allocator, key.len); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
129 | if (kd == NULL) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
130 | return -1; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
131 | } |
690 | 132 | memcpy(kd, key.data, key.len); |
133 | e->key.data = kd; | |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
134 | e->key.len = key.len; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
135 | e->key.hash = hash; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
136 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
137 | // insert the element into the linked list |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
138 | if (prev == NULL) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
139 | hash_map->buckets[slot] = e; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
140 | } else { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
141 | prev->next = e; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
142 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
143 | e->next = elm; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
144 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
145 | // increase the size |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
146 | map->size++; |
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 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
149 | return 0; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
150 | } |
549
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
152 | static void cx_hash_map_unlink( |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
153 | struct cx_hash_map_s *hash_map, |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
154 | size_t slot, |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
155 | struct cx_hash_map_element_s *prev, |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
156 | struct cx_hash_map_element_s *elm |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
157 | ) { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
158 | // unlink |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
159 | if (prev == NULL) { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
160 | hash_map->buckets[slot] = elm->next; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
161 | } else { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
162 | prev->next = elm->next; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
163 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
164 | // free element |
690 | 165 | cxFree(hash_map->base.allocator, (void *) elm->key.data); |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
166 | cxFree(hash_map->base.allocator, elm); |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
167 | // decrease size |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
168 | hash_map->base.size--; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
169 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
170 | |
549
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
171 | /** |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
172 | * Helper function to avoid code duplication. |
549
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | * |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
174 | * @param map the map |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
175 | * @param key the key to look up |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
176 | * @param remove flag indicating whether the looked up entry shall be removed |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
177 | * @param destroy flag indicating whether the destructor shall be invoked |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
178 | * @return a pointer to the value corresponding to the key or \c NULL |
549
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | */ |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
180 | static void *cx_hash_map_get_remove( |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
181 | CxMap *map, |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
182 | CxHashKey key, |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
183 | bool remove, |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
184 | bool destroy |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
185 | ) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
186 | 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
|
187 | |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
188 | unsigned hash = key.hash; |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
189 | if (hash == 0) { |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
190 | cx_hash_murmur(&key); |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
191 | hash = key.hash; |
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
192 | } |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
193 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
194 | size_t slot = hash % hash_map->bucket_count; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
195 | 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
|
196 | struct cx_hash_map_element_s *prev = NULL; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
197 | while (elm && elm->key.hash <= hash) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
198 | if (elm->key.hash == hash && elm->key.len == key.len) { |
690 | 199 | if (memcmp(elm->key.data, key.data, key.len) == 0) { |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
200 | void *data = NULL; |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
201 | if (destroy) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
202 | cx_invoke_destructor(map, elm->data); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
203 | } else { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
204 | if (map->store_pointer) { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
205 | data = *(void **) elm->data; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
206 | } else { |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
207 | data = elm->data; |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
208 | } |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
209 | } |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
210 | if (remove) { |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
211 | cx_hash_map_unlink(hash_map, slot, prev, elm); |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
212 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
213 | return data; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
214 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
215 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
216 | prev = elm; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
217 | elm = prev->next; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
218 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
219 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
220 | return NULL; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
221 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
222 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
223 | static void *cx_hash_map_get( |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
224 | CxMap const *map, |
563
69a83fad8a35
improve hash key handling
Mike Becker <universe@uap-core.de>
parents:
562
diff
changeset
|
225 | CxHashKey key |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
226 | ) { |
688
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
227 | // we can safely cast, because we know the map stays untouched |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
228 | return cx_hash_map_get_remove((CxMap *) map, key, false, false); |
550
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 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
231 | static void *cx_hash_map_remove( |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
232 | CxMap *map, |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
233 | CxHashKey key, |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
234 | bool destroy |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
235 | ) { |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
236 | return cx_hash_map_get_remove(map, key, true, destroy); |
549
d7f0b5a9a985
#189 declare basic map functions
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
237 | } |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
238 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
239 | static void *cx_hash_map_iter_current_entry(void const *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
240 | struct cx_iterator_s const *iter = it; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
241 | // struct has to have a compatible signature |
573
3f3a0d19db58
remove unused variable (return immediately)
Mike Becker <universe@uap-core.de>
parents:
563
diff
changeset
|
242 | return (struct cx_map_entry_s *) &(iter->kv_data); |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
243 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
244 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
245 | static void *cx_hash_map_iter_current_key(void const *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
246 | struct cx_iterator_s const *iter = it; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
247 | struct cx_hash_map_element_s *elm = iter->elem_handle; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
248 | return &elm->key; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
249 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
250 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
251 | static void *cx_hash_map_iter_current_value(void const *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
252 | struct cx_iterator_s const *iter = it; |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
253 | struct cx_hash_map_s const *map = iter->src_handle; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
254 | struct cx_hash_map_element_s *elm = iter->elem_handle; |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
255 | if (map->base.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
|
256 | 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
|
257 | } else { |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
258 | 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
|
259 | } |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
260 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
261 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
262 | static bool cx_hash_map_iter_valid(void const *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
263 | struct cx_iterator_s const *iter = it; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
264 | return iter->elem_handle != NULL; |
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 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
267 | static void cx_hash_map_iter_next(void *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
268 | struct cx_iterator_s *iter = it; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
269 | struct cx_hash_map_element_s *elm = iter->elem_handle; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
270 | |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
271 | // remove current element, if asked |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
272 | if (iter->base.remove) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
273 | // obtain mutable pointer to the map |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
274 | struct cx_mut_iterator_s *miter = it; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
275 | struct cx_hash_map_s *map = miter->src_handle; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
276 | |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
277 | // clear the flag |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
278 | iter->base.remove = false; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
279 | |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
280 | // determine the next element |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
281 | struct cx_hash_map_element_s *next = elm->next; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
282 | |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
283 | // search the previous element |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
284 | struct cx_hash_map_element_s *prev = NULL; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
285 | if (map->buckets[iter->slot] != elm) { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
286 | prev = map->buckets[iter->slot]; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
287 | while (prev->next != elm) { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
288 | prev = prev->next; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
289 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
290 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
291 | |
686
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
292 | // destroy |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
293 | cx_invoke_destructor((struct cx_map_s *) map, elm->data); |
64919f63f059
add destructor functions for maps - fixes #253
Mike Becker <universe@uap-core.de>
parents:
685
diff
changeset
|
294 | |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
295 | // unlink |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
296 | cx_hash_map_unlink(map, iter->slot, prev, elm); |
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 | // advance |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
299 | elm = next; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
300 | } else { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
301 | // just advance |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
302 | elm = elm->next; |
560
2d6a3e2dc8ff
fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents:
557
diff
changeset
|
303 | iter->index++; |
551
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 | |
560
2d6a3e2dc8ff
fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents:
557
diff
changeset
|
306 | // search the next bucket, if required |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
307 | struct cx_hash_map_s const *map = iter->src_handle; |
560
2d6a3e2dc8ff
fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents:
557
diff
changeset
|
308 | while (elm == NULL && ++iter->slot < map->bucket_count) { |
2d6a3e2dc8ff
fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents:
557
diff
changeset
|
309 | elm = map->buckets[iter->slot]; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
310 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
311 | |
560
2d6a3e2dc8ff
fix wrong slot and index numbers
Mike Becker <universe@uap-core.de>
parents:
557
diff
changeset
|
312 | // fill the struct with the next element |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
313 | iter->elem_handle = elm; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
314 | if (elm == NULL) { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
315 | iter->kv_data.key = NULL; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
316 | iter->kv_data.value = NULL; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
317 | } else { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
318 | iter->kv_data.key = &elm->key; |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
319 | if (map->base.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
|
320 | iter->kv_data.value = *(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
|
321 | } else { |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
322 | iter->kv_data.value = elm->data; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
323 | } |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
324 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
325 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
326 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
327 | static bool cx_hash_map_iter_flag_rm(void *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
328 | struct cx_iterator_base_s *iter = it; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
329 | if (iter->mutating) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
330 | iter->remove = true; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
331 | return true; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
332 | } else { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
333 | return false; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
334 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
335 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
336 | |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
337 | static CxIterator cx_hash_map_iterator(CxMap const *map) { |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
338 | CxIterator iter; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
339 | |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
340 | iter.src_handle = map; |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
341 | iter.base.valid = cx_hash_map_iter_valid; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
342 | iter.base.next = cx_hash_map_iter_next; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
343 | iter.base.current = cx_hash_map_iter_current_entry; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
344 | iter.base.flag_removal = cx_hash_map_iter_flag_rm; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
345 | iter.base.remove = false; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
346 | iter.base.mutating = false; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
347 | |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
348 | iter.slot = 0; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
349 | iter.index = 0; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
350 | |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
351 | if (map->size > 0) { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
352 | 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
|
353 | 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
|
354 | while (elm == NULL) { |
c4041b07165e
fix hashmap iterator skipping the second element in some cases
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
658
diff
changeset
|
355 | elm = hash_map->buckets[++iter.slot]; |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
356 | } |
554
fd3d843b839d
fix kv-pair not initialized
Mike Becker <universe@uap-core.de>
parents:
551
diff
changeset
|
357 | iter.elem_handle = elm; |
fd3d843b839d
fix kv-pair not initialized
Mike Becker <universe@uap-core.de>
parents:
551
diff
changeset
|
358 | iter.kv_data.key = &elm->key; |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
359 | if (map->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
|
360 | iter.kv_data.value = *(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
|
361 | } else { |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
362 | iter.kv_data.value = elm->data; |
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
363 | } |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
364 | } else { |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
365 | iter.elem_handle = NULL; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
366 | iter.kv_data.key = NULL; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
367 | iter.kv_data.value = NULL; |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
368 | } |
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
369 | |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
370 | return iter; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
371 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
372 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
373 | static CxIterator cx_hash_map_iterator_keys(CxMap const *map) { |
551
2946e13c89a4
#189 implement map iterators
Mike Becker <universe@uap-core.de>
parents:
550
diff
changeset
|
374 | CxIterator iter = cx_hash_map_iterator(map); |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
375 | iter.base.current = cx_hash_map_iter_current_key; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
376 | return iter; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
377 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
378 | |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
379 | static CxIterator cx_hash_map_iterator_values(CxMap const *map) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
380 | CxIterator iter = cx_hash_map_iterator(map); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
381 | iter.base.current = cx_hash_map_iter_current_value; |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
382 | return iter; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
383 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
384 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
385 | static CxMutIterator cx_hash_map_mut_iterator(CxMap *map) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
386 | CxIterator it = cx_hash_map_iterator(map); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
387 | it.base.mutating = true; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
388 | |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
389 | // we know the iterators share the same memory layout |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
390 | CxMutIterator iter; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
391 | memcpy(&iter, &it, sizeof(CxMutIterator)); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
392 | return iter; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
393 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
394 | |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
395 | static CxMutIterator cx_hash_map_mut_iterator_keys(CxMap *map) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
396 | CxMutIterator iter = cx_hash_map_mut_iterator(map); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
397 | iter.base.current = cx_hash_map_iter_current_key; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
398 | return iter; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
399 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
400 | |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
401 | static CxMutIterator cx_hash_map_mut_iterator_values(CxMap *map) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
402 | CxMutIterator iter = cx_hash_map_mut_iterator(map); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
403 | iter.base.current = cx_hash_map_iter_current_value; |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
404 | return iter; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
405 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
406 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
407 | static cx_map_class cx_hash_map_class = { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
408 | cx_hash_map_destructor, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
409 | cx_hash_map_clear, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
410 | cx_hash_map_put, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
411 | cx_hash_map_get, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
412 | cx_hash_map_remove, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
413 | cx_hash_map_iterator, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
414 | cx_hash_map_iterator_keys, |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
415 | cx_hash_map_iterator_values, |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
416 | cx_hash_map_mut_iterator, |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
417 | cx_hash_map_mut_iterator_keys, |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
575
diff
changeset
|
418 | cx_hash_map_mut_iterator_values, |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
419 | }; |
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 | CxMap *cxHashMapCreate( |
691
65baf7f45ac8
bring a generic interface to CxMap
Mike Becker <universe@uap-core.de>
parents:
690
diff
changeset
|
422 | CxAllocator const *allocator, |
658
56c62780582e
make hashmap store objects instead of pointers by default - fixes #239
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
423 | size_t itemsize, |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
424 | size_t buckets |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
425 | ) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
426 | if (buckets == 0) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
427 | // implementation defined default |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
428 | buckets = 16; |
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 | |
688
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
431 | struct cx_hash_map_s *map = cxCalloc(allocator, 1, |
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
432 | sizeof(struct cx_hash_map_s)); |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
433 | if (map == NULL) return NULL; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
434 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
435 | // initialize hash map members |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
436 | map->bucket_count = buckets; |
688
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
437 | map->buckets = cxCalloc(allocator, buckets, |
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
438 | sizeof(struct cx_hash_map_element_s *)); |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
439 | if (map->buckets == NULL) { |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
440 | cxFree(allocator, map); |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
441 | return NULL; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
442 | } |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
443 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
444 | // initialize base members |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
445 | map->base.cl = &cx_hash_map_class; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
446 | map->base.allocator = allocator; |
668
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
665
diff
changeset
|
447 | |
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
665
diff
changeset
|
448 | if (itemsize > 0) { |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
449 | map->base.store_pointer = false; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
450 | map->base.item_size = itemsize; |
668
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
665
diff
changeset
|
451 | } else { |
685
2dd841e364af
add base collection members to map interface
Mike Becker <universe@uap-core.de>
parents:
677
diff
changeset
|
452 | map->base.store_pointer = true; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
669
diff
changeset
|
453 | map->base.item_size = sizeof(void *); |
668
d7129285ac32
add CX_STORE_POINTERS special item size for maps
Mike Becker <universe@uap-core.de>
parents:
665
diff
changeset
|
454 | } |
550
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
455 | |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
456 | return (CxMap *) map; |
89b2a83728b1
#189 basic map implementation
Mike Becker <universe@uap-core.de>
parents:
549
diff
changeset
|
457 | } |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
458 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
459 | int cxMapRehash(CxMap *map) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
460 | struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
461 | if (map->size > ((hash_map->bucket_count * 3) >> 2)) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
462 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
463 | size_t new_bucket_count = (map->size * 5) >> 1; |
688
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
464 | struct cx_hash_map_element_s **new_buckets = cxCalloc( |
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
465 | map->allocator, |
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
466 | 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
|
467 | ); |
562
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
468 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
469 | if (new_buckets == NULL) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
470 | return 1; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
471 | } |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
472 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
473 | // iterate through the elements and assign them to their new slots |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
474 | cx_for_n(slot, hash_map->bucket_count) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
475 | 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
|
476 | while (elm != NULL) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
477 | 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
|
478 | 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
|
479 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
480 | // find position where to insert |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
481 | 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
|
482 | 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
|
483 | while (bucket_next != NULL && |
c27fa8b67286
serious code formatting problems ;-)
Mike Becker <universe@uap-core.de>
parents:
686
diff
changeset
|
484 | 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
|
485 | bucket_prev = bucket_next; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
486 | bucket_next = bucket_next->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 | // insert |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
490 | if (bucket_prev == NULL) { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
491 | elm->next = new_buckets[new_slot]; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
492 | new_buckets[new_slot] = elm; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
493 | } else { |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
494 | bucket_prev->next = elm; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
495 | elm->next = bucket_next; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
496 | } |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
497 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
498 | // advance |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
499 | elm = next; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
500 | } |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
501 | } |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
502 | |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
503 | // assign result to the map |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
504 | hash_map->bucket_count = new_bucket_count; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
505 | cxFree(map->allocator, hash_map->buckets); |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
506 | hash_map->buckets = new_buckets; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
507 | } |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
508 | return 0; |
fd3368c20413
#189 #199 implement and test map rehash
Mike Becker <universe@uap-core.de>
parents:
560
diff
changeset
|
509 | } |