Sanitize python object -> tag number exception handling
[platform/upstream/rpm.git] / lib / rpmhash.H
1 /**
2  * \file lib/rpmhash.h
3  * Hash table implemenation.
4  */
5
6 #include <string.h>
7 // Hackery to make sure that macros get expanded
8 #define __JOIN(a,b) a##b
9 #define JOIN(a,b) __JOIN(a,b)
10 #define HASHPREFIX(name) JOIN(HASHTYPE,name)
11 #define HASHSTRUCT JOIN(HASHTYPE,_s)
12
13 typedef struct HASHSTRUCT * HASHTYPE;
14
15 /* function pointer types to deal with the datatypes the hash works with */
16
17 #define hashFunctionType JOIN(HASHTYPE,HashFunctionType)
18 #define hashEqualityType JOIN(HASHTYPE,HashEqualityType)
19 #define hashFreeKey JOIN(HASHTYPE,FreeKey)
20
21 typedef unsigned int (*hashFunctionType) (HTKEYTYPE string);
22 typedef int (*hashEqualityType) (HTKEYTYPE key1, HTKEYTYPE key2);
23 typedef HTKEYTYPE (*hashFreeKey) (HTKEYTYPE);
24
25 #ifdef HTDATATYPE
26 #define hashFreeData JOIN(HASHTYPE,FreeData)
27 typedef HTDATATYPE (*hashFreeData) (HTDATATYPE);
28 #endif
29
30 /**
31  * Create hash table.
32  * If keySize > 0, the key is duplicated within the table (which costs
33  * memory, but may be useful anyway.
34  * @param numBuckets    number of hash buckets
35  * @param fn            function to generate hash value for key
36  * @param eq            function to compare hash keys for equality
37  * @param freeKey       function to free the keys or NULL
38  * @param freeData      function to free the data or NULL
39  * @return              pointer to initialized hash table
40  */
41 RPM_GNUC_INTERNAL
42 HASHTYPE  HASHPREFIX(Create)(int numBuckets,
43                              hashFunctionType fn, hashEqualityType eq,
44                              hashFreeKey freeKey
45 #ifdef HTDATATYPE
46 , hashFreeData freeData
47 #endif
48 );
49
50 /**
51  * Destroy hash table.
52  * @param ht            pointer to hash table
53  * @return              NULL always
54  */
55 RPM_GNUC_INTERNAL
56 HASHTYPE  HASHPREFIX(Free)( HASHTYPE ht);
57
58 /**
59  * Add item to hash table.
60  * @param ht            pointer to hash table
61  * @param key           key
62  * @param data          data value
63  */
64 RPM_GNUC_INTERNAL
65 void  HASHPREFIX(AddEntry)(HASHTYPE ht, HTKEYTYPE key
66 #ifdef HTDATATYPE
67 , HTDATATYPE data
68 #endif
69 );
70
71 #ifdef HTDATATYPE
72
73 /**
74  * Retrieve item from hash table.
75  * @param ht            pointer to hash table
76  * @param key           key value
77  * @retval data         address to store data value from bucket
78  * @retval dataCount    address to store data value size from bucket
79  * @retval tableKey     address to store key value from bucket (may be NULL)
80  * @return              1 on success, 0 if the item is not found.
81  */
82 RPM_GNUC_INTERNAL
83 int  HASHPREFIX(GetEntry)(HASHTYPE ht, HTKEYTYPE key,
84                 HTDATATYPE** data,
85                 int * dataCount,
86                 HTKEYTYPE* tableKey);
87 #endif
88
89 /**
90  * Check for key in hash table.
91  * @param ht            pointer to hash table
92  * @param key           key value
93  * @return              1 if the key is present, 0 otherwise
94  */
95 RPM_GNUC_INTERNAL
96 int  HASHPREFIX(HasEntry)(HASHTYPE ht, HTKEYTYPE key);
97
98 /**
99  * Print statistics about the hash to stderr
100  * This is for debugging only
101  * @param ht            pointer to hash table
102  */
103 RPM_GNUC_INTERNAL
104 void HASHPREFIX(PrintStats)(HASHTYPE ht);