Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

dict.c File Reference

#include "gwlib.h"

Include dependency graph for dict.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  Item
struct  Dict

Typedefs

typedef Item Item

Functions

Itemitem_create (Octstr *key, void *value)
void item_destroy (void *item)
int item_has_key (void *item, void *key)
void lock (Dict *dict)
void unlock (Dict *dict)
long key_to_index (Dict *dict, Octstr *key)
int handle_null_value (Dict *dict, Octstr *key, void *value)
int dict_put_true (Dict *dict, Octstr *key, void *value)
Dictdict_create (long size_hint, void(*destroy_value)(void *))
void dict_destroy (Dict *dict)
void dict_put (Dict *dict, Octstr *key, void *value)
int dict_put_once (Dict *dict, Octstr *key, void *value)
void * dict_get (Dict *dict, Octstr *key)
void * dict_remove (Dict *dict, Octstr *key)
long dict_key_count (Dict *dict)
Listdict_keys (Dict *dict)


Typedef Documentation

typedef struct Item Item
 

Definition at line 74 of file dict.c.

Referenced by dict_destroy(), dict_get(), dict_keys(), dict_put(), dict_put_true(), dict_remove(), item_create(), item_destroy(), and item_has_key().


Function Documentation

Dict* dict_create long  size_hint,
void(*)(void *)  destroy_value
 

Definition at line 192 of file dict.c.

References mutex_create, Dict::size, Dict::tab, and List::tab.

Referenced by brunet_parse_body(), cfg_create(), clickatell_parse_body(), conn_pool_init(), create_group(), init_concat_handler(), init_reroute(), main(), parse_value_element(), port_init(), pushusers_create(), radius_acct_init(), run_smsbox(), server(), smasi_create(), smpp_create(), smsbox_start(), store_file_init(), urltrans_create(), wap_map_add_user(), wap_push_ppg_init(), wap_push_ppg_pushuser_list_add(), wml_init(), and xmlrpc_create_struct_value().

00193 {
00194     Dict *dict;
00195     long i;
00196     
00197     dict = gw_malloc(sizeof(*dict));
00198 
00199     /*
00200      * Hash tables tend to work well until they are fill to about 50%.
00201      */
00202     dict->size = size_hint * 2;
00203 
00204     dict->tab = gw_malloc(sizeof(dict->tab[0]) * dict->size);
00205     for (i = 0; i < dict->size; ++i)
00206         dict->tab[i] = NULL;
00207     dict->lock = mutex_create();
00208     dict->destroy_value = destroy_value;
00209     dict->key_count = 0;
00210     
00211     return dict;
00212 }

void dict_destroy Dict dict  ) 
 

Definition at line 215 of file dict.c.

References Dict::destroy_value, gwlist_destroy(), gwlist_extract_first(), Item, item_destroy(), Dict::lock, mutex_destroy(), Dict::size, Dict::tab, and Item::value.

Referenced by brunet_parse_reply(), cfg_destroy(), clickatell_parse_reply(), conn_pool_shutdown(), destroy_group(), main(), port_shutdown(), radius_acct_shutdown(), run_smsbox(), shutdown_concat_handler(), smasi_destroy(), smpp_destroy(), smsboxc_run(), smscconn_destroy(), store_dumper(), urltrans_destroy(), wap_map_user_destroy(), wap_push_ppg_pushuser_list_destroy(), wap_push_ppg_shutdown(), wml_shutdown(), and xmlrpc_value_destroy().

00216 {
00217     long i;
00218     Item *p;
00219     
00220     if (dict == NULL)
00221         return;
00222 
00223     for (i = 0; i < dict->size; ++i) {
00224         if (dict->tab[i] == NULL)
00225         continue;
00226 
00227     while ((p = gwlist_extract_first(dict->tab[i])) != NULL) {
00228         if (dict->destroy_value != NULL)
00229             dict->destroy_value(p->value);
00230         item_destroy(p);
00231     }
00232     gwlist_destroy(dict->tab[i], NULL);
00233     }
00234     mutex_destroy(dict->lock);
00235     gw_free(dict->tab);
00236     gw_free(dict);
00237 }

Here is the call graph for this function:

void* dict_get Dict dict,
Octstr key
 

Definition at line 286 of file dict.c.

References gwlist_search(), Item, item_has_key(), key_to_index(), lock, Dict::tab, unlock, and Item::value.

Referenced by add_group(), brunet_parse_reply(), cfg_get_multi_group(), cfg_get_real(), cfg_get_single_group(), check_concatenation(), check_pool_conn(), clear_old_concat_parts(), clickatell_parse_reply(), conn_pool_get(), conn_pool_put(), do_dump(), do_queue_cleanup(), main(), oneuser_add(), parse_attribute(), parse_element(), port_add(), port_get_request(), port_put_request(), radius_acct_get_msisdn(), radius_get_attribute(), route_incoming_to_boxc(), route_incoming_to_smsc(), send_push_response(), store_file_status(), update_table(), update_tables(), urltrans_add_one(), urltrans_find_service(), user_find_by_username(), wap_map_user(), wap_push_ppg_pushuser_authenticate(), xmlrpc_get_member(), and xmlrpc_print_struct().

