source: svn/newcon3bcm2_21bu/BSEAV/lib/utils/bhash.h @ 46

Last change on this file since 46 was 46, checked in by megakiss, 11 years ago

459Mhz로 OTC 주파수 변경

  • Property svn:executable set to *
File size: 10.4 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2007 Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bhash.h $
11 * $brcm_Revision: 4 $
12 * $brcm_Date: 8/17/07 3:37p $
13 *
14 * Module Description:
15 *
16 * Hash table associative container
17 *
18 * Revision History:
19 *
20 * $brcm_Log: /BSEAV/lib/utils/bhash.h $
21 *
22 * 4   8/17/07 3:37p vsilyaev
23 * PR 33812: Improved use of arguments in macro
24 *
25 * 3   7/21/07 9:34p vsilyaev
26 * PR 32813: Improved BHEAD_FOREACH_BEGIN
27 *
28 * 2   7/18/07 7:02p vsilyaev
29 * PR 32813: Improved itterator
30 *
31 * 1   7/18/07 3:01p vsilyaev
32 * PR 32813: Hash table data container
33 *
34 *******************************************************************************/
35#ifndef __BHASH_H__
36#define __BHASH_H__
37
38#include "blst_slist.h"
39
40/*================== Module Overview =====================================
41This modules defines macros to control hash table associative container.
42This container allows to associate any user defined data type with the user
43defined key. Single data type could be simultaneously member of many associative
44tables.
45
46All operations are typesafe (doesn't require typecasting) and run in a constant space.
47
48This module implements hash table with chaining type of collision resolution.
49And conainer supports the following operations:
50
51  o Insert a new entry - time complexity O(1)
52  o Removal of entry by key - time complexity O(1) .. O(n) (*)
53  o Lookup by key - time complexity O(1) .. O(n) (*)
54  o Each element requires single pointer in order to be addded in to the container
55  o Code size and execution time is about twice that for singly-linked list
56
57Note *:
58Performance of hash table is strongly corellated with quality of hash function.
59With good quality hash function removal and lookup are O(1) (constant time), with poor
60hash function there are meny collisions ans peformance degraded to O(n) (e.g. proportional
61to number of elements stored in the container).
62 
63========================================================================*/
64
65/***************************************************************************
66Summary:
67    Creates new data type for the heash head
68       
69Description:
70    Creates new data type for the hash head, this type used to create variable for the hash head.
71    User should create new the hash head data type for every different element datatype.
72       
73Input:
74        name - name for the new data type (structure)
75    type - existing user data type(structure) used for element of the container
76
77Example:   
78     BHASH_HEAD(block_hash, block);
79     struct block_hash container;
80
81Returns:
82    <none>
83****************************************************************************/
84#define BHASH_HEAD(name, type, size) struct name { \
85        BLST_S_HEAD(name##_index, type) hash_bucket[size]; } 
86
87/***************************************************************************
88Summary:
89    Defines hash entry
90       
91Description:
92    Defines entrys for the hash inside the user data type.
93       
94Input:
95    type - the datatype for element
96
97Example:
98     struct block {
99        BHASH_ENTRY(block) hash_link;
100                int key;
101        char string[256];
102     }; 
103
104Returns:
105    <none>
106****************************************************************************/
107#define BHASH_ENTRY(type) BLST_S_ENTRY(type)
108
109/***************************************************************************
110Summary:
111    Defines internal functions to operate with a container
112       
113Description:
114     This macro shall be used in the C file and it defines static function
115         that are needed for internal working of the hash container.
116       
117Input:
118    name - user selected name for the hash
119        type_key - datatype of key
120    type - the datatype(structure) of the element
121        field - name of the hash link field in the element
122        hash - hash function that takes key and returns it's hash (unsigned int)
123        compare - compare function that takes key and pointer to the element and returns true
124                  if and only if key is exactly matches user element
125
126Example:
127        static unsigned block_key_hash(int key) {return key;}
128        static bool block_key_compare(const struct block *block, int key) {return block->key == key;}
129        BHASH_GENERATE(block_container, int, block, hash_link, block_key_hash, block_key_compare)
130
131Returns:
132    <none>
133****************************************************************************/
134#define BHASH_GENERATE(name, type_key, type, field, hash, compare) \
135static void name##_hash_init(struct name *h) { \
136        unsigned i; \
137        for(i=0;i<sizeof(h->hash_bucket)/sizeof(h->hash_bucket[0]);i++) { BLST_S_INIT(&h->hash_bucket[i]); } \
138        return; } \
139static void \
140name##_hash_add(struct name *h, type_key key, struct type *t) { \
141        unsigned key_hash = hash(key); \
142        BDBG_ASSERT(h); \
143        key_hash = key_hash % (sizeof(h->hash_bucket)/sizeof(h->hash_bucket[0])); \
144        BLST_S_INSERT_HEAD(&h->hash_bucket[key_hash], t, field); return; } \
145static struct type * \
146name##_hash_find(struct name *h, type_key key) { \
147        struct type *t; unsigned key_hash = hash(key); \
148        BDBG_ASSERT(h); \
149        key_hash = key_hash % (sizeof(h->hash_bucket)/sizeof(h->hash_bucket[0])); \
150        for(t = BLST_S_FIRST(&h->hash_bucket[key_hash]); t ; t = BLST_S_NEXT(t, field)) { \
151                if(compare(key, t)) { return t;} } \
152        return NULL; } \
153static bool \
154name##_hash_remove(struct name *h, type_key key) { \
155        struct type *t, *prev_t; \
156        unsigned key_hash = hash(key); \
157        BDBG_ASSERT(h); \
158        key_hash = key_hash % (sizeof(h->hash_bucket)/sizeof(h->hash_bucket[0])); \
159        for(prev_t = NULL, t = BLST_S_FIRST(&h->hash_bucket[key_hash]); t; t = BLST_S_NEXT(t, field)) { \
160                if(compare(key, t)) { \
161                        if(prev_t==NULL) { BLST_S_REMOVE_HEAD(&h->hash_bucket[key_hash], field); } \
162                        else { BLST_S_REMOVE_NEXT(&h->hash_bucket[key_hash], prev_t, field); }\
163                        return true; \
164                } prev_t = t; \
165        }return false; }
166
167
168/***************************************************************************
169Summary:
170    Initializes conteiner
171       
172Description:
173    Initializes container, e.g. wipes out all it's content.
174       
175Input:
176    name - name of the hash container
177        hash - pointer to the container
178
179See also:
180    BHASH_GENERATE
181
182Example:
183     struct block_hash container;
184         BHASH_INIT(block_container, &container);
185
186Returns:
187    <none>
188****************************************************************************/
189#define BHASH_INIT(name, hash) name##_hash_init(hash)
190
191/***************************************************************************
192Summary:
193    Adds element into the container
194       
195Description:
196    This macro is used to associate a key with the data element. Note only
197        one key could be associated with the given data element (e.g. data element
198        could be added only once in the container).
199       
200Input:
201    name - name of the hash container
202        hash - pointer to the container
203        key - key for association
204        elem - pointer to the data element
205
206See also:
207    BHASH_REMOVE
208        BHASH_FIND
209
210Returns:
211    <none>
212
213Example:
214        struct block b;
215    BHASH_ADD(block_container, &container, 0, &b );
216****************************************************************************/
217#define BHASH_ADD(name, hash, key, elm) name##_hash_add((hash), (key), (elm))
218
219/***************************************************************************
220Summary:
221    Removes element into the container
222       
223Description:
224    This macro is used to remove association between key and data element;
225       
226Input:
227    name - name of the hash container
228        hash - pointer to the container
229        key - key for removal from the container
230
231See also:
232    BHASH_ADD
233
234Returns:
235    true - if element was in the container
236        false - if element wasn't in the container
237
238Example:
239    if(!BHASH_REMOVE(block_container, &container, 0)) {
240                printf("key 0 wasn't in the table!!!");
241        }
242****************************************************************************/
243#define BHASH_REMOVE(name, hash, key) name##_hash_remove((hash), (key))
244
245/***************************************************************************
246Summary:
247    Removes element into the container
248       
249Description:
250    This macro is used to remove association between key and data element;
251       
252Input:
253    name - name of the hash container
254        hash - pointer to the container
255        key - key to search in the container
256
257See also:
258    BHASH_ADD
259
260Returns:
261    pointer to data element - if element was in the container
262        NULL - if element wasn't in the container
263
264Example:
265        struct block *b = BHASH_FIND(block_container, &container, 0)) {
266    if(b) {
267                printf("key 0 associated with %s", b->string);
268        }
269****************************************************************************/
270#define BHASH_FIND(name, hash, key) name##_hash_find((hash), (key))
271
272/***************************************************************************
273Summary:
274    Open itterator for the container
275       
276Description:
277    The BHASH_FOREACH_BEGIN and BHASH_FOREACH_END are used to itterate over
278        all elements in the container.
279        Please note that 'break' can't be used to escape from the itterator,
280        however goto could safely used if it points outside of itterator.
281
282        User shall not add or remove elements from the container, the only
283        exception is that user could remove currently itterated element, e.g.
284        itterator is widely useful for controlled removal of all elements
285        from the container
286       
287Input:
288    name - name of the hash container
289        hash - pointer to the container
290    type - the datatype(structure) of the element
291        elm - element to be itterated
292        field - name of the hash link field in the element
293
294See also:
295    BHASH_REMOVE
296        BHASH_FOREACH_END
297
298Returns:
299    <none>
300
301Example:
302        struct block *b;
303        BHASH_FOREACH_BEGIN(block_container, &container, block, b, hash_link)
304                printf("key %d associated with %s", b->key, b->string);
305        BHASH_FOREACH_END()
306****************************************************************************/
307#define BHASH_FOREACH_BEGIN(name,hash,type,elm,field) { unsigned _hash_i; for(_hash_i=0;_hash_i<sizeof((hash)->hash_bucket)/sizeof((hash)->hash_bucket[0]);_hash_i++) { \
308                                                                                struct type *_hash_next;for(_hash_next=elm=BLST_S_FIRST(&(hash)->hash_bucket[_hash_i]);elm;elm=_hash_next) { _hash_next = BLST_S_NEXT(elm, field);
309
310/***************************************************************************
311Summary:
312    Open itterator for the container
313
314See also:
315        BHASH_FOREACH_BEGIN
316
317Returns:
318    <none>
319****************************************************************************/
320#define BHASH_FOREACH_END()     }}}
321
322#endif  /* __BHASH_H__ */
323
324
325
326
327
Note: See TracBrowser for help on using the repository browser.