src/map.c

Mon, 14 May 2018 18:12:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:12:14 +0200
changeset 308
d6f580621512
parent 287
98da78a1e69a
child 327
fbc33813265b
permissions
-rw-r--r--

renames ucx_strcmp() to ucx_cmp_str()

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
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 253
diff changeset
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
dddb9348ea42 8-) f**k
Mike Becker <universe@uap-core.de>
parents: 138 137
diff changeset
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
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
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
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
80 ucx_map_free_elmlist_contents(map);
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
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
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
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
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
101 ucx_map_free_elmlist_contents(map);
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
102 memset(map->map, 0, map->size*sizeof(UcxMapElement*));
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
103 map->count = 0;
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
104 }
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
105
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
106 int ucx_map_copy(UcxMap *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
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
117 UcxMap *ucx_map_clone(UcxMap *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
118 size_t bs = (map->count * 5) >> 1;
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
119 UcxMap *newmap = ucx_map_new(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
120 if (!newmap) {
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
121 return NULL;
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
122 }
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
123 ucx_map_copy(map, newmap, fnc, data);
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
124 return newmap;
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
125 }
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
126
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
127 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
128 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
129 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
130 UcxMap oldmap;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
131 oldmap.map = map->map;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
132 oldmap.size = map->size;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
133 oldmap.count = map->count;
107
86b19c98b5fd map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
134 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
135
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
136 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
137 map->map = (UcxMapElement**)alcalloc(
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 147
diff changeset
138 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
139 if (!map->map) {
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
140 *map = oldmap;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
141 return 1;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
142 }
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
143 map->count = 0;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
144 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
145
6721482eaf8e fixed memory leak in ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
146 /* free the UcxMapElement list of oldmap */
206
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
147 ucx_map_free_elmlist_contents(&oldmap);
58b77eb51afd added ucx_map_clean()
Mike Becker <universe@uap-core.de>
parents: 192
diff changeset
148 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
149 }
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
150 return 0;
51
1c78cd19fb6b added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
151 }
1c78cd19fb6b added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
152
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
153 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
154 UcxAllocator *allocator = map->allocator;
86b19c98b5fd map uses an allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
155
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
156 if (key.hash == 0) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
157 key.hash = ucx_hash((char*)key.data, key.len);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
158 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
159
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
160 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
161 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
162 UcxMapElement *prev = NULL;
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
163
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
164 while (elm && elm->key.hash < key.hash) {
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
165 prev = elm;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
166 elm = elm->next;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
167 }
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
168
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
169 if (!elm || elm->key.hash != key.hash) {
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 147
diff changeset
170 UcxMapElement *e = (UcxMapElement*)almalloc(
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 147
diff changeset
171 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
172 if (!e) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
173 return -1;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
174 }
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
175 e->key.data = NULL;
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
176 if (prev) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
177 prev->next = e;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
178 } else {
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
179 map->map[slot] = e;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
180 }
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
181 e->next = elm;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
182 elm = e;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
183 }
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
184
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
185 if (!elm->key.data) {
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 147
diff changeset
186 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
187 if (!kd) {
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
188 return -1;
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
189 }
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
190 memcpy(kd, key.data, key.len);
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
191 key.data = kd;
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
192 elm->key = key;
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
193 map->count++;
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
194 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
195 elm->data = data;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
196
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
197 return 0;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
198 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
199
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
200 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
201 if(key.hash == 0) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
202 key.hash = ucx_hash((char*)key.data, key.len);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
203 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
204
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
205 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
206 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
207 UcxMapElement *pelm = NULL;
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
208 while (elm && elm->key.hash <= key.hash) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
209 if(elm->key.hash == key.hash) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
210 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
211 if (memcmp(elm->key.data, key.data, n) == 0) {
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
212 void *data = elm->data;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
213 if (remove) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
214 if (pelm) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
215 pelm->next = elm->next;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
216 } else {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
217 map->map[slot] = elm->next;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
218 }
173
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 147
diff changeset
219 alfree(map->allocator, elm->key.data);
31a8682fffb7 fixed some sstring issues + added allocator macros
Mike Becker <universe@uap-core.de>
parents: 147
diff changeset
220 alfree(map->allocator, elm);
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
221 map->count--;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
222 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
223
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
224 return data;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
225 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
226 }
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
227 pelm = elm;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
228 elm = pelm->next;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
229 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
230
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
231 return NULL;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
232 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
233
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
234 void *ucx_map_get(UcxMap *map, UcxKey key) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
235 return ucx_map_get_and_remove(map, key, 0);
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
236 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
237
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
238 void *ucx_map_remove(UcxMap *map, UcxKey key) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
239 return ucx_map_get_and_remove(map, key, 1);
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
240 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
241
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
242 UcxKey ucx_key(void *data, size_t len) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
243 UcxKey key;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
244 key.data = data;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
245 key.len = len;
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
246 key.hash = ucx_hash((const char*) data, len);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
247 return key;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
248 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
249
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
250
67
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
251 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
252 /* murmur hash 2 */
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
253
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
254 int m = 0x5bd1e995;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
255 int r = 24;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
256
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
257 int h = 25 ^ len;
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 int i = 0;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
260 while (len >= 4) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
261 int k = data[i + 0] & 0xFF;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
262 k |= (data[i + 1] & 0xFF) << 8;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
263 k |= (data[i + 2] & 0xFF) << 16;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
264 k |= (data[i + 3] & 0xFF) << 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 k *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
267 k ^= k >> r;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
268 k *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
269
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
270 h *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
271 h ^= k;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
272
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
273 i += 4;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
274 len -= 4;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
275 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
276
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
277 switch (len) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
278 case 3: h ^= (data[i + 2] & 0xFF) << 16;
38
35f67a8ef875 added missing stddef includes
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
279 /* no break */
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
280 case 2: h ^= (data[i + 1] & 0xFF) << 8;
38
35f67a8ef875 added missing stddef includes
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
281 /* no break */
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
282 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
283 /* no break */
20
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 h ^= h >> 13;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
287 h *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
288 h ^= h >> 15;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
289
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
290 return h;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
291 }
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
292
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
293 UcxMapIterator ucx_map_iterator(UcxMap *map) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
294 UcxMapIterator i;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
295 i.map = map;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
296 i.cur = NULL;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
297 i.index = 0;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
298 return i;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
299 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
300
111
c8c59d7f4536 new map foreach macro
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
301 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
302 UcxMapElement *e = i->cur;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
303
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
304 if (e) {
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
305 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
306 } else {
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
307 e = i->map->map[0];
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
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
310 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
311 if (e) {
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
312 if (e->data) {
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
313 i->cur = e;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
314 *elm = e->data;
111
c8c59d7f4536 new map foreach macro
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
315 *key = e->key;
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
316 return 1;
31
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
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
319 e = e->next;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
320 } else {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
321 i->index++;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
322
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
323 if (i->index < i->map->size) {
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
324 e = i->map->map[i->index];
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
325 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
326 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
327 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
328
138
7800811078b8 documented map.h + changed return value of ucx_map_iter_next()
Mike Becker <universe@uap-core.de>
parents: 136
diff changeset
329 return 0;
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
330 }
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
331

mercurial