src/map.c

Sat, 28 Oct 2017 15:59:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:59:16 +0200
changeset 260
a6184aff5108
parent 259
2f5dea574a75
child 277
f819fe5e20f5
permissions
-rw-r--r--

rename LICENSE to COPYING to be distributed by autoconf

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

mercurial