2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2006-03-23 22:59:38 +00:00
|
|
|
*
|
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-03-28 10:05:25 +00:00
|
|
|
// The hash map (associative array) implementation in this file is
|
2009-06-13 21:07:05 +00:00
|
|
|
// based on the PyDict implementation of CPython.
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2006-04-15 21:22:33 +00:00
|
|
|
#ifndef COMMON_HASHMAP_H
|
|
|
|
#define COMMON_HASHMAP_H
|
|
|
|
|
2006-03-31 22:19:39 +00:00
|
|
|
#include "common/func.h"
|
2006-03-23 22:59:38 +00:00
|
|
|
#include "common/str.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
|
2008-03-30 06:02:34 +00:00
|
|
|
#define USE_HASHMAP_MEMORY_POOL
|
|
|
|
#ifdef USE_HASHMAP_MEMORY_POOL
|
|
|
|
#include "common/memorypool.h"
|
2008-08-04 18:14:17 +00:00
|
|
|
#endif
|
2008-03-30 06:02:34 +00:00
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
namespace Common {
|
2009-08-14 17:18:03 +00:00
|
|
|
/**
|
|
|
|
* The sgi IRIX MIPSpro Compiler has difficulties with nested templates.
|
|
|
|
* This and the other __sgi conditionals below work around these problems.
|
|
|
|
*/
|
|
|
|
#ifdef __sgi
|
|
|
|
template<class T> class IteratorImpl;
|
|
|
|
#endif
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
// Enable the following #define if you want to check how many collisions the
|
|
|
|
// code produces (many collisions indicate either a bad hash function, or a
|
|
|
|
// hash table that is too small).
|
2006-03-31 22:38:27 +00:00
|
|
|
//#define DEBUG_HASH_COLLISIONS
|
2006-03-31 22:19:39 +00:00
|
|
|
|
2008-03-30 06:02:34 +00:00
|
|
|
|
2006-03-31 22:19:39 +00:00
|
|
|
/**
|
|
|
|
* HashMap<Key,Val> maps objects of type Key to objects of type Val.
|
|
|
|
* For each used Key type, we need an "uint hashit(Key,uint)" function
|
|
|
|
* that computes a hash for the given Key object and returns it as an
|
|
|
|
* an integer from 0 to hashsize-1, and also an "equality functor".
|
|
|
|
* that returns true if if its two arguments are to be considered
|
|
|
|
* equal. Also, we assume that "=" works on Val objects for assignment.
|
|
|
|
*
|
|
|
|
* If aa is an HashMap<Key,Val>, then space is allocated each time aa[key] is
|
|
|
|
* referenced, for a new key. If the object is const, then an assertion is
|
|
|
|
* triggered instead. Hence if you are not sure whether a key is contained in
|
|
|
|
* the map, use contains() first to check for its presence.
|
2007-09-19 08:40:12 +00:00
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc = Hash<Key>, class EqualFunc = EqualTo<Key> >
|
2006-03-28 10:05:25 +00:00
|
|
|
class HashMap {
|
2006-03-23 22:59:38 +00:00
|
|
|
private:
|
2007-01-14 11:06:34 +00:00
|
|
|
#if defined (PALMOS_MODE)
|
|
|
|
public:
|
|
|
|
#endif
|
|
|
|
|
2007-01-19 21:55:45 +00:00
|
|
|
typedef HashMap<Key, Val, HashFunc, EqualFunc> HM_t;
|
|
|
|
|
2006-10-02 20:13:48 +00:00
|
|
|
struct Node {
|
2007-03-04 09:27:28 +00:00
|
|
|
const Key _key;
|
2006-10-02 20:13:48 +00:00
|
|
|
Val _value;
|
2009-06-13 21:07:05 +00:00
|
|
|
explicit Node(const Key &key) : _key(key), _value() {}
|
|
|
|
Node() : _key(), _value() {}
|
2006-10-02 20:13:48 +00:00
|
|
|
};
|
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
enum {
|
|
|
|
HASHMAP_PERTURB_SHIFT = 5,
|
|
|
|
HASHMAP_MIN_CAPACITY = 16,
|
2008-12-22 11:22:15 +00:00
|
|
|
|
|
|
|
// The quotient of the next two constants controls how much the
|
2008-09-02 11:34:12 +00:00
|
|
|
// internal storage of the hashmap may fill up before being
|
|
|
|
// increased automatically.
|
|
|
|
// Note: the quotient of these two must be between and different
|
|
|
|
// from 0 and 1.
|
|
|
|
HASHMAP_LOADFACTOR_NUMERATOR = 2,
|
|
|
|
HASHMAP_LOADFACTOR_DENOMINATOR = 3,
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
HASHMAP_MEMORYPOOL_SIZE = HASHMAP_MIN_CAPACITY * HASHMAP_LOADFACTOR_NUMERATOR / HASHMAP_LOADFACTOR_DENOMINATOR
|
|
|
|
};
|
|
|
|
|
2008-03-30 06:02:34 +00:00
|
|
|
|
2008-10-12 22:05:26 +00:00
|
|
|
ObjectPool<Node, HASHMAP_MEMORYPOOL_SIZE> _nodePool;
|
2008-03-30 06:02:34 +00:00
|
|
|
|
2009-09-21 21:36:35 +00:00
|
|
|
Node **_storage; ///< hashtable of size arrsize.
|
|
|
|
uint _mask; ///< Capacity of the HashMap minus one; must be a power of two of minus one
|
2008-08-20 11:07:16 +00:00
|
|
|
uint _size;
|
2009-06-13 21:07:05 +00:00
|
|
|
uint _deleted; ///< Number of deleted elements (_dummyNodes)
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-31 22:19:39 +00:00
|
|
|
HashFunc _hash;
|
|
|
|
EqualFunc _equal;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2009-09-21 21:36:35 +00:00
|
|
|
/** Default value, returned by the const getVal. */
|
2007-01-28 13:30:26 +00:00
|
|
|
const Val _defaultVal;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2009-09-21 21:36:35 +00:00
|
|
|
/** Dummy node, used as marker for erased objects. */
|
|
|
|
#define HASHMAP_DUMMY_NODE ((Node *)1)
|
2009-06-13 21:07:05 +00:00
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
2009-06-13 21:07:05 +00:00
|
|
|
mutable int _collisions, _lookups, _dummyHits;
|
2006-03-24 16:53:32 +00:00
|
|
|
#endif
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2009-09-21 21:36:35 +00:00
|
|
|
Node *allocNode(const Key &key) {
|
|
|
|
return new (_nodePool) Node(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeNode(Node *node) {
|
|
|
|
if (node && node != HASHMAP_DUMMY_NODE)
|
|
|
|
_nodePool.deleteChunk(node);
|
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
void assign(const HM_t &map);
|
2006-03-24 15:39:07 +00:00
|
|
|
int lookup(const Key &key) const;
|
2007-01-21 00:06:50 +00:00
|
|
|
int lookupAndCreateIfMissing(const Key &key);
|
2008-09-02 11:34:12 +00:00
|
|
|
void expandStorage(uint newCapacity);
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2009-08-14 17:18:03 +00:00
|
|
|
#ifndef __sgi
|
2008-04-02 02:01:31 +00:00
|
|
|
template<class T> friend class IteratorImpl;
|
2009-08-14 17:18:03 +00:00
|
|
|
#endif
|
2008-01-28 08:40:30 +00:00
|
|
|
|
2008-01-13 14:44:29 +00:00
|
|
|
/**
|
|
|
|
* Simple HashMap iterator implementation.
|
|
|
|
*/
|
2008-04-02 02:01:31 +00:00
|
|
|
template<class NodeType>
|
|
|
|
class IteratorImpl {
|
2008-01-13 14:44:29 +00:00
|
|
|
friend class HashMap;
|
2009-08-14 17:18:03 +00:00
|
|
|
#ifdef __sgi
|
|
|
|
template<class T> friend class Common::IteratorImpl;
|
|
|
|
#else
|
2008-04-02 02:01:31 +00:00
|
|
|
template<class T> friend class IteratorImpl;
|
2009-08-14 17:18:03 +00:00
|
|
|
#endif
|
2008-01-13 14:44:29 +00:00
|
|
|
protected:
|
|
|
|
typedef const HashMap hashmap_t;
|
|
|
|
|
|
|
|
uint _idx;
|
|
|
|
hashmap_t *_hashmap;
|
|
|
|
|
|
|
|
protected:
|
2008-04-02 02:01:31 +00:00
|
|
|
IteratorImpl(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}
|
2008-01-13 14:44:29 +00:00
|
|
|
|
2008-04-02 02:01:31 +00:00
|
|
|
NodeType *deref() const {
|
2008-01-13 14:44:29 +00:00
|
|
|
assert(_hashmap != 0);
|
2008-09-02 11:34:12 +00:00
|
|
|
assert(_idx <= _hashmap->_mask);
|
2008-08-20 11:07:16 +00:00
|
|
|
Node *node = _hashmap->_storage[_idx];
|
2008-01-13 14:44:29 +00:00
|
|
|
assert(node != 0);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2008-04-02 02:01:31 +00:00
|
|
|
IteratorImpl() : _idx(0), _hashmap(0) {}
|
|
|
|
template<class T>
|
|
|
|
IteratorImpl(const IteratorImpl<T> &c) : _idx(c._idx), _hashmap(c._hashmap) {}
|
2008-01-13 14:44:29 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
NodeType &operator*() const { return *deref(); }
|
2008-04-02 02:01:31 +00:00
|
|
|
NodeType *operator->() const { return deref(); }
|
2008-01-13 14:44:29 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
bool operator==(const IteratorImpl &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }
|
|
|
|
bool operator!=(const IteratorImpl &iter) const { return !(*this == iter); }
|
2008-01-13 14:44:29 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
IteratorImpl &operator++() {
|
2008-01-13 14:44:29 +00:00
|
|
|
assert(_hashmap);
|
|
|
|
do {
|
|
|
|
_idx++;
|
2008-09-02 11:34:12 +00:00
|
|
|
} while (_idx <= _hashmap->_mask && _hashmap->_storage[_idx] == 0);
|
|
|
|
if (_idx > _hashmap->_mask)
|
2008-01-13 14:44:29 +00:00
|
|
|
_idx = (uint)-1;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
IteratorImpl operator++(int) {
|
2008-04-02 02:01:31 +00:00
|
|
|
IteratorImpl old = *this;
|
2008-01-13 14:44:29 +00:00
|
|
|
operator ++();
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
public:
|
2008-04-02 02:01:31 +00:00
|
|
|
typedef IteratorImpl<Node> iterator;
|
|
|
|
typedef IteratorImpl<const Node> const_iterator;
|
2007-12-26 21:40:32 +00:00
|
|
|
|
2006-03-28 10:05:25 +00:00
|
|
|
HashMap();
|
2008-05-03 23:02:05 +00:00
|
|
|
HashMap(const HM_t &map);
|
2006-03-28 10:05:25 +00:00
|
|
|
~HashMap();
|
2006-03-24 15:39:07 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
HM_t &operator=(const HM_t &map) {
|
2007-03-12 22:34:29 +00:00
|
|
|
if (this == &map)
|
|
|
|
return *this;
|
|
|
|
|
2007-01-19 21:55:45 +00:00
|
|
|
// Remove the previous content and ...
|
|
|
|
clear();
|
2008-08-20 11:07:16 +00:00
|
|
|
delete[] _storage;
|
2007-01-19 21:55:45 +00:00
|
|
|
// ... copy the new stuff.
|
|
|
|
assign(map);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-03-24 15:39:07 +00:00
|
|
|
bool contains(const Key &key) const;
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
Val &operator[](const Key &key);
|
|
|
|
const Val &operator[](const Key &key) const;
|
2007-01-21 00:06:50 +00:00
|
|
|
|
2007-01-21 00:11:45 +00:00
|
|
|
Val &getVal(const Key &key);
|
2007-01-21 00:06:50 +00:00
|
|
|
const Val &getVal(const Key &key) const;
|
2009-09-06 12:59:07 +00:00
|
|
|
const Val &getVal(const Key &key, const Val &defaultVal) const;
|
2007-01-21 00:06:50 +00:00
|
|
|
void setVal(const Key &key, const Val &val);
|
2006-03-24 15:39:07 +00:00
|
|
|
|
2006-03-24 14:30:33 +00:00
|
|
|
void clear(bool shrinkArray = 0);
|
2006-03-24 15:22:17 +00:00
|
|
|
|
2007-03-04 09:58:04 +00:00
|
|
|
void erase(const Key &key);
|
2006-03-28 09:42:54 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
uint size() const { return _size; }
|
2006-03-31 23:49:08 +00:00
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
iterator begin() {
|
2009-04-28 10:23:08 +00:00
|
|
|
// Find and return the first non-empty entry
|
2008-09-02 11:34:12 +00:00
|
|
|
for (uint ctr = 0; ctr <= _mask; ++ctr) {
|
2008-08-20 11:07:16 +00:00
|
|
|
if (_storage[ctr])
|
2007-12-26 21:40:32 +00:00
|
|
|
return iterator(ctr, this);
|
|
|
|
}
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
iterator end() {
|
|
|
|
return iterator((uint)-1, this);
|
|
|
|
}
|
|
|
|
|
2006-03-28 12:34:34 +00:00
|
|
|
const_iterator begin() const {
|
2006-03-31 22:19:39 +00:00
|
|
|
// Find and return the first non-empty entry
|
2008-09-02 11:34:12 +00:00
|
|
|
for (uint ctr = 0; ctr <= _mask; ++ctr) {
|
2008-08-20 11:07:16 +00:00
|
|
|
if (_storage[ctr])
|
2006-03-31 22:19:39 +00:00
|
|
|
return const_iterator(ctr, this);
|
|
|
|
}
|
|
|
|
return end();
|
2006-03-28 12:34:34 +00:00
|
|
|
}
|
|
|
|
const_iterator end() const {
|
|
|
|
return const_iterator((uint)-1, this);
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
iterator find(const Key &key) {
|
|
|
|
uint ctr = lookup(key);
|
2008-08-20 11:07:16 +00:00
|
|
|
if (_storage[ctr])
|
2007-12-26 21:40:32 +00:00
|
|
|
return iterator(ctr, this);
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2006-06-03 13:32:53 +00:00
|
|
|
const_iterator find(const Key &key) const {
|
2006-03-28 12:34:34 +00:00
|
|
|
uint ctr = lookup(key);
|
2008-08-20 11:07:16 +00:00
|
|
|
if (_storage[ctr])
|
2006-03-28 12:34:34 +00:00
|
|
|
return const_iterator(ctr, this);
|
|
|
|
return end();
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-28 12:34:34 +00:00
|
|
|
// TODO: insert() method?
|
2006-03-28 09:42:54 +00:00
|
|
|
|
|
|
|
bool empty() const {
|
2008-08-20 11:07:16 +00:00
|
|
|
return (_size == 0);
|
2006-03-28 09:42:54 +00:00
|
|
|
}
|
2006-03-23 22:59:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2006-03-28 10:05:25 +00:00
|
|
|
// HashMap functions
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2007-01-19 21:55:45 +00:00
|
|
|
/**
|
|
|
|
* Base constructor, creates an empty hashmap.
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2009-03-07 05:01:56 +00:00
|
|
|
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
|
|
|
|
//
|
|
|
|
// We have to skip _defaultVal() on PS2 to avoid gcc 3.2.2 ICE
|
|
|
|
//
|
|
|
|
#ifdef __PLAYSTATION2__
|
|
|
|
{
|
|
|
|
#else
|
2009-09-21 21:36:35 +00:00
|
|
|
: _defaultVal() {
|
2009-03-07 05:01:56 +00:00
|
|
|
#endif
|
2008-09-02 11:34:12 +00:00
|
|
|
_mask = HASHMAP_MIN_CAPACITY - 1;
|
|
|
|
_storage = new Node *[HASHMAP_MIN_CAPACITY];
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_storage != NULL);
|
2008-09-02 11:34:12 +00:00
|
|
|
memset(_storage, 0, HASHMAP_MIN_CAPACITY * sizeof(Node *));
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2009-06-13 21:07:05 +00:00
|
|
|
_deleted = 0;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_collisions = 0;
|
2006-03-24 17:13:24 +00:00
|
|
|
_lookups = 0;
|
2009-06-13 21:07:05 +00:00
|
|
|
_dummyHits = 0;
|
2006-03-24 16:53:32 +00:00
|
|
|
#endif
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 21:55:45 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor, creates a full copy of the given hashmap.
|
|
|
|
* We must provide a custom copy constructor as we use pointers
|
|
|
|
* to heap buffers for the internal storage.
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2008-12-22 11:22:15 +00:00
|
|
|
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t &map) :
|
2009-09-21 21:36:35 +00:00
|
|
|
_defaultVal() {
|
2009-06-13 21:07:05 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_collisions = 0;
|
|
|
|
_lookups = 0;
|
|
|
|
_dummyHits = 0;
|
|
|
|
#endif
|
2007-01-19 21:55:45 +00:00
|
|
|
assign(map);
|
|
|
|
}
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2007-01-19 21:55:45 +00:00
|
|
|
/**
|
|
|
|
* Destructor, frees all used memory.
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-19 21:55:45 +00:00
|
|
|
HashMap<Key, Val, HashFunc, EqualFunc>::~HashMap() {
|
2008-09-02 11:34:12 +00:00
|
|
|
for (uint ctr = 0; ctr <= _mask; ++ctr)
|
2009-06-13 21:07:05 +00:00
|
|
|
freeNode(_storage[ctr]);
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
delete[] _storage;
|
2008-08-20 10:18:59 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
2009-06-13 21:07:05 +00:00
|
|
|
extern void updateHashCollisionStats(int, int, int, int, int);
|
|
|
|
updateHashCollisionStats(_collisions, _dummyHits, _lookups, _mask+1, _size);
|
2008-08-20 10:18:59 +00:00
|
|
|
#endif
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 21:55:45 +00:00
|
|
|
/**
|
|
|
|
* Internal method for assigning the content of another HashMap
|
|
|
|
* to this one.
|
|
|
|
*
|
|
|
|
* @note We do *not* deallocate the previous storage here -- the caller is
|
|
|
|
* responsible for doing that!
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::assign(const HM_t &map) {
|
2008-09-02 11:34:12 +00:00
|
|
|
_mask = map._mask;
|
|
|
|
_storage = new Node *[_mask+1];
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_storage != NULL);
|
2008-09-02 11:34:12 +00:00
|
|
|
memset(_storage, 0, (_mask+1) * sizeof(Node *));
|
2007-01-19 21:55:45 +00:00
|
|
|
|
|
|
|
// Simply clone the map given to us, one by one.
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2009-06-13 21:07:05 +00:00
|
|
|
_deleted = 0;
|
2008-09-02 11:34:12 +00:00
|
|
|
for (uint ctr = 0; ctr <= _mask; ++ctr) {
|
2009-09-21 21:36:35 +00:00
|
|
|
if (map._storage[ctr] == HASHMAP_DUMMY_NODE) {
|
|
|
|
_storage[ctr] = HASHMAP_DUMMY_NODE;
|
2009-06-13 21:07:05 +00:00
|
|
|
_deleted++;
|
|
|
|
} else if (map._storage[ctr] != NULL) {
|
2008-08-20 11:07:16 +00:00
|
|
|
_storage[ctr] = allocNode(map._storage[ctr]->_key);
|
|
|
|
_storage[ctr]->_value = map._storage[ctr]->_value;
|
|
|
|
_size++;
|
2007-01-19 21:55:45 +00:00
|
|
|
}
|
2007-03-04 09:58:04 +00:00
|
|
|
}
|
|
|
|
// Perform a sanity check (to help track down hashmap corruption)
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_size == map._size);
|
2009-06-13 21:07:05 +00:00
|
|
|
assert(_deleted == map._deleted);
|
2007-01-19 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2006-10-02 20:13:48 +00:00
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::clear(bool shrinkArray) {
|
2008-09-02 11:34:12 +00:00
|
|
|
for (uint ctr = 0; ctr <= _mask; ++ctr) {
|
2009-06-13 21:07:05 +00:00
|
|
|
freeNode(_storage[ctr]);
|
|
|
|
_storage[ctr] = NULL;
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
#ifdef USE_HASHMAP_MEMORY_POOL
|
|
|
|
_nodePool.freeUnusedPages();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (shrinkArray && _mask >= HASHMAP_MIN_CAPACITY) {
|
2008-08-20 11:07:16 +00:00
|
|
|
delete[] _storage;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
_mask = HASHMAP_MIN_CAPACITY;
|
|
|
|
_storage = new Node *[HASHMAP_MIN_CAPACITY];
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_storage != NULL);
|
2008-09-02 11:34:12 +00:00
|
|
|
memset(_storage, 0, HASHMAP_MIN_CAPACITY * sizeof(Node *));
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2009-06-13 21:07:05 +00:00
|
|
|
_deleted = 0;
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2008-09-02 11:34:12 +00:00
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::expandStorage(uint newCapacity) {
|
|
|
|
assert(newCapacity > _mask+1);
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2009-07-03 11:05:59 +00:00
|
|
|
#ifndef NDEBUG
|
2008-08-20 11:07:16 +00:00
|
|
|
const uint old_size = _size;
|
2009-07-03 11:05:59 +00:00
|
|
|
#endif
|
2008-09-02 11:34:12 +00:00
|
|
|
const uint old_mask = _mask;
|
2008-08-20 11:07:16 +00:00
|
|
|
Node **old_storage = _storage;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
// allocate a new array
|
2008-08-20 11:07:16 +00:00
|
|
|
_size = 0;
|
2009-06-13 21:07:05 +00:00
|
|
|
_deleted = 0;
|
2008-09-02 11:34:12 +00:00
|
|
|
_mask = newCapacity - 1;
|
|
|
|
_storage = new Node *[newCapacity];
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_storage != NULL);
|
2008-09-02 11:34:12 +00:00
|
|
|
memset(_storage, 0, newCapacity * sizeof(Node *));
|
2006-03-23 22:59:38 +00:00
|
|
|
|
|
|
|
// rehash all the old elements
|
2008-09-02 11:34:12 +00:00
|
|
|
for (uint ctr = 0; ctr <= old_mask; ++ctr) {
|
2009-09-21 21:36:35 +00:00
|
|
|
if (old_storage[ctr] == NULL || old_storage[ctr] == HASHMAP_DUMMY_NODE)
|
2006-03-23 22:59:38 +00:00
|
|
|
continue;
|
|
|
|
|
2006-03-28 11:21:13 +00:00
|
|
|
// Insert the element from the old table into the new table.
|
2006-10-03 11:33:06 +00:00
|
|
|
// Since we know that no key exists twice in the old table, we
|
2006-03-28 11:21:13 +00:00
|
|
|
// can do this slightly better than by calling lookup, since we
|
2006-03-31 22:19:39 +00:00
|
|
|
// don't have to call _equal().
|
2008-09-02 11:34:12 +00:00
|
|
|
const uint hash = _hash(old_storage[ctr]->_key);
|
|
|
|
uint idx = hash & _mask;
|
2009-09-21 21:36:35 +00:00
|
|
|
for (uint perturb = hash; _storage[idx] != NULL && _storage[idx] != HASHMAP_DUMMY_NODE; perturb >>= HASHMAP_PERTURB_SHIFT) {
|
2008-09-02 11:34:12 +00:00
|
|
|
idx = (5 * idx + perturb + 1) & _mask;
|
2006-03-24 17:13:24 +00:00
|
|
|
}
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
_storage[idx] = old_storage[ctr];
|
2008-08-20 11:07:16 +00:00
|
|
|
_size++;
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2006-03-28 10:54:02 +00:00
|
|
|
// Perform a sanity check: Old number of elements should match the new one!
|
2007-03-04 09:58:04 +00:00
|
|
|
// This check will fail if some previous operation corrupted this hashmap.
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_size == old_size);
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-08-20 11:07:16 +00:00
|
|
|
delete[] old_storage;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2006-10-02 20:13:48 +00:00
|
|
|
int HashMap<Key, Val, HashFunc, EqualFunc>::lookup(const Key &key) const {
|
2008-09-02 11:34:12 +00:00
|
|
|
const uint hash = _hash(key);
|
|
|
|
uint ctr = hash & _mask;
|
|
|
|
for (uint perturb = hash; ; perturb >>= HASHMAP_PERTURB_SHIFT) {
|
2009-06-13 21:07:05 +00:00
|
|
|
if (_storage[ctr] == NULL)
|
|
|
|
break;
|
2009-09-21 21:36:35 +00:00
|
|
|
if (_storage[ctr] == HASHMAP_DUMMY_NODE) {
|
2009-06-13 21:07:05 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_dummyHits++;
|
|
|
|
#endif
|
|
|
|
} else if (_equal(_storage[ctr]->_key, key))
|
2008-09-02 11:34:12 +00:00
|
|
|
break;
|
2006-03-28 11:21:13 +00:00
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
ctr = (5 * ctr + perturb + 1) & _mask;
|
2006-03-28 11:21:13 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_collisions++;
|
|
|
|
#endif
|
|
|
|
}
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-28 11:21:13 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_lookups++;
|
2009-06-13 21:07:05 +00:00
|
|
|
fprintf(stderr, "collisions %d, dummies hit %d, lookups %d, ratio %f in HashMap %p; size %d num elements %d\n",
|
|
|
|
_collisions, _dummyHits, _lookups, ((double) _collisions / (double)_lookups),
|
2008-09-02 11:34:12 +00:00
|
|
|
(const void *)this, _mask+1, _size);
|
2006-03-28 11:21:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-21 00:06:50 +00:00
|
|
|
int HashMap<Key, Val, HashFunc, EqualFunc>::lookupAndCreateIfMissing(const Key &key) {
|
2009-06-13 21:07:05 +00:00
|
|
|
const uint hash = _hash(key);
|
|
|
|
uint ctr = hash & _mask;
|
|
|
|
const uint NONE_FOUND = _mask + 1;
|
|
|
|
uint first_free = NONE_FOUND;
|
|
|
|
bool found = false;
|
|
|
|
for (uint perturb = hash; ; perturb >>= HASHMAP_PERTURB_SHIFT) {
|
|
|
|
if (_storage[ctr] == NULL)
|
|
|
|
break;
|
2009-09-21 21:36:35 +00:00
|
|
|
if (_storage[ctr] == HASHMAP_DUMMY_NODE) {
|
2009-06-13 21:07:05 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_dummyHits++;
|
|
|
|
#endif
|
|
|
|
if (first_free != _mask + 1)
|
|
|
|
first_free = ctr;
|
|
|
|
} else if (_equal(_storage[ctr]->_key, key)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2009-06-13 21:07:05 +00:00
|
|
|
ctr = (5 * ctr + perturb + 1) & _mask;
|
|
|
|
|
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_collisions++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
|
|
|
_lookups++;
|
|
|
|
fprintf(stderr, "collisions %d, dummies hit %d, lookups %d, ratio %f in HashMap %p; size %d num elements %d\n",
|
|
|
|
_collisions, _dummyHits, _lookups, ((double) _collisions / (double)_lookups),
|
|
|
|
(const void *)this, _mask+1, _size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!found && first_free != _mask + 1)
|
|
|
|
ctr = first_free;
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
if (_storage[ctr])
|
|
|
|
_deleted--;
|
2008-08-20 11:07:16 +00:00
|
|
|
_storage[ctr] = allocNode(key);
|
2009-02-16 01:58:20 +00:00
|
|
|
assert(_storage[ctr] != NULL);
|
2008-08-20 11:07:16 +00:00
|
|
|
_size++;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-09-02 11:34:12 +00:00
|
|
|
// Keep the load factor below a certain threshold.
|
2009-06-13 21:07:05 +00:00
|
|
|
// Deleted nodes are also counted
|
2008-09-02 11:34:12 +00:00
|
|
|
uint capacity = _mask + 1;
|
2009-06-13 21:07:05 +00:00
|
|
|
if ((_size + _deleted) * HASHMAP_LOADFACTOR_DENOMINATOR >
|
|
|
|
capacity * HASHMAP_LOADFACTOR_NUMERATOR) {
|
2008-09-02 11:34:12 +00:00
|
|
|
capacity = capacity < 500 ? (capacity * 4) : (capacity * 2);
|
|
|
|
expandStorage(capacity);
|
2006-03-24 15:39:07 +00:00
|
|
|
ctr = lookup(key);
|
2009-02-16 01:58:20 +00:00
|
|
|
assert(_storage[ctr] != NULL);
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 00:06:50 +00:00
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-21 00:06:50 +00:00
|
|
|
bool HashMap<Key, Val, HashFunc, EqualFunc>::contains(const Key &key) const {
|
|
|
|
uint ctr = lookup(key);
|
2008-08-20 11:07:16 +00:00
|
|
|
return (_storage[ctr] != NULL);
|
2007-01-21 00:06:50 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator[](const Key &key) {
|
2007-01-21 00:11:45 +00:00
|
|
|
return getVal(key);
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
const Val &HashMap<Key, Val, HashFunc, EqualFunc>::operator[](const Key &key) const {
|
2007-01-21 00:06:50 +00:00
|
|
|
return getVal(key);
|
2006-03-24 15:39:07 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-21 00:11:45 +00:00
|
|
|
Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) {
|
|
|
|
uint ctr = lookupAndCreateIfMissing(key);
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_storage[ctr] != NULL);
|
|
|
|
return _storage[ctr]->_value;
|
2007-01-21 00:11:45 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-21 00:06:50 +00:00
|
|
|
const Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) const {
|
2009-09-06 12:59:07 +00:00
|
|
|
return getVal(key, _defaultVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
const Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key, const Val &defaultVal) const {
|
2006-03-24 16:53:32 +00:00
|
|
|
uint ctr = lookup(key);
|
2008-08-20 11:07:16 +00:00
|
|
|
if (_storage[ctr] != NULL)
|
|
|
|
return _storage[ctr]->_value;
|
2007-01-28 13:30:26 +00:00
|
|
|
else
|
2009-09-06 12:59:07 +00:00
|
|
|
return defaultVal;
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-21 00:06:50 +00:00
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::setVal(const Key &key, const Val &val) {
|
|
|
|
uint ctr = lookupAndCreateIfMissing(key);
|
2008-08-20 11:07:16 +00:00
|
|
|
assert(_storage[ctr] != NULL);
|
|
|
|
_storage[ctr]->_value = val;
|
2007-01-21 00:06:50 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class Key, class Val, class HashFunc, class EqualFunc>
|
2007-03-04 09:58:04 +00:00
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::erase(const Key &key) {
|
2008-09-02 11:34:12 +00:00
|
|
|
|
2009-06-13 21:07:05 +00:00
|
|
|
uint ctr = lookup(key);
|
|
|
|
if (_storage[ctr] == NULL)
|
|
|
|
return;
|
2008-09-02 11:34:12 +00:00
|
|
|
|
2009-06-13 21:07:05 +00:00
|
|
|
// If we remove a key, we replace it with a dummy node.
|
|
|
|
freeNode(_storage[ctr]);
|
2009-09-21 21:36:35 +00:00
|
|
|
_storage[ctr] = HASHMAP_DUMMY_NODE;
|
2008-08-20 11:07:16 +00:00
|
|
|
_size--;
|
2009-06-13 21:07:05 +00:00
|
|
|
_deleted++;
|
2007-03-04 09:58:04 +00:00
|
|
|
return;
|
2006-03-28 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 21:36:35 +00:00
|
|
|
#undef HASHMAP_DUMMY_NODE
|
|
|
|
|
2006-03-23 22:59:38 +00:00
|
|
|
} // End of namespace Common
|
|
|
|
|
|
|
|
#endif
|