xref: /Universal-ctags/main/htable.h (revision 553897a62077cd5c507aec7cb684135e9c82a666)
1 /*
2 *
3 *   Copyright (c) 2014, Red Hat, Inc.
4 *   Copyright (c) 2014, Masatake YAMATO
5 *
6 *   This source code is released for free distribution under the terms of the
7 *   GNU General Public License version 2 or (at your option) any later version.
8 *
9 *   Defines hashtable
10 */
11 #ifndef CTAGS_MAIN_HTABLE_H
12 #define CTAGS_MAIN_HTABLE_H
13 
14 #include "general.h"
15 #include <stdint.h>
16 
17 /* This hashtable allows adding multiple items for the same key.
18  *
19  * hashTablePutItem() adds a key/item pair to the htable even if the
20  * htable has an item for the key already.
21  *
22  * hashTableGetItem() returns the first occurrence item for the given
23  * key.
24  *
25  * hashTableDeleteItem() deletes the first occurrence item for the given
26  * key.
27  *
28  * Use hashTableForeachItemOnChain () to process all items for the same key.
29  */
30 typedef struct sHashTable hashTable;
31 typedef unsigned int (* hashTableHashFunc)  (const void * const key);
32 typedef bool      (* hashTableEqualFunc) (const void* a, const void* b);
33 typedef void         (* hashTableDeleteFunc)  (void * ptr);
34 
35 /* To continue interation, return true.
36  * To break interation, return false. */
37 typedef bool         (* hashTableForeachFunc) (const void *key, void *value, void *user_data);
38 
39 unsigned int hashPtrhash (const void * x);
40 bool hashPtreq (const void * a, const void * constb);
41 
42 unsigned int hashCstrhash (const void * x);
43 bool hashCstreq (const void * a, const void * b);
44 
45 unsigned int hashCstrcasehash (const void * x);
46 bool hashCstrcaseeq (const void * a, const void * b);
47 
48 unsigned int hashInthash (const void * x);
49 bool hashInteq (const void * a, const void * b);
50 
51 extern hashTable* hashTableNew         (unsigned int size,
52 					hashTableHashFunc hashfn,
53 					hashTableEqualFunc equalfn,
54 					hashTableDeleteFunc keyfreefn,
55 					hashTableDeleteFunc valfreefn);
56 
57 /* By default, hashTableGetItem() returns NULL for a unknown key.
58  * It means you cannot store NULL as a value for a key.
59  * With hashTableSetValueForUnknownKey(), you can specific
60  * an alternative address representing the value for for unknown
61  * keys.
62  */
63 extern void hashTableSetValueForUnknownKey (hashTable *htable,
64 											void *val,
65 											hashTableDeleteFunc valfreefn);
66 
67 extern void       hashTableDelete      (hashTable *htable);
68 extern void       hashTableClear       (hashTable *htable);
69 extern void       hashTablePutItem     (hashTable *htable, void *key, void *value);
70 extern void*      hashTableGetItem     (hashTable *htable, const void * key);
71 extern bool    hashTableHasItem     (hashTable * htable, const void * key);
72 extern bool    hashTableDeleteItem  (hashTable *htable, const void *key);
73 extern bool       hashTableUpdateItem  (hashTable *htable, void *key, void *value);
74 
75 /* Return true if proc never returns false; proc returns true for all
76  * the items, or htable holds no item.
77  *
78  * Return false if htable holds at least one item and proc returns false
79  * for one of the items. */
80 extern bool       hashTableForeachItem (hashTable *htable, hashTableForeachFunc proc, void *user_data);
81 
82 /* This function is useful for htable having multiple items for a key.
83  * By giving a key, you can traverse all the items associated with the
84  * key via proc.  * "Chain" means group of items associated with a
85  * key. */
86 extern bool       hashTableForeachItemOnChain (hashTable *htable, const void *key, hashTableForeachFunc proc, void *user_data);
87 
88 extern unsigned int hashTableCountItem   (hashTable *htable);
89 
90 #define HT_PTR_TO_INT(P) ((int)(intptr_t)(P))
91 #define HT_INT_TO_PTR(P) ((void*)(intptr_t)(P))
92 #define HT_PTR_TO_UINT(P) ((unsigned int)(uintptr_t)(P))
93 #define HT_UINT_TO_PTR(P) ((void*)(uintptr_t)(P))
94 
95 #endif	/* CTAGS_MAIN_HTABLE_H */
96