src/server/ucx/map.c

Mon, 23 Jul 2012 15:13:19 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 23 Jul 2012 15:13:19 +0200
changeset 32
ebba53de8b18
parent 31
280250e45ba6
child 36
450d2d5f4735
permissions
-rw-r--r--

added solaris 10 support

15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 *
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 */
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #include <stdlib.h>
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 #include <string.h>
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 #include "map.h"
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 UcxMap *ucx_map_new(size_t size) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 if(map == NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
16 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 if(map->map == NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 free(map);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 map->size = size;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 return map;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
26 void ucx_map_free(UcxMap *map) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
27 for (size_t n = 0 ; n < map->size ; n++) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
28 UcxMapElement *elem = map->map[n];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
29 if (elem != NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
30 do {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
31 UcxMapElement *next = elem->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
32 free(elem->key.data);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
33 free(elem);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
34 elem = next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
35 } while (elem != NULL);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
36 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
37 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
38 free(map->map);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
39 free(map);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
40 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
41
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 if(key.hash == 0) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 key.hash = ucx_hash((char*)key.data, key.len);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
47 size_t slot = key.hash%map->size;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
48 UcxMapElement *elm = map->map[slot];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
49 UcxMapElement *prev = NULL;
21
627b09ee74e4 New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
50
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
51 while (elm != NULL && elm->key.hash < key.hash) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
52 prev = elm;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
53 elm = elm->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
54 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
55
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
56 if (elm == NULL || elm->key.hash != key.hash) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
57 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
58 if(e == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
59 return -1;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
61 e->key.data = NULL;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
62 if (prev == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
63 map->map[slot] = e;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
64 } else {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
65 prev->next = e;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
66 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
67 e->next = elm;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
68 elm = e;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
70
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
71 if(elm->key.data == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
72 void *kd = malloc(key.len);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
73 if (kd == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
74 return -1;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
75 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
76 memcpy(kd, key.data, key.len);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
77 key.data = kd;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
78 elm->key = key;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
79 }
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 elm->data = data;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 return 0;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 void* ucx_map_get(UcxMap *map, UcxKey key) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if(key.hash == 0) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 key.hash = ucx_hash((char*)key.data, key.len);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
89
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
90 UcxMapElement *elm = map->map[key.hash%map->size];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
91 while (elm != NULL && elm->key.hash <= key.hash) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 if(elm->key.hash == key.hash) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
94 if (memcmp(elm->key.data, key.data, n) == 0) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 return elm->data;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 elm = elm->next;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 UcxKey ucx_key(void *data, size_t len) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 UcxKey key;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 key.data = data;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 key.len = len;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 key.hash = ucx_hash(data, len);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 return key;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 int ucx_hash(char *data, size_t len) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 /* murmur hash 2 */
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 int m = 0x5bd1e995;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 int r = 24;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 int h = 25 ^ len;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 int i = 0;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 while (len >= 4) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 int k = data[i + 0] & 0xFF;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 k |= (data[i + 1] & 0xFF) << 8;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 k |= (data[i + 2] & 0xFF) << 16;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 k |= (data[i + 3] & 0xFF) << 24;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 k *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 k ^= k >> r;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 k *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 h *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 h ^= k;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 i += 4;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 len -= 4;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 switch (len) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 case 3: h ^= (data[i + 2] & 0xFF) << 16;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 case 2: h ^= (data[i + 1] & 0xFF) << 8;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 h ^= h >> 13;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 h *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 h ^= h >> 15;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 return h;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
151
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
152 UcxMapIterator ucx_map_iterator(UcxMap *map) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
153 UcxMapIterator i;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
154 i.map = map;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
155 i.cur = NULL;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
156 i.index = 0;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
157 return i;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
158 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
159
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
160 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
161 UcxMapElement *e = i->cur;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
162
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
163 if(e == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
164 e = i->map->map[0];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
165 } else {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
166 e = e->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
167 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
168
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
169 while(i->index < i->map->size) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
170 if(e != NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
171 if(e->data != NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
172 i->cur = e;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
173 *elm = e->data;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
174 return 0;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
175 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
176
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
177 e = e->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
178 } else {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
179 i->index++;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
180
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
181 if(i->index < i->map->size) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
182 e = i->map->map[i->index];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
183 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
184 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
185 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
186
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
187 return 1;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
188 }

mercurial