Added helper stock to check if a command exists. (
bug 5510, r=asherkin)
1 #ifndef _INCLUDE_KNIGHT_KE_HASHTABLE_H_
2 #define _INCLUDE_KNIGHT_KE_HASHTABLE_H_
6 #include <KnightAllocator.h>
13 * @brief Must generate a hash function given a key.
15 * @param key Pointer to the key.
18 typedef uint32_t (*KeHashGenerator)(const void *key);
21 * @brief Must compare two values.
23 * @param val1 First value.
24 * @param val2 Second value.
25 * @return True if equal, false if not.
27 typedef bool (*KeHashComparator)(const void *val1, const void *val2);
30 * @brief Must call the destructor of the given data, and free if necessary.
32 * @param val Pointer to data.
34 typedef void (*KeHashDestructor)(const void *val);
37 * @brief Must transfer the contents of an object from the source to the destination.
39 * @param dest Destination address.
40 * @param source Source address.
42 typedef void (*KeHashCopyCtor)(void *dest, const void *source);
45 * @brief Contains information about how to process keys and values in a hash table.
49 size_t bytes; /**< Bytes of storage needed (0 to use pointers). */
50 KeHashComparator cmp; /**< Comparator (if NULL, void * comparison used) */
51 KeHashDestructor dtor; /**< Optional function for performing dtor cleanup. */
52 KeHashCopyCtor ctor; /**< If bytes != 0, must be a valid function
53 (ignored otherwise). */
57 * @brief Creates a new hash table structure.
59 * @param bits Dictates starting number of buckets as a power of two.
60 * Pass 0 for the default (which is 4).
61 * @param key_gen Key generation function.
62 * @param key_marshal Structure detailing how to marshal keys.
63 * @param vak_marshal Structure detailing how to marshal values.
64 * @param nodeAlloc Node allocator (can be NULL for malloc/free).
65 * @param keep_free_list True to keep a free list of nodes, false otherwise.
66 * @return New hash table container.
68 extern KeHashTable *KE_CreateHashTable(
70 KeHashGenerator key_gen,
71 const KeHashMarshal *key_marshal,
72 const KeHashMarshal *val_marshal,
73 ke_allocator_t *nodeAlloc,
78 * @brief Destroys a hash table.
80 * @param table Hash table.
82 extern void KE_DestroyHashTable(KeHashTable *table);
85 * @brief Adds a key/value to the hash table. If the pair already exists, the old value
86 * is overwritten (calling any destructors as necessary).
88 * @param table Hash table.
89 * @param key Key pointer.
90 * @param val Value pointer.
92 extern void KE_AddToHashTable(KeHashTable *table, const void *key, void *val);
95 * @brief Removes a key entry from the hash table.
97 * @param table Hash table.
98 * @param key Key pointer.
100 extern void KE_RemoveFromHashTable(KeHashTable *table, const void *key);
103 * @brief Finds an entry in the hash table.
105 * @param table Hash table.
106 * @param key Key pointer.
107 * @param value Pointer to store the value (optional).
108 * @return On success, true is returned and value is filled if given.
109 * On failure, false is failed and outputs are undefined.
111 extern bool KE_FindInHashTable(KeHashTable *table, const void *key, void **value);
114 * @brief Clears all entries in the hash table (caching free entries when possible).
116 * @param table Hash table.
118 extern void KE_ClearHashTable(KeHashTable *table);
121 * @brief Generic function for hashing strings.
123 * @param str Key string.
124 * @return Hash value.
126 extern uint32_t KE_HashString(const void *str);
129 * @brief Generic case-sensitive comparison of strings.
131 * @param str1 First string.
132 * @param str2 Second string.
133 * @return True if equal, false otherwise.
135 extern bool KE_AreStringsEqual(const void* str1, const void* str2);
138 #endif //_INCLUDE_KNIGHT_KE_HASHTABLE_H_