Skip to content
This repository
tree: 1958353a1e
Fetching contributors…

Cannot retrieve contributors at this time

file 398 lines (331 sloc) 9.078 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/************************************************************************/
/* */
/* This file is part of VDrift. */
/* */
/* VDrift is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* VDrift is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with VDrift. If not, see <http://www.gnu.org/licenses/>. */
/* */
/************************************************************************/

#ifndef _BUCKETED_HASHMAP_H
#define _BUCKETED_HASHMAP_H

#include <vector>
#include <map>
#include <cassert>
#include <string>

namespace HASH_NAMESPACE
{

template <class T>
class hasher
{
public:
unsigned int GetHash(T & val) const {assert(0);return 0;} //no hash function specified for this type
};

//DJB hash from http://www.partow.net/programming/hashfunctions/
//very simple, excellent performance
//the one at http://burtleburtle.net/bob/c/lookup3.c sounds good, but seems much slower
template <>
class hasher <std::string>
{
private:

public:
unsigned int GetHash(const std::string & str) const
{
unsigned int hash = 5381;
unsigned int strlen = str.length();
for (std::size_t i = 0; i < strlen; i++)
{
hash = ((hash << 5) + hash) + str[i];
}

return (hash & 0x7FFFFFFF);
}
};

template <>
class hasher <int>
{
private:

public:
unsigned int GetHash(int & key) const
{
unsigned int retval = *((unsigned int*) (&key));
return retval;
}
};

template <>
class hasher <float>
{
private:

public:
unsigned int GetHash(float & key) const
{
unsigned int retval = *((unsigned int *) (&key));
return retval;
}
};

template <>
class hasher <double>
{
private:

public:
unsigned int GetHash(double & key) const
{
//unsigned int retval = *((unsigned int *) (&key) + (sizeof(double)/2));
unsigned int retval = *((unsigned int *) (&key));
return retval;
}
};

template <>
class hasher <unsigned int>
{
private:

public:
unsigned int GetHash(unsigned int & key) const
{
return key;
}
};

template <class KEYCLASS, class DATACLASS, class VI_ITERATOR, class MI_ITERATOR>
class hash_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef DATACLASS value_type;
typedef value_type & reference;
typedef value_type * pointer;
typedef int difference_type;

private:
VI_ITERATOR vi;
VI_ITERATOR data_end;
MI_ITERATOR mi;

public:
hash_iterator(VI_ITERATOR vi_cur, VI_ITERATOR vi_end) : vi(vi_cur), data_end(vi_end)
{
while (vi != data_end && vi->empty()) //move vi to first non-empty bucket
{
vi++;
if (vi != data_end)
mi = vi->begin();
}
if (vi != data_end)
mi = vi->begin();
}

pointer operator->() const
{
//is it OK to assert inside an iterator? well, here goes nothin'.
assert (vi != data_end);
assert (mi != vi->end());

return &(operator*());
}

reference operator*() const
{
//is it OK to assert inside an iterator? well, here goes nothin'.
assert (vi != data_end);
assert (mi != vi->end());

return mi->second; //only valid if mi and vi are valid
}

hash_iterator& operator++()
{
if (vi == data_end) //vi is at the end of buckets already
return (*this);

if (mi != vi->end()) //mi is valid
{
mi++;

if (mi == vi->end()) //we moved to the end of our bucket
{
vi++;
while (vi != data_end && vi->empty()) //move vi to next non-empty bucket
{
vi++;
if (vi != data_end) //found a bucket
mi = vi->begin();
}
if (vi != data_end)
mi = vi->begin();
}
}

return (*this);
}

hash_iterator operator++(int)
{
// postincrement
hash_iterator _Tmp = *this;
++*this;
return (_Tmp);
}

bool operator==(const hash_iterator & other) const
{
// test for iterator equality
if (vi == data_end && other.vi == other.data_end)
return true;

return (vi == other.vi && mi == other.mi);
}

bool operator!=(const hash_iterator & other) const
{
// test for iterator inequality
return (!(*this == other));
}

const KEYCLASS & first() const
{
assert (vi != data_end);
assert (mi != vi->end());

return mi->first; //only valid if mi and vi are valid
}
};

} //namespace HASH_NAMESPACE