00287 {
00288     long i;
00289     Item *p;
00290     void *value;
00291 
00292     lock(dict);
00293     i = key_to_index(dict, key);
00294     if (dict->tab[i] == NULL)
00295     p = NULL;
00296     else
00297         p = gwlist_search(dict->tab[i], key, item_has_key);
00298     if (p == NULL)
00299         value = NULL;
00300     else
00301         value = p->value;
00302     unlock(dict);
00303     return value;
00304 }

Here is the call graph for this function:

long dict_key_count Dict dict  ) 
 

Definition at line 335 of file dict.c.

References Dict::key_count, lock, result, and unlock.

Referenced by boxc_status(), main(), proxy_thread(), run_smsbox(), server(), store_file_dump(), store_file_load(), store_file_messages(), xmlrpc_count_members(), and xmlrpc_print_struct().

00336 {
00337     long result;
00338 
00339     lock(dict);
00340     result = dict->key_count;
00341     unlock(dict);
00342 
00343     return result;
00344 }

List* dict_keys Dict dict  ) 
 

Definition at line 347 of file dict.c.

References gwlist_append(), gwlist_create, gwlist_get(), gwlist_len(), Item, Item::key, lock, octstr_duplicate, Dict::size, Dict::tab, and unlock.

Referenced by add_group(), cfg_dump(), clear_old_concat_parts(), do_dump(), do_queue_cleanup(), grp_dump(), io_thread(), run_smsbox(), store_file_load(), store_file_status(), and xmlrpc_print_struct().

00348 {
00349     List *list;
00350     Item *item;
00351     long i, j;
00352     
00353     list = gwlist_create();
00354 
00355     lock(dict);
00356     for (i = 0; i < dict->size; ++i) {
00357     if (dict->tab[i] == NULL)
00358         continue;
00359     for (j = 0; j < gwlist_len(dict->tab[i]); ++j) {
00360         item = gwlist_get(dict->tab[i], j);
00361         gwlist_append(list, octstr_duplicate(item->key));
00362     }
00363     }
00364     unlock(dict);
00365     
00366     return list;
00367 }

Here is the call graph for this function:

void dict_put Dict dict,
Octstr key,
void *  value
 

Definition at line 240 of file dict.c.

References Dict::destroy_value, dict_remove(), gwlist_append(), gwlist_create, gwlist_search(), Item, item_create(), item_has_key(), Dict::key_count, key_to_index(), lock, Dict::tab, unlock, and Item::value.

Referenced by add_group(), attr_dict_construct(), boxc_receiver(), boxc_sent_push(), brunet_parse_body(), cfg_set(), check_concatenation(), clear_old_concat_parts(), clickatell_parse_body(), conn_pool_put(), main(), oneuser_add(), pap_request_thread(), port_add(), send_messages(), smsbox_req_handle(), smsbox_req_sendota(), smsbox_sendota_post(), store_to_dict(), update_table(), update_tables(), urltrans_add_one(), wap_map_add_user(), wap_push_ppg_pushuser_authenticate(), and wml_init().

00241 {
00242     long i;
00243     Item *p;
00244 
00245     if (value == NULL) {
00246         value = dict_remove(dict, key);
00247     if (dict->destroy_value != NULL)
00248         dict->destroy_value(value);
00249         return;
00250     }
00251 
00252     lock(dict);
00253     i = key_to_index(dict, key);
00254     if (dict->tab[i] == NULL) {
00255     dict->tab[i] = gwlist_create();
00256     p = NULL;
00257     } else
00258     p = gwlist_search(dict->tab[i], key, item_has_key);
00259     if (p == NULL) {
00260         p = item_create(key, value);
00261     gwlist_append(dict->tab[i], p);
00262         dict->key_count++;
00263     } else {
00264     if (dict->destroy_value != NULL)
00265         dict->destroy_value(p->value);
00266     p->value = value;
00267     }
00268     unlock(dict);
00269 }

Here is the call graph for this function:

int dict_put_once Dict dict,
Octstr key,
void *  value
 

Definition at line 271 of file dict.c.

References dict_put_true(), and handle_null_value().

Referenced by init_reroute(), init_smsbox_routes(), pap_request_thread(), parse_struct_element(), and xmlrpc_add_member().

00272 {
00273     int ret;
00274 
00275     ret = 1;
00276     if (handle_null_value(dict, key, value))
00277         return 1;
00278     if (dict_put_true(dict, key, value)) {
00279         ret = 1;
00280     } else {
00281         ret = 0;
00282     }
00283     return ret;
00284 }

Here is the call graph for this function:

int dict_put_true Dict dict,
Octstr key,
void *  value
[static]
 

Definition at line 154 of file dict.c.

