00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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