1 |
/* |
2 |
* Cisco router simulation platform. |
3 |
* Copyright (c) 2006 Christophe Fillot (cf@utc.fr) |
4 |
* |
5 |
* Generic Hash Tables. |
6 |
*/ |
7 |
|
8 |
#ifndef __HASH_H__ |
9 |
#define __HASH_H__ 1 |
10 |
|
11 |
#include <sys/types.h> |
12 |
#include "utils.h" |
13 |
|
14 |
/* Key computation function */ |
15 |
typedef u_int (*hash_fcompute)(void *key); |
16 |
|
17 |
/* Comparison function for 2 keys */ |
18 |
typedef int (*hash_fcompare)(void *key1,void *key2); |
19 |
|
20 |
/* User function to call when using hash_table_foreach */ |
21 |
typedef void (*hash_fforeach)(void *key,void *value,void *opt_arg); |
22 |
|
23 |
/* Hash element (pair key,value) */ |
24 |
typedef struct hash_node hash_node_t; |
25 |
struct hash_node { |
26 |
void *key, *value; |
27 |
hash_node_t *next; |
28 |
}; |
29 |
|
30 |
/* Hash Table definition */ |
31 |
typedef struct hash_table hash_table_t; |
32 |
struct hash_table { |
33 |
int size,nnodes; |
34 |
hash_node_t **nodes; |
35 |
hash_fcompute hash_func; |
36 |
hash_fcompare key_cmp; |
37 |
}; |
38 |
|
39 |
#define hash_string_create(hash_size) \ |
40 |
hash_table_create(str_hash,str_equal,hash_size) |
41 |
|
42 |
#define hash_int_create(hash_size) \ |
43 |
hash_table_create(int_hash,int_equal,hash_size) |
44 |
|
45 |
#define hash_u64_create(hash_size) \ |
46 |
hash_table_create(u64_hash,u64_equal,hash_size) |
47 |
|
48 |
#define hash_ptr_create(hash_size) \ |
49 |
hash_table_create(ptr_hash,ptr_equal,hash_size) |
50 |
|
51 |
#define HASH_TABLE_FOREACH(i,ht,hn) \ |
52 |
for(i=0;i<ht->size;i++) \ |
53 |
for(hn=ht->nodes[i];hn;hn=hn->next) |
54 |
|
55 |
/* Create a new hash table */ |
56 |
hash_table_t *hash_table_create(hash_fcompute hash_func,hash_fcompare key_cmp, |
57 |
int hash_size); |
58 |
|
59 |
/* Delete an existing Hash Table */ |
60 |
void hash_table_delete(hash_table_t *ht); |
61 |
|
62 |
/* Insert a new (key,value). If key already exist in table, replace value */ |
63 |
int hash_table_insert(hash_table_t *ht,void *key,void *value); |
64 |
|
65 |
/* Remove a pair (key,value) from an hash table */ |
66 |
void *hash_table_remove(hash_table_t *ht,void *key); |
67 |
|
68 |
/* Hash Table Lookup */ |
69 |
void *hash_table_lookup(hash_table_t *ht,void *key); |
70 |
|
71 |
/* Call the specified function for each node found in hash table */ |
72 |
int hash_table_foreach(hash_table_t *ht,hash_fforeach user_fn,void *opt_arg); |
73 |
|
74 |
/* Hash Table Lookup - key direct comparison */ |
75 |
void *hash_table_lookup_dcmp(hash_table_t *ht,void *key); |
76 |
|
77 |
/* Hash Functions for strings */ |
78 |
int str_equal(void *s1,void *s2); |
79 |
u_int str_hash(void *str); |
80 |
|
81 |
/* Hash Functions for integers */ |
82 |
int int_equal(void *i1,void *i2); |
83 |
u_int int_hash(void *i); |
84 |
|
85 |
/* Hash Functions for u64 */ |
86 |
int u64_equal(void *i1,void *i2); |
87 |
u_int u64_hash(void *i); |
88 |
|
89 |
/* Hash Function for pointers */ |
90 |
int ptr_equal(void *i1,void *i2); |
91 |
u_int ptr_hash(void *i); |
92 |
|
93 |
#endif |