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
|
|
|
|
// based on code by Andrew Y. Ng, 1996:
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1998-2003 Massachusetts Institute of Technology.
|
|
|
|
* This code was developed as part of the Haystack research project
|
|
|
|
* (http://haystack.lcs.mit.edu/). Permission is hereby granted,
|
|
|
|
* free of charge, to any person obtaining a copy of this software
|
|
|
|
* and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute,
|
|
|
|
* sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
* persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
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"
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
namespace Common {
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
// The table sizes ideally are primes. We use a helper function to find
|
|
|
|
// suitable table sizes.
|
|
|
|
uint nextTableSize(uint x);
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2006-10-02 20:13:48 +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;
|
2007-12-26 21:40:32 +00:00
|
|
|
Node(const Key &key) : _key(key), _value() {}
|
2006-10-02 20:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Node **_arr; // hashtable of size arrsize.
|
2006-03-24 16:53:32 +00:00
|
|
|
uint _arrsize, _nele;
|
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
|
|
|
|
2007-01-28 13:30:26 +00:00
|
|
|
// Default value, returned by the const getVal.
|
|
|
|
const Val _defaultVal;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
#ifdef DEBUG_HASH_COLLISIONS
|
2006-03-24 17:13:24 +00:00
|
|
|
mutable int _collisions, _lookups;
|
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
|
|
|
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);
|
2006-03-28 10:54:02 +00:00
|
|
|
void expand_array(uint newsize);
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2008-01-13 14:44:29 +00:00
|
|
|
/**
|
|
|
|
* Simple HashMap iterator implementation.
|
|
|
|
*/
|
2007-12-26 21:40:32 +00:00
|
|
|
class Iterator {
|
2008-01-13 14:44:29 +00:00
|
|
|
protected:
|
|
|
|
typedef const HashMap hashmap_t;
|
|
|
|
friend class HashMap;
|
|
|
|
|
|
|
|
// Allow ConstIterator to read member vars, so that Iterators can be converted to ConstIterator
|
|
|
|
friend class HashMap::ConstIterator;
|
|
|
|
|
2006-03-28 12:34:34 +00:00
|
|
|
uint _idx;
|
2008-01-13 14:44:29 +00:00
|
|
|
hashmap_t *_hashmap;
|
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
protected:
|
2008-01-13 14:44:29 +00:00
|
|
|
Iterator(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}
|
2006-03-28 12:34:34 +00:00
|
|
|
|
2008-01-13 14:44:29 +00:00
|
|
|
Node *deref() const {
|
2006-03-28 12:34:34 +00:00
|
|
|
assert(_hashmap != 0);
|
2008-01-23 17:19:58 +00:00
|
|
|
assert(_idx < _arrsize);
|
2008-01-13 14:44:29 +00:00
|
|
|
Node *node = _hashmap->_arr[_idx];
|
2006-03-28 12:34:34 +00:00
|
|
|
assert(node != 0);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2007-12-26 21:40:32 +00:00
|
|
|
Iterator() : _idx(0), _hashmap(0) {}
|
2007-12-28 00:08:29 +00:00
|
|
|
|
2008-01-13 14:44:29 +00:00
|
|
|
Node &operator *() const { return *deref(); }
|
|
|
|
Node *operator->() const { return deref(); }
|
2007-12-26 21:40:32 +00:00
|
|
|
|
|
|
|
bool operator ==(const Iterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }
|
|
|
|
bool operator !=(const Iterator &iter) const { return !(*this == iter); }
|
2006-03-28 12:34:34 +00:00
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
Iterator &operator ++() {
|
2006-03-28 12:34:34 +00:00
|
|
|
assert(_hashmap);
|
|
|
|
do {
|
|
|
|
_idx++;
|
|
|
|
} while (_idx < _hashmap->_arrsize && _hashmap->_arr[_idx] == 0);
|
|
|
|
if (_idx >= _hashmap->_arrsize)
|
|
|
|
_idx = (uint)-1;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-03-31 22:19:39 +00:00
|
|
|
return *this;
|
2006-03-28 12:34:34 +00:00
|
|
|
}
|
2007-12-26 21:40:32 +00:00
|
|
|
|
|
|
|
Iterator operator ++(int) {
|
|
|
|
Iterator old = *this;
|
|
|
|
operator ++();
|
|
|
|
return old;
|
|
|
|
}
|
2006-03-28 12:34:34 +00:00
|
|
|
};
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2008-01-13 14:44:29 +00:00
|
|
|
/**
|
|
|
|
* Simple HashMap const iterator implementation.
|
|
|
|
* This is almost completely identical to the normal iterator class, only
|
|
|
|
* with some const keywords added here and there, plus a conversion
|
|
|
|
* operator which makes it possible to transparently convert iterators to
|
2008-01-27 19:47:41 +00:00
|
|
|
* const iterators.
|
2008-01-13 14:44:29 +00:00
|
|
|
* It is sadly not really possible to reduce this code duplication using
|
|
|
|
* template, unless one is willing to accept various warnings on certain
|
|
|
|
* compilers. Note that many (most? all?) implementations of the standard
|
|
|
|
* C++ library use a similar approach for their implementations.
|
|
|
|
*/
|
|
|
|
class ConstIterator {
|
|
|
|
protected:
|
|
|
|
typedef const HashMap hashmap_t;
|
|
|
|
friend class HashMap;
|
|
|
|
|
|
|
|
uint _idx;
|
|
|
|
hashmap_t *_hashmap;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ConstIterator(uint idx, hashmap_t *hashmap) : _idx(idx), _hashmap(hashmap) {}
|
|
|
|
|
|
|
|
const Node *deref() const {
|
|
|
|
assert(_hashmap != 0);
|
|
|
|
const Node *node = _hashmap->_arr[_idx];
|
|
|
|
assert(node != 0);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ConstIterator() : _idx(0), _hashmap(0) {}
|
|
|
|
|
|
|
|
// Converting a non-const iterator to a const one is allowed
|
|
|
|
ConstIterator(const Iterator &iter) : _idx(iter._idx), _hashmap(iter._hashmap) {}
|
|
|
|
|
|
|
|
const Node &operator *() const { return *deref(); }
|
|
|
|
const Node *operator->() const { return deref(); }
|
|
|
|
|
|
|
|
bool operator ==(const ConstIterator &iter) const { return _idx == iter._idx && _hashmap == iter._hashmap; }
|
|
|
|
bool operator !=(const ConstIterator &iter) const { return !(*this == iter); }
|
|
|
|
|
|
|
|
ConstIterator &operator ++() {
|
|
|
|
assert(_hashmap);
|
|
|
|
do {
|
|
|
|
_idx++;
|
|
|
|
} while (_idx < _hashmap->_arrsize && _hashmap->_arr[_idx] == 0);
|
|
|
|
if (_idx >= _hashmap->_arrsize)
|
|
|
|
_idx = (uint)-1;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator operator ++(int) {
|
|
|
|
ConstIterator old = *this;
|
|
|
|
operator ++();
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
public:
|
2008-01-13 14:44:29 +00:00
|
|
|
typedef Iterator iterator;
|
|
|
|
typedef ConstIterator const_iterator;
|
2007-12-26 21:40:32 +00:00
|
|
|
|
2006-03-28 10:05:25 +00:00
|
|
|
HashMap();
|
2007-01-19 21:55:45 +00:00
|
|
|
HashMap(const HM_t& map);
|
2006-03-28 10:05:25 +00:00
|
|
|
~HashMap();
|
2006-03-24 15:39:07 +00:00
|
|
|
|
2007-01-19 21:55:45 +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();
|
|
|
|
delete[] _arr;
|
|
|
|
// ... copy the new stuff.
|
|
|
|
assign(map);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-03-24 15:39:07 +00:00
|
|
|
bool contains(const Key &key) const;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
2006-04-25 19:20:57 +00:00
|
|
|
uint size() const { return _nele; }
|
2006-03-31 23:49:08 +00:00
|
|
|
|
2007-12-26 21:40:32 +00:00
|
|
|
iterator begin() {
|
|
|
|
// Find and return the _key non-empty entry
|
|
|
|
for (uint ctr = 0; ctr < _arrsize; ++ctr) {
|
|
|
|
if (_arr[ctr])
|
|
|
|
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
|
|
|
|
for (uint ctr = 0; ctr < _arrsize; ++ctr) {
|
|
|
|
if (_arr[ctr])
|
|
|
|
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);
|
|
|
|
if (_arr[ctr])
|
|
|
|
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);
|
|
|
|
if (_arr[ctr])
|
|
|
|
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 {
|
|
|
|
return (_nele == 0);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2006-10-02 20:13:48 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-28 13:30:26 +00:00
|
|
|
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
|
|
|
|
: _defaultVal() {
|
2006-03-24 16:53:32 +00:00
|
|
|
_arrsize = nextTableSize(0);
|
2006-10-02 20:13:48 +00:00
|
|
|
_arr = new Node *[_arrsize];
|
2006-03-24 15:22:17 +00:00
|
|
|
assert(_arr != NULL);
|
2006-10-02 20:13:48 +00:00
|
|
|
memset(_arr, 0, _arrsize * sizeof(Node *));
|
2006-03-23 22:59:38 +00:00
|
|
|
|
|
|
|
_nele = 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;
|
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.
|
|
|
|
*/
|
2006-10-02 20:13:48 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
2007-01-28 13:30:26 +00:00
|
|
|
HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map)
|
|
|
|
: _defaultVal() {
|
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.
|
|
|
|
*/
|
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
HashMap<Key, Val, HashFunc, EqualFunc>::~HashMap() {
|
|
|
|
for (uint ctr = 0; ctr < _arrsize; ++ctr)
|
2006-03-23 22:59:38 +00:00
|
|
|
if (_arr[ctr] != NULL)
|
|
|
|
delete _arr[ctr];
|
|
|
|
|
|
|
|
delete[] _arr;
|
|
|
|
}
|
|
|
|
|
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!
|
|
|
|
*/
|
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::assign(const HM_t& map) {
|
|
|
|
_arrsize = map._arrsize;
|
|
|
|
_arr = new Node *[_arrsize];
|
|
|
|
assert(_arr != NULL);
|
|
|
|
memset(_arr, 0, _arrsize * sizeof(Node *));
|
|
|
|
|
|
|
|
// Simply clone the map given to us, one by one.
|
2007-03-04 09:58:04 +00:00
|
|
|
_nele = 0;
|
|
|
|
for (uint ctr = 0; ctr < _arrsize; ++ctr) {
|
2007-01-19 21:55:45 +00:00
|
|
|
if (map._arr[ctr] != NULL) {
|
|
|
|
_arr[ctr] = new Node(*map._arr[ctr]);
|
2007-03-04 09:58:04 +00:00
|
|
|
_nele++;
|
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)
|
|
|
|
assert(_nele == map._nele);
|
2007-01-19 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-02 20:13:48 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::clear(bool shrinkArray) {
|
2007-01-19 21:55:45 +00:00
|
|
|
for (uint ctr = 0; ctr < _arrsize; ++ctr) {
|
2006-03-23 22:59:38 +00:00
|
|
|
if (_arr[ctr] != NULL) {
|
|
|
|
delete _arr[ctr];
|
|
|
|
_arr[ctr] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
if (shrinkArray && _arrsize > nextTableSize(0)) {
|
|
|
|
delete[] _arr;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2006-03-24 16:53:32 +00:00
|
|
|
_arrsize = nextTableSize(0);
|
2006-10-02 20:13:48 +00:00
|
|
|
_arr = new Node *[_arrsize];
|
2006-03-24 16:53:32 +00:00
|
|
|
assert(_arr != NULL);
|
2006-10-02 20:13:48 +00:00
|
|
|
memset(_arr, 0, _arrsize * sizeof(Node *));
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_nele = 0;
|
|
|
|
}
|
|
|
|
|
2006-10-02 20:13:48 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::expand_array(uint newsize) {
|
2006-03-28 10:54:02 +00:00
|
|
|
assert(newsize > _arrsize);
|
2007-03-04 09:58:04 +00:00
|
|
|
uint ctr, dex;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2007-03-04 09:58:04 +00:00
|
|
|
const uint old_nele = _nele;
|
|
|
|
const uint old_arrsize = _arrsize;
|
|
|
|
Node **old_arr = _arr;
|
2006-03-23 22:59:38 +00:00
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
// allocate a new array
|
2007-03-04 09:58:04 +00:00
|
|
|
_nele = 0;
|
2006-03-28 10:54:02 +00:00
|
|
|
_arrsize = newsize;
|
2006-10-02 20:13:48 +00:00
|
|
|
_arr = new Node *[_arrsize];
|
2006-03-23 22:59:38 +00:00
|
|
|
assert(_arr != NULL);
|
2006-10-02 20:13:48 +00:00
|
|
|
memset(_arr, 0, _arrsize * sizeof(Node *));
|
2006-03-23 22:59:38 +00:00
|
|
|
|
|
|
|
// rehash all the old elements
|
2007-01-19 21:55:45 +00:00
|
|
|
for (ctr = 0; ctr < old_arrsize; ++ctr) {
|
2006-03-23 22:59:38 +00:00
|
|
|
if (old_arr[ctr] == NULL)
|
|
|
|
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().
|
|
|
|
dex = _hash(old_arr[ctr]->_key) % _arrsize;
|
2006-03-24 17:13:24 +00:00
|
|
|
while (_arr[dex] != NULL) {
|
2006-03-28 11:21:13 +00:00
|
|
|
dex = (dex + 1) % _arrsize;
|
2006-03-24 17:13:24 +00:00
|
|
|
}
|
2006-03-23 22:59:38 +00:00
|
|
|
|
|
|
|
_arr[dex] = old_arr[ctr];
|
|
|
|
_nele++;
|
|
|
|
}
|
|
|
|
|
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.
|
2006-03-23 22:59:38 +00:00
|
|
|
assert(_nele == old_nele);
|
|
|
|
|
|
|
|
delete[] old_arr;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-02 20:13:48 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
int HashMap<Key, Val, HashFunc, EqualFunc>::lookup(const Key &key) const {
|
2006-03-31 22:19:39 +00:00
|
|
|
uint ctr = _hash(key) % _arrsize;
|
2006-03-28 11:21:13 +00:00
|
|
|
|
2006-03-31 22:19:39 +00:00
|
|
|
while (_arr[ctr] != NULL && !_equal(_arr[ctr]->_key, key)) {
|
2006-03-28 11:21:13 +00:00
|
|
|
ctr = (ctr + 1) % _arrsize;
|
|
|
|
|
|
|
|
#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++;
|
|
|
|
fprintf(stderr, "collisions %d, lookups %d, ratio %f in HashMap %p; size %d num elements %d\n",
|
|
|
|
_collisions, _lookups, ((double) _collisions / (double)_lookups),
|
|
|
|
(const void *)this, _arrsize, _nele);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
2006-10-02 20:13:48 +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) {
|
2006-03-24 16:53:32 +00:00
|
|
|
uint ctr = lookup(key);
|
2006-03-23 22:59:38 +00:00
|
|
|
|
|
|
|
if (_arr[ctr] == NULL) {
|
2006-10-02 20:13:48 +00:00
|
|
|
_arr[ctr] = new Node(key);
|
2006-03-23 22:59:38 +00:00
|
|
|
_nele++;
|
|
|
|
|
2006-03-28 10:54:02 +00:00
|
|
|
// Keep the load factor below 75%.
|
2006-03-28 16:19:18 +00:00
|
|
|
if (_nele > _arrsize * 75 / 100) {
|
2006-03-28 10:54:02 +00:00
|
|
|
expand_array(nextTableSize(_arrsize));
|
2006-03-24 15:39:07 +00:00
|
|
|
ctr = lookup(key);
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 00:06:50 +00:00
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
bool HashMap<Key, Val, HashFunc, EqualFunc>::contains(const Key &key) const {
|
|
|
|
uint ctr = lookup(key);
|
|
|
|
return (_arr[ctr] != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-10-02 20:13:48 +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
|
|
|
}
|
|
|
|
|
2007-01-21 00:11:45 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
Val &HashMap<Key, Val, HashFunc, EqualFunc>::getVal(const Key &key) {
|
|
|
|
uint ctr = lookupAndCreateIfMissing(key);
|
|
|
|
assert(_arr[ctr] != NULL);
|
|
|
|
return _arr[ctr]->_value;
|
|
|
|
}
|
|
|
|
|
2006-10-02 20:13:48 +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 {
|
2006-03-24 16:53:32 +00:00
|
|
|
uint ctr = lookup(key);
|
2007-01-28 13:30:26 +00:00
|
|
|
if (_arr[ctr] != NULL)
|
|
|
|
return _arr[ctr]->_value;
|
|
|
|
else
|
|
|
|
return _defaultVal;
|
2006-03-23 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 00:06:50 +00:00
|
|
|
template <class Key, class Val, class HashFunc, class EqualFunc>
|
|
|
|
void HashMap<Key, Val, HashFunc, EqualFunc>::setVal(const Key &key, const Val &val) {
|
|
|
|
uint ctr = lookupAndCreateIfMissing(key);
|
|
|
|
assert(_arr[ctr] != NULL);
|
|
|
|
_arr[ctr]->_value = val;
|
|
|
|
}
|
|
|
|
|
2006-10-02 20:13:48 +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) {
|
2006-03-28 12:34:34 +00:00
|
|
|
// This is based on code in the Wikipedia article on Hash tables.
|
|
|
|
uint i = lookup(key);
|
|
|
|
if (_arr[i] == NULL)
|
2007-03-04 09:58:04 +00:00
|
|
|
return; // key wasn't present, so no work has to be done
|
|
|
|
// If we remove a key, we must check all subsequent keys and possibly
|
|
|
|
// reinsert them.
|
2006-03-28 12:34:34 +00:00
|
|
|
uint j = i;
|
2007-03-09 16:19:50 +00:00
|
|
|
delete _arr[i];
|
|
|
|
_arr[i] = NULL;
|
2006-03-28 12:34:34 +00:00
|
|
|
while (true) {
|
2007-03-04 09:58:04 +00:00
|
|
|
// Look at the next table slot
|
2006-03-28 12:34:34 +00:00
|
|
|
j = (j + 1) % _arrsize;
|
2007-03-04 09:58:04 +00:00
|
|
|
// If the next slot is empty, we are done
|
2006-03-28 12:34:34 +00:00
|
|
|
if (_arr[j] == NULL)
|
|
|
|
break;
|
2007-03-04 09:58:04 +00:00
|
|
|
// Compute the slot where the content of the next slot should normally be,
|
|
|
|
// assuming an empty table, and check whether we have to move it.
|
2006-03-31 22:19:39 +00:00
|
|
|
uint k = _hash(_arr[j]->_key) % _arrsize;
|
2006-03-28 12:34:34 +00:00
|
|
|
if ((j > i && (k <= i || k > j)) ||
|
|
|
|
(j < i && (k <= i && k > j)) ) {
|
|
|
|
_arr[i] = _arr[j];
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_arr[i] = NULL;
|
2007-03-04 09:58:04 +00:00
|
|
|
return;
|
2006-03-28 12:34:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-23 22:59:38 +00:00
|
|
|
} // End of namespace Common
|
|
|
|
|
|
|
|
#endif
|