Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Propagate file renaming to windows
[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         int rv;
80         
81         if(!(*dictionaryptr))
82                 return EINVAL;
83         
84         if((rv = htable_free(&((*dictionaryptr)->htable))))
85                 return rv;
86         
87         free(*dictionaryptr);
88         *dictionaryptr = NULL;
89         
90         return 0;
91 }
92
93 int
94 dictionary_clear(dictionary_t dictionary)
95 {
96         if(!dictionary)
97                 return EINVAL;
98         
99         return htable_clear(dictionary->htable);
100 }
101
102 int
103 dictionary_get_size(dictionary_t dictionary)
104 {
105         if(!dictionary)
106         {
107                 errno = EINVAL;
108                 return -1;
109         }
110         
111         return htable_get_size(dictionary->htable);
112 }
113
114 int
115 dictionary_is_empty(dictionary_t dictionary)
116 {
117         if(!dictionary)
118         {
119                 errno = EINVAL;
120                 return 0;
121         }
122         
123         return htable_is_empty(dictionary->htable);     
124 }
125
126 void*
127 dictionary_remove(dictionary_t dictionary,const char* key)
128 {
129         if(!dictionary)
130         {
131                 errno = EINVAL;
132                 return NULL;
133         }
134         
135         return htable_remove(dictionary->htable,key);   
136 }
137
138