Mon, 30 Dec 2019 09:54:10 +0100
Added tag v2.1 for changeset 871a8ffe6c9d
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
1 | /* |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
3 | * |
259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
27 | */ |
2
79646375a420
Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
29 | #include "ucx/map.h" |
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
30 | |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
31 | #include <stdlib.h> |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
32 | #include <string.h> |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
33 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
34 | UcxMap *ucx_map_new(size_t size) { |
137
81a02ad99388
changed parameter order of ucx_map_new_a
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
136
diff
changeset
|
35 | return ucx_map_new_a(NULL, size); |
107
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
36 | } |
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
37 | |
137
81a02ad99388
changed parameter order of ucx_map_new_a
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
136
diff
changeset
|
38 | UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size) { |
45
dd03226c1a6b
map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
39 | if(size == 0) { |
dd03226c1a6b
map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
40 | size = 16; |
dd03226c1a6b
map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
41 | } |
108
d2b1e67b2b48
new properties parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
107
diff
changeset
|
42 | |
107
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
43 | if(!allocator) { |
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
44 | allocator = ucx_default_allocator(); |
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
45 | } |
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
46 | |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
47 | UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap)); |
139 | 48 | if (!map) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
49 | return NULL; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
50 | } |
107
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
51 | |
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
52 | map->allocator = allocator; |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
53 | map->map = (UcxMapElement**)alcalloc( |
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
54 | allocator, size, sizeof(UcxMapElement*)); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
55 | if(map->map == NULL) { |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
56 | alfree(allocator, map); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
57 | return NULL; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
58 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
59 | map->size = size; |
45
dd03226c1a6b
map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
60 | map->count = 0; |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
61 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
62 | return map; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
63 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
64 | |
206 | 65 | static void ucx_map_free_elmlist_contents(UcxMap *map) { |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
66 | for (size_t n = 0 ; n < map->size ; n++) { |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
67 | UcxMapElement *elem = map->map[n]; |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
68 | if (elem != NULL) { |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
69 | do { |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
70 | UcxMapElement *next = elem->next; |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
71 | alfree(map->allocator, elem->key.data); |
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
72 | alfree(map->allocator, elem); |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
73 | elem = next; |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
74 | } while (elem != NULL); |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
75 | } |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
76 | } |
70
6721482eaf8e
fixed memory leak in ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
77 | } |
6721482eaf8e
fixed memory leak in ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
78 | |
6721482eaf8e
fixed memory leak in ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
79 | void ucx_map_free(UcxMap *map) { |
206 | 80 | ucx_map_free_elmlist_contents(map); |
81 | alfree(map->allocator, map->map); | |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
82 | alfree(map->allocator, map); |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
83 | } |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
84 | |
209
4f02199d8aae
moved ucx_destructor type to ucx.h and added destructor argument to ucx_map_free_content
Mike Becker <universe@uap-core.de>
parents:
208
diff
changeset
|
85 | void ucx_map_free_content(UcxMap *map, ucx_destructor destr) { |
208
262c7be94eba
added convenience function ucx_map_free_contents()
Mike Becker <universe@uap-core.de>
parents:
207
diff
changeset
|
86 | UcxMapIterator iter = ucx_map_iterator(map); |
262c7be94eba
added convenience function ucx_map_free_contents()
Mike Becker <universe@uap-core.de>
parents:
207
diff
changeset
|
87 | void *val; |
262c7be94eba
added convenience function ucx_map_free_contents()
Mike Becker <universe@uap-core.de>
parents:
207
diff
changeset
|
88 | UCX_MAP_FOREACH(key, val, iter) { |
277
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
89 | if (destr) { |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
90 | destr(val); |
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
91 | } else { |
287
98da78a1e69a
adds ucx_avl_free_content() function and documentation in modules.md
Mike Becker <universe@uap-core.de>
parents:
277
diff
changeset
|
92 | alfree(map->allocator, val); |
277
f819fe5e20f5
makes destructor functions for *_free_content() optional + more documentation for UcxProperties
Mike Becker <universe@uap-core.de>
parents:
259
diff
changeset
|
93 | } |
208
262c7be94eba
added convenience function ucx_map_free_contents()
Mike Becker <universe@uap-core.de>
parents:
207
diff
changeset
|
94 | } |
262c7be94eba
added convenience function ucx_map_free_contents()
Mike Becker <universe@uap-core.de>
parents:
207
diff
changeset
|
95 | } |
262c7be94eba
added convenience function ucx_map_free_contents()
Mike Becker <universe@uap-core.de>
parents:
207
diff
changeset
|
96 | |
206 | 97 | void ucx_map_clear(UcxMap *map) { |
207
1de85ecf6adc
optimized ucx_map_clear (noop for count == 0)
Mike Becker <universe@uap-core.de>
parents:
206
diff
changeset
|
98 | if (map->count == 0) { |
1de85ecf6adc
optimized ucx_map_clear (noop for count == 0)
Mike Becker <universe@uap-core.de>
parents:
206
diff
changeset
|
99 | return; // nothing to do |
1de85ecf6adc
optimized ucx_map_clear (noop for count == 0)
Mike Becker <universe@uap-core.de>
parents:
206
diff
changeset
|
100 | } |
206 | 101 | ucx_map_free_elmlist_contents(map); |
102 | memset(map->map, 0, map->size*sizeof(UcxMapElement*)); | |
103 | map->count = 0; | |
104 | } | |
105 | ||
374
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
106 | int ucx_map_copy(UcxMap const *from, UcxMap *to, copy_func fnc, void *data) { |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
107 | UcxMapIterator i = ucx_map_iterator(from); |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
108 | void *value; |
111
c8c59d7f4536
new map foreach macro
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
109 | UCX_MAP_FOREACH(key, value, i) { |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
110 | if (ucx_map_put(to, key, fnc ? fnc(value, data) : value)) { |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
111 | return 1; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
112 | } |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
113 | } |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
114 | return 0; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
115 | } |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
116 | |
374
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
117 | UcxMap *ucx_map_clone(UcxMap const *map, copy_func fnc, void *data) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
118 | return ucx_map_clone_a(ucx_default_allocator(), map, fnc, data); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
119 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
120 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
121 | UcxMap *ucx_map_clone_a(UcxAllocator *allocator, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
122 | UcxMap const *map, copy_func fnc, void *data) { |
51
1c78cd19fb6b
added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
123 | size_t bs = (map->count * 5) >> 1; |
374
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
124 | UcxMap *newmap = ucx_map_new_a(allocator, bs > map->size ? bs : map->size); |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
125 | if (!newmap) { |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
126 | return NULL; |
44 | 127 | } |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
128 | ucx_map_copy(map, newmap, fnc, data); |
44 | 129 | return newmap; |
130 | } | |
131 | ||
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
132 | int ucx_map_rehash(UcxMap *map) { |
51
1c78cd19fb6b
added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
133 | size_t load = (map->size * 3) >> 2; |
1c78cd19fb6b
added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
134 | if (map->count > load) { |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
135 | UcxMap oldmap; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
136 | oldmap.map = map->map; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
137 | oldmap.size = map->size; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
138 | oldmap.count = map->count; |
107
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
139 | oldmap.allocator = map->allocator; |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
140 | |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
141 | map->size = (map->count * 5) >> 1; |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
142 | map->map = (UcxMapElement**)alcalloc( |
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
143 | map->allocator, map->size, sizeof(UcxMapElement*)); |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
144 | if (!map->map) { |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
145 | *map = oldmap; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
146 | return 1; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
147 | } |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
148 | map->count = 0; |
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
149 | ucx_map_copy(&oldmap, map, NULL, NULL); |
70
6721482eaf8e
fixed memory leak in ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
150 | |
6721482eaf8e
fixed memory leak in ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
151 | /* free the UcxMapElement list of oldmap */ |
206 | 152 | ucx_map_free_elmlist_contents(&oldmap); |
153 | alfree(map->allocator, oldmap.map); | |
51
1c78cd19fb6b
added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
154 | } |
52
34f50d0bada4
added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
155 | return 0; |
51
1c78cd19fb6b
added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
156 | } |
1c78cd19fb6b
added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
157 | |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
158 | int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
107
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
159 | UcxAllocator *allocator = map->allocator; |
86b19c98b5fd
map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
160 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
161 | if (key.hash == 0) { |
328 | 162 | key.hash = ucx_hash((const char*)key.data, key.len); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
163 | } |
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
164 | |
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
165 | struct UcxMapKey mapkey; |
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
166 | mapkey.hash = key.hash; |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
167 | |
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
168 | size_t slot = mapkey.hash%map->size; |
253
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
169 | UcxMapElement *elm = map->map[slot]; |
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
170 | UcxMapElement *prev = NULL; |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
171 | |
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
172 | while (elm && elm->key.hash < mapkey.hash) { |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
173 | prev = elm; |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
174 | elm = elm->next; |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
175 | } |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
176 | |
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
177 | if (!elm || elm->key.hash != mapkey.hash) { |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
178 | UcxMapElement *e = (UcxMapElement*)almalloc( |
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
179 | allocator, sizeof(UcxMapElement)); |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
180 | if (!e) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
181 | return -1; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
182 | } |
30 | 183 | e->key.data = NULL; |
53 | 184 | if (prev) { |
185 | prev->next = e; | |
186 | } else { | |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
187 | map->map[slot] = e; |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
188 | } |
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
189 | e->next = elm; |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
190 | elm = e; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
191 | } |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
192 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
193 | if (!elm->key.data) { |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
194 | void *kd = almalloc(allocator, key.len); |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
195 | if (!kd) { |
30 | 196 | return -1; |
197 | } | |
198 | memcpy(kd, key.data, key.len); | |
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
199 | mapkey.data = kd; |
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
200 | mapkey.len = key.len; |
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
201 | elm->key = mapkey; |
45
dd03226c1a6b
map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
202 | map->count++; |
30 | 203 | } |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
204 | elm->data = data; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
205 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
206 | return 0; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
207 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
208 | |
253
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
209 | static void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, int remove) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
210 | if(key.hash == 0) { |
328 | 211 | key.hash = ucx_hash((const char*)key.data, key.len); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
212 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
213 | |
53 | 214 | size_t slot = key.hash%map->size; |
253
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
215 | UcxMapElement *elm = map->map[slot]; |
e19825a1430a
removes unnecessary macros from ucx.h + removes the usage of restrict and _Bool completely, instead of defining macros
Mike Becker <universe@uap-core.de>
parents:
251
diff
changeset
|
216 | UcxMapElement *pelm = NULL; |
53 | 217 | while (elm && elm->key.hash <= key.hash) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
218 | if(elm->key.hash == key.hash) { |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
219 | int n = (key.len > elm->key.len) ? elm->key.len : key.len; |
29
bce0d7f2912b
fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
220 | if (memcmp(elm->key.data, key.data, n) == 0) { |
53 | 221 | void *data = elm->data; |
222 | if (remove) { | |
223 | if (pelm) { | |
224 | pelm->next = elm->next; | |
225 | } else { | |
226 | map->map[slot] = elm->next; | |
227 | } | |
173
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
228 | alfree(map->allocator, elm->key.data); |
31a8682fffb7
fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
229 | alfree(map->allocator, elm); |
53 | 230 | map->count--; |
231 | } | |
232 | ||
233 | return data; | |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
234 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
235 | } |
53 | 236 | pelm = elm; |
237 | elm = pelm->next; | |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
238 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
239 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
240 | return NULL; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
241 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
242 | |
374
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
243 | void *ucx_map_get(UcxMap const *map, UcxKey key) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
244 | return ucx_map_get_and_remove((UcxMap *)map, key, 0); |
53 | 245 | } |
246 | ||
247 | void *ucx_map_remove(UcxMap *map, UcxKey key) { | |
248 | return ucx_map_get_and_remove(map, key, 1); | |
249 | } | |
250 | ||
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
251 | UcxKey ucx_key(const void *data, size_t len) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
252 | UcxKey key; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
253 | key.data = data; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
254 | key.len = len; |
328 | 255 | key.hash = ucx_hash((const char*)data, len); |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
256 | return key; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
257 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
258 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
259 | |
67
27e67e725d35
added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents:
53
diff
changeset
|
260 | int ucx_hash(const char *data, size_t len) { |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
261 | /* murmur hash 2 */ |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
262 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
263 | int m = 0x5bd1e995; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
264 | int r = 24; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
265 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
266 | int h = 25 ^ len; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
267 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
268 | int i = 0; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
269 | while (len >= 4) { |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
270 | int k = data[i + 0] & 0xFF; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
271 | k |= (data[i + 1] & 0xFF) << 8; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
272 | k |= (data[i + 2] & 0xFF) << 16; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
273 | k |= (data[i + 3] & 0xFF) << 24; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
274 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
275 | k *= m; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
276 | k ^= k >> r; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
277 | k *= m; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
278 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
279 | h *= m; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
280 | h ^= k; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
281 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
282 | i += 4; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
283 | len -= 4; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
284 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
285 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
286 | switch (len) { |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
287 | case 3: h ^= (data[i + 2] & 0xFF) << 16; |
38
35f67a8ef875
added missing stddef includes
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
288 | /* no break */ |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
289 | case 2: h ^= (data[i + 1] & 0xFF) << 8; |
38
35f67a8ef875
added missing stddef includes
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
290 | /* no break */ |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
291 | case 1: h ^= (data[i + 0] & 0xFF); h *= m; |
38
35f67a8ef875
added missing stddef includes
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
292 | /* no break */ |
20
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
293 | } |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
294 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
295 | h ^= h >> 13; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
296 | h *= m; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
297 | h ^= h >> 15; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
298 | |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
299 | return h; |
db7d9860dbbd
added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
300 | } |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
301 | |
374
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
302 | UcxMapIterator ucx_map_iterator(UcxMap const *map) { |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
303 | UcxMapIterator i; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
304 | i.map = map; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
305 | i.cur = NULL; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
306 | i.index = 0; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
307 | return i; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
308 | } |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
309 | |
111
c8c59d7f4536
new map foreach macro
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
108
diff
changeset
|
310 | int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) { |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
311 | UcxMapElement *e = i->cur; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
312 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
313 | if (e) { |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
314 | e = e->next; |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
315 | } else { |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
316 | e = i->map->map[0]; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
317 | } |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
318 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
319 | while (i->index < i->map->size) { |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
320 | if (e) { |
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
321 | if (e->data) { |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
322 | i->cur = e; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
323 | *elm = e->data; |
327
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
324 | key->data = e->key.data; |
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
325 | key->hash = e->key.hash; |
fbc33813265b
UcxMap now separates internal non-const keys from public const keys
Mike Becker <universe@uap-core.de>
parents:
287
diff
changeset
|
326 | key->len = e->key.len; |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
327 | return 1; |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
328 | } |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
329 | |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
330 | e = e->next; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
331 | } else { |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
332 | i->index++; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
333 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
334 | if (i->index < i->map->size) { |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
335 | e = i->map->map[i->index]; |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
336 | } |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
337 | } |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
338 | } |
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
339 | |
138
7800811078b8
documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents:
136
diff
changeset
|
340 | return 0; |
31
91ac86557290
added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
341 | } |
42
ff3dd1ee7dee
(broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents:
38
diff
changeset
|
342 | |
374
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
343 | UcxMap* ucx_map_union(const UcxMap *first, const UcxMap *second, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
344 | copy_func cpfnc, void* cpdata) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
345 | return ucx_map_union_a(ucx_default_allocator(), |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
346 | first, second, cpfnc, cpdata); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
347 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
348 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
349 | UcxMap* ucx_map_union_a(UcxAllocator *allocator, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
350 | const UcxMap *first, const UcxMap *second, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
351 | copy_func cpfnc, void* cpdata) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
352 | UcxMap* result = ucx_map_clone_a(allocator, first, cpfnc, cpdata); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
353 | ucx_map_copy(second, result, cpfnc, cpdata); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
354 | return result; |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
355 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
356 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
357 | UcxMap* ucx_map_intersection(const UcxMap *first, const UcxMap *second, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
358 | copy_func cpfnc, void* cpdata) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
359 | return ucx_map_intersection_a(ucx_default_allocator(), |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
360 | first, second, cpfnc, cpdata); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
361 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
362 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
363 | UcxMap* ucx_map_intersection_a(UcxAllocator *allocator, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
364 | const UcxMap *first, const UcxMap *second, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
365 | copy_func cpfnc, void* cpdata) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
366 | UcxMap *result = ucx_map_new_a(allocator, first->size < second->size ? |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
367 | first->size : second->size); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
368 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
369 | UcxMapIterator iter = ucx_map_iterator(first); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
370 | void* value; |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
371 | UCX_MAP_FOREACH(key, value, iter) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
372 | if (ucx_map_get(second, key)) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
373 | ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
374 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
375 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
376 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
377 | return result; |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
378 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
379 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
380 | UcxMap* ucx_map_difference(const UcxMap *first, const UcxMap *second, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
381 | copy_func cpfnc, void* cpdata) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
382 | return ucx_map_difference_a(ucx_default_allocator(), |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
383 | first, second, cpfnc, cpdata); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
384 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
385 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
386 | UcxMap* ucx_map_difference_a(UcxAllocator *allocator, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
387 | const UcxMap *first, const UcxMap *second, |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
388 | copy_func cpfnc, void* cpdata) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
389 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
390 | UcxMap *result = ucx_map_new_a(allocator, first->size - second->count); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
391 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
392 | UcxMapIterator iter = ucx_map_iterator(first); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
393 | void* value; |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
394 | UCX_MAP_FOREACH(key, value, iter) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
395 | if (!ucx_map_get(second, key)) { |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
396 | ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
397 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
398 | } |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
399 | |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
400 | ucx_map_rehash(result); |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
401 | return result; |
be77fb2da242
adds set operations for UcxMap
Mike Becker <universe@uap-core.de>
parents:
328
diff
changeset
|
402 | } |