Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh version 2
[simgrid.git] / tools / tesh2 / src / dictionary.c
1
2 #include <dictionary.h>
3 #include <htable.h>
4
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #define __DEFAULT_BLOCK_CAPACITY        ((int)512)
10 #define __DEFAULT_TABLE_SIZE            ((int)256)
11
12 static unsigned int 
13 hfunc(const char* key) 
14 {
15   unsigned int hval = 5381;
16   int ch;
17   
18   while ( (ch = *key++) ) 
19     hval = ((hval << 5) + hval) + ch;
20   
21   return hval;
22 }
23
24 static int 
25 cmp_key(const char* key1, const char* key2)
26 {
27         return !strcmp(key1,key2);
28 }
29
30
31 dictionary_t
32 dictionary_new(fn_finalize_t fn_finalize)
33 {
34         dictionary_t dictionary;
35                 
36         if(!(dictionary = (dictionary_t)calloc(1,sizeof(s_dictionary_t))))
37                 return NULL;
38         
39         if(!(dictionary->htable = htable_new(
40                                                         __DEFAULT_BLOCK_CAPACITY,
41                                                         __DEFAULT_TABLE_SIZE,
42                                                         (fn_hfunc_t)hfunc,
43                                                         (fn_cmp_key_t)cmp_key,
44                                                         fn_finalize)))
45         {
46                 free(dictionary);
47                 return NULL;
48         }
49         
50         return dictionary;
51 }
52
53 int
54 dictionary_set(dictionary_t dictionary,const char* key, const void* val)
55 {
56         if(!dictionary || !key || !val)
57                 return EINVAL;
58         
59         return htable_set(dictionary->htable,(const void*)key,val);
60 }
61
62 void*
63 dictionary_get(dictionary_t dictionary, const char* key)
64 {
65         
66         if(!dictionary || !key)
67         {
68                 errno = EINVAL;
69                 return NULL;
70         }
71         
72         return htable_lookup(dictionary->htable,(const void*)key);
73 }
74
75
76 int
77 dictionary_free(dictionary_t* dictionaryptr)
78 {
79         if(!(*dictionaryptr))
80                 return EINVAL;
81         
82         if((errno = htable_free(&((*dictionaryptr)->htable))))
83                 return errno;
84         
85         free(*dictionaryptr);
86         *dictionaryptr = NULL;
87         
88         return 0;
89 }
90
91 int
92 dictionary_clear(dictionary_t dictionary)
93 {
94         if(!dictionary)
95                 return EINVAL;
96         
97         return htable_clear(dictionary->htable);
98 }
99
100 int
101 dictionary_get_size(dictionary_t dictionary)
102 {
103         if(!dictionary)
104         {
105                 errno = EINVAL;
106                 return -1;
107         }
108         
109         return htable_get_size(dictionary->htable);
110 }
111
112 int
113 dictionary_is_empty(dictionary_t dictionary)
114 {
115         if(!dictionary)
116         {
117                 errno = EINVAL;
118                 return 0;
119         }
120         
121         return htable_is_empty(dictionary->htable);     
122 }
123
124 void*
125 dictionary_remove(dictionary_t dictionary,const char* key)
126 {
127         if(!dictionary)
128         {
129                 errno = EINVAL;
130                 return NULL;
131         }
132         
133         return htable_remove(dictionary->htable,key);   
134 }
135
136