ucx/map.c

Fri, 12 Oct 2012 10:54:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 10:54:55 +0200
changeset 69
fb59270b1de3
parent 67
27e67e725d35
child 70
6721482eaf8e
permissions
-rw-r--r--

made the code work with VC++ compiler (use make CONF=windows)

20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
1 /*
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
2 *
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
3 */
2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
5 #include <stdlib.h>
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
6 #include <string.h>
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
7
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
8 #include "map.h"
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
9
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
10 UcxMap *ucx_map_new(size_t size) {
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
11 if(size == 0) {
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
12 size = 16;
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
13 }
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
14
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
15 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
16 if(map == NULL) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
17 return NULL;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
18 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
19
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
20 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
21 if(map->map == NULL) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
22 free(map);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
23 return NULL;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
24 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
25 map->size = size;
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
26 map->count = 0;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
27
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
28 return map;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
29 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
30
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
31 void ucx_map_free(UcxMap *map) {
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
32 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
33 UcxMapElement *elem = map->map[n];
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
34 if (elem != NULL) {
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
35 do {
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
36 UcxMapElement *next = elem->next;
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
37 free(elem->key.data);
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
38 free(elem);
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
39 elem = next;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
40 } while (elem != NULL);
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
41 }
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
42 }
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
43 free(map->map);
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
44 free(map);
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
45 }
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
46
67
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
47 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
48 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
49 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
50 void *value;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
51 UCX_MAP_FOREACH(value, i) {
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
52 int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
53 if(ret != 0) {
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
54 return 1;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
55 }
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
56 }
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
57 return 0;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
58 }
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
59
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
60 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
61 size_t bs = (map->count * 5) >> 1;
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
62 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
63 if(newmap == NULL) {
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
64 return NULL;
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
65 }
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
66 ucx_map_copy(map, newmap, fnc, data);
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
67 return newmap;
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
68 }
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
69
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
70 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
71 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
72 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
73 UcxMap oldmap;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
74 oldmap.map = map->map;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
75 oldmap.size = map->size;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
76 oldmap.count = map->count;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
77
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
78 map->size = (map->count * 5) >> 1;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
79 map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*));
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
80 if(map->map == NULL) {
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
81 *map = oldmap;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
82 return 1;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
83 }
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
84 map->count = 0;
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
85 ucx_map_copy(&oldmap, map, NULL, NULL);
67
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
86 /* TODO: free the UcxMapElement list of oldmap */
51
1c78cd19fb6b added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
87 }
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
88 return 0;
51
1c78cd19fb6b added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
89 }
1c78cd19fb6b added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
90
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
91 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
92 if(key.hash == 0) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
93 key.hash = ucx_hash((char*)key.data, key.len);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
94 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
95
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
96 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
97 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
98 UcxMapElement *restrict prev = NULL;
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
99
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
100 while (elm != NULL && elm->key.hash < key.hash) {
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
101 prev = elm;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
102 elm = elm->next;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
103 }
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
104
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
105 if (elm == NULL || elm->key.hash != key.hash) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
106 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
107 if(e == NULL) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
108 return -1;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
109 }
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
110 e->key.data = NULL;
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
111 if (prev) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
112 prev->next = e;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
113 } else {
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
114 map->map[slot] = e;
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
115 }
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
116 e->next = elm;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
117 elm = e;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
118 }
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
119
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
120 if(elm->key.data == NULL) {
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
121 void *kd = malloc(key.len);
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
122 if (kd == NULL) {
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
123 return -1;
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
124 }
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
125 memcpy(kd, key.data, key.len);
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
126 key.data = kd;
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
127 elm->key = key;
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
128 map->count++;
30
23bb65cbf7a4 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
129 }
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
130 elm->data = data;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
131
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
132 return 0;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
133 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
134
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
135 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
136 if(key.hash == 0) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
137 key.hash = ucx_hash((char*)key.data, key.len);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
138 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
139
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
140 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
141 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
142 UcxMapElement *restrict pelm = NULL;
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
143 while (elm && elm->key.hash <= key.hash) {
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
144 if(elm->key.hash == key.hash) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
145 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
146 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
147 void *data = elm->data;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
148 if (remove) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
149 if (pelm) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
150 pelm->next = elm->next;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
151 } else {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
152 map->map[slot] = elm->next;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
153 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
154 free(elm);
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
155 map->count--;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
156 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
157
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
158 return data;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
159 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
160 }
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
161 pelm = elm;
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
162 elm = pelm->next;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
163 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
164
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
165 return NULL;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
166 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
167
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
168 void *ucx_map_get(UcxMap *map, UcxKey key) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
169 return ucx_map_get_and_remove(map, key, 0);
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
170 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
171
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
172 void *ucx_map_remove(UcxMap *map, UcxKey key) {
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
173 return ucx_map_get_and_remove(map, key, 1);
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
174 }
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
175
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
176 UcxKey ucx_key(void *data, size_t len) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
177 UcxKey key;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
178 key.data = data;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
179 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
180 key.hash = ucx_hash((const char*) data, len);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
181 return key;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
182 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
183
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
184
67
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
185 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
186 /* murmur hash 2 */
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
187
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
188 int m = 0x5bd1e995;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
189 int r = 24;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
190
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
191 int h = 25 ^ len;
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 int i = 0;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
194 while (len >= 4) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
195 int k = data[i + 0] & 0xFF;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
196 k |= (data[i + 1] & 0xFF) << 8;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
197 k |= (data[i + 2] & 0xFF) << 16;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
198 k |= (data[i + 3] & 0xFF) << 24;
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 k *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
201 k ^= k >> r;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
202 k *= m;
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 h *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
205 h ^= k;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
206
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
207 i += 4;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
208 len -= 4;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
209 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
210
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
211 switch (len) {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
212 case 3: h ^= (data[i + 2] & 0xFF) << 16;
38
35f67a8ef875 added missing stddef includes
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
213 /* no break */
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
214 case 2: h ^= (data[i + 1] & 0xFF) << 8;
38
35f67a8ef875 added missing stddef includes
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
215 /* no break */
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
216 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
217 /* no break */
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
218 }
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
219
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
220 h ^= h >> 13;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
221 h *= m;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
222 h ^= h >> 15;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
223
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
224 return h;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
225 }
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
226
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
227 UcxMapIterator ucx_map_iterator(UcxMap *map) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
228 UcxMapIterator i;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
229 i.map = map;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
230 i.cur = NULL;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
231 i.index = 0;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
232 return i;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
233 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
234
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
235 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
236 UcxMapElement *e = i->cur;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
237
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
238 if(e == NULL) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
239 e = i->map->map[0];
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
240 } else {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
241 e = e->next;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
242 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
243
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
244 while(i->index < i->map->size) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
245 if(e != NULL) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
246 if(e->data != NULL) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
247 i->cur = e;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
248 *elm = e->data;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
249 return 0;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
250 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
251
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
252 e = e->next;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
253 } else {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
254 i->index++;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
255
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
256 if(i->index < i->map->size) {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
257 e = i->map->map[i->index];
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
258 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
259 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
260 }
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
261
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
262 return 1;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
263 }
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
264
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
265 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
266 ucx_map_coder decoder, void* decdata) {
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
267
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
268 int c; int r, n;
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
269
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
270 char *key, *value;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
271
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
272 while ((c = fgetc(f)) > 0) {
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
273 /* Discard leading spaces and comments */
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
274 if (c < 33) continue;
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
275 if (c == '#' || c == '!') {
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
276 while ((c = (char) fgetc(f)) > 0) {
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
277 if (c == '\n') break;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
278 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
279 continue;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
280 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
281
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
282 /* read into key buffer */
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
283 n = 16;
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
284 key = (char*) malloc(n);
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
285 r = 0;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
286 do {
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
287 if (c == '=') break;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
288 if (r > n - 2) {
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
289 n *= 2;
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
290 key = (char*) realloc(key, n);
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
291 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
292 key[r] = c;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
293 r++;
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
294 } while ((c = fgetc(f)) > 0);
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
295 if (c <= 0) {
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
296 free(key);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
297 return 1;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
298 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
299 key[r] = 0;
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
300 while (key[--r] == ' ') key[r] = 0;
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
301
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
302 /* skip whitespaces */
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
303 while ((c = fgetc(f)) > 0) {
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
304 if (c > 32) break;
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
305 }
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
306 if (c <= 0) {
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
307 free(key);
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
308 return 1;
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
309 }
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
310
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
311 /* read into value buffer */
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
312 n = 64;
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
313 value = (char*) malloc(n);
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
314 r = 0;
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
315 do {
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
316 if (c == '\n') break;
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
317 if (r > n - 2) {
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
318 n *= 2;
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
319 value = (char*) realloc(value, n);
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
320 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
321 value[r] = c;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
322 r++;
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
323 } while ((c = fgetc(f)) > 0);
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
324 value[r] = 0;
43
02f38adea013 fixed crash fails by completing the implementation of the tested function....
Mike Becker <universe@uap-core.de>
parents: 42
diff changeset
325 while (value[--r] < 33) value[r] = 0;
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
326
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
327 if (decoder) {
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
328 size_t decodedSize;
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
329 void *decoded = decoder(value, decdata, &decodedSize);
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
330 free(value);
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
331 value = (char*) decoded;
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
332 r = decodedSize;
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
333 } else {
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
334 r += 2;
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
335 value = (char*) realloc(value, r);
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
336 }
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
337
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
338 if (allocator.pool) {
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
339 void *pooledValue = allocator.malloc(allocator.pool, r);
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
340 memcpy(pooledValue, value, r);
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
341 free(value);
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
342 value = (char*) pooledValue;
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
343 }
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
344
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
345 ucx_map_cstr_put(map, key, value);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
346 free(key);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
347 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
348
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
349 return 0;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
350 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
351
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
352 int ucx_map_store_enc(UcxMap *map, FILE *f,
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
353 ucx_map_coder encoder, void *encdata) {
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
354 UcxMapIterator iter = ucx_map_iterator(map);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
355 char *k, *v;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
356 sstr_t key, value;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
357 int written;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
358
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
359 UCX_MAP_FOREACH(v, iter) {
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
360 k = (char*) iter.cur->key.data;
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
361 key = sstr(k);
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
362 if (encoder) {
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
363 size_t encodedSize;
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
364 void *encoded = encoder(v, encdata, &encodedSize);
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
365 value = sstrn((char*) encoded,encodedSize - 1);
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
366 } else {
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
367 value = sstr(v);
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
368 }
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
369
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
370 written = 0;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
371 written += fwrite(key.ptr, 1, key.length, f);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
372 written += fwrite(" = ", 1, 3, f);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
373 written += fwrite(value.ptr, 1, value.length, f);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
374 written += fwrite("\n", 1, 1, f);
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
375
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
376 if (encoder) {
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
377 free(value.ptr);
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
378 }
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
379
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
380 if (written != key.length + value.length + 4) return 1;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
381 }
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
382
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
383 return 0;
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
384 }

mercurial