Main Page | Class List | Directories | File List | Class Members | File Members

ght_hash_table.h

Go to the documentation of this file.
00001 /*-*-c-*- ************************************************************
00002  * Copyright (C) 2001-2005,  Simon Kagstrom
00003  *
00004  * Filename:      ght_hash_table.h.in
00005  * Description:   The definitions used in the hash table.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020  * 02111-1307, USA.
00021  *
00022  * $Id: ght_hash_table.h.in 5628 2005-11-27 07:57:45Z ska $
00023  *
00024  ********************************************************************/
00025 
00059 #ifndef GHT_HASH_TABLE_H
00060 #define GHT_HASH_TABLE_H
00061 
00062 #include <stdlib.h>                    /* size_t */
00063 
00064 #ifdef __cplusplus
00065 extern "C" {
00066 #endif
00067 
00068 #define GHT_HEURISTICS_NONE          0
00069 #define GHT_HEURISTICS_TRANSPOSE     1
00070 #define GHT_HEURISTICS_MOVE_TO_FRONT 2
00071 #define GHT_AUTOMATIC_REHASH         4
00072 
00073 #ifndef TRUE
00074 #define TRUE 1
00075 #endif
00076 
00077 #ifndef FALSE
00078 #define FALSE 0
00079 #endif
00080 
00082 typedef unsigned int ght_uint32_t;
00083 
00088 typedef struct s_hash_key
00089 {
00090   unsigned int i_size;       
00091   void *p_key;               
00092 } ght_hash_key_t;
00093 
00094 /*
00095  * The structure for hash entries.
00096  *
00097  * LOCK: Should be possible to do somewhat atomically
00098  */
00099 typedef struct s_hash_entry
00100 {
00101   void *p_data;
00102 
00103   struct s_hash_entry *p_next;
00104   struct s_hash_entry *p_prev;
00105   ght_hash_key_t key;
00106 } ght_hash_entry_t;
00107 
00108 /*
00109  * The structure used in iterations. You should not care about the
00110  * contents of this, it will be filled and updated by ght_first() and
00111  * ght_next().
00112  */
00113 typedef struct
00114 {
00115   int i_curr_bucket;         /* The current bucket */
00116   ght_hash_entry_t *p_entry; /* The current entry */
00117   ght_hash_entry_t *p_next;  /* The next entry */
00118 } ght_iterator_t;
00119 
00133 typedef ght_uint32_t (*ght_fn_hash_t)(ght_hash_key_t *p_key);
00134 
00145 typedef void *(*ght_fn_alloc_t)(size_t size);
00146 
00153 typedef void (*ght_fn_free_t)(void *ptr);
00154 
00158 typedef void (*ght_fn_bucket_free_callback_t)(void *data, void *key);
00159 
00163 typedef struct
00164 {
00165   unsigned int i_items;              
00166   unsigned int i_size;               
00167   ght_fn_hash_t fn_hash;             
00168   ght_fn_alloc_t fn_alloc;           
00169   ght_fn_free_t fn_free;             
00170   ght_fn_bucket_free_callback_t fn_bucket_free; 
00171   int i_heuristics;                  
00172   int i_automatic_rehash;            
00174   /* private: */
00175   ght_hash_entry_t **pp_entries;
00176   int *p_nr;                         /* The number of entries in each bucket */
00177   int i_size_mask;                   /* The number of bits used in the size */
00178   unsigned int bucket_limit;
00179 } ght_hash_table_t;
00180 
00200 ght_hash_table_t *ght_create(unsigned int i_size);
00201 
00223 void ght_set_alloc(ght_hash_table_t *p_ht, ght_fn_alloc_t fn_alloc, ght_fn_free_t fn_free);
00224 
00235 void ght_set_hash(ght_hash_table_t *p_ht, ght_fn_hash_t fn_hash);
00236 
00252 void ght_set_heuristics(ght_hash_table_t *p_ht, int i_heuristics);
00253 
00268 void ght_set_rehash(ght_hash_table_t *p_ht, int b_rehash);
00269 
00290 void ght_set_bounded_buckets(ght_hash_table_t *p_ht, unsigned int limit, ght_fn_bucket_free_callback_t fn);
00291 
00292 
00300 unsigned int ght_size(ght_hash_table_t *p_ht);
00301 
00309 unsigned int ght_table_size(ght_hash_table_t *p_ht);
00310 
00311 
00346 int ght_insert(ght_hash_table_t *p_ht,
00347                void *p_entry_data,
00348                unsigned int i_key_size, void *p_key_data);
00349 
00362 void *ght_replace(ght_hash_table_t *p_ht,
00363                   void *p_entry_data,
00364                   unsigned int i_key_size, void *p_key_data);
00365 
00366 
00377 void *ght_get(ght_hash_table_t *p_ht,
00378               unsigned int i_key_size, void *p_key_data);
00379 
00390 void *ght_remove(ght_hash_table_t *p_ht,
00391                  unsigned int i_key_size, void *p_key_data);
00392 
00434 void *ght_first(ght_hash_table_t *p_ht, ght_iterator_t *p_iterator, void **pp_key);
00435 
00455 void *ght_next(ght_hash_table_t *p_ht, ght_iterator_t *p_iterator, void **pp_key);
00456 
00473 void ght_rehash(ght_hash_table_t *p_ht, unsigned int i_size);
00474 
00497 void ght_finalize(ght_hash_table_t *p_ht);
00498 
00499 /* exported hash functions */
00500 
00513 ght_uint32_t ght_one_at_a_time_hash(ght_hash_key_t *p_key);
00514 
00525 ght_uint32_t ght_rotating_hash(ght_hash_key_t *p_key);
00526 
00537 ght_uint32_t ght_crc_hash(ght_hash_key_t *p_key);
00538 
00539 #ifdef USE_PROFILING
00540 
00544 void ght_print(ght_hash_table_t *p_ht);
00545 #endif
00546 
00547 #ifdef __cplusplus
00548 }
00549 #endif
00550 
00551 #endif /* GHT_HASH_TABLE_H */

Generated on Sun Nov 27 10:49:06 2005 for libghthash by  doxygen 1.4.2