31 namespace OpenSkyNet {
48 return x_ | (y_ << 10) | (z_ << 20);
55 hash += (key_ & 0x000000FF);
58 hash += (key_ & 0x0000FF00);
61 hash += (key_ & 0x00FF0000);
64 hash += (key_ & 0xFF000000);
85 template<
class T=u
int>
91 TableNode() : _hash(0), _key(0) {}
95 _load(0), _size(powerOf2InitialSize_),
96 _initSize(_size), _sizeMinusOne(_size - 1),
97 _maxLoad(static_cast<
uint>(static_cast<float>(_size) * 0.75f)),
98 _isUsingDefaultValue(false),
99 _table(new TableNode[_size]) {
101 assert(_size < 2147483649u);
102 double logSize =
log(static_cast<double>(powerOf2InitialSize_));
103 double log2 =
log(2.0);
104 double logSizeBase2 = logSize / log2;
105 double logSizeBase2Truncated =
static_cast<double>(
static_cast<uint>(logSizeBase2));
106 assert(logSizeBase2 == logSizeBase2Truncated);
111 _load(0), _size(powerOf2InitialSize_),
112 _initSize(_size), _sizeMinusOne(_size - 1),
113 _maxLoad(static_cast<
uint>(static_cast<float>(_size) * 0.75f)),
114 _isUsingDefaultValue(true), _defaultValue(defaultValue_),
115 _table(new TableNode[_size]) {
117 assert(_size < 2147483649u);
118 double logSize =
log(static_cast<double>(powerOf2InitialSize_));
119 double log2 =
log(2.0);
120 double logSizeBase2 = logSize / log2;
121 double logSizeBase2Truncated =
static_cast<double>(
static_cast<uint>(logSizeBase2));
122 assert(logSizeBase2 == logSizeBase2Truncated);
124 for (
uint i = 0; i < _size; i++)
125 _table[i]._value = _defaultValue;
138 if (
this == &hashTable_)
return this;
140 _load = hashTable_._load;
141 _size = hashTable_._size;
142 _initSize = hashTable_._initSize;
143 _sizeMinusOne = hashTable_._sizeMinusOne;
144 _maxLoad = hashTable_._maxLoad;
145 _isUsingDefaultValue = hashTable_._isUsingDefaultValue;
146 _defaultValue = hashTable_._defaultValue;
148 if (_table)
delete[] _table;
149 _table =
new TableNode[_size];
150 for (
uint i = 0; i < _size; ++i) {
151 _table[i]._hash = hashTable_[i]._hash;
152 _table[i]._key = hashTable_[i]._key;
153 _table[i]._value = hashTable_[i]._value;
158 return _table[getBin(key_)]._value;
161 inline bool find(
const uint& key_, T& value_)
const {
162 uint bin = getBin(key_);
163 value_ = _table[bin]._value;
164 if (_table[bin]._hash)
return true;
168 inline bool find(
const uint& key_, T*& value_)
const {
169 uint bin = getBin(key_);
170 if (_table[bin]._hash) {
171 value_ = &_table[bin]._value;
185 inline void add(
const uint& key_,
const T& value_) {
187 uint bin = getBin(hash,key_,_table);
188 if (_table[bin]._hash) _table[bin]._value = value_;
190 _table[bin]._hash = hash;
191 _table[bin]._key = key_;
192 _table[bin]._value = value_;
194 if (_load > _maxLoad) resize();
202 uint bin = getBin(hash,key_,_table);
203 if (!_table[bin]._hash) {
204 _table[bin]._hash = hash;
205 _table[bin]._key = key_;
207 if (_load > _maxLoad) {
209 bin = getBin(hash,key_,_table);
212 return _table[bin]._value;
216 inline bool remove(
const uint& key_) {
217 uint i = getBin(key_);
218 if (!_table[i]._hash)
return false;
220 uint j = (i + 1) % _size;
222 while (_table[j]._hash) {
223 k = _table[j]._hash & _sizeMinusOne;
225 if ((j > i && (k <= i || k > j)) ||
226 (j < i && (k <= i && k > j))) {
227 _table[i] = _table[j];
237 _table[i]._value = _defaultValue;
247 inline void clear(
bool doRealloc_=
true) {
252 _sizeMinusOne = _size - 1;
253 _maxLoad =
static_cast<uint>(
static_cast<float>(_size) * 0.75f);
255 _table =
new TableNode[_size];
256 if (_isUsingDefaultValue)
257 for (
uint i = 0; i < _size; ++i)
258 _table[i]._value = _defaultValue;
261 if (_isUsingDefaultValue) {
262 for (
uint i = 0; i < _size; ++i) {
264 _table[i]._value = _defaultValue;
267 else for (
uint i = 0; i < _size; ++i)
281 for (
uint i = 0; i < _size; i++) {
282 if (_table[i]._hash) {
283 values_[numFound] = &(_table[i]._value);
289 inline uint getBin(
const uint& hash_,
const uint& key_,
const TableNode* table_)
const {
290 uint bin = hash_ & _sizeMinusOne;
291 while (table_[bin]._hash && (table_[bin]._key != key_))
292 bin = (bin + 1) % _size;
296 inline uint getBin(
const uint& key_)
const {
300 inline void resize() {
301 uint _oldSize = _size;
303 assert(_size < 2147483649u);
304 _sizeMinusOne = _size - 1;
305 _maxLoad =
static_cast<uint>(
static_cast<float>(_size) * 0.75f);
308 TableNode* tempTable =
new TableNode[_size];
309 if (_isUsingDefaultValue)
310 for (
uint a = 0; a < _size; a++)
311 tempTable[a]._value = _defaultValue;
313 for (
uint i = 0; i < _oldSize; i++) {
314 if (_table[i]._hash) {
315 bin = getBin(_table[i]._hash,_table[i]._key,tempTable);
316 tempTable[bin]._hash = _table[i]._hash;
317 tempTable[bin]._key = _table[i]._key;
318 tempTable[bin]._value = _table[i]._value;
325 uint _load, _size, _initSize, _sizeMinusOne, _maxLoad;
326 bool _isUsingDefaultValue;
333 #endif //UHASH_TABLE_H
HashTableUIntKeys(const HashTableUIntKeys< T > &hashTable_)
Definition: UHash.h:132
const uint MAX_3D_COMPONENT_KEY
Definition: UHash.h:40
const uint INITIAL_TABLE_SIZE
Definition: UHash.h:34
void getAllValues(T **values_) const
Definition: UHash.h:279
uint getLoad() const
Definition: UHash.h:273
Utils::uint getCompositeKey(const Math::Point< Utils::uint > &bin_)
Definition: CDGrid.h:41
HashTableUIntKeys(uint powerOf2InitialSize_, const T &defaultValue_)
Definition: UHash.h:110
ofstream log
Definition: UHashBenchmarkTests.cpp:26
bool find(const uint &key_, T *&value_) const
Definition: UHash.h:168
HashTableUIntKeys(uint powerOf2InitialSize_)
Definition: UHash.h:94
unsigned int uint
Definition: UTypes.h:39
~HashTableUIntKeys()
Definition: UHash.h:128
void add(const uint &key_, const T &value_)
Definition: UHash.h:185
void clear(bool doRealloc_=true)
Definition: UHash.h:247
const T & find(const uint &key_) const
Definition: UHash.h:157
HashTableUIntKeys & operator=(const HashTableUIntKeys< T > &hashTable_)
Definition: UHash.h:137
uint HashUIntToUInt(const uint &key_)
Definition: UHash.h:52
T & add(const uint &key_)
Definition: UHash.h:200
bool find(const uint &key_, T &value_) const
Definition: UHash.h:161
const T & operator[](const uint &key_) const
Definition: UHash.h:180