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 "xbt/mallocator.h"
14 #include "xbt_modinter.h"
15 #include "dict_private.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict,xbt,
18 "Dictionaries provide the same functionnalities than hash tables");
19 /*####[ Private prototypes ]#################################################*/
21 static xbt_mallocator_t dict_mallocator = NULL;
22 static void* dict_mallocator_new_f(void);
23 static void dict_mallocator_free_f(void* dict);
24 static void dict_mallocator_reset_f(void* dict);
27 /*####[ Code ]###############################################################*/
31 * \return pointer to the destination
32 * \see xbt_dict_new_ext(), xbt_dict_free()
34 * Creates and initialize a new dictionnary with a default hashtable size.
36 xbt_dict_t xbt_dict_new(void) {
37 return xbt_dict_new_ext(256);
41 * \brief Create a new dictionary with the specified hashtable size
42 * \param hashsize the hashtable size
43 * \return a pointer to the created object
44 * \see xbt_dict_new(), xbt_dict_free()
46 xbt_dict_t xbt_dict_new_ext(int hashsize) {
49 if (dict_mallocator == NULL) {
51 dict_mallocator = xbt_mallocator_new(256,
52 dict_mallocator_new_f,
53 dict_mallocator_free_f,
54 dict_mallocator_reset_f);
55 dict_elm_mallocator = xbt_mallocator_new(256,
56 dict_elm_mallocator_new_f,
57 dict_elm_mallocator_free_f,
58 dict_elm_mallocator_reset_f);
61 dict = xbt_mallocator_get(dict_mallocator);
62 dict->table_size = hashsize;
63 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size);
71 * \param dict the dictionnary to be freed
73 * Frees a dictionary with all the data
75 void xbt_dict_free(xbt_dict_t *dict) {
77 xbt_dictelm_t current, previous;
81 if (dict != NULL && *dict != NULL) {
82 table_size = (*dict)->table_size;
83 table = (*dict)->table;
84 for (i = 0; i < table_size; i++) {
86 while (current != NULL) {
88 current = current->next;
89 xbt_dictelm_free(previous);
93 xbt_mallocator_release(dict_mallocator, *dict);
99 * \brief Change the hashtable size
100 * \param dict a dictionary
101 * \param hashsize the new hashtable size
103 * Change the hashtable size is a long operation, so it's better
104 * to use xbt_dict_new_ext or to call xbt_dict_hashsize_set when
105 * the dictionary is empty.
107 void xbt_dict_hashsize_set(xbt_dict_t dict, int hashsize) {
108 xbt_dict_t new_dict = xbt_dict_new_ext(hashsize);
109 xbt_dictelm_t element, next;
112 for (i = 0; i < dict->table_size; i++) {
113 element = dict->table[i];
114 while (element != NULL) {
115 next = element->next; /* save the next because it will be lost */
116 xbt_dict_add_element(new_dict, element); /* no new xbt_dictelm_t is mallocated */
121 xbt_free(dict->table);
122 dict->table = new_dict->table;
123 dict->table_size = hashsize;
128 * Returns the hash code of a string.
130 unsigned int xbt_dict_hash(const char *str) {
131 /* fast implementation of djb2 algorithm */
132 unsigned int hash = 5381;
135 while ((c = *str++)) {
136 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
143 * \brief Add data to the dict (arbitrary key)
144 * \param dict the container
145 * \param key the key to set the new data
146 * \param key_len the size of the \a key
147 * \param data the data to add in the dict
148 * \param free_ctn function to call with (\a key as argument) when
149 * \a key is removed from the dictionnary
151 * Set the \a data in the structure under the \a key, which can be any kind
152 * of data, as long as its length is provided in \a key_len.
154 void xbt_dict_set_ext(xbt_dict_t dict,
158 void_f_pvoid_t *free_ctn) {
161 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
162 xbt_dictelm_t current, previous = NULL;
164 current = dict->table[hash_code];
165 while (current != NULL &&
166 (key_len != current->key_len || strncmp(key, current->key, key_len))) {
168 current = current->next;
171 if (current == NULL) {
172 /* this key doesn't exist yet */
173 current = xbt_dictelm_new(key, key_len, data, free_ctn, NULL);
174 if (previous == NULL) {
175 dict->table[hash_code] = current;
178 previous->next = current;
183 /* there is already an element with the same key: we overwrite it */
184 if (current->content != NULL && current->free_f != NULL) {
185 current->free_f(current->content);
187 current->content = data;
188 current->free_f = free_ctn;
193 * \brief Add data to the dict (null-terminated key)
195 * \param dict the dict
196 * \param key the key to set the new data
197 * \param data the data to add in the dict
198 * \param free_ctn function to call with (\a key as argument) when
199 * \a key is removed from the dictionnary
201 * set the \a data in the structure under the \a key, which is a
202 * null terminated string.
204 void xbt_dict_set(xbt_dict_t dict,
207 void_f_pvoid_t *free_ctn) {
211 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
215 * \brief Retrieve data from the dict (arbitrary key)
217 * \param dict the dealer of data
218 * \param key the key to find data
219 * \param key_len the size of the \a key
220 * \return the data that we are looking for
222 * Search the given \a key. Throws not_found_error when not found.
224 void *xbt_dict_get_ext(xbt_dict_t dict,
229 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
230 xbt_dictelm_t current;
232 current = dict->table[hash_code];
233 while (current != NULL &&
234 (key_len != current->key_len || strncmp(key, current->key, key_len))) {
235 current = current->next;
238 if (current == NULL) {
239 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
242 return current->content;
246 * \brief Retrieve data from the dict (null-terminated key)
248 * \param dict the dealer of data
249 * \param key the key to find data
250 * \return the data that we are looking for
252 * Search the given \a key. Throws not_found_error when not found.
253 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
256 void *xbt_dict_get(xbt_dict_t dict,
260 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
261 xbt_dictelm_t current;
263 current = dict->table[hash_code];
264 while (current != NULL && (strcmp(key, current->key))) {
265 current = current->next;
268 if (current == NULL) {
269 THROW1(not_found_error, 0, "key %s not found", key);
272 return current->content;
276 * \brief like xbt_dict_get(), but returning NULL when not found
278 void *xbt_dict_get_or_null(xbt_dict_t dict,
283 result = xbt_dict_get(dict, key);
285 if (e.category != not_found_error)
295 * \brief Remove data from the dict (arbitrary key)
297 * \param dict the trash can
298 * \param key the key of the data to be removed
299 * \param key_len the size of the \a key
301 * Remove the entry associated with the given \a key (throws not_found)
303 void xbt_dict_remove_ext(xbt_dict_t dict,
308 unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
309 xbt_dictelm_t current, previous = NULL;
311 current = dict->table[hash_code];
312 while (current != NULL &&
313 (key_len != current->key_len || strncmp(key, current->key, key_len))) {
314 previous = current; /* save the previous node */
315 current = current->next;
318 if (current == NULL) {
319 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
322 if (previous != NULL) {
323 xbt_assert0(previous->next == current, "previous-next != current");
324 previous->next = current->next;
327 dict->table[hash_code] = current->next;
330 xbt_dictelm_free(current);
335 * \brief Remove data from the dict (null-terminated key)
337 * \param dict the dict
338 * \param key the key of the data to be removed
340 * Remove the entry associated with the given \a key
342 void xbt_dict_remove(xbt_dict_t dict,
346 xbt_dict_remove_ext(dict, key, strlen(key));
350 * \brief Remove all data from the dict
351 * \param dict the dict
353 void xbt_dict_reset(xbt_dict_t dict) {
357 xbt_dictelm_t current, previous = NULL;
358 for (i = 0; i < dict->table_size; i++) {
359 current = dict->table[i];
360 while (current != NULL) {
362 current = current->next;
363 xbt_dictelm_free(previous);
365 dict->table[i] = NULL;
372 * \brief Return the number of elements in the dict.
373 * \param dict a dictionary
375 int xbt_dict_length(xbt_dict_t dict) {
382 * Add an already mallocated element to a dictionary.
384 void xbt_dict_add_element(xbt_dict_t dict, xbt_dictelm_t element) {
387 int hashcode = xbt_dict_hash(element->key) % dict->table_size;
388 element->next = dict->table[hashcode];
389 dict->table[hashcode] = element;
393 * \brief Outputs the content of the structure (debuging purpose)
395 * \param dict the exibitionist
396 * \param output a function to dump each data in the tree
398 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
399 * function to output the data. If NULL, data won't be displayed.
402 void xbt_dict_dump(xbt_dict_t dict,
403 void_f_pvoid_t *output) {
405 xbt_dictelm_t element;
406 printf("Dict %p:\n", dict);
408 for (i = 0; i < dict->table_size; i++) {
409 element = dict->table[i];
410 while (element != NULL) {
411 printf("%s -> ", element->key);
412 if (output != NULL) {
413 output(element->content);
416 element = element->next;
423 * Destroy the dict mallocators.
424 * This is an internal XBT function called by xbt_exit().
426 void xbt_dict_exit(void) {
427 if (dict_mallocator != NULL) {
428 xbt_mallocator_free(dict_mallocator);
429 xbt_mallocator_free(dict_elm_mallocator);
433 static void* dict_mallocator_new_f(void) {
434 return xbt_new(s_xbt_dict_t, 1);
437 static void dict_mallocator_free_f(void* dict) {
441 static void dict_mallocator_reset_f(void* dict) {
442 /* nothing to do because all fields are
443 * initialized in xbt_dict_new
450 #include "portable.h"
452 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
453 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
455 XBT_TEST_SUITE("dict","Dict data container");
457 static void print_str(void *str) {
458 printf("%s",(char*)PRINTF_STR(str));
461 static void debuged_add(xbt_dict_t head,const char*key)
463 char *data=xbt_strdup(key);
465 xbt_test_log1("Add %s",PRINTF_STR(key));
467 xbt_dict_set(head,key,data,&free);
468 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
469 xbt_dict_dump(head,(void (*)(void*))&printf);
474 static void fill(xbt_dict_t *head) {
475 xbt_test_add0("Fill in the dictionnary");
477 *head = xbt_dict_new();
478 debuged_add(*head,"12");
479 debuged_add(*head,"12a");
480 debuged_add(*head,"12b");
481 debuged_add(*head,"123");
482 debuged_add(*head,"123456");
483 /* Child becomes child of what to add */
484 debuged_add(*head,"1234");
485 /* Need of common ancestor */
486 debuged_add(*head,"123457");
489 static void search(xbt_dict_t head,const char*key) {
492 xbt_test_add1("Search %s",key);
493 data=xbt_dict_get(head,key);
494 xbt_test_log1("Found %s",(char *)data);
496 xbt_test_assert0(!strcmp((char*)data,key),"Key and data do not match");
499 static void debuged_remove(xbt_dict_t head,const char*key) {
501 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
502 xbt_dict_remove(head,key);
503 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
507 static void traverse(xbt_dict_t head) {
508 xbt_dict_cursor_t cursor=NULL;
512 xbt_dict_foreach(head,cursor,key,data) {
513 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
514 xbt_test_assert2(!data || !strcmp(key,data),
515 "Key(%s) != value(%s). Abording\n",key,data);
519 static void search_not_found(xbt_dict_t head, const char *data) {
523 xbt_test_add1("Search %s (expected not to be found)",data);
526 data = xbt_dict_get(head, data);
527 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
529 if (e.category != not_found_error)
530 xbt_test_exception(e);
534 xbt_test_assert0(ok,"Exception not raised");
537 static void count(xbt_dict_t dict, int length) {
538 xbt_test_add1("Count elements (expecting %d)", length);
539 xbt_test_assert2(xbt_dict_length(dict) == length, "Length(%d) != %d.", xbt_dict_length(dict), length);
543 xbt_dict_t head=NULL;
547 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
548 xbt_test_add0("Traversal the empty dictionnary");
551 xbt_test_add0("Traverse the full dictionnary");
558 xbt_test_add0("Free the dictionnary (twice)");
559 xbt_dict_free(&head);
560 xbt_dict_free(&head);
565 xbt_test_add0("Change 123 to 'Changed 123'");
566 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
569 xbt_test_add0("Change 123 back to '123'");
570 xbt_dict_set(head,"123",strdup("123"),&free);
573 xbt_test_add0("Change 12a to 'Dummy 12a'");
574 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
577 xbt_test_add0("Change 12a to '12a'");
578 xbt_dict_set(head,"12a",strdup("12a"),&free);
581 xbt_test_add0("Traverse the resulting dictionnary");
585 xbt_test_add0("Search 123");
586 data = xbt_dict_get(head,"123");
587 xbt_test_assert(data);
588 xbt_test_assert(!strcmp("123",data));
590 search_not_found(head,"Can't be found");
591 search_not_found(head,"123 Can't be found");
592 search_not_found(head,"12345678 NOT");
597 search(head,"123456");
599 search(head,"123457");
601 xbt_test_add0("Traverse the resulting dictionnary");
604 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
606 xbt_test_add0("Free the dictionnary twice");
607 xbt_dict_free(&head);
608 xbt_dict_free(&head);
610 xbt_test_add0("Traverse the resulting dictionnary");
614 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
617 xbt_test_add0("Remove non existing data");
619 debuged_remove(head,"Does not exist");
621 if (e.category != not_found_error)
622 xbt_test_exception(e);
627 xbt_dict_free(&head);
629 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
631 debuged_remove(head,"12a"); traverse(head);
633 debuged_remove(head,"12b"); traverse(head);
635 debuged_remove(head,"12"); traverse(head);
637 debuged_remove(head,"123456"); traverse(head);
640 debuged_remove(head,"12346");
642 if (e.category != not_found_error)
643 xbt_test_exception(e);
647 debuged_remove(head,"1234"); traverse(head);
648 debuged_remove(head,"123457"); traverse(head);
649 debuged_remove(head,"123"); traverse(head);
651 debuged_remove(head,"12346");
653 if (e.category != not_found_error)
654 xbt_test_exception(e);
658 xbt_test_add0("Remove all values");
659 xbt_dict_free(&head);
661 xbt_dict_reset(head);
665 xbt_test_add0("Free the dictionnary twice");
666 xbt_dict_free(&head);
667 xbt_dict_free(&head);
670 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
673 xbt_test_add0("Store NULL under 'null'");
674 xbt_dict_set(head,"null",NULL,NULL);
677 xbt_test_add0("Check whether I see it while traversing...");
679 xbt_dict_cursor_t cursor=NULL;
683 xbt_dict_foreach(head,cursor,key,data) {
684 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
685 if (!strcmp(key,"null"))
688 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
690 xbt_dict_free(&head);
694 #define SIZEOFKEY 1024
695 static int countelems(xbt_dict_t head) {
696 xbt_dict_cursor_t cursor;
701 xbt_dict_foreach(head,cursor,key,data) {
707 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
708 xbt_dict_t head=NULL;
713 srand((unsigned int)time(NULL));
715 xbt_test_add0("CRASH test");
716 xbt_test_log0("Fill the struct, count its elems and frees the structure (x10)");
717 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
721 // if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
723 for (j=0;j<1000;j++) {
724 key=xbt_malloc(SIZEOFKEY);
726 for (k=0;k<SIZEOFKEY-1;k++)
727 key[k]=rand() % ('z' - 'a') + 'a';
729 /* printf("[%d %s]\n",j,key); */
730 xbt_dict_set(head,key,key,&free);
732 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
733 nb = countelems(head);
734 xbt_test_assert1(nb == 1000,"found %d elements instead of 1000",nb);
736 xbt_dict_free(&head);
737 xbt_dict_free(&head);
742 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
743 for (j=0;j<NB_ELM;j++) {
744 // if (!(j%1000)) { printf("."); fflush(stdout); }
746 key = xbt_malloc(10);
749 xbt_dict_set(head,key,key,&free);
752 xbt_test_add0("Count the elements (retrieving the key and data for each)");
753 i = countelems(head);
754 xbt_test_log1("There is %d elements",i);
756 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
759 // if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
760 for (j=0;j<NB_ELM;j++) {
763 data = xbt_dict_get(head,key);
764 xbt_test_assert2(!strcmp(key,(char*)data),
765 "key=%s != data=%s\n",key,(char*)data);
770 xbt_test_add1("Remove my %d elements",NB_ELM);
772 for (j=0;j<NB_ELM;j++) {
773 //if (!(j%10000)) printf("."); fflush(stdout);
776 xbt_dict_remove(head,key);
781 xbt_test_add0("Free the structure (twice)");
782 xbt_dict_free(&head);
783 xbt_dict_free(&head);
786 static void str_free(void *s) {
791 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
794 #define NB_ELM 100 /*00*/
797 #define NB_TEST 20 /*20*/
800 xbt_dict_t mdict = NULL;
802 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
807 xbt_test_add0("Generic multicache CRASH test");
808 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
809 "depth of multicache=%d, key size=%d",
810 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
812 for (l=0 ; l<DEPTH ; l++) {
813 key=xbt_malloc(KEY_SIZE);
814 xbt_dynar_push(keys,&key);
817 for (i=0;i<NB_TEST;i++) {
818 mdict = xbt_dict_new();
819 VERB1("mdict=%p",mdict);
821 printf("Test %d\n",i);
822 /* else if (i%10) printf("."); else printf("%d",i/10);*/
824 for (j=0;j<NB_ELM;j++) {
825 if (verbose>0) printf (" Add {");
827 for (l=0 ; l<DEPTH ; l++) {
828 key=*(char**)xbt_dynar_get_ptr(keys,l);
830 for (k=0;k<KEY_SIZE-1;k++)
831 key[k]=rand() % ('z' - 'a') + 'a';
835 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
837 if (verbose>0) printf("in multitree %p.\n",mdict);
839 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
841 data = xbt_multidict_get(mdict,keys);
843 xbt_test_assert2(data && !strcmp((char*)data,key),
844 "Retrieved value (%s) does not match the entrered one (%s)\n",
847 xbt_dict_free(&mdict);
850 xbt_dynar_free(&keys);
853 xbt_dict_dump(mdict,&xbt_dict_print);*/
855 xbt_dict_free(&mdict);
856 xbt_dynar_free(&keys);
859 #endif /* SIMGRID_TEST */