sofia-sip/htable.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef HTABLE_H
00026 
00027 #define HTABLE_H
00028 
00059 typedef unsigned long hash_value_t;
00060 
00062 #define HTABLE_MIN_SIZE 31
00063 
00074 #define HTABLE_DECLARE(prefix, pr, entry_t) \
00075 typedef struct prefix##_s { \
00076   unsigned pr##_size; \
00077   unsigned pr##_used; \
00078   entry_t**pr##_table;  \
00079 } prefix##_t
00080 
00081 #ifndef HTABLE_SCOPE
00082 
00083 #define HTABLE_SCOPE static inline
00084 #endif
00085 
00096 #define HTABLE_PROTOS(prefix, pr, entry_t) \
00097 HTABLE_SCOPE int prefix##_resize(su_home_t *, prefix##_t pr[1], unsigned); \
00098 HTABLE_SCOPE int prefix##_is_full(prefix##_t const *); \
00099 HTABLE_SCOPE entry_t **prefix##_hash(prefix##_t const *, hash_value_t hv); \
00100 HTABLE_SCOPE entry_t **prefix##_next(prefix##_t const *, entry_t * const *ee); \
00101 HTABLE_SCOPE void prefix##_append(prefix##_t *pr, entry_t const *e); \
00102 HTABLE_SCOPE void prefix##_insert(prefix##_t *pr, entry_t const *e); \
00103 HTABLE_SCOPE void prefix##_remove(prefix##_t *, entry_t const *e)
00104 
00117 #define HTABLE_BODIES(prefix, pr, entry_t, hfun) \
00118  \
00119 HTABLE_SCOPE \
00120 int prefix##_resize(su_home_t *home, \
00121                     prefix##_t pr[], \
00122                     unsigned new_size) \
00123 { \
00124   entry_t **new_hash; \
00125   entry_t **old_hash = pr->pr##_table; \
00126   unsigned old_size; \
00127   unsigned i, j, i0; \
00128   unsigned again = 0, used = 0, collisions = 0; \
00129 \
00130   if (new_size == 0) \
00131     new_size = 2 * pr->pr##_size + 1; \
00132   if (new_size < HTABLE_MIN_SIZE) \
00133     new_size = HTABLE_MIN_SIZE; \
00134 \
00135   if (!(new_hash = su_zalloc(home, sizeof(*new_hash) * new_size))) \
00136     return -1; \
00137 \
00138   old_size = pr->pr##_size; \
00139 \
00140   do for (j = 0; j < old_size; j++) { \
00141     if (!old_hash[j]) \
00142       continue; \
00143 \
00144     if (again < 2 && hfun(old_hash[j]) % old_size > j) { \
00145       /* Wrapped, leave entry for second pass */ \
00146       again = 1; continue; \
00147     } \
00148 \
00149     i0 = hfun(old_hash[j]) % new_size; \
00150 \
00151     for (i = i0; new_hash[i]; i = (i + 1) % new_size, assert(i != i0)) \
00152       collisions++; \
00153 \
00154     new_hash[i] = old_hash[j], old_hash[j] = NULL; \
00155     used++; \
00156   } \
00157   while (again++ == 1); \
00158 \
00159   pr->pr##_table = new_hash, pr->pr##_size = new_size; \
00160 \
00161   assert(pr->pr##_used == used);\
00162 \
00163   return 0; \
00164 } \
00165 \
00166 HTABLE_SCOPE \
00167 int prefix##_is_full(prefix##_t const *pr) \
00168 { \
00169   return pr->pr##_table == NULL || 3 * pr->pr##_used > 2 * pr->pr##_size; \
00170 } \
00171 \
00172 HTABLE_SCOPE \
00173 entry_t **prefix##_hash(prefix##_t const *pr, hash_value_t hv) \
00174 { \
00175   return pr->pr##_table + hv % pr->pr##_size; \
00176 } \
00177 \
00178 HTABLE_SCOPE \
00179 entry_t **prefix##_next(prefix##_t const *pr, entry_t * const *ee) \
00180 { \
00181   if (++ee < pr->pr##_table + pr->pr##_size && ee >= pr->pr##_table) \
00182     return (entry_t **)ee; \
00183   else \
00184     return pr->pr##_table; \
00185 }  \
00186 \
00187 HTABLE_SCOPE \
00188 void prefix##_append(prefix##_t *pr, entry_t const *e) \
00189 { \
00190   entry_t **ee; \
00191 \
00192   pr->pr##_used++; \
00193   for (ee = prefix##_hash(pr, hfun(e)); *ee; ee = prefix##_next(pr, ee)) \
00194    ; \
00195   *ee = (entry_t *)e; \
00196 } \
00197 \
00198 HTABLE_SCOPE \
00199 void prefix##_insert(prefix##_t *pr, entry_t const *e) \
00200 { \
00201   entry_t *e0, **ee; \
00202 \
00203   pr->pr##_used++; \
00204   /* Insert entry into hash table (before other entries with same hash) */ \
00205   for (ee = prefix##_hash(pr, hfun(e));  \
00206        (e0 = *ee); \
00207        ee = prefix##_next(pr, ee)) \
00208     *ee = (entry_t *)e, e = e0; \
00209   *ee = (entry_t *)e; \
00210 } \
00211 \
00212 HTABLE_SCOPE \
00213 void prefix##_remove(prefix##_t *pr, entry_t const *e) \
00214 { \
00215   unsigned i, j, k, size = pr->pr##_size; \
00216   entry_t **htable = pr->pr##_table; \
00217 \
00218   /* Search for entry */ \
00219   for (i = hfun(e) % size; htable[i]; i = (i + 1) % size) \
00220     if (e == htable[i]) \
00221       break; \
00222 \
00223   assert(htable[i]); if (!htable[i]) abort(); \
00224 \
00225   /* Move table entries towards their primary place  */ \
00226   for (j = (i + 1) % size; htable[j]; j = (j + 1) % size) { \
00227     /* k is primary place for entry */ \
00228     k = hfun(htable[j]) % size; \
00229     if (k == j)                 /* entry is in its primary place? */ \
00230       continue; \
00231     /* primary place is between i and j - do not move this to i */ \
00232     if (j > i ? (i < k && k < j) : (i < k || k < j)) \
00233       continue; \
00234 \
00235     htable[i] = htable[j], i = j; \
00236   } \
00237 \
00238   pr->pr##_used--; \
00239 \
00240   htable[i] = NULL; \
00241 } \
00242 extern int prefix##_dummy
00243 
00244 #endif 

Sofia-SIP 1.12.1 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.