removed useless things
merged changes for xash csdm into master
This commit is contained in:
parent
579971c5ff
commit
4b8c18001d
11 changed files with 102 additions and 334 deletions
|
|
@ -1643,7 +1643,8 @@ protected:
|
|||
public:
|
||||
HashItem (void)
|
||||
{
|
||||
m_next = NULL;
|
||||
m_next = NULL;
|
||||
m_index = 0;
|
||||
}
|
||||
|
||||
HashItem (int index, HashItem *next)
|
||||
|
|
@ -1948,291 +1949,6 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Class: HashTable
|
||||
// Represents Hash Table container.
|
||||
//
|
||||
template <class K, class V> class HashTable
|
||||
{
|
||||
protected:
|
||||
struct HashItem
|
||||
{
|
||||
public:
|
||||
K m_key;
|
||||
int m_index;
|
||||
|
||||
public:
|
||||
HashItem (void) { m_index = 0; }
|
||||
HashItem (const K &key, int index) { m_key = key; m_index = index; }
|
||||
};
|
||||
|
||||
int m_hashSize;
|
||||
Array <HashItem> *m_table;
|
||||
Array <V> m_symTable;
|
||||
|
||||
V *InternalGet (const K &keyName, bool create)
|
||||
{
|
||||
int index = GetIndex (keyName, create);
|
||||
|
||||
if (index < 0)
|
||||
return 0;
|
||||
|
||||
return &m_symTable[index];
|
||||
}
|
||||
|
||||
// Group: (Con/De)structors
|
||||
public:
|
||||
|
||||
//
|
||||
// Function: HashTable
|
||||
// Default hash table container constructor.
|
||||
//
|
||||
// Parameters:
|
||||
// hashSize - Initial hash size.
|
||||
//
|
||||
HashTable (int hashSize = 36)
|
||||
{
|
||||
m_hashSize = hashSize;
|
||||
m_table = new Array <HashItem> [hashSize];
|
||||
}
|
||||
|
||||
//
|
||||
// Function: ~HashTable
|
||||
// Default hash table container destructor.
|
||||
//
|
||||
virtual ~HashTable (void)
|
||||
{
|
||||
RemoveAll ();
|
||||
delete [] m_table;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: IsEmpty
|
||||
// Checks whether container is empty.
|
||||
//
|
||||
// Returns:
|
||||
// True if no elements in container, false otherwise.
|
||||
//
|
||||
bool IsEmpty (void)
|
||||
{
|
||||
return m_symTable.GetSize () == 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: SetupHashTable
|
||||
// Setups the hash table.
|
||||
//
|
||||
// Parameters:
|
||||
// hashSize - Initial hash size.
|
||||
//
|
||||
void SetupHashTable (int hashSize)
|
||||
{
|
||||
m_hashSize = hashSize;
|
||||
m_table = new Array <HashItem> [hashSize];
|
||||
}
|
||||
|
||||
//
|
||||
// Function: IsExists
|
||||
// Checks whether element exists in container.
|
||||
//
|
||||
// Parameters:
|
||||
// keyName - Key name of element, that should be checked.
|
||||
//
|
||||
// Returns:
|
||||
// True if element exists, false otherwise.
|
||||
//
|
||||
bool IsExists (const K &keyName)
|
||||
{
|
||||
return InternalGet (keyName, false) != 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: GetSize
|
||||
// Gets the size of container.
|
||||
//
|
||||
// Returns:
|
||||
// Number of elements in container.
|
||||
//
|
||||
int GetSize (void)
|
||||
{
|
||||
return m_symTable.GetSize ();
|
||||
}
|
||||
|
||||
V &operator [] (const K &keyName)
|
||||
{
|
||||
return *InternalGet (keyName, true);
|
||||
}
|
||||
|
||||
//
|
||||
// Function: Get
|
||||
// Gets the value, by it's index.
|
||||
//
|
||||
// Parameters:
|
||||
// index - Index of element.
|
||||
//
|
||||
// Returns:
|
||||
// Reference to element.
|
||||
//
|
||||
V &Get (int index)
|
||||
{
|
||||
return m_symTable[index];
|
||||
}
|
||||
|
||||
//
|
||||
// Function: GetElements
|
||||
// Gets the all elements of container.
|
||||
//
|
||||
// Returns:
|
||||
// Array of elements, containing inside container.
|
||||
//
|
||||
// See Also:
|
||||
// <Array>
|
||||
//
|
||||
const Array <V> &GetElements (void) const
|
||||
{
|
||||
return m_symTable;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: Find
|
||||
// Finds element by his key name.
|
||||
//
|
||||
// Parameters:
|
||||
// keyName - Key name to be searched.
|
||||
// element - Holder for element object.
|
||||
//
|
||||
// Returns:
|
||||
// True if element found, false otherwise.
|
||||
//
|
||||
bool Find (const K &keyName, V &element) const
|
||||
{
|
||||
V *hashPtr = const_cast <HashTable <K, V> *> (this)->InternalGet (keyName, false);
|
||||
|
||||
if (hashPtr != NULL)
|
||||
{
|
||||
element = *hashPtr;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: Find
|
||||
// Finds element by his key name.
|
||||
//
|
||||
// Parameters:
|
||||
// keyName - Key name to be searched.
|
||||
// elementPtr - Holder for element pointer.
|
||||
//
|
||||
// Returns:
|
||||
// True if element found, false otherwise.
|
||||
//
|
||||
bool Find (const K &keyName, V *&elementPtr) const
|
||||
{
|
||||
V *hashPtr = const_cast <HashTable <K, V> *> (this)->InternalGet (keyName, false);
|
||||
|
||||
if (hashPtr != NULL)
|
||||
{
|
||||
elementPtr = hashPtr;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: Remove
|
||||
// Removes element from container.
|
||||
//
|
||||
// Parameters:
|
||||
// keyName - Key name of element, that should be removed.
|
||||
//
|
||||
// Returns:
|
||||
// Removed element.
|
||||
//
|
||||
V Remove (const K &keyName)
|
||||
{
|
||||
V removeResult;
|
||||
|
||||
int hashId = Hash <K> (keyName) % m_hashSize;
|
||||
Array <HashItem> &bucket = m_table[hashId];
|
||||
|
||||
for (int i = 0; i < bucket.GetSize (); i++)
|
||||
{
|
||||
const HashItem &item = bucket[i];
|
||||
|
||||
if (item.m_key == keyName)
|
||||
{
|
||||
int index = item.m_index;
|
||||
|
||||
removeResult = m_symTable.RemoveAt (index);
|
||||
bucket.RemoveAt (i);
|
||||
|
||||
for (hashId = 0; hashId < m_hashSize; hashId++)
|
||||
{
|
||||
Array <HashItem> &bucket = m_table[hashId];
|
||||
|
||||
for (int j = 0; j < bucket.GetSize (); j++)
|
||||
{
|
||||
const HashItem &item = bucket[j];
|
||||
|
||||
if (item.m_index > index)
|
||||
item.m_index--;
|
||||
}
|
||||
}
|
||||
return removeResult;
|
||||
}
|
||||
}
|
||||
return removeResult;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: GetIndex
|
||||
// Gets index of element.
|
||||
//
|
||||
// Parameters:
|
||||
// keyName - Key of element.
|
||||
// create - If true and no element found by a keyName, create new element.
|
||||
//
|
||||
// Returns:
|
||||
// Either found index, created index, or -1 in case of error.
|
||||
//
|
||||
int GetIndex (const K &keyName, bool create)
|
||||
{
|
||||
int hashId = Hash <K> (keyName) % m_hashSize;
|
||||
Array <HashItem> &bucket = m_table[hashId];
|
||||
|
||||
for (int i = 0; i < bucket.GetSize (); i++)
|
||||
{
|
||||
const HashItem &item = bucket[i];
|
||||
|
||||
if (item.m_key == keyName)
|
||||
return item.m_index;
|
||||
}
|
||||
|
||||
if (create)
|
||||
{
|
||||
int symSize = m_symTable.GetSize ();
|
||||
m_symTable.SetSize (static_cast <int> (symSize + 1));
|
||||
|
||||
bucket.Push (HashItem (keyName, symSize));
|
||||
return symSize;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//
|
||||
// Function: RemoveAll
|
||||
// Removes all elements from container.
|
||||
//
|
||||
void RemoveAll (void)
|
||||
{
|
||||
for (int i = 0; i < m_hashSize; ++i)
|
||||
m_table[i].RemoveAll ();
|
||||
|
||||
m_symTable.RemoveAll ();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Class: String
|
||||
// Reference counted string class.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue