Serious-Engine/Sources/Engine/Templates/HashTableTemplate.h

73 lines
2.4 KiB
C
Raw Normal View History

2016-03-11 14:57:17 +01:00
/* Copyright (c) 2002-2012 Croteam Ltd. All rights reserved. */
#if !defined(TYPE) || !defined(VALUE_TYPE) || !defined(CHashTableSlot_TYPE) || !defined(CHashTable_TYPE)
#error
#endif
#include <Engine/Templates/StaticArray.h>
class CHashTableSlot_TYPE {
public:
ULONG hts_ulKey; // hashing key
TYPE *hts_ptElement; // the element inhere
CHashTableSlot_TYPE(void) { hts_ptElement = NULL; };
void Clear(void) { hts_ptElement = NULL; };
};
/*
* Template class for storing pointers to objects for fast access by name.
*/
class CHashTable_TYPE {
// implementation:
public:
INDEX ht_ctCompartments; // number of compartments in table
INDEX ht_ctSlotsPerComp; // number of slots in one compartment
INDEX ht_ctSlotsPerCompStep; // allocation step for number of slots in one compartment
CStaticArray<CHashTableSlot_TYPE> ht_ahtsSlots; // all slots are here
ULONG (*ht_GetItemKey)(VALUE_TYPE &Value);
VALUE_TYPE (*ht_GetItemValue)(TYPE* Item);
// internal finding, returns pointer to the the slot
CHashTableSlot_TYPE *FindSlot(ULONG ulKey, VALUE_TYPE &Value);
// internal finding, returns the index of the item in the nametable
INDEX FindSlotIndex(ULONG ulKey, VALUE_TYPE &Value);
// get the item stored in the hashtable by it's index
TYPE* GetItemFromIndex(INDEX iIndex);
// get the value of the item stored in the hashtable by it's index
VALUE_TYPE GetValueFromIndex(INDEX iIndex);
// expand the hash table to next step
void Expand(void);
// interface:
public:
// default constructor
CHashTable_TYPE(void);
// destructor -- frees all memory
~CHashTable_TYPE(void);
// remove all slots, and reset the nametable to initial (empty) state
void Clear(void);
/* Set allocation parameters. */
void SetAllocationParameters(INDEX ctCompartments, INDEX ctSlotsPerComp, INDEX ctSlotsPerCompStep);
// set callbacks
void SetCallbacks(ULONG (*GetItemKey)(VALUE_TYPE &Item), VALUE_TYPE (*GetItemValue)(TYPE* Item));
// find an object by value, return a pointer to it
TYPE* Find(VALUE_TYPE &Value);
// find an object by value, return it's index
INDEX FindIndex(VALUE_TYPE &Value);
// add a new object
void Add(TYPE *ptNew);
// remove an object
void Remove(TYPE *ptOld);
// remove an object
void RemoveAll();
// remove all objects but keep slots
void Reset(void);
// get estimated efficiency of the hashtable
void ReportEfficiency(void);
};