3 /* dict - a generic dictionnary, variation over the B-tree concept */
5 /* Copyright (c) 2003,2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "dict_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict,xbt,
16 "Dictionaries provide the same functionnalities than hash tables");
17 /*####[ Private prototypes ]#################################################*/
19 /*####[ Code ]###############################################################*/
23 * \return pointer to the destination
24 * \see xbt_dict_new_ext(), xbt_dict_free()
26 * Creates and initialize a new dictionnary with a default hashtable size.
28 xbt_dict_t xbt_dict_new(void) {
29 return xbt_dict_new_ext(256);
33 * \brief Create a new dictionary with the specified hashtable size
34 * \param hashsize the hashtable size
35 * \return a pointer to the created object
36 * \see xbt_dict_new(), xbt_dict_free()
38 xbt_dict_t xbt_dict_new_ext(int hashsize) {
40 xbt_dict_t dict = xbt_new0(s_xbt_dict_t, 1);
41 dict->table_size = hashsize;
42 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size);
44 for (i = 0; i < hashsize; i++) {
45 dict->table[i] = NULL;
54 * \param dict the dictionnary to be freed
56 * Frees a dictionary with all the data
58 void xbt_dict_free(xbt_dict_t *dict) {
60 xbt_dictelm_t current, previous;
61 if (dict != NULL && *dict != NULL) {
62 for (i = 0; i < (*dict)->table_size; i++) {
63 current = (*dict)->table[i];
64 while (current != NULL) {
66 current = current->next;
67 xbt_dictelm_free(previous);
70 xbt_free((*dict)->table);
77 * \brief Change the hashtable size
78 * \param dict a dictionary
79 * \param hashsize the new hashtable size
81 * Change the hashtable size is a long operation, so it's better
82 * to use xbt_dict_new_ext or to call xbt_dict_hashsize_set when
83 * the dictionary is empty.
85 void xbt_dict_hashsize_set(xbt_dict_t dict, int hashsize) {
86 xbt_dict_t new_dict = xbt_dict_new_ext(hashsize);
87 xbt_dictelm_t element, next;
90 for (i = 0; i < dict->table_size; i++) {
91 element = dict->table[i];
92 while (element != NULL) {
93 next = element->next; /* save the next because it will be lost */
94 xbt_dict_add_element(new_dict, element); /* no new xbt_dictelm_t is mallocated */
99 xbt_free(dict->table);
100 dict->table = new_dict->table;
101 dict->table_size = hashsize;
106 * Returns the hash code of a string.
108 unsigned int xbt_dict_hash(const char *str) {
109 /* fast implementation of djb2 algorithm */
110 unsigned int hash = 5381;
113 while ((c = *str++)) {
114 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
121 * \brief Add data to the dict (arbitrary key)
122 * \param dict the container
123 * \param key the key to set the new data
124 * \param key_len the size of the \a key
125 * \param data the data to add in the dict
126 * \param free_ctn function to call with (\a key as argument) when
127 * \a key is removed from the dictionnary
129 * Set the \a data in the structure under the \a key, which can be any kind
130 * of data, as long as its length is provided in \a key_len.
132 void xbt_dict_set_ext(xbt_dict_t dict,
136 void_f_pvoid_t *free_ctn) {
139 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
140 xbt_dictelm_t current, previous = NULL;
142 current = dict->table[hash_code];
143 while (current != NULL &&
144 (key_len != current->key_len || strncmp(key, current->key, key_len))) {
146 current = current->next;
149 if (current == NULL) {
150 /* this key doesn't exist yet */
151 current = xbt_dictelm_new(key, key_len, data, free_ctn, NULL);
152 if (previous == NULL) {
153 dict->table[hash_code] = current;
156 previous->next = current;
161 /* there is already an element with the same key: we overwrite it */
162 if (current->content != NULL && current->free_f != NULL) {
163 current->free_f(current->content);
165 current->content = data;
166 current->free_f = free_ctn;
171 * \brief Add data to the dict (null-terminated key)
173 * \param dict the dict
174 * \param key the key to set the new data
175 * \param data the data to add in the dict
176 * \param free_ctn function to call with (\a key as argument) when
177 * \a key is removed from the dictionnary
179 * set the \a data in the structure under the \a key, which is a
180 * null terminated string.
182 void xbt_dict_set(xbt_dict_t dict,
185 void_f_pvoid_t *free_ctn) {
189 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
193 * \brief Retrieve data from the dict (arbitrary key)
195 * \param dict the dealer of data
196 * \param key the key to find data
197 * \param key_len the size of the \a key
198 * \return the data that we are looking for
200 * Search the given \a key. Throws not_found_error when not found.
202 void *xbt_dict_get_ext(xbt_dict_t dict,
207 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
208 xbt_dictelm_t current;
210 current = dict->table[hash_code];
211 while (current != NULL &&
212 (key_len != current->key_len || strncmp(key, current->key, key_len))) {
213 current = current->next;
216 if (current == NULL) {
217 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
220 return current->content;
224 * \brief Retrieve data from the dict (null-terminated key)
226 * \param dict the dealer of data
227 * \param key the key to find data
228 * \return the data that we are looking for
230 * Search the given \a key. Throws not_found_error when not found.
231 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
234 void *xbt_dict_get(xbt_dict_t dict,
238 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
239 xbt_dictelm_t current;
241 current = dict->table[hash_code];
242 while (current != NULL && (strcmp(key, current->key))) {
243 current = current->next;
246 if (current == NULL) {
247 THROW1(not_found_error, 0, "key %s not found", key);
250 return current->content;
254 * \brief like xbt_dict_get(), but returning NULL when not found
256 void *xbt_dict_get_or_null(xbt_dict_t dict,
261 result = xbt_dict_get(dict, key);
263 if (e.category != not_found_error)
273 * \brief Remove data from the dict (arbitrary key)
275 * \param dict the trash can
276 * \param key the key of the data to be removed
277 * \param key_len the size of the \a key
279 * Remove the entry associated with the given \a key (throws not_found)
281 void xbt_dict_remove_ext(xbt_dict_t dict,
286 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
287 xbt_dictelm_t current, previous = NULL;
289 current = dict->table[hash_code];
290 while (current != NULL &&
291 (key_len != current->key_len || strncmp(key, current->key, key_len))) {
292 previous = current; /* save the previous node */
293 current = current->next;
296 if (current == NULL) {
297 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
300 if (previous != NULL) {
301 xbt_assert0(previous->next == current, "previous-next != current");
302 previous->next = current->next;
305 dict->table[hash_code] = current->next;
308 xbt_dictelm_free(current);
313 * \brief Remove data from the dict (null-terminated key)
315 * \param dict the dict
316 * \param key the key of the data to be removed
318 * Remove the entry associated with the given \a key
320 void xbt_dict_remove(xbt_dict_t dict,
324 xbt_dict_remove_ext(dict, key, strlen(key));
328 * \brief Remove all data from the dict
329 * \param dict the dict
331 void xbt_dict_reset(xbt_dict_t dict) {
335 xbt_dictelm_t current, previous = NULL;
336 for (i = 0; i < dict->table_size; i++) {
337 current = dict->table[i];
338 while (current != NULL) {
340 current = current->next;
341 xbt_dictelm_free(previous);
343 dict->table[i] = NULL;
350 * \brief Return the number of elements in the dict.
351 * \param dict a dictionary
353 int xbt_dict_length(xbt_dict_t dict) {
360 * Add an already mallocated element to a dictionary.
362 void xbt_dict_add_element(xbt_dict_t dict, xbt_dictelm_t element) {
365 int hashcode = xbt_dict_hash(element->key) % dict->table_size;
366 element->next = dict->table[hashcode];
367 dict->table[hashcode] = element;
371 * \brief Outputs the content of the structure (debuging purpose)
373 * \param dict the exibitionist
374 * \param output a function to dump each data in the tree
376 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
377 * function to output the data. If NULL, data won't be displayed.
380 void xbt_dict_dump(xbt_dict_t dict,
381 void_f_pvoid_t *output) {
383 xbt_dictelm_t element;
384 printf("Dict %p:\n", dict);
386 for (i = 0; i < dict->table_size; i++) {
387 element = dict->table[i];
388 while (element != NULL) {
389 printf("%s -> ", element->key);
390 if (output != NULL) {
391 output(element->content);
394 element = element->next;
403 #include "portable.h"
405 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
406 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
408 XBT_TEST_SUITE("dict","Dict data container");
410 static void print_str(void *str) {
411 printf("%s",(char*)PRINTF_STR(str));
414 static void debuged_add(xbt_dict_t head,const char*key)
416 char *data=xbt_strdup(key);
418 xbt_test_log1("Add %s",PRINTF_STR(key));
420 xbt_dict_set(head,key,data,&free);
421 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
422 xbt_dict_dump(head,(void (*)(void*))&printf);
427 static void fill(xbt_dict_t *head) {
428 xbt_test_add0("Fill in the dictionnary");
430 *head = xbt_dict_new();
431 debuged_add(*head,"12");
432 debuged_add(*head,"12a");
433 debuged_add(*head,"12b");
434 debuged_add(*head,"123");
435 debuged_add(*head,"123456");
436 /* Child becomes child of what to add */
437 debuged_add(*head,"1234");
438 /* Need of common ancestor */
439 debuged_add(*head,"123457");
442 static void search(xbt_dict_t head,const char*key) {
445 xbt_test_add1("Search %s",key);
446 data=xbt_dict_get(head,key);
447 xbt_test_log1("Found %s",(char *)data);
449 xbt_test_assert0(!strcmp((char*)data,key),"Key and data do not match");
452 static void debuged_remove(xbt_dict_t head,const char*key) {
454 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
455 xbt_dict_remove(head,key);
456 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
460 static void traverse(xbt_dict_t head) {
461 xbt_dict_cursor_t cursor=NULL;
465 xbt_dict_foreach(head,cursor,key,data) {
466 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
467 xbt_test_assert2(!data || !strcmp(key,data),
468 "Key(%s) != value(%s). Abording\n",key,data);
472 static void search_not_found(xbt_dict_t head, const char *data) {
475 xbt_test_add1("Search %s (expected not to be found)",data);
478 data = xbt_dict_get(head,"Can't be found");
479 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
481 if (e.category != not_found_error)
482 xbt_test_exception(e);
487 static void count(xbt_dict_t dict, int length) {
488 xbt_test_add1("Count elements (expecting %d)", length);
489 xbt_test_assert2(xbt_dict_length(dict) == length, "Length(%d) != %d.", xbt_dict_length(dict), length);
493 xbt_dict_t head=NULL;
497 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
498 xbt_test_add0("Traversal the empty dictionnary");
501 xbt_test_add0("Traverse the full dictionnary");
508 xbt_test_add0("Free the dictionnary (twice)");
509 xbt_dict_free(&head);
510 xbt_dict_free(&head);
515 xbt_test_add0("Change 123 to 'Changed 123'");
516 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
519 xbt_test_add0("Change 123 back to '123'");
520 xbt_dict_set(head,"123",strdup("123"),&free);
523 xbt_test_add0("Change 12a to 'Dummy 12a'");
524 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
527 xbt_test_add0("Change 12a to '12a'");
528 xbt_dict_set(head,"12a",strdup("12a"),&free);
531 xbt_test_add0("Traverse the resulting dictionnary");
535 xbt_test_add0("Search 123");
536 data = xbt_dict_get(head,"123");
537 xbt_test_assert(data);
538 xbt_test_assert(!strcmp("123",data));
540 search_not_found(head,"Can't be found");
541 search_not_found(head,"123 Can't be found");
542 search_not_found(head,"12345678 NOT");
547 search(head,"123456");
549 search(head,"123457");
551 xbt_test_add0("Traverse the resulting dictionnary");
554 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
556 xbt_test_add0("Free the dictionnary twice");
557 xbt_dict_free(&head);
558 xbt_dict_free(&head);
560 xbt_test_add0("Traverse the resulting dictionnary");
564 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
567 xbt_test_add0("Remove non existing data");
569 debuged_remove(head,"Does not exist");
571 if (e.category != not_found_error)
572 xbt_test_exception(e);
577 xbt_dict_free(&head);
579 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
581 debuged_remove(head,"12a"); traverse(head);
583 debuged_remove(head,"12b"); traverse(head);
585 debuged_remove(head,"12"); traverse(head);
587 debuged_remove(head,"123456"); traverse(head);
590 debuged_remove(head,"12346");
592 if (e.category != not_found_error)
593 xbt_test_exception(e);
597 debuged_remove(head,"1234"); traverse(head);
598 debuged_remove(head,"123457"); traverse(head);
599 debuged_remove(head,"123"); traverse(head);
601 debuged_remove(head,"12346");
603 if (e.category != not_found_error)
604 xbt_test_exception(e);
608 xbt_test_add0("Remove all values");
609 xbt_dict_free(&head);
611 xbt_dict_reset(head);
615 xbt_test_add0("Free the dictionnary twice");
616 xbt_dict_free(&head);
617 xbt_dict_free(&head);
620 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
623 xbt_test_add0("Store NULL under 'null'");
624 xbt_dict_set(head,"null",NULL,NULL);
627 xbt_test_add0("Check whether I see it while traversing...");
629 xbt_dict_cursor_t cursor=NULL;
633 xbt_dict_foreach(head,cursor,key,data) {
634 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
635 if (!strcmp(key,"null"))
638 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
640 xbt_dict_free(&head);
644 #define SIZEOFKEY 1024
645 static int countelems(xbt_dict_t head) {
646 xbt_dict_cursor_t cursor;
651 xbt_dict_foreach(head,cursor,key,data) {
657 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
658 xbt_dict_t head=NULL;
663 srand((unsigned int)time(NULL));
665 xbt_test_add0("CRASH test");
666 xbt_test_log0("Fill the struct, count its elems and frees the structure (x10)");
667 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
671 // if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
673 for (j=0;j<1000;j++) {
674 key=xbt_malloc(SIZEOFKEY);
676 for (k=0;k<SIZEOFKEY-1;k++)
677 key[k]=rand() % ('z' - 'a') + 'a';
679 /* printf("[%d %s]\n",j,key); */
680 xbt_dict_set(head,key,key,&free);
682 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
683 nb = countelems(head);
684 xbt_test_assert1(nb == 1000,"found %d elements instead of 1000",nb);
686 xbt_dict_free(&head);
687 xbt_dict_free(&head);
692 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
693 for (j=0;j<NB_ELM;j++) {
694 // if (!(j%1000)) { printf("."); fflush(stdout); }
696 key = xbt_malloc(10);
699 xbt_dict_set(head,key,key,&free);
702 xbt_test_add0("Count the elements (retrieving the key and data for each)");
703 i = countelems(head);
704 xbt_test_log1("There is %d elements",i);
706 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
709 // if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
710 for (j=0;j<NB_ELM;j++) {
713 data = xbt_dict_get(head,key);
714 xbt_test_assert2(!strcmp(key,(char*)data),
715 "key=%s != data=%s\n",key,(char*)data);
720 xbt_test_add1("Remove my %d elements",NB_ELM);
722 for (j=0;j<NB_ELM;j++) {
723 //if (!(j%10000)) printf("."); fflush(stdout);
726 xbt_dict_remove(head,key);
731 xbt_test_add0("Free the structure (twice)");
732 xbt_dict_free(&head);
733 xbt_dict_free(&head);
736 static void str_free(void *s) {
741 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
744 #define NB_ELM 100 /*00*/
747 #define NB_TEST 20 /*20*/
750 xbt_dict_t mdict = NULL;
752 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
757 xbt_test_add0("Generic multicache CRASH test");
758 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
759 "depth of multicache=%d, key size=%d",
760 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
762 for (l=0 ; l<DEPTH ; l++) {
763 key=xbt_malloc(KEY_SIZE);
764 xbt_dynar_push(keys,&key);
767 for (i=0;i<NB_TEST;i++) {
768 mdict = xbt_dict_new();
769 VERB1("mdict=%p",mdict);
771 printf("Test %d\n",i);
772 /* else if (i%10) printf("."); else printf("%d",i/10);*/
774 for (j=0;j<NB_ELM;j++) {
775 if (verbose>0) printf (" Add {");
777 for (l=0 ; l<DEPTH ; l++) {
778 key=*(char**)xbt_dynar_get_ptr(keys,l);
780 for (k=0;k<KEY_SIZE-1;k++)
781 key[k]=rand() % ('z' - 'a') + 'a';
785 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
787 if (verbose>0) printf("in multitree %p.\n",mdict);
789 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
791 data = xbt_multidict_get(mdict,keys);
793 xbt_test_assert2(data && !strcmp((char*)data,key),
794 "Retrieved value (%s) does not match the entrered one (%s)\n",
797 xbt_dict_free(&mdict);
800 xbt_dynar_free(&keys);
803 xbt_dict_dump(mdict,&xbt_dict_print);*/
805 xbt_dict_free(&mdict);
806 xbt_dynar_free(&keys);
809 #endif /* SIMGRID_TEST */