1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Special case hash table for console commands
//
// $NoKeywords: $
//
//===========================================================================//
#if !defined( CONCOMMANDHASH_H )
#define CONCOMMANDHASH_H
#ifdef _WIN32
#pragma once
#endif
#include "utllinkedlist.h"
#include "generichash.h"
// This is a hash table class very similar to the CUtlHashFast, but
// modified specifically so that we can look up ConCommandBases
// by string names without having to actually store those strings in
// the dictionary, and also iterate over all of them.
// It uses separate chaining: each key hashes to a bucket, each
// bucket is a linked list of hashed commands. We store the hash of
// the command's string name as well as its pointer, so we can do
// the linked list march part of the Find() operation more quickly.
class CConCommandHash
{
public:
typedef int CCommandHashHandle_t;
typedef unsigned int HashKey_t;
// Constructor/Deconstructor.
CConCommandHash();
~CConCommandHash();
// Memory.
void Purge( bool bReinitialize );
// Invalid handle.
static CCommandHashHandle_t InvalidHandle( void ) { return ( CCommandHashHandle_t )~0; }
inline bool IsValidHandle( CCommandHashHandle_t hHash ) const;
/// Initialize.
void Init( void ); // bucket count is hardcoded in enum below.
/// Get hash value for a concommand
static inline HashKey_t Hash( const ConCommandBase *cmd );
// Size not available; count is meaningless for multilists.
// int Count( void ) const;
// Insertion.
CCommandHashHandle_t Insert( ConCommandBase *cmd );
CCommandHashHandle_t FastInsert( ConCommandBase *cmd );
// Removal.
void Remove( CCommandHashHandle_t hHash );
void RemoveAll( void );
// Retrieval.
inline CCommandHashHandle_t Find( const char *name ) const;
CCommandHashHandle_t Find( const ConCommandBase *cmd ) const;
// A convenience version of Find that skips the handle part
// and returns a pointer to a concommand, or NULL if none was found.
inline ConCommandBase * FindPtr( const char *name ) const;
inline ConCommandBase * &operator[]( CCommandHashHandle_t hHash );
inline ConCommandBase *const &operator[]( CCommandHashHandle_t hHash ) const;
#ifdef _DEBUG
// Dump a report to MSG
void Report( void );
#endif
// Iteration
struct CCommandHashIterator_t
{
int bucket;
CCommandHashHandle_t handle;
CCommandHashIterator_t(int _bucket, const CCommandHashHandle_t &_handle)
: bucket(_bucket), handle(_handle) {};
// inline operator UtlHashFastHandle_t() const { return handle; };
};
inline CCommandHashIterator_t First() const;
inline CCommandHashIterator_t Next( const CCommandHashIterator_t &hHash ) const;
inline bool IsValidIterator( const CCommandHashIterator_t &iter ) const;
inline ConCommandBase * &operator[]( const CCommandHashIterator_t &iter ) { return (*this)[iter.handle]; }
inline ConCommandBase * const &operator[]( const CCommandHashIterator_t &iter ) const { return (*this)[iter.handle]; }
private:
// a find func where we've already computed the hash for the string.
// (hidden private in case we decide to invent a custom string hash func
// for this class)
CCommandHashHandle_t Find( const char *name, HashKey_t hash) const;
protected:
enum
{
kNUM_BUCKETS = 256,
kBUCKETMASK = kNUM_BUCKETS - 1,
};
struct HashEntry_t
{
HashKey_t m_uiKey;
ConCommandBase *m_Data;
HashEntry_t(unsigned int _hash, ConCommandBase * _cmd)
: m_uiKey(_hash), m_Data(_cmd) {};
HashEntry_t(){};
};
typedef CUtlFixedLinkedList<HashEntry_t> datapool_t;
CUtlVector<CCommandHashHandle_t> m_aBuckets;
datapool_t m_aDataPool;
};
inline bool CConCommandHash::IsValidHandle( CCommandHashHandle_t hHash ) const
{
return m_aDataPool.IsValidIndex(hHash);
}
inline CConCommandHash::CCommandHashHandle_t CConCommandHash::Find( const char *name ) const
{
return Find( name, HashStringCaseless(name) );
}
inline ConCommandBase * &CConCommandHash::operator[]( CCommandHashHandle_t hHash )
{
return ( m_aDataPool[hHash].m_Data );
}
inline ConCommandBase *const &CConCommandHash::operator[]( CCommandHashHandle_t hHash ) const
{
return ( m_aDataPool[hHash].m_Data );
}
//-----------------------------------------------------------------------------
// Purpose: For iterating over the whole hash, return the index of the first element
//-----------------------------------------------------------------------------
CConCommandHash::CCommandHashIterator_t CConCommandHash::First() const
{
// walk through the buckets to find the first one that has some data
int bucketCount = m_aBuckets.Count();
const CCommandHashHandle_t invalidIndex = m_aDataPool.InvalidIndex();
for ( int bucket = 0 ; bucket < bucketCount ; ++bucket )
{
CCommandHashHandle_t iElement = m_aBuckets[bucket]; // get the head of the bucket
if ( iElement != invalidIndex )
return CCommandHashIterator_t( bucket, iElement );
}
// if we are down here, the list is empty
return CCommandHashIterator_t( -1, invalidIndex );
}
//-----------------------------------------------------------------------------
// Purpose: For iterating over the whole hash, return the next element after
// the param one. Or an invalid iterator.
//-----------------------------------------------------------------------------
CConCommandHash::CCommandHashIterator_t
CConCommandHash::Next( const CConCommandHash::CCommandHashIterator_t &iter ) const
{
// look for the next entry in the current bucket
CCommandHashHandle_t next = m_aDataPool.Next(iter.handle);
const CCommandHashHandle_t invalidIndex = m_aDataPool.InvalidIndex();
if ( next != invalidIndex )
{
// this bucket still has more elements in it
return CCommandHashIterator_t(iter.bucket, next);
}
// otherwise look for the next bucket with data
int bucketCount = m_aBuckets.Count();
for ( int bucket = iter.bucket+1 ; bucket < bucketCount ; ++bucket )
{
CCommandHashHandle_t next = m_aBuckets[bucket]; // get the head of the bucket
if (next != invalidIndex)
return CCommandHashIterator_t( bucket, next );
}
// if we're here, there's no more data to be had
return CCommandHashIterator_t(-1, invalidIndex);
}
bool CConCommandHash::IsValidIterator( const CCommandHashIterator_t &iter ) const
{
return ( (iter.bucket >= 0) && (m_aDataPool.IsValidIndex(iter.handle)) );
}
inline CConCommandHash::HashKey_t CConCommandHash::Hash( const ConCommandBase *cmd )
{
return HashStringCaseless( cmd->GetName() );
}
inline ConCommandBase * CConCommandHash::FindPtr( const char *name ) const
{
CCommandHashHandle_t handle = Find(name);
if (handle == InvalidHandle())
{
return NULL;
}
else
{
return (*this)[handle];
}
}
#endif
|