hash_table.h

00001 /* 
00002  * Copyright (C) 2005 iptelorg GmbH
00003  *
00004  * This file is part of ser, a free SIP server.
00005  *
00006  * ser is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version
00010  *
00011  * For a license to use the ser software under conditions
00012  * other than those described here, or to purchase support for this
00013  * software, please contact iptel.org by e-mail at the following addresses:
00014  *    info@iptel.org
00015  *
00016  * ser is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  */
00025 
00026 #ifndef __HASH_TABLE_H
00027 #define __HASH_TABLE_H
00028 
00029 typedef struct ht_statistic {
00030         int find_cnt;
00032         int cmp_cnt;
00034         int nocmp_cnt;
00036         int missed_cnt;
00037 } ht_statistic_t;
00038 
00039 typedef const void* ht_key_t;
00040 typedef void* ht_data_t;
00041 
00042 typedef unsigned int (*hash_func_t)(ht_key_t k);
00043 typedef int (*key_cmp_func_t)(ht_key_t a, ht_key_t b);
00044 
00045 typedef struct ht_element {
00046         ht_key_t key;
00047         ht_data_t data;
00048         struct ht_element *next;
00049 } ht_element_t;
00050 
00051 typedef struct ht_cslot {
00052         ht_element_t *first;
00053         ht_element_t *last;
00054         int cnt;
00055 } ht_cslot_t;
00056 
00057 typedef struct hash_table {
00058         hash_func_t hash;
00059         key_cmp_func_t cmp;
00060         ht_cslot_t *cslots;
00061         int size;
00062 
00063         int find_cnt;
00064         int cmp_cnt;
00065         int nocmp_cnt;
00066         int missed_cnt;
00067 } hash_table_t;
00068 
00069 int ht_init(hash_table_t *ht, hash_func_t hash_func, key_cmp_func_t cmp_keys, int size);
00070 void ht_destroy(hash_table_t *ht);
00071 int ht_add(hash_table_t *ht, ht_key_t key, ht_data_t data);
00072 ht_data_t ht_remove(hash_table_t *ht, ht_key_t key);
00073 ht_data_t ht_find(hash_table_t *ht, ht_key_t key);
00074 void ht_get_statistic(hash_table_t *ht, ht_statistic_t *s);
00075 void ht_clear_statistic(hash_table_t *ht);
00076 
00077 /* traversing through whole hash table */
00078 typedef struct {
00079         hash_table_t *ht;
00080         int slot_pos;
00081         ht_element_t *current;
00082 } ht_traversal_info_t;
00083 
00084 ht_element_t *get_first_ht_element(hash_table_t *ht, ht_traversal_info_t *info);
00085 ht_element_t *get_next_ht_element(ht_traversal_info_t *info);
00086 
00087 /* hash functions */
00088 unsigned int rshash(const char* str, unsigned int len);
00089 
00090 #endif