Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some more xbt_error_t eradication; change mismatch_error to not_found_error where...
[simgrid.git] / src / xbt / dict_elm.c
1 /* $Id$ */
2
3 /* dict - a generic dictionnary, variation over the B-tree concept          */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #include "xbt/ex.h"
11 #include "portable.h" /* PRINTF_STR */
12 #include "dict_private.h"  /* prototypes of this module */
13
14 XBT_LOG_EXTERNAL_CATEGORY(dict);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_elm,dict,"Dictionaries internals");
16
17 XBT_LOG_NEW_SUBCATEGORY(dict_add,dict,"Dictionaries internals: elements addition");
18 XBT_LOG_NEW_SUBCATEGORY(dict_search,dict,"Dictionaries internals: searching");
19 XBT_LOG_NEW_SUBCATEGORY(dict_remove,dict,"Dictionaries internals: elements removal");
20 XBT_LOG_NEW_SUBCATEGORY(dict_collapse,dict,"Dictionaries internals: post-removal cleanup");
21
22 /*####[ Private prototypes ]#################################################*/
23
24 static _XBT_INLINE void _xbt_dictelm_alloc(char                *key,
25                                            int                  offset,
26                                            int                  key_len,
27                                            int                  internal,
28                                            void                *data,
29                                            void_f_pvoid_t      *free_f,
30                                            /*OUT*/s_xbt_dictelm_t **where);
31 static void         _dictelm_wrapper_free(void*);
32
33 static _XBT_INLINE void  _str_prefix_lgr(const char *key1,
34                                           int         key_len1,
35                                           const char *key2,
36                                           int         key_len2,
37                                           int        *offset,
38                                           int        *match);
39
40
41 static void _xbt_dictelm_dump_rec(s_xbt_dictelm_t *head,
42                                    int             offset,
43                                    void_f_pvoid_t *output);
44
45
46
47 static void _xbt_dictelm_set_rec(s_xbt_dictelm_t *head,
48                                   char           *key,
49                                   int             key_len,
50                                   int             offset,
51                                   void           *data,
52                                   void_f_pvoid_t *free_f);
53 static void * _xbt_dictelm_get_rec(s_xbt_dictelm_t *head,
54                                    const char     *key,
55                                    int             key_len,
56                                    int             offset);
57 static void _xbt_dictelm_remove_rec(s_xbt_dictelm_t *head,
58                                     const char     *key,
59                                     int             key_len,
60                                     int             offset);
61
62 static _XBT_INLINE
63 void
64 _collapse_if_need(s_xbt_dictelm_t *p_head,
65                   int             pos,
66                   int             offset);
67
68 /* ---- */
69
70 static _XBT_INLINE
71 void *
72 xbt_memdup(const void * const ptr,
73             const size_t       length) {
74   void * new_ptr = NULL;
75
76   new_ptr = xbt_malloc(length);
77   memcpy(new_ptr, ptr, length);
78    
79   return new_ptr;
80 }
81
82 /*
83  * _xbt_nibble_to_char:
84  *
85  * Change any byte to a printable char
86  */
87
88 static _XBT_INLINE
89 char
90 _xbt_nibble_to_char(unsigned char c) {
91   c &= 0x0f;
92   return c>9 ? c-10+'a' : c + '0';
93 }
94
95 /*
96  * _xbt_bytes_to_string:
97  *
98  * Change any byte array to a printable string
99  * The length of string_container should at least be data_len*2+1 
100  */
101 static _XBT_INLINE
102 char *
103 _xbt_bytes_to_string(char * const ptr,
104                       int          data_len,
105                       char * const string_container) {
106   unsigned char *src = (unsigned char *)ptr;
107            char *dst = string_container;
108
109   while (data_len--) {
110     *dst++ = _xbt_nibble_to_char(*src   & 0x0f     );
111     *dst++ = _xbt_nibble_to_char(*src++ & 0xf0 >> 4);
112   }
113
114   *dst = 0;
115
116   return ptr;
117 }
118
119 /* ---- */
120
121 /*
122  * _xbt_dictelm_alloc:
123  *
124  * Alloc a dict element with no child.
125  */
126 static _XBT_INLINE
127 void
128 _xbt_dictelm_alloc(char                *key,
129                    int                  key_len,
130                    int                  offset,
131                    int                  internal,
132                    void                *data,
133                    void_f_pvoid_t      *free_f,
134                  /*OUT*/s_xbt_dictelm_t **pp_elm) {
135   s_xbt_dictelm_t *p_elm  = NULL;
136
137   p_elm = xbt_new(s_xbt_dictelm_t,1);
138
139   p_elm->key      = key;
140   p_elm->key_len  = key_len;
141   p_elm->offset   = offset;
142   p_elm->internal   = internal;  
143   p_elm->content  = data;
144   p_elm->free_f = free_f;
145   p_elm->sub      = xbt_dynar_new(sizeof(s_xbt_dictelm_t*), _dictelm_wrapper_free);
146
147   *pp_elm = p_elm;
148
149 }
150
151 /**
152  * xbt_dictelm_free:
153  *
154  * @pp_elm: the dict elem to be freed
155  *
156  * Frees a dictionnary element with all its childs.
157  */
158 void
159 xbt_dictelm_free(s_xbt_dictelm_t **pp_elm)  {
160   if (*pp_elm) {
161     s_xbt_dictelm_t *p_elm = *pp_elm;
162
163     xbt_dynar_free(&(p_elm->sub));
164
165     if (p_elm->key) {
166       free(p_elm->key);
167     }
168
169     if (p_elm->free_f && p_elm->content) {
170       p_elm->free_f(p_elm->content);
171     }
172
173     free(p_elm);
174     *pp_elm = NULL;
175   }
176 }
177
178 /**
179  * _dictelm_wrapper_free:
180  *
181  * a wrapper to free dictelm with the right prototype to be usable within dynar
182  */
183 static
184 void
185 _dictelm_wrapper_free(void *pp_elm) {
186   DEBUG3("Free dictelm '%.*s' %p", 
187          (*(s_xbt_dictelm_t**)pp_elm)->key_len, (*(s_xbt_dictelm_t**)pp_elm)->key,
188          *(void**)pp_elm);
189   xbt_dictelm_free((s_xbt_dictelm_t**)pp_elm);
190 }
191
192 /*####[ utility functions ]##################################################*/
193 /**
194  * _str_prefix_lgr:
195  *
196  *
197  * Returns the length of the common prefix of @str1 and @str2.
198  * Do make sure the strings are not null, this function don't
199  */
200 static _XBT_INLINE
201 void
202 _str_prefix_lgr(const char *key1,
203                 int         key_len1,
204                 const char *key2,
205                 int         key_len2,
206                 int        *p_offset,
207                 int        *p_match) {
208   const int old_offset = *p_offset;
209   int       o          = *p_offset;
210   int       m          = *p_match;
211
212   m = 0;
213
214   /*CDEBUG5(dict_search, "%s: [%.*s] <=> [%.*s]", __FUNCTION__, 
215             key1,key_len1,key2,key_len2);*/
216
217   if (o < key_len1  &&  o < key_len2) {
218
219     while (key1[o] == key2[o]) {
220       o++;
221
222       if (!(o < key_len1  &&  o < key_len2))
223         break;
224
225     }
226
227   }
228
229
230   if (o != old_offset) {
231
232     if (o >= key_len1) {
233
234       if (o >= key_len2) {
235         m = 1; /* exact match */
236       } else {
237         m = 2; /* child is prefix */
238       }
239
240     } else if (o >= key_len2) {
241       m = 3; /* key prefix of child */
242     } else { 
243       DEBUG7("Common prefix. o=%d; key1=%.*s; key_len1=%d; key2=%.*s; key_len2=%d", 
244              o, 
245              key_len1, key1, 
246              key_len1, 
247              key_len2, key2, 
248              key_len2);
249       m = 4; /* Common prefix (=> common ancestor) */
250     }
251   }
252
253
254   *p_offset = o;
255   *p_match  = m;
256 }
257
258 /**
259  * _dictelm_child_cmp:
260  *
261  * Compares two dictelm keys and return their matching (using the same 
262  * convention than @_xbt_dict_child_search() )
263  */
264 static _XBT_INLINE
265 void
266 _dict_child_cmp(s_xbt_dictelm_t *p_dict,
267                 int          pos,
268                 const char  *key,
269                 const int    key_len,
270                 int         *p_offset,
271                 int         *p_match,
272                 int         *p_cmp) {
273   s_xbt_dictelm_t  *p_child = NULL;
274   int           cmp     = 0;
275   int           o       = *p_offset;
276   int           m       = *p_match;
277
278   p_child = xbt_dynar_get_as(p_dict->sub, pos, s_xbt_dictelm_t*);
279
280   /* Compute the length of the prefix
281      and if the searched key is before or after cur */
282   _str_prefix_lgr(p_child->key, p_child->key_len,
283                   key,          key_len,
284                   &o, &m);
285
286
287   if (m) /* found, get out */
288     goto end;
289
290   if (o < p_child->key_len  &&  (o >= key_len  ||  key[o] < p_child->key[o])) {
291     cmp = -1;
292   } else {
293     cmp =  1;
294   }
295
296   CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d", 
297           p_child->key_len - *p_offset, p_child->key + *p_offset,
298           key_len - *p_offset, key + *p_offset,
299           *p_offset,cmp);
300
301  end:
302   *p_offset = o;
303   *p_match  = m;
304   *p_cmp    = cmp;
305 }
306
307 /**
308  * _xbt_dict_child_search:
309  *
310  * Search where would be inserted @key between the childs of @p_elm.
311  * 
312  * Returns position of the child having a common prefix with this key        
313  * If *match==0, no child have a common prefix                               
314  *               *pos is where to add the key                                
315  * If *match==1, A child (located at *pos) have exactly this key             
316  * If *match==2, A child (located at *pos) constitutes a prefix of the key   
317  *               the recursion have to go on that guy                        
318  *               *prefix = the size of the key eaten at this level           
319  * If *match==3  The key is a prefix of the child at *pos                    
320  * If *match==4, A child (loc. at *pos) share a common prefix with this key  
321  *               *prefix = size of the prefix.                               
322  *               If searching, that's a mismatch.                            
323  *               If inserting, you have to break the child and create an     
324  *                 internal node having {child, key} as childs               
325  * offset is used in input and output. In input, that's the length of the key
326  *  handled by previous levels of recursion. In output, that the one counting
327  *  also this level.                                                         
328  */
329 static _XBT_INLINE
330 void
331 _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
332                           const char  *key,
333                           int          key_len,
334                           int         *p_pos,
335                           int         *p_offset,
336                           int         *p_match) {
337
338   int          p       = 0;
339   int          o       = *p_offset;
340   int          m       = 0;
341   int          len     = 0;
342
343   
344   CDEBUG6(dict_search, "search child [%.*s] under [%.*s]=%p (len=%lu)",
345           key_len, key,
346           p_elm ? (p_elm->key_len?p_elm->key_len:6) : 6, 
347           p_elm ? PRINTF_STR(p_elm->key) : "(head)",
348           p_elm,
349           (p_elm&&p_elm->sub) ? xbt_dynar_length(p_elm->sub) : 0);
350   
351
352   len = xbt_dynar_length(p_elm->sub);
353
354   {
355     int p_min = 0;
356     int p_max = len-1;
357     int cmp = 0;
358     
359     p = p_min;
360     if(len==0) {
361       p=0;
362     } else {
363       _dict_child_cmp(p_elm, p_min, key, key_len, &o, &m, &cmp);
364       if(!m) { /* OK, maybe it is somewhere else. */
365         o = *p_offset;
366         if (cmp<0) { /* Insert at the very beginning */
367           p=0;
368         } else if (p_max<=0) { /* No way. It is not there. Insert at the very end */
369           p=p_max+1;
370           m = 0;
371         } else { 
372           p=p_max;
373           _dict_child_cmp(p_elm, p_max, key, key_len, &o, &m, &cmp);
374           if(!m) {
375             o = *p_offset;
376             if(cmp>0) { /* Should be located at the end of the table */
377               p=p_max+1;
378             } else { /* Too bad, let's go for a dichotomic search. */
379               while(p_max-p_min>1) {
380                 _dict_child_cmp(p_elm, (p_min+p_max)/2, key, key_len, &o, &m, &cmp);
381                 if(m) break;
382                 o = *p_offset;
383                 if(cmp<0) p_max=(p_min+p_max)/2;
384                 if(cmp>0) p_min=(p_min+p_max)/2;
385               } 
386               if(m) /* We have the element */
387                 p=(p_min+p_max)/2 ;
388               else /* it should be inserted just after p_min */
389                 p=p_min + 1;
390             }
391           } 
392         }
393       }
394     }
395   }
396
397   *p_offset = o;
398   *p_pos    = p;
399   *p_match  = m;
400   CDEBUG6(dict_search, "search [%.*s] in [%.*s]=%p => %s",
401           key_len, key,
402           p_elm?(p_elm->key_len?p_elm->key_len:6):6, p_elm?PRINTF_STR(p_elm->key):"(head)",
403           p_elm,
404           ( m == 0 ? "no child have a common prefix" :
405             ( m == 1 ? "selected child have exactly this key" :
406               ( m == 2 ? "selected child constitutes a prefix" :
407                 ( m == 3 ? "key is a prefix of selected child" :
408                   (m == 4 ? "selected child share a prefix" :
409                    "internal error")))))
410           );  
411 }
412
413 /**
414  * _xbt_dictelm_change_value:
415  *
416  * Change the value of the dictelm, making sure to free the old one, if any. The node also become a non-internal one.
417  */
418 static _XBT_INLINE
419 void
420 _xbt_dictelm_change_value(s_xbt_dictelm_t    *p_elm,
421                            void           *data,
422                            void_f_pvoid_t *free_f) {
423
424   if (p_elm->content && p_elm->free_f) {
425     p_elm->free_f(p_elm->content);
426   }
427
428   p_elm->free_f = free_f;
429   p_elm->content  = data;
430   p_elm->internal = FALSE;
431 }
432
433 /**
434  * _xbt_dictelm_set_rec:
435  *
436  * @head: the head of the dict
437  * @key: the key to set the new data
438  * @offset: offset on key.
439  * @data: the data to add in the dict
440  *
441  * set the @data in the structure under the @key. The @key is destroyed
442  * in the process. Think to strdup it before.
443  *
444  * This is a helper function to xbt_dict_set which locks the struct and
445  * strdup the key before action. 
446  */
447 void
448 _xbt_dictelm_set_rec(s_xbt_dictelm_t     *p_head,
449                          char            *key,
450                          int              key_len,
451                          int              offset,
452                          void            *data,
453                          void_f_pvoid_t  *free_f) {
454   int          match      = 0;
455   int          pos        = 0;
456   const int    old_offset = offset;
457
458   CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
459           key_len, key, 
460           ((p_head && p_head->key) ? p_head->key_len : 6),
461           ((p_head && p_head->key) ? p_head->key : "(head)"), 
462           offset, (void*)p_head);
463
464   /*** The trivial cases first ***/
465
466   /* there is no key (we did enough recursion), change the value of head */
467   if (offset >= key_len) {
468
469     CDEBUG0(dict_add, "--> Change the value of head");
470
471     _xbt_dictelm_change_value(p_head, data, free_f);
472     free(key); /* Keep the key used in the tree */
473
474     return;
475   }
476
477   /*** Search where to add this child, and how ***/
478   _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
479
480   CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
481           pos, offset, match);
482
483   switch (match) {
484
485   case 0: /* no child have a common prefix */
486     {
487       s_xbt_dictelm_t *p_child = NULL;
488
489       _xbt_dictelm_alloc(key, key_len, offset, FALSE, data, free_f, &p_child);
490       CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
491       xbt_dynar_insert_at(p_head->sub, pos, &p_child);
492
493       return;
494     }
495
496   case 1: /* A child have exactly this key => change its value*/
497     {
498       s_xbt_dictelm_t *p_child = NULL;
499
500       p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
501       CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
502       _xbt_dictelm_change_value(p_child, data, free_f);
503
504       free(key);
505
506       return;
507     }
508
509   case 2: /* A child constitutes a prefix of the key => recurse */
510     {
511       s_xbt_dictelm_t *p_child = NULL;
512
513       p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
514       CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
515
516       _xbt_dictelm_set_rec(p_child, key, key_len, 
517                             offset, data, free_f);
518       return;
519     }
520
521   case 3: /* The key is a prefix of the child => child becomes child of p_new */
522     {
523       s_xbt_dictelm_t *p_new   = NULL;
524       s_xbt_dictelm_t *p_child = NULL;
525
526       p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
527       _xbt_dictelm_alloc(key, key_len, old_offset, FALSE, data, free_f, &p_new);
528
529       CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
530               (void*)p_child, (void*)p_new);
531
532       xbt_dynar_push(p_new->sub, &p_child);
533       p_child->offset = offset;
534       xbt_dynar_set(p_head->sub, pos, &p_new);
535
536       return;
537     }
538
539   case 4: /* A child share a common prefix with this key => Common ancestor */
540     {
541       s_xbt_dictelm_t *p_new       = NULL;
542       s_xbt_dictelm_t *p_child     = NULL;
543       s_xbt_dictelm_t *p_anc       = NULL;
544       char        *anc_key     = NULL;
545       int          anc_key_len = offset;
546
547       _xbt_dictelm_alloc(key, key_len, offset, FALSE, data, free_f, &p_new);
548       p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
549
550       anc_key = xbt_memdup(key, anc_key_len);
551
552       _xbt_dictelm_alloc(anc_key, anc_key_len, old_offset, TRUE, NULL, NULL, &p_anc);
553
554       CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
555               (void*)p_anc, anc_key_len, anc_key);
556
557       if (key[offset] < p_child->key[offset]) {
558         xbt_dynar_push(p_anc->sub, &p_new);
559         xbt_dynar_push(p_anc->sub, &p_child);
560       } else {
561         xbt_dynar_push(p_anc->sub, &p_child);
562         xbt_dynar_push(p_anc->sub, &p_new);
563       }
564
565       p_child->offset = offset;
566
567       xbt_dynar_set(p_head->sub, pos, &p_anc);
568
569       return;
570     }
571
572   default:
573     THROW_IMPOSSIBLE;
574   }
575 }
576
577 /**
578  * xbt_dictelm_set_ext:
579  *
580  * @head: the head of the dict
581  * @key: the key to set the new data
582  * @data: the data to add in the dict
583  *
584  * set the @data in the structure under the @key, which can be any kind 
585  * of data, as long as its length is provided in @key_len.
586  */
587 void
588 xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
589                         const char      *_key,
590                         int              key_len,
591                         void            *data,
592                         void_f_pvoid_t  *free_f) {
593   s_xbt_dictelm_t  *p_head  = *pp_head;
594   char         *key     =  NULL;
595
596   key = xbt_memdup(_key, key_len+1);
597
598   /* there is no head, create it */
599   if (!p_head) {
600     s_xbt_dictelm_t *p_child = NULL;
601
602     CDEBUG0(dict_add, "Create an head");
603
604     /* The head is priviledged by being the only one with a NULL key */
605     _xbt_dictelm_alloc(NULL, 0, 0, TRUE, NULL, NULL, &p_head);
606
607     CDEBUG2(dict_add, "Push %.*s as child of this head",key_len,key);
608     _xbt_dictelm_alloc(key, key_len, 0, FALSE, data, free_f, &p_child);
609     xbt_dynar_insert_at(p_head->sub, 0, &p_child);
610
611     *pp_head = p_head;
612
613     return;
614   }
615
616   _xbt_dictelm_set_rec(p_head, key, key_len, 0, data, free_f);
617 }
618
619 /**
620  * xbt_dictelm_set:
621  *
622  * @head: the head of the dict
623  * @key: the key to set the new data
624  * @data: the data to add in the dict
625  *
626  * set the @data in the structure under the @key, which is a 
627  * null terminated string.
628  */
629 void
630 xbt_dictelm_set(s_xbt_dictelm_t **pp_head,
631                     const char      *_key,
632                     void            *data,
633                     void_f_pvoid_t  *free_f) {
634
635   xbt_dictelm_set_ext(pp_head, _key, strlen(_key), data, free_f);
636 }
637
638 /**
639  * _xbt_dict_get_rec:
640  *
641  * @head: the head of the dict
642  * @key: the key to find data
643  * @offset: offset on the key
644  * @data: the data that we are looking for
645  *
646  * Search the given @key. Throws not_found_error when not found.
647  */
648 static 
649 void *
650 _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
651                       const char     *key,
652                       int             key_len,
653                       int             offset) {
654
655   CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head); 
656
657   /*** The trivial case first ***/
658
659   /* we did enough recursion, we're done */
660   if (offset >= key_len) {
661     return p_head->content;
662   }
663
664   {
665     int match = 0;
666     int pos   = 0;
667
668     /*** Search where is the good child, and how good it is ***/
669     _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
670
671     switch (match) {
672
673     case 0: /* no child have a common prefix */
674       THROW1(not_found_error,0,"key '%s' not found",key);
675
676     case 1: /* A child have exactly this key => Got it */
677       {
678         s_xbt_dictelm_t *p_child = NULL;
679         p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
680         return p_child->content;
681       }
682
683     case 2: /* A child constitutes a prefix of the key => recurse */
684       {
685         s_xbt_dictelm_t *p_child = NULL;
686
687         p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
688
689         return _xbt_dictelm_get_rec(p_child, key, key_len, offset);
690       }
691
692     case 3: /* The key is a prefix of the child => not found */
693       THROW1(not_found_error,0,"key %s not found",key);
694
695     case 4: /* A child share a common prefix with this key => not found */
696       THROW1(not_found_error,0,"key %s not found",key);
697
698     default:
699       THROW_IMPOSSIBLE;
700     }
701   }
702 }
703
704 /**
705  * xbt_dictelm_get_ext:
706  *
707  * @head: the head of the dict
708  * @key: the key to find data
709  * @data: the data that we are looking for
710  *
711  * Search the given @key. Throws not_found_error when not found.
712  */
713 void *
714 xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
715                     const char      *key,
716                     int              key_len) {
717   /* there is no head, go to hell */
718   if (!p_head)
719     THROW2(not_found_error,0,"Key '%*s' not found in dict",key_len,key);
720
721   return _xbt_dictelm_get_rec(p_head, key, key_len, 0);
722 }
723
724 /**
725  * xbt_dictelm_get:
726  *
727  * @head: the head of the dict
728  * @key: the key to find data
729  * @data: the data that we are looking for
730  *
731  * Search the given @key. Throws not_found_error when not found.
732  */
733 void *
734 xbt_dictelm_get(s_xbt_dictelm_t *p_head,
735                 const char      *key) {
736
737   return xbt_dictelm_get_ext(p_head, key, strlen(key));
738 }
739
740 /*----[ _xbt_dict_collapse ]------------------------------------------------*/
741 static _XBT_INLINE
742 void
743 _collapse_if_need(xbt_dictelm_t head,
744                   int            pos,
745                   int            offset) {
746   xbt_dictelm_t child = NULL;
747
748   CDEBUG2(dict_collapse, "Collapse %d of %p... ", pos, (void*)head);
749
750   if (pos >= 0) {
751     /* Remove the child if |it's key| == 0 (meaning it's dead) */
752     child = xbt_dynar_get_as(head->sub, pos, xbt_dictelm_t);
753
754     if (offset >= child->key_len) {
755
756       xbt_assert0(xbt_dynar_length(child->sub) == 0,
757                    "Found a dead child with grand childs. Internal error");
758
759       CDEBUG1(dict_collapse, "Remove dead child %p.... ", (void*)child);
760       xbt_dynar_remove_at(head->sub, pos, &child);
761       xbt_dictelm_free(&child);
762     }
763   }
764
765   if (!head->key) {
766     CDEBUG0(dict_collapse, "Do not collapse the head, you stupid programm");
767     return;
768   }
769
770   if (head->content || head->free_f ||
771       xbt_dynar_length(head->sub) != 1) {
772     CDEBUG0(dict_collapse, "Cannot collapse");
773     return; /* cannot collapse */
774   }
775
776   child = xbt_dynar_get_as(head->sub, 0, xbt_dictelm_t);
777
778   /* Get the child's key as new key */
779   CDEBUG2(dict_collapse,
780           "Do collapse with only child %.*s", child->key_len, child->key);
781
782   head->content  = child->content;
783   head->free_f = child->free_f;
784   free(head->key);
785   head->key      = child->key;
786   head->key_len  = child->key_len;
787
788   xbt_dynar_free_container(&(head->sub)) ;
789
790   head->sub = child->sub;
791   free(child);
792 }
793
794 /**
795  * _xbt_dict_remove_rec:
796  *
797  * @head: the head of the dict
798  * @key: the key of the data to be removed
799  * @offset: offset on the key
800  *
801  * Remove the entry associated with the given @key. Throws not_found_error.
802  */
803 void
804 _xbt_dictelm_remove_rec(xbt_dictelm_t head,
805                          const char  *key,
806                          int          key_len,
807                          int          offset) {
808
809   /* there is no key to search, we did enough recursion => kill current */
810   if (offset >= key_len) {
811     int killme = 0; /* do I need to suicide me ? */
812
813     if (head->content && head->free_f) {
814       head->free_f(head->content);
815     }
816
817     killme = !xbt_dynar_length(head->sub);
818     head->content  = NULL;
819     head->free_f = NULL;
820     _collapse_if_need(head, -1, offset);
821
822     if (killme) {
823       DEBUG0("kill this node");
824       head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
825     }
826
827     return;
828
829   } else {
830     int match      =      0;
831     int pos        =      0;
832     int old_offset = offset;
833
834     /*** Search where is the good child, and how good it is ***/
835     _xbt_dictelm_child_search(head, key, key_len, &pos, &offset, &match);
836
837     switch (match) {
838
839     case 1: /* A child have exactly this key           => recurse */
840     case 2: /* A child constitutes a prefix of the key => recurse */
841
842       {
843         s_xbt_dictelm_t *p_child = NULL;
844
845         p_child = xbt_dynar_get_as(head->sub, pos, s_xbt_dictelm_t*);
846         /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
847           pos, (void*)p_child, key+offset, key_len-offset,offset);*/
848         _xbt_dictelm_remove_rec(p_child, key, key_len, offset);
849
850         _collapse_if_need(head, pos, old_offset);
851         return;
852       }
853
854
855     case 0: /* no child have a common prefix */
856     case 3: /* The key is a prefix of the child => not found */
857     case 4: /* A child share a common prefix with this key => not found */
858
859       THROW2(not_found_error,0,"Unable to remove key '%*s': not found",key_len,key);
860
861     default:
862       THROW_IMPOSSIBLE;
863
864     }
865   }
866 }
867
868 /**
869  * xbt_dictelm_remove_ext:
870  *
871  * @head: the head of the dict
872  * @key: the key of the data to be removed
873  *
874  * Remove the entry associated with the given @key. Throws not_found_err
875  */
876 void
877 xbt_dictelm_remove_ext(xbt_dictelm_t head,
878                         const char  *key,
879                         int          key_len) {
880   /* there is no head, go to hell */
881   if (!head) 
882     THROW1(arg_error,0,"Asked to remove key %s from NULL dict",key);
883   
884   _xbt_dictelm_remove_rec(head, key, key_len, 0);
885 }
886
887 /**
888  * xbt_dictelm_remove:
889  *
890  * @head: the head of the dict
891  * @key: the key of the data to be removed
892  *
893  * Throws not_found when applicable
894  */
895 void
896 xbt_dictelm_remove(xbt_dictelm_t head,
897                     const char     *key) {
898   _xbt_dictelm_remove_rec(head, key, strlen(key),0);
899 }
900
901 /*----[ _xbt_dict_dump_rec ]------------------------------------------------*/
902 /* private function to do the job of xbt_dict_dump recursively              */
903 /*---------------------------------------------------------------------------*/
904 static
905 void
906 _xbt_dictelm_dump_rec(xbt_dictelm_t  head,
907                       int             offset,
908                       void_f_pvoid_t *output) {
909   xbt_dictelm_t child   =     NULL;
910   char          *key     =     NULL;
911   int            key_len =        0;
912   int            i       =        0;
913
914   if (!head)
915     return;
916
917   printf("[%p] ", (void*)head);
918
919   key     = head->key;
920   key_len = head->key_len;
921
922   if (key_len)
923     printf ("  ");
924
925   for (i = 0; i < offset; i++)
926     printf("-");
927
928   fflush(stdout);
929
930   if (key) {
931      
932      if (!key_len) {
933         printf ("HEAD");
934      } else if (key[key_len] != '\0') {
935         char *key_string = NULL;
936         
937         key_string = xbt_malloc(key_len*2+1);
938         _xbt_bytes_to_string(key, key_len, key_string);
939         
940         printf("%.*s|(%d)", key_len-2*offset, key_string + 2*offset, offset);
941         
942         free(key_string);
943      } else {
944         printf("%.*s|(%d)", key_len-offset, key + offset , offset);
945      }
946   }
947
948   printf(" -> ");
949
950   if (head->content) {
951
952     if (output) {
953       output(head->content);
954     } else {
955       printf("(data)");
956     }
957
958   } else if (head->internal) {
959     printf("(internal node)");
960   } else {
961     printf("(null)");
962   }
963
964   printf("    \t\t\t[ %lu child(s) ]\n", xbt_dynar_length(head->sub));
965
966   xbt_dynar_foreach(head->sub, i, child) 
967     _xbt_dictelm_dump_rec(child, child->offset, output);
968
969 }
970
971 /**
972  * xbt_dictelm_dump:
973  *
974  * @head: the head of the dict
975  * @output: a function to dump each data in the tree
976  *
977  * Ouputs the content of the structure. (for debuging purpose). @ouput is a
978  * function to output the data. If NULL, data won't be displayed.
979  */
980
981 void
982 xbt_dictelm_dump(xbt_dictelm_t  head,
983                   void_f_pvoid_t *output) {
984   _xbt_dictelm_dump_rec(head, 0, output);
985 }
986
987 /**
988  * xbt_dictelm_print_fct:
989  *
990  * @data:
991  *
992  * To dump multidict, this function dumps a dict
993  */
994
995 void
996 xbt_dictelm_print_fct(void *data) {
997   printf("tree %p", (void*)data);
998 }
999