knight/shared/KeHashTable.h
author David Anderson <dvander@alliedmods.net>
Mon Aug 12 00:22:54 2013 +0100 (2013-08-12)
changeset 3990 c7fb79059d7a
parent 3980 dc7dfcd855dc
permissions -rw-r--r--
Eliminate InfoVars::cip (bug 5844 part 7, r=ds).
     1 #ifndef _INCLUDE_KNIGHT_KE_HASHTABLE_H_
     2 #define _INCLUDE_KNIGHT_KE_HASHTABLE_H_
     3 
     4 #include <stddef.h>
     5 #include <stdint.h>
     6 #include <KnightAllocator.h>
     7 
     8 namespace Knight
     9 {
    10 	class KeHashTable;
    11 
    12 	/**
    13 	 * @brief Must generate a hash function given a key.
    14 	 *
    15 	 * @param key				Pointer to the key.
    16 	 * @return					Hash value.
    17 	 */
    18 	typedef uint32_t (*KeHashGenerator)(const void *key);
    19 
    20 	/**
    21 	 * @brief Must compare two values.
    22 	 *
    23 	 * @param val1				First value.
    24 	 * @param val2				Second value.
    25 	 * @return					True if equal, false if not.
    26 	 */
    27 	typedef bool (*KeHashComparator)(const void *val1, const void *val2);
    28 
    29 	/**
    30 	 * @brief Must call the destructor of the given data, and free if necessary.
    31 	 *
    32 	 * @param val				Pointer to data.
    33 	 */
    34 	typedef void (*KeHashDestructor)(const void *val);
    35 
    36 	/**
    37 	 * @brief Must transfer the contents of an object from the source to the destination.
    38 	 *
    39 	 * @param dest				Destination address.
    40 	 * @param source			Source address.
    41 	 */
    42 	typedef void (*KeHashCopyCtor)(void *dest, const void *source);
    43 
    44 	/**
    45 	 * @brief Contains information about how to process keys and values in a hash table.
    46 	 */
    47 	struct KeHashMarshal
    48 	{
    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). */
    54 	};
    55 
    56 	/**
    57 	 * @brief Creates a new hash table structure.
    58 	 *
    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.
    67 	 */
    68 	extern KeHashTable *KE_CreateHashTable(
    69 		uint32_t bits,
    70 		KeHashGenerator key_gen,
    71 		const KeHashMarshal *key_marshal,
    72 		const KeHashMarshal *val_marshal,
    73 		ke_allocator_t *nodeAlloc,
    74 		bool keep_free_list
    75 		);
    76 
    77 	/**
    78 	 * @brief Destroys a hash table.
    79 	 *
    80 	 * @param table				Hash table.
    81 	 */
    82 	extern void KE_DestroyHashTable(KeHashTable *table);
    83 
    84 	/**
    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).
    87 	 *
    88 	 * @param table				Hash table.
    89 	 * @param key				Key pointer.
    90 	 * @param val				Value pointer.
    91 	 */
    92 	extern void KE_AddToHashTable(KeHashTable *table, const void *key, void *val);
    93 
    94 	/**
    95 	 * @brief Removes a key entry from the hash table.
    96 	 *
    97 	 * @param table				Hash table.
    98 	 * @param key				Key pointer.
    99 	 */
   100 	extern void KE_RemoveFromHashTable(KeHashTable *table, const void *key);
   101 
   102 	/**
   103 	 * @brief Finds an entry in the hash table.
   104 	 *
   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.
   110 	 */
   111 	extern bool KE_FindInHashTable(KeHashTable *table, const void *key, void **value);
   112 
   113 	/**
   114 	 * @brief Clears all entries in the hash table (caching free entries when possible).
   115 	 *
   116 	 * @param table				Hash table.
   117 	 */
   118 	extern void KE_ClearHashTable(KeHashTable *table);
   119 
   120 	/**
   121 	 * @brief Generic function for hashing strings.
   122 	 *
   123 	 * @param str				Key string.
   124 	 * @return					Hash value.
   125 	 */
   126 	extern uint32_t KE_HashString(const void *str);
   127 
   128     /**
   129      * @brief Generic case-sensitive comparison of strings.
   130      *
   131      * @param str1              First string.
   132      * @param str2              Second string.
   133      * @return                  True if equal, false otherwise.
   134      */
   135     extern bool KE_AreStringsEqual(const void* str1, const void* str2);
   136 }
   137 
   138 #endif //_INCLUDE_KNIGHT_KE_HASHTABLE_H_
   139