18 return (
unsigned int)value;
30 template <
class TKey,
class TValue>
37 vrpn_Hash(
unsigned int (*func)(
const TKey &key),
int init=16);
46 TValue&
Find(
const TKey &key);
48 const TValue&
Find(
const TKey &key)
const;
50 bool IsPresent(
const TValue &value, TKey &key)
const;
59 bool Add(TKey key, TValue value);
71 struct HashItem *next;
76 void MakeNull(HashItem **table,
int size);
78 unsigned int m_NrItems;
79 unsigned int m_SizeHash;
80 unsigned int m_InitialSize;
85 unsigned int (*HashFunction)(
const TKey &key);
87 mutable HashItem *m_CurrentItem;
96 template <
class TKey,
class TValue>
99 HashFunction = vrpn_LinearHashFunction<TKey>;
101 m_InitialSize = m_SizeHash = init;
102 m_Items =
new HashItem*[m_SizeHash];
103 MakeNull( m_Items, m_SizeHash );
114 template <
class TKey,
class TValue>
119 m_InitialSize = m_SizeHash = init;
120 m_Items =
new HashItem*[m_SizeHash];
121 MakeNull( m_Items, m_SizeHash );
127 template <
class TKey,
class TValue>
135 template <
class TKey,
class TValue>
143 m_SizeHash = m_InitialSize;
144 m_Items =
new HashItem*[m_SizeHash];
145 MakeNull( m_Items, m_SizeHash );
150 template <
class TKey,
class TValue>
154 TValue &result = zero ;
156 unsigned int HashValue = HashFunction( key ) % m_SizeHash;
157 if ( m_Items[ HashValue ] != 0 )
158 if ( m_Items[ HashValue ]->key == key )
160 result = m_Items[ HashValue ]->value;
161 m_CurrentItem = m_Items[ HashValue ];
167 template <
class TKey,
class TValue>
171 TValue &result = zero ;
173 unsigned int HashValue = HashFunction( key ) % m_SizeHash;
174 if ( m_Items[ HashValue ] != 0 )
175 if ( m_Items[ HashValue ]->key == key )
176 result = m_Items[ HashValue ]->value;
181 template <
class TKey,
class TValue>
184 bool searching = MoveFirst();
188 if( GetCurrentValue() == value )
190 key = GetCurrentKey();
193 searching = MoveNext();
200 template <
class TKey,
class TValue>
206 unsigned int HashValue = HashFunction( key ) % m_SizeHash;
207 HashItem* elementPointer = m_Items[ HashValue ];
208 if (elementPointer != 0)
209 theKey = m_Items[ HashValue ]->key;
212 if ( elementPointer != 0 )
220 result = Add( key, value );
228 HashItem *item =
new HashItem;
231 item->next = m_First;
233 m_Items[ HashValue ] = item;
234 m_CurrentItem = m_Items[ HashValue ];
242 template <
class TKey,
class TValue>
247 unsigned int HashValue = HashFunction( key ) % m_SizeHash;
248 if ( m_Items[ HashValue ] != 0 ) {
249 if ( m_Items[ HashValue ]->key == key ) {
254 if ( m_Items[ HashValue ] == m_First ) {
255 m_First = m_First->next;
258 for ( item = m_First ; item->next != m_Items[ HashValue ]; item = item->next );
259 item->next = item->next->next;
263 delete m_Items[ HashValue ];
264 m_Items[ HashValue ] = 0;
271 template <
class TKey,
class TValue>
274 m_CurrentItem = m_First;
276 return ( m_First==NULL ) ? false :
true;
279 template <
class TKey,
class TValue>
282 if ( m_CurrentItem == NULL )
285 m_CurrentItem = m_CurrentItem->next;
286 return ( m_CurrentItem != NULL ) ;
289 template <
class TKey,
class TValue>
293 return m_CurrentItem->value;
298 template <
class TKey,
class TValue>
302 m_CurrentItem->value=theValue;
305 template <
class TKey,
class TValue>
310 return m_CurrentItem->key;
315 template <
class TKey,
class TValue>
323 theKey = m_CurrentItem->key;
324 theValue = m_CurrentItem->value;
331 template <
class TKey,
class TValue>
334 for (
int i = 0 ; i < size ; i++ )
338 template <
class TKey,
class TValue>
342 int OldSizeHash = m_SizeHash;
344 temp =
new HashItem*[m_SizeHash];
345 MakeNull( temp, m_SizeHash );
346 HashItem *NewFirst = 0;
347 for ( HashItem *item = m_First ; item != 0 ; item = item->next )
349 unsigned int HashValue = HashFunction( item->key )% OldSizeHash;
350 HashItem *NewItem =
new HashItem;
351 NewItem->key = item->key;
352 NewItem->value = item->value;
353 NewItem->next = NewFirst;
355 HashValue = HashFunction( item->key ) % m_SizeHash;
356 temp[ HashValue ] = NewItem;
366 template <
class TKey,
class TValue>
369 for ( HashItem *item = m_First ; item != 0 ; )
bool GetCurrentKeyAndValue(TKey &theKey, TValue &theValue) const
returns the key and the value of the current item
bool MoveNext() const
moves the iterator to the next element and returns false if no more element is present ...
bool MoveFirst() const
moves an iterator to the first element and returns false if no element is present ...
TKey GetCurrentKey() const
returns the key of the current item
bool IsPresent(const TValue &value, TKey &key) const
checks if the Hash contains a value and returns its key
bool Add(TKey key, TValue value)
adds a new (key, value) pair, returns true if succeeded
unsigned int GetNrItems() const
returns the number of items in the Hash
Hash class (not thread-safe)
void Clear()
clears the Hash
void SetCurrentValue(TValue theValue)
sets the Value of the current key
unsigned int vrpn_LinearUnsignedIntHashFunction(const unsigned int &i)
unsigned int vrpn_LinearHashFunction(const T &value)
TValue GetCurrentValue() const
returns the value of the current item
virtual ~vrpn_Hash()
destructor
vrpn_Hash(int init=16)
constructor
TValue & Find(const TKey &key)
returns the value that belongs to this key
bool Remove(TKey key)
removes the value that belongs to this key, returns true if succeeded