References Dict::destroy_value, gwlist_append(), gwlist_create, gwlist_search(), Item, item_create(), item_has_key(), Dict::key_count, key_to_index(), lock, Dict::tab, and unlock.

Referenced by dict_put_once().

00155 {
00156     Item *p;
00157     long i;
00158     int item_unique;
00159 
00160     item_unique = 0;
00161     lock(dict);
00162     i = key_to_index(dict, key);
00163 
00164     if (dict->tab[i] == NULL) {
00165     dict->tab[i] = gwlist_create();
00166     p = NULL;
00167     } else {
00168     p = gwlist_search(dict->tab[i], key, item_has_key);
00169     }
00170 
00171     if (p == NULL) {
00172         p = item_create(key, value);
00173     gwlist_append(dict->tab[i], p);
00174         dict->key_count++;
00175         item_unique = 1;
00176     } else {
00177         if (dict->destroy_value != NULL)
00178             dict->destroy_value(value);
00179         item_unique = 0;
00180     }
00181 
00182     unlock(dict);
00183 
00184     return item_unique;
00185 }

Here is the call graph for this function:

void* dict_remove Dict dict,
Octstr key
 

Definition at line 307 of file dict.c.

References gw_assert, gwlist_destroy(), gwlist_extract_matching(), gwlist_get(), gwlist_len(), Item, item_destroy(), item_has_key(), Dict::key_count, key_to_index(), lock, Dict::tab, unlock, and Item::value.

Referenced by boxc_receiver(), boxc_sent_pop(), clear_old_concat_parts(), delayed_http_reply(), dict_put(), do_queue_cleanup(), handle_null_value(), handle_pdu(), io_thread(), port_remove(), run_smsbox(), send_push_response(), smsbox_req_handle(), smsbox_req_sendota(), smsbox_sendota_post(), store_file_load(), store_to_dict(), update_table(), update_tables(), and wap_push_ppg_pushuser_authenticate().

00308 {
00309     long i;
00310     Item *p;
00311     void *value;
00312     List *list;
00313 
00314     lock(dict);
00315     i = key_to_index(dict, key);
00316     if (dict->tab[i] == NULL)
00317         list = NULL;
00318     else
00319         list = gwlist_extract_matching(dict->tab[i], key, item_has_key);
00320     gw_assert(list == NULL || gwlist_len(list) == 1);
00321     if (list == NULL)
00322         value = NULL;
00323     else {
00324     p = gwlist_get(list, 0);
00325     gwlist_destroy(list, NULL);
00326         value = p->value;
00327     item_destroy(p);
00328     dict->key_count--;
00329     }
00330     unlock(dict);
00331     return value;
00332 }

Here is the call graph for this function:

int handle_null_value Dict dict,
Octstr key,
void *  value
[static]
 

Definition at line 142 of file dict.c.

References Dict::destroy_value, and dict_remove().

Referenced by dict_put_once().

00143 {
00144     if (value == NULL) {
00145         value = dict_remove(dict, key);
00146     if (dict->destroy_value != NULL)
00147         dict->destroy_value(value);
00148         return 1;
00149     }
00150 
00151     return 0;
00152 }

Here is the call graph for this function:

Item* item_create Octstr key,
void *  value
[static]
 

Definition at line 81 of file dict.c.

References Item, Item::key, and octstr_duplicate.

Referenced by dict_put(), and dict_put_true().

00082 {
00083     Item *item;
00084     
00085     item = gw_malloc(sizeof(*item));
00086     item->key = octstr_duplicate(key);
00087     item->value = value;
00088     return item;
00089 }

void item_destroy void *  item  )  [static]
 

Definition at line 91 of file dict.c.

References Item, Item::key, and octstr_destroy().

Referenced by dict_destroy(), dict_remove(), and gw_prioqueue_destroy().

00092 {
00093     Item *p;
00094     
00095     p = item;
00096     octstr_destroy(p->key);
00097     gw_free(p);
00098 }

Here is the call graph for this function:

int item_has_key void *  item,
void *  key
[static]
 

Definition at line 101 of file dict.c.

References Item, and octstr_compare().

Referenced by dict_get(), dict_put(), dict_put_true(), and dict_remove().

00102 {
00103     return octstr_compare(key, ((Item *) item)->key) == 0;
00104 }

Here is the call graph for this function:

long key_to_index Dict dict,
Octstr key
[static]
 

Definition at line 137 of file dict.c.

References octstr_hash_key(), and Dict::size.

Referenced by dict_get(), dict_put(), dict_put_true(), and dict_remove().

00138 {
00139     return octstr_hash_key(key) % dict->size;
00140 }

Here is the call graph for this function:

void lock Dict dict  )  [static]
 

Definition at line 125 of file dict.c.

References Dict::lock, and mutex_lock.

00126 {
00127     mutex_lock(dict->lock);
00128 }

void unlock Dict dict  )  [static]
 

Definition at line 131 of file dict.c.

References Dict::lock, and mutex_unlock.

00132 {
00133     mutex_unlock(dict->lock);
00134 }

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.