template <class KEYCLASS, class DATACLASS>
class bucketed_hashmap
{
private:
unsigned int nbuckets;
std::vector < std::map <KEYCLASS, DATACLASS> > data;
void set_buckets(unsigned int num_buckets) {data.resize(num_buckets);nbuckets = num_buckets;}
HASH_NAMESPACE::hasher <KEYCLASS> hash;

public:
typedef HASH_NAMESPACE::hash_iterator <KEYCLASS, DATACLASS, typename std::vector < std::map <KEYCLASS, DATACLASS> >::iterator, typename std::map <KEYCLASS, DATACLASS>::iterator> iterator;
typedef HASH_NAMESPACE::hash_iterator <const KEYCLASS, const DATACLASS, typename std::vector < std::map <KEYCLASS, DATACLASS> >::const_iterator, typename std::map <KEYCLASS, DATACLASS>::const_iterator> const_iterator;

bucketed_hashmap() {set_buckets(256.0);}
bucketed_hashmap(unsigned int num_buckets) {set_buckets(num_buckets);}

iterator begin()
{
return iterator(data.begin(), data.end());
}

const_iterator begin() const
{
return const_iterator(data.begin(), data.end());
}

iterator end()
{
return iterator(data.end(), data.end());
}

const_iterator end() const
{
return const_iterator(data.end(), data.end());
}

///returns true if the element was found and erased
bool Erase(KEYCLASS & key)
{
unsigned int idx = hash.GetHash(key) % nbuckets;

if (data[idx].find(key) == data[idx].end())
return false;
else
{
data[idx].erase(key);
return true;
}
}

///iterators will be invalidated
void Clear()
{
for (typename std::vector < std::map <KEYCLASS, DATACLASS> >::iterator i = data.begin(); i != data.end(); ++i)
{
i->clear();
}
}

const DATACLASS * Get(KEYCLASS & key) const
{
unsigned int idx = hash.GetHash(key) % nbuckets;
//typedef std::map <KEYCLASS, DATACLASS> mymap;
//mymap::iterator i;// = data[idx].find(key);
/*if (i == data[idx].end())
return NULL;
else
return i;*/

if (data[idx].find(key) == data[idx].end())
return NULL;
else
return &(((data[idx]).find(key))->second);
}

DATACLASS * Get(KEYCLASS & key)
{
unsigned int idx = hash.GetHash(key) % nbuckets;
//typedef std::map <KEYCLASS, DATACLASS> mymap;
//mymap::iterator i;// = data[idx].find(key);
/*if (i == data[idx].end())
return NULL;
else
return i;*/

if (data[idx].find(key) == data[idx].end())
return NULL;
else
return &(((data[idx]).find(key))->second);
}

DATACLASS & Set(const KEYCLASS & key, const DATACLASS & ndata)
{
unsigned int idx = (hash.GetHash(key)) % nbuckets;
(data[idx])[key] = ndata;
return (data[idx])[key];
}

unsigned int GetNumCollisions()
{
unsigned int col = 0;
unsigned int lim = data.size();

for (unsigned int i = 0; i < lim; i++)
{
unsigned int subsize = data[i].size();
if (subsize > 1)
col += subsize-1;
}

return col;
}

unsigned int GetTotalObjects() const
{
unsigned int obj = 0;
//unsigned int lim = data.size();

/*for (unsigned int i = 0; i < lim; i++)
{
obj += data[i].size();
}*/

for (typename std::vector < std::map <KEYCLASS, DATACLASS> >::const_iterator i = data.begin(); i != data.end(); ++i)
{
obj += i->size();
}

return obj;
}

int size() const {return GetTotalObjects();}

float GetAvgBucketSize()
{
float avg = 0;
unsigned int lim = data.size();

for (unsigned int i = 0; i < lim; i++)
{
avg += data[i].size();
}

return avg / nbuckets;
}

float GetBucketEvenness()
{
return (GetTotalObjects() - (float) GetNumCollisions()) / nbuckets;
}

float GetEmptyBucketPercent()
{
unsigned int lim = data.size();
unsigned int empty = 0;

for (unsigned int i = 0; i < lim; i++)
{
if (data[i].empty())
empty++;
}

return (float) empty / (float) nbuckets;
}

unsigned int GetLongestBucket()
{
unsigned int lim = data.size();
unsigned int longest = 0;

for (unsigned int i = 0; i < lim; i++)
{
if (data[i].size() > longest)
longest = data[i].size();
}

return longest;
}
};

#endif
Something went wrong with that request. Please try again.