blob: 8a85f07ca586c9f62865c5015901f71a8ecc5be4 [file] [log] [blame]
Petr Machataa11cbed2012-05-05 00:07:38 +02001/*
2 * This file is part of ltrace.
Petr Machatad7e4ca82012-11-28 03:38:47 +01003 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
Petr Machataa11cbed2012-05-05 00:07:38 +02004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
Juan Cespedes7186e2a2003-01-31 19:56:34 +010021#include <string.h>
Petr Machatad7e4ca82012-11-28 03:38:47 +010022#include <stdlib.h>
23#include <stdio.h>
24#include "dict.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010025
Petr Machatad7e4ca82012-11-28 03:38:47 +010026struct status_bits {
27 unsigned char taken : 1;
28 unsigned char erased : 1;
Juan Cespedescac15c32003-01-31 18:58:58 +010029};
30
Petr Machatad7e4ca82012-11-28 03:38:47 +010031static struct status_bits *
32bitp(struct dict *dict, size_t n)
Petr Machata345c9b52012-02-10 13:10:03 +010033{
Petr Machatad7e4ca82012-11-28 03:38:47 +010034 return VECT_ELEMENT(&dict->status, struct status_bits, n);
Juan Cespedescac15c32003-01-31 18:58:58 +010035}
36
Juan Cespedesf1350522008-12-16 18:19:58 +010037void
Petr Machatad7e4ca82012-11-28 03:38:47 +010038dict_init(struct dict *dict,
39 size_t key_size, size_t value_size,
40 size_t (*hash1)(const void *),
41 int (*eq)(const void *, const void *),
42 size_t (*hash2)(size_t))
43{
44 assert(hash1 != NULL);
45 assert(eq != NULL);
Juan Cespedescac15c32003-01-31 18:58:58 +010046
Petr Machatad7e4ca82012-11-28 03:38:47 +010047 vect_init(&dict->keys, key_size);
48 vect_init(&dict->values, value_size);
49 VECT_INIT(&dict->status, struct status_bits);
50 dict->size = 0;
51
52 dict->hash1 = hash1;
53 dict->hash2 = hash2;
54 dict->eq = eq;
55}
56
57struct clone_data {
58 struct dict *target;
59 int (*clone_key)(void *tgt, const void *src, void *data);
60 int (*clone_value)(void *tgt, const void *src, void *data);
61 void (*dtor_key)(void *tgt, void *data);
62 void (*dtor_value)(void *tgt, void *data);
63 void *data;
64};
65
66enum callback_status
67clone_cb(void *key, void *value, void *data)
68{
69 struct clone_data *clone_data = data;
70
71 char nkey[clone_data->target->keys.elt_size];
72 if (clone_data->clone_key == NULL)
73 memmove(nkey, key, sizeof(nkey));
74 else if (clone_data->clone_key(&nkey, key, clone_data->data) < 0)
75 return CBS_STOP;
76
77 char nvalue[clone_data->target->values.elt_size];
78 if (clone_data->clone_value == NULL) {
79 memmove(nvalue, value, sizeof(nvalue));
80 } else if (clone_data->clone_value(&nvalue, value,
81 clone_data->data) < 0) {
82 fail:
83 if (clone_data->clone_key != NULL)
84 clone_data->dtor_key(&nkey, clone_data->data);
85 return CBS_STOP;
Juan Cespedescac15c32003-01-31 18:58:58 +010086 }
Petr Machatad7e4ca82012-11-28 03:38:47 +010087
88 if (dict_insert(clone_data->target, nkey, nvalue) < 0) {
89 if (clone_data->clone_value != NULL)
90 clone_data->dtor_value(&nvalue, clone_data->data);
91 goto fail;
92 }
93
94 return CBS_CONT;
Juan Cespedescac15c32003-01-31 18:58:58 +010095}
96
Juan Cespedesf1350522008-12-16 18:19:58 +010097int
Petr Machatad7e4ca82012-11-28 03:38:47 +010098dict_clone(struct dict *target, const struct dict *source,
99 int (*clone_key)(void *tgt, const void *src, void *data),
100 void (*dtor_key)(void *tgt, void *data),
101 int (*clone_value)(void *tgt, const void *src, void *data),
102 void (*dtor_value)(void *tgt, void *data),
103 void *data)
104{
105 assert((clone_key != NULL) == (dtor_key != NULL));
106 assert((clone_value != NULL) == (dtor_value != NULL));
Juan Cespedescd8976d2009-05-14 13:47:58 +0200107
Petr Machatad7e4ca82012-11-28 03:38:47 +0100108 dict_init(target, source->keys.elt_size, source->values.elt_size,
109 source->hash1, source->eq, source->hash2);
110 struct clone_data clone_data = {
111 target, clone_key, clone_value, dtor_key, dtor_value, data
112 };
113 if (dict_each((struct dict *)source, NULL,
114 clone_cb, &clone_data) != NULL) {
115 dict_destroy(target, dtor_key, dtor_value, data);
116 return -1;
117 }
118 return 0;
119}
Juan Cespedescd8976d2009-05-14 13:47:58 +0200120
Petr Machatad7e4ca82012-11-28 03:38:47 +0100121size_t
122dict_size(const struct dict *dict)
123{
124 return dict->size;
125}
Juan Cespedescac15c32003-01-31 18:58:58 +0100126
Petr Machatad7e4ca82012-11-28 03:38:47 +0100127int
128dict_empty(const struct dict *dict)
129{
130 return dict->size == 0;
131}
132
133struct destroy_data {
134 void (*dtor_key)(void *tgt, void *data);
135 void (*dtor_value)(void *tgt, void *data);
136 void *data;
137};
138
139enum callback_status
140destroy_cb(void *key, void *value, void *data)
141{
142 struct destroy_data *destroy_data = data;
143 if (destroy_data->dtor_key)
144 destroy_data->dtor_key(key, destroy_data->data);
145 if (destroy_data->dtor_value)
146 destroy_data->dtor_value(value, destroy_data->data);
147 return CBS_CONT;
148}
149
150void
151dict_destroy(struct dict *dict,
152 void (*dtor_key)(void *tgt, void *data),
153 void (*dtor_value)(void *tgt, void *data),
154 void *data)
155{
156 /* Some keys and values are not initialized, so we can't call
157 * dtors for them. Iterate DICT instead. */
158 if (dtor_key != NULL || dtor_value != NULL) {
159 struct destroy_data destroy_data = {
160 dtor_key, dtor_value, data
161 };
162 dict_each(dict, NULL, destroy_cb, &destroy_data);
Juan Cespedescac15c32003-01-31 18:58:58 +0100163 }
164
Petr Machatad7e4ca82012-11-28 03:38:47 +0100165 vect_destroy(&dict->keys, NULL, NULL);
166 vect_destroy(&dict->values, NULL, NULL);
167 vect_destroy(&dict->status, NULL, NULL);
168}
Juan Cespedescac15c32003-01-31 18:58:58 +0100169
Petr Machatad7e4ca82012-11-28 03:38:47 +0100170static size_t
171default_secondary_hash(size_t pos)
172{
173 return pos % 97 + 1;
174}
Juan Cespedescac15c32003-01-31 18:58:58 +0100175
Petr Machata2718e3f2012-11-28 20:48:11 +0100176static size_t
177small_secondary_hash(size_t pos)
178{
179 return 1;
180}
181
Petr Machatad7e4ca82012-11-28 03:38:47 +0100182static inline size_t
183n(struct dict *dict)
184{
185 return vect_size(&dict->keys);
186}
187
188static inline size_t (*
189hash2(struct dict *dict))(size_t)
190{
191 if (dict->hash2 != NULL)
192 return dict->hash2;
Petr Machata2718e3f2012-11-28 20:48:11 +0100193 else if (n(dict) < 100)
194 return small_secondary_hash;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 else
Petr Machatad7e4ca82012-11-28 03:38:47 +0100196 return default_secondary_hash;
197}
Juan Cespedescac15c32003-01-31 18:58:58 +0100198
Petr Machatad7e4ca82012-11-28 03:38:47 +0100199static void *
200getkey(struct dict *dict, size_t pos)
201{
202 return ((unsigned char *)dict->keys.data)
203 + dict->keys.elt_size * pos;
204}
205
206static void *
207getvalue(struct dict *dict, size_t pos)
208{
209 return ((unsigned char *)dict->values.data)
210 + dict->values.elt_size * pos;
211}
212
213static size_t
214find_slot(struct dict *dict, const void *key,
215 int *foundp, int *should_rehash, size_t *pi)
216{
217 size_t pos = dict->hash1(key) % n(dict);
218 size_t pos0 = -1;
219 size_t d = hash2(dict)(pos);
220 size_t i = 0;
221 *foundp = 0;
222
223 /* We skip over any taken or erased slots. But we remember
224 * the first erased that we find, and if we don't find the key
225 * later, we return that position. */
226 for (; bitp(dict, pos)->taken || bitp(dict, pos)->erased;
227 pos = (pos + d) % n(dict)) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100228 if (pos0 == (size_t)-1 && bitp(dict, pos)->erased)
229 pos0 = pos;
230
Petr Machata2718e3f2012-11-28 20:48:11 +0100231 /* If there is a loop, but we've seen an erased
232 * element, take that one. Otherwise give up. */
233 if (++i > dict->size) {
234 if (pos0 != (size_t)-1)
235 break;
236 return (size_t)-1;
237 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100238
239 if (bitp(dict, pos)->taken
240 && dict->eq(getkey(dict, pos), key)) {
241 *foundp = 1;
242 break;
243 }
244 }
245
246 if (!*foundp && pos0 != (size_t)-1)
247 pos = pos0;
248
249 /* If the hash table degraded into a linked list, request a
250 * rehash. */
251 if (should_rehash != NULL)
252 *should_rehash = i > 10 && i > n(dict) / 10;
253
254 if (pi != NULL)
255 *pi = i;
256 return pos;
257}
258
259enum callback_status
260rehash_move(void *key, void *value, void *data)
261{
262 if (dict_insert(data, key, value) < 0)
263 return CBS_STOP;
264 else
265 return CBS_CONT;
266}
267
268int
269rehash(struct dict *dict, size_t nn)
270{
Petr Machata2718e3f2012-11-28 20:48:11 +0100271 assert(nn != n(dict));
Petr Machatad7e4ca82012-11-28 03:38:47 +0100272 int ret = -1;
273
274 struct dict tmp;
275 dict_init(&tmp, dict->keys.elt_size, dict->values.elt_size,
276 dict->hash1, dict->eq, dict->hash2);
277
278 /* To honor all invariants (so that we can safely call
279 * dict_destroy), we first make a request to _reserve_ enough
280 * room in all vectors. This has no observable effect on
281 * contents of vectors. */
282 if (vect_reserve(&tmp.keys, nn) < 0
283 || vect_reserve(&tmp.values, nn) < 0
284 || vect_reserve(&tmp.status, nn) < 0)
285 goto done;
286
287 /* Now that we know that there is enough size in vectors, we
288 * simply bump the size. */
289 tmp.keys.size = nn;
290 tmp.values.size = nn;
291 size_t old_size = tmp.status.size;
292 tmp.status.size = nn;
293 memset(VECT_ELEMENT(&tmp.status, struct status_bits, old_size),
294 0, (tmp.status.size - old_size) * tmp.status.elt_size);
295
296 /* At this point, TMP is once more an empty dictionary with NN
297 * slots. Now move stuff from DICT to TMP. */
298 if (dict_each(dict, NULL, rehash_move, &tmp) != NULL)
299 goto done;
300
301 /* And now swap contents of DICT and TMP, and we are done. */
302 {
303 struct dict tmp2 = *dict;
304 *dict = tmp;
305 tmp = tmp2;
306 }
307
308 ret = 0;
309
310done:
311 /* We only want to release the containers, not the actual data
312 * that they hold, so it's fine if we don't pass any dtor. */
313 dict_destroy(&tmp, NULL, NULL, NULL);
314 return ret;
315
316}
317
318static const size_t primes[] = {
319 13, 31, 61, 127, 251, 509, 1021, 2039, 4093,
320 8191, 16381, 32749, 65521, 130981, 0
321};
322
323static size_t
324larger_size(size_t current)
325{
326 if (current == 0)
327 return primes[0];
328
329 if (current < primes[sizeof(primes)/sizeof(*primes) - 2]) {
330 size_t i;
331 for (i = 0; primes[i] != 0; ++i)
332 if (primes[i] > current)
333 return primes[i];
334 abort();
335 }
336
337 /* We ran out of primes, so invent a new one. The following
338 * gives primes until about 17M elements (and then some more
339 * later). */
340 return 2 * current + 6585;
341}
342
343static size_t
344smaller_size(size_t current)
345{
346 if (current <= primes[0])
347 return primes[0];
348
349 if (current <= primes[sizeof(primes)/sizeof(*primes) - 2]) {
350 size_t i;
351 size_t prev = 0;
352 for (i = 0; primes[i] != 0; ++i) {
353 if (primes[i] >= current)
354 return prev;
355 prev = primes[i];
356 }
357 abort();
358 }
359
360 return (current - 6585) / 2;
361}
362
363int
364dict_insert(struct dict *dict, void *key, void *value)
365{
366 if (n(dict) == 0 || dict->size > 0.7 * n(dict))
367 rehash:
368 if (rehash(dict, larger_size(n(dict))) < 0)
369 return -1;
370
371 int found;
372 int should_rehash;
373 size_t slot_n = find_slot(dict, key, &found, &should_rehash, NULL);
Petr Machata2718e3f2012-11-28 20:48:11 +0100374 if (slot_n == (size_t)-1)
375 return -1;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100376 if (found)
377 return 1;
Petr Machata2718e3f2012-11-28 20:48:11 +0100378 assert(!bitp(dict, slot_n)->taken);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100379
380 /* If rehash was requested, do that, and retry. But just live
381 * with it for apparently sparse tables. No resizing can fix
382 * a rubbish hash. */
383 if (should_rehash && dict->size > 0.3 * n(dict))
384 goto rehash;
385
386 memmove(getkey(dict, slot_n), key, dict->keys.elt_size);
387 memmove(getvalue(dict, slot_n), value, dict->values.elt_size);
388
389 bitp(dict, slot_n)->taken = 1;
390 bitp(dict, slot_n)->erased = 0;
391 ++dict->size;
392
Juan Cespedescac15c32003-01-31 18:58:58 +0100393 return 0;
394}
395
Juan Cespedesf1350522008-12-16 18:19:58 +0100396void *
Petr Machatad7e4ca82012-11-28 03:38:47 +0100397dict_find(struct dict *dict, const void *key)
Petr Machata77fbb8f2012-04-19 16:57:57 +0200398{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100399 if (dict->size == 0)
400 return NULL;
Petr Machata77fbb8f2012-04-19 16:57:57 +0200401
Petr Machatad7e4ca82012-11-28 03:38:47 +0100402 int found;
403 size_t slot_n = find_slot(dict, key, &found, NULL, NULL);
404 if (found)
405 return getvalue(dict, slot_n);
406 else
407 return NULL;
408}
Petr Machata77fbb8f2012-04-19 16:57:57 +0200409
Petr Machatad7e4ca82012-11-28 03:38:47 +0100410int
411dict_erase(struct dict *dict, const void *key,
412 void (*dtor_key)(void *tgt, void *data),
413 void (*dtor_value)(void *tgt, void *data),
414 void *data)
415{
416 int found;
417 size_t i;
418 size_t slot_n = find_slot(dict, key, &found, NULL, &i);
419 if (!found)
420 return -1;
421
422 if (dtor_key != NULL)
423 dtor_key(getkey(dict, slot_n), data);
424 if (dtor_value != NULL)
425 dtor_value(getvalue(dict, slot_n), data);
426
427 bitp(dict, slot_n)->taken = 0;
428 bitp(dict, slot_n)->erased = 1;
429 --dict->size;
430
431 if (dict->size < 0.3 * n(dict)) {
Petr Machata2718e3f2012-11-28 20:48:11 +0100432 size_t smaller = smaller_size(n(dict));
433 if (smaller != n(dict))
434 /* Don't mind if it fails when shrinking. */
435 rehash(dict, smaller);
Petr Machata77fbb8f2012-04-19 16:57:57 +0200436 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100437
438 return 0;
Petr Machata77fbb8f2012-04-19 16:57:57 +0200439}
440
441void *
Petr Machatad7e4ca82012-11-28 03:38:47 +0100442dict_each(struct dict *dict, void *start_after,
443 enum callback_status (*cb)(void *, void *, void *), void *data)
Petr Machata345c9b52012-02-10 13:10:03 +0100444{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100445 size_t i;
446 if (start_after != NULL)
447 i = ((start_after - dict->keys.data) / dict->keys.elt_size) + 1;
448 else
449 i = 0;
Juan Cespedescac15c32003-01-31 18:58:58 +0100450
Petr Machatad7e4ca82012-11-28 03:38:47 +0100451 for (; i < dict->keys.size; ++i)
452 if (bitp(dict, i)->taken && !bitp(dict, i)->erased) {
453 void *key = getkey(dict, i);
454 if (cb(key, getvalue(dict, i), data) != CBS_CONT)
455 return key;
Juan Cespedescac15c32003-01-31 18:58:58 +0100456 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100457
458 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100459}
460
Petr Machatad7e4ca82012-11-28 03:38:47 +0100461size_t
462dict_hash_int(const int *key)
Petr Machata345c9b52012-02-10 13:10:03 +0100463{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100464 return (size_t)(*key * 2654435761);
Juan Cespedescac15c32003-01-31 18:58:58 +0100465}
466
Juan Cespedesf1350522008-12-16 18:19:58 +0100467int
Petr Machatad7e4ca82012-11-28 03:38:47 +0100468dict_eq_int(const int *key1, const int *key2)
Petr Machata345c9b52012-02-10 13:10:03 +0100469{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100470 return *key1 == *key2;
Juan Cespedescac15c32003-01-31 18:58:58 +0100471}
472
Petr Machatad7e4ca82012-11-28 03:38:47 +0100473size_t
474dict_hash_string(const char **key)
Petr Machata345c9b52012-02-10 13:10:03 +0100475{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100476 size_t h = 5381;
477 const char *str = *key;
478 while (*str != 0)
479 h = h * 33 ^ *str++;
480 return h;
Juan Cespedescac15c32003-01-31 18:58:58 +0100481}
482
Juan Cespedesf1350522008-12-16 18:19:58 +0100483int
Petr Machatad7e4ca82012-11-28 03:38:47 +0100484dict_eq_string(const char **key1, const char **key2)
Petr Machata345c9b52012-02-10 13:10:03 +0100485{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100486 return strcmp(*key1, *key2) == 0;
Juan Cespedescac15c32003-01-31 18:58:58 +0100487}
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200488
Petr Machata3e865762012-11-28 22:06:17 +0100489void
490dict_dtor_string(const char **key, void *data)
491{
492 free((char *)*key);
493}
494
495int
496dict_clone_string(const char **tgt, const char **src, void *data)
497{
498 *tgt = strdup(*src);
499 return *tgt != NULL ? 0 : -1;
500}
501
Petr Machatad7e4ca82012-11-28 03:38:47 +0100502#ifdef TEST
503static enum callback_status
504dump(int *key, int *value, void *data)
Petr Machata534e00f2011-09-27 17:58:38 +0200505{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100506 char *seen = data;
507 assert(seen[*key] == 0);
508 seen[*key] = 1;
509 assert(*value == *key * 2 + 1);
510 return CBS_STOP;
511}
512
513static size_t
514dict_hash_int_silly(const int *key)
515{
516 return *key % 10;
517}
518
519static void
520verify(struct dict *di, size_t len, char *seen)
521{
522 size_t ct = 0;
523 int *it;
524 for (it = NULL; (it = DICT_EACH(di, int, int, it, dump, seen)) != NULL;)
525 ct++;
526 assert(ct == len);
527 memset(seen, 0, len);
528}
529
530static enum callback_status
531fill_keys(int *key, int *value, void *data)
532{
533 int *array = data;
534 array[++array[0]] = *key;
535 return CBS_CONT;
536}
537
538static void
539test1(void)
540{
541 struct dict di;
542 DICT_INIT(&di, int, int, dict_hash_int, dict_eq_int, NULL);
543
544 char seen[100000] = {};
545 size_t i;
546 for (i = 0; i < sizeof(seen); ++i) {
547 int key = i;
548 int value = 2 * i + 1;
549 DICT_INSERT(&di, &key, &value);
550 int *valp = DICT_FIND(&di, &key, int);
551 assert(valp != NULL);
552 assert(*valp == value);
553 assert(dict_size(&di) == i + 1);
554 }
555
556 verify(&di, sizeof(seen), seen);
557
558 struct dict d2;
559 DICT_CLONE(&d2, &di, int, int, NULL, NULL, NULL, NULL, NULL);
560 DICT_DESTROY(&di, int, int, NULL, NULL, NULL);
561 verify(&d2, sizeof(seen), seen);
562
563 /* Now we try to gradually erase all elements. We can't erase
564 * inside a DICT_EACH call, so copy first keys to a separate
565 * memory area first. */
566 int keys[d2.size + 1];
567 size_t ct = 0;
568 keys[0] = 0;
569 DICT_EACH(&d2, int, int, NULL, fill_keys, keys);
570 for (i = 0; i < (size_t)keys[0]; ++i) {
571 assert(DICT_ERASE(&d2, &keys[i + 1], int,
572 NULL, NULL, NULL) == 0);
573 ++ct;
574 }
575 assert(ct == sizeof(seen));
576 DICT_DESTROY(&d2, int, int, NULL, NULL, NULL);
577}
578
579static void
580test_erase(void)
581{
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200582 int i;
583
Petr Machatad7e4ca82012-11-28 03:38:47 +0100584 /* To test erase, we need a relatively bad hash function, so
585 * that there are some overlapping chains in the table. */
586 struct dict d2;
587 DICT_INIT(&d2, int, int, dict_hash_int_silly, dict_eq_int, NULL);
588 const int limit = 500;
589 for (i = 0; i < limit; ++i) {
590 int key = 2 * i + 1;
591 int value = 2 * key + 1;
592 DICT_INSERT(&d2, &key, &value);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200593 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200594
Petr Machatad7e4ca82012-11-28 03:38:47 +0100595 /* Now we try to delete each of the keys, and verify that none
596 * of the chains was broken. */
597 for (i = 0; i < limit; ++i) {
598 struct dict copy;
599 DICT_CLONE(&copy, &d2, int, int, NULL, NULL, NULL, NULL, NULL);
600 int key = 2 * i + 1;
601 DICT_ERASE(&copy, &key, int, NULL, NULL, NULL);
602 assert(dict_size(&copy) == dict_size(&d2) - 1);
603
604 int j;
605 for (j = 0; j < limit; ++j) {
606 key = 2 * j + 1;
607 int *valp = DICT_FIND(&copy, &key, int);
608 if (i != j) {
609 assert(valp != NULL);
610 assert(*valp == 2 * key + 1);
611 } else {
612 assert(valp == NULL);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200613 }
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200614 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100615
616 DICT_DESTROY(&copy, int, int, NULL, NULL, NULL);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200617 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100618 DICT_DESTROY(&d2, int, int, NULL, NULL, NULL);
Juan Cespedesbc8caf02009-05-07 19:38:38 +0200619}
Petr Machata534e00f2011-09-27 17:58:38 +0200620
Petr Machatad7e4ca82012-11-28 03:38:47 +0100621int main(int argc, char *argv[])
Petr Machata534e00f2011-09-27 17:58:38 +0200622{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100623 test1();
624 test_erase();
625 return 0;
Petr Machata534e00f2011-09-27 17:58:38 +0200626}
627
Petr Machatad7e4ca82012-11-28 03:38:47 +0100628#endif