• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

str_hash.h

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2006 iptelorg GmbH 
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 /*
00019  * History:
00020  * --------
00021  *  2006-02-02  created by andrei
00022  *  2006-11-24  added numeric string optimized hash function (andrei)
00023  *  2006-12-13  split into hashes.h (more generic) and str_hash.h (andrei)
00024  */
00025 
00026 
00027 #ifndef _str_hashs_h
00028 #define _str_hashs_h
00029 
00030 #include "str.h"
00031 #include "hashes.h"
00032 #include "mem/mem.h"
00033 #include "clist.h"
00034 #include <string.h>
00035 
00036 
00037 /* generic, simple str keyed hash */
00038 
00039 struct str_hash_entry{
00040         struct str_hash_entry* next;
00041         struct str_hash_entry* prev;
00042         str key;
00043         unsigned int flags;
00044         union{
00045                 void* p;
00046                 char* s;
00047                 int   n;
00048                 char  data[sizeof(void*)];
00049         }u;
00050 };
00051 
00052 
00053 struct str_hash_head{
00054         struct str_hash_entry* next;
00055         struct str_hash_entry* prev;
00056 };
00057 
00058 
00059 struct str_hash_table{
00060         struct str_hash_head* table;
00061         int size;
00062 };
00063 
00064 
00065 
00066 /* returns 0 on success, <0 on failure */
00067 inline static int str_hash_alloc(struct str_hash_table* ht, int size)
00068 {
00069         ht->table=(struct str_hash_head*)pkg_malloc(sizeof(struct str_hash_head)*size);
00070         if (ht->table==0)
00071                 return -1;
00072         ht->size=size;
00073         return 0;
00074 }
00075 
00076 
00077 
00078 inline static void str_hash_init(struct str_hash_table* ht)
00079 {
00080         int r;
00081         
00082         for (r=0; r<ht->size; r++) clist_init(&(ht->table[r]), next, prev);
00083 }
00084 
00085 
00086 
00087 inline static void str_hash_add(struct str_hash_table* ht, 
00088                                                                 struct str_hash_entry* e)
00089 {
00090         int h;
00091         
00092         h=get_hash1_raw(e->key.s, e->key.len) % ht->size;
00093         clist_insert(&ht->table[h], e, next, prev);
00094 }
00095 
00096 
00097 
00098 inline static struct str_hash_entry* str_hash_get(struct str_hash_table* ht,
00099                                                                         const char* key, int len)
00100 {
00101         int h;
00102         struct str_hash_entry* e;
00103         
00104         h=get_hash1_raw(key, len) % ht->size;
00105         clist_foreach(&ht->table[h], e, next){
00106                 if ((e->key.len==len) && (memcmp(e->key.s, key, len)==0))
00107                         return e;
00108         }
00109         return 0;
00110 }
00111 
00112 
00113 #define str_hash_del(e) clist_rm(e, next, prev)
00114 
00115 #endif

Generated on Tue May 22 2012 13:10:16 for SIP Router by  doxygen 1.7.1