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/boflib/list.h"
27 
28 namespace Bagel {
29 
30 template<class T, int S>
32 public:
33  CBofVHashTable(unsigned(*hashFun)(const T &));
34  virtual ~CBofVHashTable();
35  bool contains(const T &val);
36  void insert(const T &val);
37 
38 private:
39  // Default constructor is not allowed. A declaration is provided,
40  // but an implementation is not. A link error will result if an
41  // attempt is made to use this constructor. The location which
42  // is attempting to reference the default constructor should be changed
43  // so that it calls the class constructor which takes a pointer to a
44  // function and a table size.
46 
47  // Member which holds the hash table itself.
48  CBofList<T *> _xHashTable[S];
49 
50  // Member which holds a pointer to the table's hashing
51  // function.
52  unsigned(*_pHashFunction)(const T &);
53 
54  // Member which holds the count of buckets in the hash table.
55  int _nHashTableSize;
56 
57  // Boolean which affords a light-weight test of whether the hash
58  // table is empty.
59  //
60  bool _bisEmpty;
61 };
62 
63 // CBofVHashTable::CBofVHashTable - class constructor.
64 //
65 template<class T, int S>
66 CBofVHashTable<T, S>::CBofVHashTable(unsigned(*hashFun)(const T &)) : _nHashTableSize(S),
67  _pHashFunction(hashFun), _bisEmpty(true) {
68 }
69 
70 // CBofVHashTable::~CBofVHashTable - class destructor.
71 template<class T, int S>
73  for (int x = 0; x < _nHashTableSize; x++) {
74  CBofList<T *> *pHashBucket = &_xHashTable[x];
75  int nListEntries = pHashBucket->getActualCount();
76 
77  for (int i = 0; i < nListEntries; i++) {
78  T *pListItem = pHashBucket->getNodeItem(i);
79  delete pListItem;
80  pHashBucket->setNodeItem(i, (T *)nullptr);
81  }
82  }
83 }
84 
85 // CBofVHashTable<T, S>::insert - add a value to the hash table.
86 template<class T, int S>
87 void CBofVHashTable<T, S>::insert(const T &val) {
88  T *pNodeValue = new T(val);
89  CBofListNode<T *> *pNode = new CBofListNode<T *>(pNodeValue);
90  assert(pNode != nullptr);
91 
92  int nHashBucketIndex = ((*_pHashFunction)(val)) % _nHashTableSize;
93  assert(nHashBucketIndex < _nHashTableSize);
94 
95  CBofList<T *> *pHashBucket = &_xHashTable[nHashBucketIndex];
96  assert(pHashBucket != nullptr);
97  pHashBucket->addToTail(pNode);
98 }
99 
100 // CBofVHashTable<T, S>contains - predicate to test whether a value is stored in the hash table.
101 template<class T, int S>
102 bool CBofVHashTable<T, S>::contains(const T &val) {
103  bool returnValue = false;
104  int nHashBucketIndex = ((*_pHashFunction)(val)) % _nHashTableSize;
105  assert(nHashBucketIndex < _nHashTableSize);
106 
107  CBofVHashTable<T, S> *const fakeThis = (CBofVHashTable<T, S> *const)this;
108  CBofList<T *> *pHashBucket = &(fakeThis->_xHashTable[nHashBucketIndex]);
109  assert(pHashBucket != nullptr);
110  int nItemsInBucket = pHashBucket->getCount();
111  for (int i = 0; i < nItemsInBucket; i++) {
112  T *TableEntry = pHashBucket->getNodeItem(i);
113  if (TableEntry->compareNoCase((const char *)val) == 0) {
114  returnValue = true;
115  break;
116  }
117  }
118  return returnValue;
119 }
120 
121 } // namespace Bagel
122 
123 #endif
Definition: vhash_table.h:31
int getActualCount() const
Definition: list.h:193
Definition: bagel.h:31
void addToTail(CBofListNode< T > *pNewNode)
Definition: list.h:459
Definition: list.h:60
T getNodeItem(int nNodeIndex)
Definition: list.h:220
Definition: list.h:35