xref: /Universal-ctags/main/htable.h (revision 553897a62077cd5c507aec7cb684135e9c82a666)
1d4c6f1e6SMasatake YAMATO /*
2d4c6f1e6SMasatake YAMATO *
3d4c6f1e6SMasatake YAMATO *   Copyright (c) 2014, Red Hat, Inc.
4d4c6f1e6SMasatake YAMATO *   Copyright (c) 2014, Masatake YAMATO
5d4c6f1e6SMasatake YAMATO *
6d4c6f1e6SMasatake YAMATO *   This source code is released for free distribution under the terms of the
70ce38835Sviccuad *   GNU General Public License version 2 or (at your option) any later version.
8d4c6f1e6SMasatake YAMATO *
9d4c6f1e6SMasatake YAMATO *   Defines hashtable
10d4c6f1e6SMasatake YAMATO */
115474c2e5SMasatake YAMATO #ifndef CTAGS_MAIN_HTABLE_H
125474c2e5SMasatake YAMATO #define CTAGS_MAIN_HTABLE_H
13d4c6f1e6SMasatake YAMATO 
14d4c6f1e6SMasatake YAMATO #include "general.h"
15c3062a32SMasatake YAMATO #include <stdint.h>
16d4c6f1e6SMasatake YAMATO 
17c24650e4SMasatake YAMATO /* This hashtable allows adding multiple items for the same key.
18990f41faSMasatake YAMATO  *
19990f41faSMasatake YAMATO  * hashTablePutItem() adds a key/item pair to the htable even if the
20990f41faSMasatake YAMATO  * htable has an item for the key already.
21990f41faSMasatake YAMATO  *
22c24650e4SMasatake YAMATO  * hashTableGetItem() returns the first occurrence item for the given
23990f41faSMasatake YAMATO  * key.
24990f41faSMasatake YAMATO  *
25c24650e4SMasatake YAMATO  * hashTableDeleteItem() deletes the first occurrence item for the given
26990f41faSMasatake YAMATO  * key.
27990f41faSMasatake YAMATO  *
28c24650e4SMasatake YAMATO  * Use hashTableForeachItemOnChain () to process all items for the same key.
29990f41faSMasatake YAMATO  */
30d4c6f1e6SMasatake YAMATO typedef struct sHashTable hashTable;
311b8321bfSMasatake YAMATO typedef unsigned int (* hashTableHashFunc)  (const void * const key);
321b8321bfSMasatake YAMATO typedef bool      (* hashTableEqualFunc) (const void* a, const void* b);
339be5bba1SMasatake YAMATO typedef void         (* hashTableDeleteFunc)  (void * ptr);
34994c1ccfSMasatake YAMATO 
35994c1ccfSMasatake YAMATO /* To continue interation, return true.
36994c1ccfSMasatake YAMATO  * To break interation, return false. */
378e77de48SMasatake YAMATO typedef bool         (* hashTableForeachFunc) (const void *key, void *value, void *user_data);
38d4c6f1e6SMasatake YAMATO 
391b8321bfSMasatake YAMATO unsigned int hashPtrhash (const void * x);
401b8321bfSMasatake YAMATO bool hashPtreq (const void * a, const void * constb);
41d4c6f1e6SMasatake YAMATO 
421b8321bfSMasatake YAMATO unsigned int hashCstrhash (const void * x);
431b8321bfSMasatake YAMATO bool hashCstreq (const void * a, const void * b);
44f2f92e1fSMasatake YAMATO 
4593ce570cSMasatake YAMATO unsigned int hashCstrcasehash (const void * x);
4693ce570cSMasatake YAMATO bool hashCstrcaseeq (const void * a, const void * b);
4793ce570cSMasatake YAMATO 
481b8321bfSMasatake YAMATO unsigned int hashInthash (const void * x);
491b8321bfSMasatake YAMATO bool hashInteq (const void * a, const void * b);
50f416402fSMasatake YAMATO 
51d4c6f1e6SMasatake YAMATO extern hashTable* hashTableNew         (unsigned int size,
52d4c6f1e6SMasatake YAMATO 					hashTableHashFunc hashfn,
53d4c6f1e6SMasatake YAMATO 					hashTableEqualFunc equalfn,
549be5bba1SMasatake YAMATO 					hashTableDeleteFunc keyfreefn,
559be5bba1SMasatake YAMATO 					hashTableDeleteFunc valfreefn);
56c3062a32SMasatake YAMATO 
57*553897a6SMasatake YAMATO /* By default, hashTableGetItem() returns NULL for a unknown key.
58*553897a6SMasatake YAMATO  * It means you cannot store NULL as a value for a key.
59*553897a6SMasatake YAMATO  * With hashTableSetValueForUnknownKey(), you can specific
60*553897a6SMasatake YAMATO  * an alternative address representing the value for for unknown
61*553897a6SMasatake YAMATO  * keys.
62*553897a6SMasatake YAMATO  */
63*553897a6SMasatake YAMATO extern void hashTableSetValueForUnknownKey (hashTable *htable,
64*553897a6SMasatake YAMATO 											void *val,
65*553897a6SMasatake YAMATO 											hashTableDeleteFunc valfreefn);
66*553897a6SMasatake YAMATO 
67d4c6f1e6SMasatake YAMATO extern void       hashTableDelete      (hashTable *htable);
68d41b6eb3SMasatake YAMATO extern void       hashTableClear       (hashTable *htable);
69d4c6f1e6SMasatake YAMATO extern void       hashTablePutItem     (hashTable *htable, void *key, void *value);
701b8321bfSMasatake YAMATO extern void*      hashTableGetItem     (hashTable *htable, const void * key);
711b8321bfSMasatake YAMATO extern bool    hashTableHasItem     (hashTable * htable, const void * key);
728e77de48SMasatake YAMATO extern bool    hashTableDeleteItem  (hashTable *htable, const void *key);
732b57298dSMasatake YAMATO extern bool       hashTableUpdateItem  (hashTable *htable, void *key, void *value);
74994c1ccfSMasatake YAMATO 
75994c1ccfSMasatake YAMATO /* Return true if proc never returns false; proc returns true for all
76994c1ccfSMasatake YAMATO  * the items, or htable holds no item.
77994c1ccfSMasatake YAMATO  *
78994c1ccfSMasatake YAMATO  * Return false if htable holds at least one item and proc returns false
79994c1ccfSMasatake YAMATO  * for one of the items. */
80994c1ccfSMasatake YAMATO extern bool       hashTableForeachItem (hashTable *htable, hashTableForeachFunc proc, void *user_data);
81994c1ccfSMasatake YAMATO 
82990f41faSMasatake YAMATO /* This function is useful for htable having multiple items for a key.
83990f41faSMasatake YAMATO  * By giving a key, you can traverse all the items associated with the
84990f41faSMasatake YAMATO  * key via proc.  * "Chain" means group of items associated with a
85990f41faSMasatake YAMATO  * key. */
86990f41faSMasatake YAMATO extern bool       hashTableForeachItemOnChain (hashTable *htable, const void *key, hashTableForeachFunc proc, void *user_data);
87990f41faSMasatake YAMATO 
88d198ad0cSMasatake YAMATO extern unsigned int hashTableCountItem   (hashTable *htable);
89d4c6f1e6SMasatake YAMATO 
90c3062a32SMasatake YAMATO #define HT_PTR_TO_INT(P) ((int)(intptr_t)(P))
91c3062a32SMasatake YAMATO #define HT_INT_TO_PTR(P) ((void*)(intptr_t)(P))
921b7f9b40SMasatake YAMATO #define HT_PTR_TO_UINT(P) ((unsigned int)(uintptr_t)(P))
931b7f9b40SMasatake YAMATO #define HT_UINT_TO_PTR(P) ((void*)(uintptr_t)(P))
94c3062a32SMasatake YAMATO 
955474c2e5SMasatake YAMATO #endif	/* CTAGS_MAIN_HTABLE_H */
96