ucx/map.c

Thu, 15 Oct 2015 14:59:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2015 14:59:25 +0200
changeset 209
4f02199d8aae
parent 208
262c7be94eba
child 225
a1a068c2c4ef
permissions
-rw-r--r--

moved ucx_destructor type to ucx.h and added destructor argument to ucx_map_free_content

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

mercurial