ScummVM API documentation
vhash_table.h
1
2/* ScummVM - Graphic Adventure Engine
3 *
4 * ScummVM is the legal property of its developers, whose names
5 * are too numerous to list here. Please refer to the COPYRIGHT
6 * file distributed with this source distribution.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef BAGEL_BOFLIB_VHASH_TABLE_H
24#define BAGEL_BOFLIB_VHASH_TABLE_H
25
26#include "bagel/spacebar/boflib/list.h"
27
28namespace Bagel {
29namespace SpaceBar {
30
31template<class T, int S>
33public:
34 CBofVHashTable(unsigned(*hashFun)(const T &));
35 virtual ~CBofVHashTable();
36 bool contains(const T &val);
37 void insert(const T &val);
38
39private:
40 // Default constructor is not allowed. A declaration is provided,
41 // but an implementation is not. A link error will result if an
42 // attempt is made to use this constructor. The location which
43 // is attempting to reference the default constructor should be changed
44 // so that it calls the class constructor which takes a pointer to a
45 // function and a table size.
47
48 // Member which holds the hash table itself.
49 CBofList<T *> _xHashTable[S];
50
51 // Member which holds a pointer to the table's hashing
52 // function.
53 unsigned(*_pHashFunction)(const T &);
54
55 // Member which holds the count of buckets in the hash table.
56 int _nHashTableSize;
57
58 // Boolean which affords a light-weight test of whether the hash
59 // table is empty.
60 //
61 bool _bisEmpty;
62};
63
64// CBofVHashTable::CBofVHashTable - class constructor.
65//
66template<class T, int S>
67CBofVHashTable<T, S>::CBofVHashTable(unsigned(*hashFun)(const T &)) : _nHashTableSize(S),
68 _pHashFunction(hashFun), _bisEmpty(true) {
69}
70
71// CBofVHashTable::~CBofVHashTable - class destructor.
72template<class T, int S>
73CBofVHashTable<T, S>::~CBofVHashTable() {
74 for (int x = 0; x < _nHashTableSize; x++) {
75 CBofList<T *> *pHashBucket = &_xHashTable[x];
76 int nListEntries = pHashBucket->getActualCount();
77
78 for (int i = 0; i < nListEntries; i++) {
79 T *pListItem = pHashBucket->getNodeItem(i);
80 delete pListItem;
81 pHashBucket->setNodeItem(i, (T *)nullptr);
82 }
83 }
84}
85
86// CBofVHashTable<T, S>::insert - add a value to the hash table.
87template<class T, int S>
88void CBofVHashTable<T, S>::insert(const T &val) {
89 T *pNodeValue = new T(val);
90 CBofListNode<T *> *pNode = new CBofListNode<T *>(pNodeValue);
91 assert(pNode != nullptr);
92
93 int nHashBucketIndex = ((*_pHashFunction)(val)) % _nHashTableSize;
94 assert(nHashBucketIndex < _nHashTableSize);
95
96 CBofList<T *> *pHashBucket = &_xHashTable[nHashBucketIndex];
97 assert(pHashBucket != nullptr);
98 pHashBucket->addToTail(pNode);
99}
100
101// CBofVHashTable<T, S>contains - predicate to test whether a value is stored in the hash table.
102template<class T, int S>
103bool CBofVHashTable<T, S>::contains(const T &val) {
104 bool returnValue = false;
105 int nHashBucketIndex = ((*_pHashFunction)(val)) % _nHashTableSize;
106 assert(nHashBucketIndex < _nHashTableSize);
107
108 CBofVHashTable<T, S> *const fakeThis = (CBofVHashTable<T, S> *const)this;
109 CBofList<T *> *pHashBucket = &(fakeThis->_xHashTable[nHashBucketIndex]);
110 assert(pHashBucket != nullptr);
111 int nItemsInBucket = pHashBucket->getCount();
112 for (int i = 0; i < nItemsInBucket; i++) {
113 T *TableEntry = pHashBucket->getNodeItem(i);
114 if (TableEntry->compareNoCase((const char *)val) == 0) {
115 returnValue = true;
116 break;
117 }
118 }
119 return returnValue;
120}
121
122} // namespace SpaceBar
123} // namespace Bagel
124
125#endif
Definition: vhash_table.h:32