root/src/include/private/hashtable.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. FT_DECLARE
  2. FT_DECLARE
  3. FT_DECLARE
  4. FT_DECLARE

   1 /* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
   2 
   3 #ifndef __HASHTABLE_CWC22_H__
   4 #define __HASHTABLE_CWC22_H__
   5 #ifdef _MSC_VER
   6 #ifndef __inline__
   7 #define __inline__ __inline
   8 #endif
   9 #endif
  10 #include "freetdm.h"
  11 
  12 #ifdef __cplusplus
  13 extern "C" {
  14 #endif
  15 struct hashtable;
  16 struct hashtable_iterator;
  17 
  18 /* Example of use:
  19  *
  20  *      struct hashtable  *h;
  21  *      struct some_key   *k;
  22  *      struct some_value *v;
  23  *
  24  *      static unsigned int         hash_from_key_fn( void *k );
  25  *      static int                  keys_equal_fn ( void *key1, void *key2 );
  26  *
  27  *      h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
  28  *      k = (struct some_key *)     malloc(sizeof(struct some_key));
  29  *      v = (struct some_value *)   malloc(sizeof(struct some_value));
  30  *
  31  *      (initialise k and v to suitable values)
  32  * 
  33  *      if (! hashtable_insert(h,k,v) )
  34  *      {     exit(-1);               }
  35  *
  36  *      if (NULL == (found = hashtable_search(h,k) ))
  37  *      {    printf("not found!");                  }
  38  *
  39  *      if (NULL == (found = hashtable_remove(h,k) ))
  40  *      {    printf("Not found\n");                 }
  41  *
  42  */
  43 
  44 /* Macros may be used to define type-safe(r) hashtable access functions, with
  45  * methods specialized to take known key and value types as parameters.
  46  * 
  47  * Example:
  48  *
  49  * Insert this at the start of your file:
  50  *
  51  * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
  52  * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
  53  * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
  54  *
  55  * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
  56  * These operate just like hashtable_insert etc., with the same parameters,
  57  * but their function signatures have 'struct some_key *' rather than
  58  * 'void *', and hence can generate compile time errors if your program is
  59  * supplying incorrect data as a key (and similarly for value).
  60  *
  61  * Note that the hash and key equality functions passed to create_hashtable
  62  * still take 'void *' parameters instead of 'some key *'. This shouldn't be
  63  * a difficult issue as they're only defined and passed once, and the other
  64  * functions will ensure that only valid keys are supplied to them.
  65  *
  66  * The cost for this checking is increased code size and runtime overhead
  67  * - if performance is important, it may be worth switching back to the
  68  * unsafe methods once your program has been debugged with the safe methods.
  69  * This just requires switching to some simple alternative defines - eg:
  70  * #define insert_some hashtable_insert
  71  *
  72  */
  73 
  74 /*****************************************************************************
  75  * create_hashtable
  76    
  77  * @name                    create_hashtable
  78  * @param   minsize         minimum initial size of hashtable
  79  * @param   hashfunction    function for hashing keys
  80  * @param   key_eq_fn       function for determining key equality
  81  * @return                  newly created hashtable or NULL on failure
  82  */
  83 
  84 FT_DECLARE(struct hashtable *)
  85 create_hashtable(unsigned int minsize,
  86                  unsigned int (*hashfunction) (void*),
  87                  int (*key_eq_fn) (void*,void*));
  88 
  89 /*****************************************************************************
  90  * hashtable_insert
  91    
  92  * @name        hashtable_insert
  93  * @param   h   the hashtable to insert into
  94  * @param   k   the key - hashtable claims ownership and will free on removal
  95  * @param   v   the value - does not claim ownership
  96  * @return      non-zero for successful insertion
  97  *
  98  * This function will cause the table to expand if the insertion would take
  99  * the ratio of entries to table size over the maximum load factor.
 100  *
 101  * This function does not check for repeated insertions with a duplicate key.
 102  * The value returned when using a duplicate key is undefined -- when
 103  * the hashtable changes size, the order of retrieval of duplicate key
 104  * entries is reversed.
 105  * If in doubt, remove before insert.
 106  */
 107 
 108 
 109 typedef enum {
 110         HASHTABLE_FLAG_NONE = 0,
 111         HASHTABLE_FLAG_FREE_KEY = (1 << 0),
 112         HASHTABLE_FLAG_FREE_VALUE = (1 << 1)
 113 } hashtable_flag_t;
 114 
 115 FT_DECLARE(int)
 116 hashtable_insert(struct hashtable *h, void *k, void *v, hashtable_flag_t flags);
 117 
 118 #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype)             \
 119         int fnname (struct hashtable *h, keytype *k, valuetype *v)      \
 120         {                                                                                                                       \
 121                 return hashtable_insert(h,k,v);                                                 \
 122         }
 123 
 124 /*****************************************************************************
 125  * hashtable_search
 126    
 127  * @name        hashtable_search
 128  * @param   h   the hashtable to search
 129  * @param   k   the key to search for  - does not claim ownership
 130  * @return      the value associated with the key, or NULL if none found
 131  */
 132 
 133 FT_DECLARE(void *)
 134 hashtable_search(struct hashtable *h, void *k);
 135 
 136 #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
 137         valuetype * fnname (struct hashtable *h, keytype *k)    \
 138         {                                                                                                               \
 139                 return (valuetype *) (hashtable_search(h,k));           \
 140         }
 141 
 142 /*****************************************************************************
 143  * hashtable_remove
 144    
 145  * @name        hashtable_remove
 146  * @param   h   the hashtable to remove the item from
 147  * @param   k   the key to search for  - does not claim ownership
 148  * @return      the value associated with the key, or NULL if none found
 149  */
 150 
 151 FT_DECLARE(void *) /* returns value */
 152 hashtable_remove(struct hashtable *h, void *k);
 153 
 154 #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \
 155         valuetype * fnname (struct hashtable *h, keytype *k)    \
 156         {                                                                                                               \
 157                 return (valuetype *) (hashtable_remove(h,k));           \
 158         }
 159 
 160 
 161 /*****************************************************************************
 162  * hashtable_count
 163    
 164  * @name        hashtable_count
 165  * @param   h   the hashtable
 166  * @return      the number of items stored in the hashtable
 167  */
 168 FT_DECLARE(unsigned int)
 169 hashtable_count(struct hashtable *h);
 170 
 171 
 172 /*****************************************************************************
 173  * hashtable_destroy
 174    
 175  * @name        hashtable_destroy
 176  * @param   h   the hashtable
 177  * @param       free_values     whether to call 'free' on the remaining values
 178  */
 179 
 180 FT_DECLARE(void)
 181 hashtable_destroy(struct hashtable *h);
 182 
 183 FT_DECLARE(struct hashtable_iterator*) hashtable_first(struct hashtable *h);
 184 FT_DECLARE(struct hashtable_iterator*) hashtable_next(struct hashtable_iterator *i);
 185 FT_DECLARE(void) hashtable_this(struct hashtable_iterator *i, const void **key, int *klen, void **val);
 186 
 187 #ifdef __cplusplus
 188 } /* extern C */
 189 #endif
 190 
 191 #endif /* __HASHTABLE_CWC22_H__ */
 192 
 193 /*
 194  * Copyright (c) 2002, Christopher Clark
 195  * All rights reserved.
 196  * 
 197  * Redistribution and use in source and binary forms, with or without
 198  * modification, are permitted provided that the following conditions
 199  * are met:
 200  * 
 201  * * Redistributions of source code must retain the above copyright
 202  * notice, this list of conditions and the following disclaimer.
 203  * 
 204  * * Redistributions in binary form must reproduce the above copyright
 205  * notice, this list of conditions and the following disclaimer in the
 206  * documentation and/or other materials provided with the distribution.
 207  * 
 208  * * Neither the name of the original author; nor the names of any contributors
 209  * may be used to endorse or promote products derived from this software
 210  * without specific prior written permission.
 211  * 
 212  * 
 213  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 214  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 215  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 216  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
 217  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 218  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 219  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 220  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 221  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 222  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 223  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 224  */
 225 
 226 /* For Emacs:
 227  * Local Variables:
 228  * mode:c
 229  * indent-tabs-mode:t
 230  * tab-width:4
 231  * c-basic-offset:4
 232  * End:
 233  * For VIM:
 234  * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
 235  */

/* [<][>][^][v][top][bottom][index][help] */