Ult3D
Implementation of "Ultimate 3D Game Engine Design & Architecture" by Allan Sherrod
Loading...
Searching...
No Matches
HashTable.h
Go to the documentation of this file.
1#ifndef ULT_HASH_TABLE_H_INCLUDED
2#define ULT_HASH_TABLE_H_INCLUDED
3
4#include <string>
5#include <vector>
6
7namespace Ult
8{
9
14template<typename T>
16{
17public:
22 mKey(0)
23 {}
24
28 {}
29
31 int GetKey() const { return mKey; }
33 void SetKey(int k) { mKey = k; }
34
36 T GetObject() { return mObj; }
38 void SetObj(T obj) { mObj = obj; }
39
41 bool operator==(const HashItem& item) {
42 return (mKey == item.mKey);
43 }
44
45 HashItem& operator=(const HashItem& item) {
46 mKey = item.mKey;
47 mObj = item.mObj;
48 return *this;
49 }
50
51private:
52 int mKey;
53 T mObj;
54};
55
60template<typename T>
62{
63public:
69 HashTable(int size) :
70 mSize(0),
71 mTable(nullptr)
72 {
73 if (size > 0) {
74 mSize = size;
75 mTable = new std::vector<HashItem<T>>[mSize];
76 }
77 }
78
82 {
83 mSize = 0;
84 if (mTable) {
85 delete[] mTable;
86 mTable = nullptr;
87 }
88 }
89
93 void Insert(const HashItem<T>& obj) {
94 int hash = HashFunction(obj.GetKey());
95 mTable[hash].push_back(obj);
96 }
97
100 void Delete(const int key) {
101 HashItem<T> item;
102 item.SetKey(key);
103 int hash = HashFunction(key);
104 std::vector<HashItem<T>>* ptr = &mTable[hash];
105 for (auto it = ptr->begin(); it != ptr->end(); it++) {
106 if (*it == item) {
107 ptr->erase(it);
108 break;
109 }
110 }
111 }
112
117 HashItem<T> Find(int key) {
118 HashItem<T> item, tmp;
119 item.SetKey(key);
120 int hash = HashFunction(key);
121 std::vector<HashItem<T>>* ptr = &mTable[hash];
122 for (int i = 0; i<(int)ptr->size(); i++) {
123 tmp = (*ptr)[i];
124 if (tmp == item) {
125 return tmp;
126 }
127 }
128 item.SetKey(-1);
129 return item;
130 }
131
135 int HashFunction(const int key) const {
136 return key % mSize;
137 }
138
141 int HashFunction(const std::string& str) {
142 // book
143 /*int hash = 0;
144 for (int i = 0; i<(int)str.size(); i++) {
145 int val = (int)str[i];
146 hash = (hash * 256 + val) % mSize;
147 }
148 return hash;*/
149 // https://stackoverflow.com/questions/8317508/hash-function-for-a-string
150 unsigned long hash = 0;
151 for (int i=0; i<(int)str.size(); i++) {
152 hash = (hash * 131) + str[i];
153 }
154 return int(hash % mSize);
155 }
156
160 int GetSize() const { return mSize; }
161
162private:
163 int mSize;
164 std::vector<HashItem<T>>* mTable;
165};
166
167} // namespace Ult
168
169#endif // ULT_HASH_TABLE_H_INCLUDED
Definition HashTable.h:16
T GetObject()
Definition HashTable.h:36
bool operator==(const HashItem &item)
Definition HashTable.h:41
void SetObj(T obj)
Definition HashTable.h:38
HashItem & operator=(const HashItem &item)
Definition HashTable.h:45
HashItem()
Definition HashTable.h:21
void SetKey(int k)
Definition HashTable.h:33
int GetKey() const
Definition HashTable.h:31
~HashItem()
Definition HashTable.h:27
void Delete(const int key)
Definition HashTable.h:100
int HashFunction(const int key) const
Definition HashTable.h:135
void Insert(const HashItem< T > &obj)
Definition HashTable.h:93
HashItem< T > Find(int key)
Definition HashTable.h:117
HashTable(int size)
Definition HashTable.h:69
int GetSize() const
Definition HashTable.h:160
~HashTable()
Definition HashTable.h:81
int HashFunction(const std::string &str)
Definition HashTable.h:141
Definition Archive.h:13