knight/shared/KeHashTable.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed Mar 31 08:51:25 2010 -0400 (2010-03-31)
changeset 2943 1bbdb33fee53
parent 2541 0352326a1f3f
child 3980 dc7dfcd855dc
permissions -rw-r--r--
Added TF2_StunPlayer native and TF2_GetPlayerConditionFlags stock to TF2 ext. (bug 4331, r=pred)
     1 #ifndef _INCLUDE_KNIGHT_KE_HASHTABLE_H_
     2 #define _INCLUDE_KNIGHT_KE_HASHTABLE_H_
     3 
     4 #include <KeStdTypes.h>
     5 #include <KnightAllocator.h>
     6 
     7 namespace Knight
     8 {
     9 	class KeHashTable;
    10 
    11 	/**
    12 	 * @brief Must generate a hash function given a key.
    13 	 *
    14 	 * @param key				Pointer to the key.
    15 	 * @return					Hash value.
    16 	 */
    17 	typedef uint32_t (*KeHashGenerator)(const void *key);
    18 
    19 	/**
    20 	 * @brief Must compare two values.
    21 	 *
    22 	 * @param val1				First value.
    23 	 * @param val2				Second value.
    24 	 * @return					True if equal, false if not.
    25 	 */
    26 	typedef bool (*KeHashComparator)(const void *val1, const void *val2);
    27 
    28 	/**
    29 	 * @brief Must call the destructor of the given data, and free if necessary.
    30 	 *
    31 	 * @param val				Pointer to data.
    32 	 */
    33 	typedef void (*KeHashDestructor)(const void *val);
    34 
    35 	/**
    36 	 * @brief Must transfer the contents of an object from the source to the destination.
    37 	 *
    38 	 * @param dest				Destination address.
    39 	 * @param source			Source address.
    40 	 */
    41 	typedef void (*KeHashCopyCtor)(void *dest, const void *source);
    42 
    43 	/**
    44 	 * @brief Contains information about how to process keys and values in a hash table.
    45 	 */
    46 	struct KeHashMarshal
    47 	{
    48 		size_t bytes;				/**< Bytes of storage needed (0 to use pointers). */
    49 		KeHashComparator cmp;		/**< Comparator (if NULL, void * comparison used) */
    50 		KeHashDestructor dtor;		/**< Optional function for performing dtor cleanup. */
    51 		KeHashCopyCtor ctor;		/**< If bytes != 0, must be a valid function 
    52 										 (ignored otherwise). */
    53 	};
    54 
    55 	/**
    56 	 * @brief Creates a new hash table structure.
    57 	 *
    58 	 * @param bits				Dictates starting number of buckets as a power of two.
    59 	 *							Pass 0 for the default (which is 4).
    60 	 * @param key_gen			Key generation function.
    61 	 * @param key_marshal		Structure detailing how to marshal keys.
    62 	 * @param vak_marshal		Structure detailing how to marshal values.
    63 	 * @param nodeAlloc			Node allocator (can be NULL for malloc/free).
    64 	 * @param keep_free_list	True to keep a free list of nodes, false otherwise.
    65 	 * @return					New hash table container.
    66 	 */
    67 	extern KeHashTable *KE_CreateHashTable(
    68 		uint32_t bits,
    69 		KeHashGenerator key_gen,
    70 		const KeHashMarshal *key_marshal,
    71 		const KeHashMarshal *val_marshal,
    72 		ke_allocator_t *nodeAlloc,
    73 		bool keep_free_list
    74 		);
    75 
    76 	/**
    77 	 * @brief Destroys a hash table.
    78 	 *
    79 	 * @param table				Hash table.
    80 	 */
    81 	extern void KE_DestroyHashTable(KeHashTable *table);
    82 
    83 	/**
    84 	 * @brief Adds a key/value to the hash table.  If the pair already exists, the old value 
    85 	 * is overwritten (calling any destructors as necessary).
    86 	 *
    87 	 * @param table				Hash table.
    88 	 * @param key				Key pointer.
    89 	 * @param val				Value pointer.
    90 	 */
    91 	extern void KE_AddToHashTable(KeHashTable *table, const void *key, void *val);
    92 
    93 	/**
    94 	 * @brief Removes a key entry from the hash table.
    95 	 *
    96 	 * @param table				Hash table.
    97 	 * @param key				Key pointer.
    98 	 */
    99 	extern void KE_RemoveFromHashTable(KeHashTable *table, const void *key);
   100 
   101 	/**
   102 	 * @brief Finds an entry in the hash table.
   103 	 *
   104 	 * @param table				Hash table.
   105 	 * @param key				Key pointer.
   106 	 * @param value				Pointer to store the value (optional).
   107 	 * @return					On success, true is returned and value is filled if given.
   108 	 *							On failure, false is failed and outputs are undefined.
   109 	 */
   110 	extern bool KE_FindInHashTable(KeHashTable *table, const void *key, void **value);
   111 
   112 	/**
   113 	 * @brief Clears all entries in the hash table (caching free entries when possible).
   114 	 *
   115 	 * @param table				Hash table.
   116 	 */
   117 	extern void KE_ClearHashTable(KeHashTable *table);
   118 
   119 	/**
   120 	 * @brief Generic function for hashing strings.
   121 	 *
   122 	 * @param str				Key string.
   123 	 * @return					Hash value.
   124 	 */
   125 	extern uint32_t KE_HashString(const void *str);
   126 
   127     /**
   128      * @brief Generic case-sensitive comparison of strings.
   129      *
   130      * @param str1              First string.
   131      * @param str2              Second string.
   132      * @return                  True if equal, false otherwise.
   133      */
   134     extern bool KE_AreStringsEqual(const void* str1, const void* str2);
   135 }
   136 
   137 #endif //_INCLUDE_KNIGHT_KE_HASHTABLE_H_
   138