2010-02-06 10:14:05 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sw=4 et tw=99 ft=cpp:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
|
|
|
|
* November 13, 2009.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Corporation.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Brendan Eich <brendan@mozilla.org> (Original Author)
|
|
|
|
* Chris Waterson <waterson@netscape.com>
|
|
|
|
* L. David Baron <dbaron@dbaron.org>, Mozilla Corporation
|
|
|
|
* Luke Wagner <lw@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef jshashtable_h_
|
|
|
|
#define jshashtable_h_
|
|
|
|
|
|
|
|
#include "jstl.h"
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
|
|
|
/* Integral types for all hash functions. */
|
|
|
|
typedef uint32 HashNumber;
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/* Reusable implementation of HashMap and HashSet. */
|
|
|
|
template <class T, class HashPolicy, class AllocPolicy>
|
2011-03-22 14:19:10 -07:00
|
|
|
class HashTable : private AllocPolicy
|
2010-02-06 10:14:05 -08:00
|
|
|
{
|
2010-02-19 15:56:46 -08:00
|
|
|
typedef typename tl::StripConst<T>::result NonConstT;
|
2010-02-06 10:14:05 -08:00
|
|
|
typedef typename HashPolicy::KeyType Key;
|
|
|
|
typedef typename HashPolicy::Lookup Lookup;
|
|
|
|
|
2010-02-19 15:56:46 -08:00
|
|
|
/*
|
|
|
|
* T::operator= is a private operation for HashMap::Entry. HashMap::Entry
|
|
|
|
* makes HashTable a friend, but MSVC does not allow HashMap::Entry to make
|
|
|
|
* HashTable::Entry a friend. So do assignment here:
|
|
|
|
*/
|
|
|
|
static void assignT(NonConstT &dst, const T &src) { dst = src; }
|
|
|
|
|
2010-02-19 14:06:18 -08:00
|
|
|
public:
|
2010-02-06 10:14:05 -08:00
|
|
|
class Entry {
|
|
|
|
HashNumber keyHash;
|
2010-02-19 14:06:18 -08:00
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
public:
|
|
|
|
Entry() : keyHash(0), t() {}
|
2010-02-19 15:56:46 -08:00
|
|
|
void operator=(const Entry &rhs) { keyHash = rhs.keyHash; assignT(t, rhs.t); }
|
|
|
|
|
|
|
|
NonConstT t;
|
2010-02-06 10:14:05 -08:00
|
|
|
|
2010-05-10 23:27:53 -07:00
|
|
|
bool isFree() const { return keyHash == sFreeKey; }
|
|
|
|
void setFree() { keyHash = sFreeKey; assignT(t, T()); }
|
|
|
|
bool isRemoved() const { return keyHash == sRemovedKey; }
|
|
|
|
void setRemoved() { keyHash = sRemovedKey; assignT(t, T()); }
|
|
|
|
bool isLive() const { return isLiveHash(keyHash); }
|
|
|
|
void setLive(HashNumber hn) { JS_ASSERT(isLiveHash(hn)); keyHash = hn; }
|
|
|
|
|
|
|
|
void setCollision() { JS_ASSERT(isLive()); keyHash |= sCollisionBit; }
|
|
|
|
void setCollision(HashNumber collisionBit) {
|
|
|
|
JS_ASSERT(isLive()); keyHash |= collisionBit;
|
|
|
|
}
|
|
|
|
void unsetCollision() { JS_ASSERT(isLive()); keyHash &= ~sCollisionBit; }
|
|
|
|
bool hasCollision() const { JS_ASSERT(isLive()); return keyHash & sCollisionBit; }
|
2010-02-06 10:14:05 -08:00
|
|
|
bool matchHash(HashNumber hn) { return (keyHash & ~sCollisionBit) == hn; }
|
|
|
|
HashNumber getKeyHash() const { JS_ASSERT(!hasCollision()); return keyHash; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A nullable pointer to a hash table element. A Ptr |p| can be tested
|
|
|
|
* either explicitly |if (p.found()) p->...| or using boolean conversion
|
|
|
|
* |if (p) p->...|. Ptr objects must not be used after any mutating hash
|
|
|
|
* table operations unless |generation()| is tested.
|
|
|
|
*/
|
|
|
|
class Ptr
|
|
|
|
{
|
|
|
|
friend class HashTable;
|
|
|
|
typedef void (Ptr::* ConvertibleToBool)();
|
|
|
|
void nonNull() {}
|
|
|
|
|
|
|
|
Entry *entry;
|
|
|
|
|
2010-02-19 11:03:18 -08:00
|
|
|
protected:
|
|
|
|
Ptr(Entry &entry) : entry(&entry) {}
|
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
public:
|
2010-03-05 11:55:42 -08:00
|
|
|
bool found() const { return entry->isLive(); }
|
|
|
|
operator ConvertibleToBool() const { return found() ? &Ptr::nonNull : 0; }
|
|
|
|
bool operator==(const Ptr &rhs) const { JS_ASSERT(found() && rhs.found()); return entry == rhs.entry; }
|
|
|
|
bool operator!=(const Ptr &rhs) const { return !(*this == rhs); }
|
2010-02-06 10:14:05 -08:00
|
|
|
|
2010-03-05 11:55:42 -08:00
|
|
|
T &operator*() const { return entry->t; }
|
|
|
|
T *operator->() const { return &entry->t; }
|
2010-02-06 10:14:05 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A Ptr that can be used to add a key after a failed lookup. */
|
|
|
|
class AddPtr : public Ptr
|
|
|
|
{
|
|
|
|
friend class HashTable;
|
|
|
|
HashNumber keyHash;
|
2010-05-10 23:27:53 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
uint64 mutationCount;
|
|
|
|
|
|
|
|
AddPtr(Entry &entry, HashNumber hn, uint64 mutationCount)
|
|
|
|
: Ptr(entry), keyHash(hn), mutationCount(mutationCount) {}
|
|
|
|
#else
|
|
|
|
AddPtr(Entry &entry, HashNumber hn) : Ptr(entry), keyHash(hn) {}
|
|
|
|
#endif
|
2010-02-06 10:14:05 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A collection of hash table entries. The collection is enumerated by
|
|
|
|
* calling |front()| followed by |popFront()| as long as |!empty()|. As
|
|
|
|
* with Ptr/AddPtr, Range objects must not be used after any mutating hash
|
|
|
|
* table operation unless the |generation()| is tested.
|
|
|
|
*/
|
|
|
|
class Range
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
friend class HashTable;
|
|
|
|
|
|
|
|
Range(Entry *c, Entry *e) : cur(c), end(e) {
|
|
|
|
while (cur != end && !cur->isLive())
|
|
|
|
++cur;
|
|
|
|
}
|
|
|
|
|
2010-03-03 09:26:04 -08:00
|
|
|
Entry *cur, *end;
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
bool empty() const {
|
|
|
|
return cur == end;
|
|
|
|
}
|
|
|
|
|
2010-05-25 16:23:54 -07:00
|
|
|
T &front() const {
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(!empty());
|
|
|
|
return cur->t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void popFront() {
|
|
|
|
JS_ASSERT(!empty());
|
|
|
|
while (++cur != end && !cur->isLive());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A Range whose lifetime delimits a mutating enumeration of a hash table.
|
|
|
|
* Since rehashing when elements were removed during enumeration would be
|
|
|
|
* bad, it is postponed until |endEnumeration()| is called. If
|
2010-02-19 10:02:16 -08:00
|
|
|
* |endEnumeration()| is not called before an Enum's constructor, it will
|
|
|
|
* be called automatically. Since |endEnumeration()| touches the hash
|
2010-02-06 10:14:05 -08:00
|
|
|
* table, the user must ensure that the hash table is still alive when this
|
|
|
|
* happens.
|
|
|
|
*/
|
|
|
|
class Enum : public Range
|
|
|
|
{
|
|
|
|
friend class HashTable;
|
|
|
|
|
|
|
|
HashTable &table;
|
|
|
|
bool removed;
|
|
|
|
|
|
|
|
/* Not copyable. */
|
|
|
|
Enum(const Enum &);
|
|
|
|
void operator=(const Enum &);
|
|
|
|
|
|
|
|
public:
|
2010-05-10 23:27:53 -07:00
|
|
|
template<class Map> explicit
|
2010-02-26 12:11:50 -08:00
|
|
|
Enum(Map &map) : Range(map.all()), table(map.impl), removed(false) {}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes the |front()| element from the table, leaving |front()|
|
|
|
|
* invalid until the next call to |popFront()|. For example:
|
|
|
|
*
|
|
|
|
* HashSet<int> s;
|
2010-02-26 12:11:50 -08:00
|
|
|
* for (HashSet<int>::Enum e(s); !e.empty(); e.popFront())
|
2010-02-06 10:14:05 -08:00
|
|
|
* if (e.front() == 42)
|
|
|
|
* e.removeFront();
|
|
|
|
*/
|
|
|
|
void removeFront() {
|
|
|
|
table.remove(*this->cur);
|
|
|
|
removed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Potentially rehashes the table. */
|
|
|
|
~Enum() {
|
|
|
|
if (removed)
|
|
|
|
table.checkUnderloaded();
|
|
|
|
}
|
|
|
|
|
2010-02-19 10:02:16 -08:00
|
|
|
/* Can be used to end the enumeration before the destructor. */
|
2010-02-06 10:14:05 -08:00
|
|
|
void endEnumeration() {
|
|
|
|
if (removed) {
|
|
|
|
table.checkUnderloaded();
|
|
|
|
removed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32 hashShift; /* multiplicative hash shift */
|
|
|
|
uint32 tableCapacity; /* = JS_BIT(sHashBits - hashShift) */
|
|
|
|
uint32 entryCount; /* number of entries in table */
|
|
|
|
uint32 gen; /* entry storage generation number */
|
|
|
|
uint32 removedCount; /* removed entry sentinels in table */
|
|
|
|
Entry *table; /* entry storage */
|
|
|
|
|
|
|
|
void setTableSizeLog2(unsigned sizeLog2) {
|
|
|
|
hashShift = sHashBits - sizeLog2;
|
|
|
|
tableCapacity = JS_BIT(sizeLog2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mutable struct Stats {
|
|
|
|
uint32 searches; /* total number of table searches */
|
|
|
|
uint32 steps; /* hash chain links traversed */
|
|
|
|
uint32 hits; /* searches that found key */
|
|
|
|
uint32 misses; /* searches that didn't find key */
|
|
|
|
uint32 addOverRemoved; /* adds that recycled a removed entry */
|
|
|
|
uint32 removes; /* calls to remove */
|
|
|
|
uint32 removeFrees; /* calls to remove that freed the entry */
|
|
|
|
uint32 grows; /* table expansions */
|
|
|
|
uint32 shrinks; /* table contractions */
|
|
|
|
uint32 compresses; /* table compressions */
|
|
|
|
} stats;
|
|
|
|
# define METER(x) x
|
|
|
|
#else
|
|
|
|
# define METER(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
friend class js::ReentrancyGuard;
|
|
|
|
mutable bool entered;
|
2010-05-10 23:27:53 -07:00
|
|
|
uint64 mutationCount;
|
2010-02-06 10:14:05 -08:00
|
|
|
#endif
|
|
|
|
|
2010-02-13 12:02:12 -08:00
|
|
|
static const unsigned sMinSizeLog2 = 4;
|
|
|
|
static const unsigned sMinSize = 1 << sMinSizeLog2;
|
2010-02-15 16:07:57 -08:00
|
|
|
static const unsigned sSizeLimit = JS_BIT(24);
|
2010-02-06 10:14:05 -08:00
|
|
|
static const unsigned sHashBits = tl::BitSize<HashNumber>::result;
|
|
|
|
static const uint8 sMinAlphaFrac = 64; /* (0x100 * .25) taken from jsdhash.h */
|
|
|
|
static const uint8 sMaxAlphaFrac = 192; /* (0x100 * .75) taken from jsdhash.h */
|
2010-05-06 09:14:43 -07:00
|
|
|
static const uint8 sInvMaxAlpha = 171; /* (ceil(0x100 / .75) >> 1) */
|
2010-05-10 23:27:53 -07:00
|
|
|
static const HashNumber sGoldenRatio = 0x9E3779B9U; /* taken from jsdhash.h */
|
|
|
|
static const HashNumber sCollisionBit = 1;
|
|
|
|
static const HashNumber sFreeKey = 0;
|
|
|
|
static const HashNumber sRemovedKey = 1;
|
|
|
|
|
|
|
|
static bool isLiveHash(HashNumber hash)
|
|
|
|
{
|
|
|
|
return hash > sRemovedKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HashNumber prepareHash(const Lookup& l)
|
|
|
|
{
|
|
|
|
HashNumber keyHash = HashPolicy::hash(l);
|
|
|
|
|
|
|
|
/* Improve keyHash distribution. */
|
|
|
|
keyHash *= sGoldenRatio;
|
|
|
|
|
|
|
|
/* Avoid reserved hash codes. */
|
|
|
|
if (!isLiveHash(keyHash))
|
|
|
|
keyHash -= (sRemovedKey + 1);
|
|
|
|
return keyHash & ~sCollisionBit;
|
|
|
|
}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
static Entry *createTable(AllocPolicy &alloc, uint32 capacity)
|
|
|
|
{
|
|
|
|
Entry *newTable = (Entry *)alloc.malloc(capacity * sizeof(Entry));
|
|
|
|
if (!newTable)
|
|
|
|
return NULL;
|
|
|
|
for (Entry *e = newTable, *end = e + capacity; e != end; ++e)
|
|
|
|
new(e) Entry();
|
|
|
|
return newTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroyTable(AllocPolicy &alloc, Entry *oldTable, uint32 capacity)
|
|
|
|
{
|
|
|
|
for (Entry *e = oldTable, *end = e + capacity; e != end; ++e)
|
|
|
|
e->~Entry();
|
|
|
|
alloc.free(oldTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
HashTable(AllocPolicy ap)
|
|
|
|
: AllocPolicy(ap),
|
|
|
|
entryCount(0),
|
|
|
|
gen(0),
|
|
|
|
removedCount(0),
|
|
|
|
table(NULL)
|
|
|
|
#ifdef DEBUG
|
2010-05-10 23:27:53 -07:00
|
|
|
, entered(false),
|
|
|
|
mutationCount(0)
|
2010-02-06 10:14:05 -08:00
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
2010-05-06 09:14:43 -07:00
|
|
|
bool init(uint32 length)
|
2010-02-06 10:14:05 -08:00
|
|
|
{
|
2010-09-27 15:35:10 -07:00
|
|
|
/* Make sure that init isn't called twice. */
|
|
|
|
JS_ASSERT(table == NULL);
|
|
|
|
|
2010-05-06 09:14:43 -07:00
|
|
|
/*
|
|
|
|
* Correct for sMaxAlphaFrac such that the table will not resize
|
|
|
|
* when adding 'length' entries.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(length < (uint32(1) << 23));
|
|
|
|
uint32 capacity = (length * sInvMaxAlpha) >> 7;
|
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
if (capacity < sMinSize)
|
|
|
|
capacity = sMinSize;
|
2010-02-13 12:02:12 -08:00
|
|
|
|
|
|
|
/* FIXME: use JS_CEILING_LOG2 when PGO stops crashing (bug 543034). */
|
2010-02-15 16:07:57 -08:00
|
|
|
uint32 roundUp = sMinSize, roundUpLog2 = sMinSizeLog2;
|
2010-02-13 12:02:12 -08:00
|
|
|
while (roundUp < capacity) {
|
|
|
|
roundUp <<= 1;
|
|
|
|
++roundUpLog2;
|
|
|
|
}
|
|
|
|
|
|
|
|
capacity = roundUp;
|
2010-05-12 20:35:56 -07:00
|
|
|
if (capacity >= sSizeLimit) {
|
|
|
|
this->reportAllocOverflow();
|
2010-02-06 10:14:05 -08:00
|
|
|
return false;
|
2010-05-12 20:35:56 -07:00
|
|
|
}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
table = createTable(*this, capacity);
|
|
|
|
if (!table)
|
|
|
|
return false;
|
|
|
|
|
2010-02-13 12:02:12 -08:00
|
|
|
setTableSizeLog2(roundUpLog2);
|
2010-02-06 10:14:05 -08:00
|
|
|
METER(memset(&stats, 0, sizeof(stats)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-13 10:50:43 -07:00
|
|
|
bool initialized() const
|
|
|
|
{
|
|
|
|
return !!table;
|
|
|
|
}
|
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
~HashTable()
|
|
|
|
{
|
|
|
|
if (table)
|
|
|
|
destroyTable(*this, table, tableCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-05-10 23:27:53 -07:00
|
|
|
static HashNumber hash1(HashNumber hash0, uint32 shift) {
|
2010-02-06 10:14:05 -08:00
|
|
|
return hash0 >> shift;
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:27:53 -07:00
|
|
|
static HashNumber hash2(HashNumber hash0, uint32 log2, uint32 shift) {
|
2010-02-06 10:14:05 -08:00
|
|
|
return ((hash0 << log2) >> shift) | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool overloaded() {
|
|
|
|
return entryCount + removedCount >= ((sMaxAlphaFrac * tableCapacity) >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool underloaded() {
|
|
|
|
return tableCapacity > sMinSize &&
|
|
|
|
entryCount <= ((sMinAlphaFrac * tableCapacity) >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool match(Entry &e, const Lookup &l) {
|
|
|
|
return HashPolicy::match(HashPolicy::getKey(e.t), l);
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:27:53 -07:00
|
|
|
Entry &lookup(const Lookup &l, HashNumber keyHash, unsigned collisionBit) const
|
2010-02-06 10:14:05 -08:00
|
|
|
{
|
2010-05-10 23:27:53 -07:00
|
|
|
JS_ASSERT(isLiveHash(keyHash));
|
|
|
|
JS_ASSERT(!(keyHash & sCollisionBit));
|
|
|
|
JS_ASSERT(collisionBit == 0 || collisionBit == sCollisionBit);
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(table);
|
|
|
|
METER(stats.searches++);
|
|
|
|
|
|
|
|
/* Compute the primary hash address. */
|
2010-05-10 23:27:53 -07:00
|
|
|
HashNumber h1 = hash1(keyHash, hashShift);
|
2010-02-06 10:14:05 -08:00
|
|
|
Entry *entry = &table[h1];
|
|
|
|
|
|
|
|
/* Miss: return space for a new entry. */
|
|
|
|
if (entry->isFree()) {
|
|
|
|
METER(stats.misses++);
|
2010-05-10 23:27:53 -07:00
|
|
|
return *entry;
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Hit: return entry. */
|
|
|
|
if (entry->matchHash(keyHash) && match(*entry, l)) {
|
|
|
|
METER(stats.hits++);
|
2010-05-10 23:27:53 -07:00
|
|
|
return *entry;
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Collision: double hash. */
|
|
|
|
unsigned sizeLog2 = sHashBits - hashShift;
|
2010-05-10 23:27:53 -07:00
|
|
|
HashNumber h2 = hash2(keyHash, sizeLog2, hashShift);
|
|
|
|
HashNumber sizeMask = (HashNumber(1) << sizeLog2) - 1;
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
/* Save the first removed entry pointer so we can recycle later. */
|
|
|
|
Entry *firstRemoved = NULL;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
if (JS_UNLIKELY(entry->isRemoved())) {
|
|
|
|
if (!firstRemoved)
|
|
|
|
firstRemoved = entry;
|
|
|
|
} else {
|
2010-05-10 23:27:53 -07:00
|
|
|
entry->setCollision(collisionBit);
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
METER(stats.steps++);
|
|
|
|
h1 -= h2;
|
|
|
|
h1 &= sizeMask;
|
|
|
|
|
|
|
|
entry = &table[h1];
|
|
|
|
if (entry->isFree()) {
|
|
|
|
METER(stats.misses++);
|
2010-05-10 23:27:53 -07:00
|
|
|
return firstRemoved ? *firstRemoved : *entry;
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->matchHash(keyHash) && match(*entry, l)) {
|
|
|
|
METER(stats.hits++);
|
2010-05-10 23:27:53 -07:00
|
|
|
return *entry;
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a copy of lookup hardcoded to the assumptions:
|
2010-02-22 19:43:41 -08:00
|
|
|
* 1. the lookup is a lookupForAdd
|
2010-02-06 10:14:05 -08:00
|
|
|
* 2. the key, whose |keyHash| has been passed is not in the table,
|
|
|
|
* 3. no entries have been removed from the table.
|
|
|
|
* This specialized search avoids the need for recovering lookup values
|
|
|
|
* from entries, which allows more flexible Lookup/Key types.
|
|
|
|
*/
|
|
|
|
Entry &findFreeEntry(HashNumber keyHash)
|
|
|
|
{
|
|
|
|
METER(stats.searches++);
|
|
|
|
JS_ASSERT(!(keyHash & sCollisionBit));
|
|
|
|
|
|
|
|
/* N.B. the |keyHash| has already been distributed. */
|
|
|
|
|
|
|
|
/* Compute the primary hash address. */
|
2010-05-10 23:27:53 -07:00
|
|
|
HashNumber h1 = hash1(keyHash, hashShift);
|
2010-02-06 10:14:05 -08:00
|
|
|
Entry *entry = &table[h1];
|
|
|
|
|
|
|
|
/* Miss: return space for a new entry. */
|
|
|
|
if (entry->isFree()) {
|
|
|
|
METER(stats.misses++);
|
|
|
|
return *entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Collision: double hash. */
|
|
|
|
unsigned sizeLog2 = sHashBits - hashShift;
|
2010-05-10 23:27:53 -07:00
|
|
|
HashNumber h2 = hash2(keyHash, sizeLog2, hashShift);
|
|
|
|
HashNumber sizeMask = (HashNumber(1) << sizeLog2) - 1;
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
while(true) {
|
|
|
|
JS_ASSERT(!entry->isRemoved());
|
|
|
|
entry->setCollision();
|
|
|
|
|
|
|
|
METER(stats.steps++);
|
|
|
|
h1 -= h2;
|
|
|
|
h1 &= sizeMask;
|
|
|
|
|
|
|
|
entry = &table[h1];
|
|
|
|
if (entry->isFree()) {
|
|
|
|
METER(stats.misses++);
|
|
|
|
return *entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool changeTableSize(int deltaLog2)
|
|
|
|
{
|
|
|
|
/* Look, but don't touch, until we succeed in getting new entry store. */
|
|
|
|
Entry *oldTable = table;
|
|
|
|
uint32 oldCap = tableCapacity;
|
|
|
|
uint32 newLog2 = sHashBits - hashShift + deltaLog2;
|
|
|
|
uint32 newCapacity = JS_BIT(newLog2);
|
2010-05-12 20:35:56 -07:00
|
|
|
if (newCapacity >= sSizeLimit) {
|
|
|
|
this->reportAllocOverflow();
|
2010-02-06 10:14:05 -08:00
|
|
|
return false;
|
2010-05-12 20:35:56 -07:00
|
|
|
}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
Entry *newTable = createTable(*this, newCapacity);
|
|
|
|
if (!newTable)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* We can't fail from here on, so update table parameters. */
|
|
|
|
setTableSizeLog2(newLog2);
|
|
|
|
removedCount = 0;
|
|
|
|
gen++;
|
|
|
|
table = newTable;
|
|
|
|
|
|
|
|
/* Copy only live entries, leaving removed ones behind. */
|
|
|
|
for (Entry *src = oldTable, *end = src + oldCap; src != end; ++src) {
|
|
|
|
if (src->isLive()) {
|
|
|
|
src->unsetCollision();
|
|
|
|
findFreeEntry(src->getKeyHash()) = *src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroyTable(*this, oldTable, oldCap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(Entry &e)
|
|
|
|
{
|
|
|
|
METER(stats.removes++);
|
|
|
|
if (e.hasCollision()) {
|
|
|
|
e.setRemoved();
|
|
|
|
removedCount++;
|
|
|
|
} else {
|
|
|
|
METER(stats.removeFrees++);
|
|
|
|
e.setFree();
|
|
|
|
}
|
|
|
|
entryCount--;
|
2010-05-10 23:27:53 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
mutationCount++;
|
|
|
|
#endif
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void checkUnderloaded()
|
|
|
|
{
|
|
|
|
if (underloaded()) {
|
|
|
|
METER(stats.shrinks++);
|
|
|
|
(void) changeTableSize(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
for (Entry *e = table, *end = table + tableCapacity; e != end; ++e)
|
|
|
|
*e = Entry();
|
|
|
|
removedCount = 0;
|
|
|
|
entryCount = 0;
|
2010-05-10 23:27:53 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
mutationCount++;
|
|
|
|
#endif
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Range all() const {
|
|
|
|
return Range(table, table + tableCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return !entryCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 count() const{
|
|
|
|
return entryCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 generation() const {
|
|
|
|
return gen;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr lookup(const Lookup &l) const {
|
|
|
|
ReentrancyGuard g(*this);
|
2010-05-10 23:27:53 -07:00
|
|
|
HashNumber keyHash = prepareHash(l);
|
|
|
|
return Ptr(lookup(l, keyHash, 0));
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AddPtr lookupForAdd(const Lookup &l) const {
|
|
|
|
ReentrancyGuard g(*this);
|
2010-05-10 23:27:53 -07:00
|
|
|
HashNumber keyHash = prepareHash(l);
|
|
|
|
Entry &entry = lookup(l, keyHash, sCollisionBit);
|
|
|
|
#ifdef DEBUG
|
|
|
|
return AddPtr(entry, keyHash, mutationCount);
|
|
|
|
#else
|
|
|
|
return AddPtr(entry, keyHash);
|
|
|
|
#endif
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
2010-07-21 19:47:02 -07:00
|
|
|
bool add(AddPtr &p)
|
2010-02-06 10:14:05 -08:00
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
2010-05-10 23:27:53 -07:00
|
|
|
JS_ASSERT(mutationCount == p.mutationCount);
|
2010-02-06 10:14:05 -08:00
|
|
|
JS_ASSERT(table);
|
|
|
|
JS_ASSERT(!p.found());
|
|
|
|
JS_ASSERT(!(p.keyHash & sCollisionBit));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Changing an entry from removed to live does not affect whether we
|
|
|
|
* are overloaded and can be handled separately.
|
|
|
|
*/
|
|
|
|
if (p.entry->isRemoved()) {
|
|
|
|
METER(stats.addOverRemoved++);
|
|
|
|
removedCount--;
|
|
|
|
p.keyHash |= sCollisionBit;
|
|
|
|
} else {
|
|
|
|
/* If alpha is >= .75, grow or compress the table. */
|
|
|
|
if (overloaded()) {
|
|
|
|
/* Compress if a quarter or more of all entries are removed. */
|
|
|
|
int deltaLog2;
|
|
|
|
if (removedCount >= (tableCapacity >> 2)) {
|
|
|
|
METER(stats.compresses++);
|
|
|
|
deltaLog2 = 0;
|
|
|
|
} else {
|
|
|
|
METER(stats.grows++);
|
|
|
|
deltaLog2 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changeTableSize(deltaLog2))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Preserve the validity of |p.entry|. */
|
|
|
|
p.entry = &findFreeEntry(p.keyHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.entry->setLive(p.keyHash);
|
|
|
|
entryCount++;
|
2010-05-10 23:27:53 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
mutationCount++;
|
|
|
|
#endif
|
2010-02-06 10:14:05 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-21 19:47:02 -07:00
|
|
|
/*
|
|
|
|
* There is an important contract between the caller and callee for this
|
|
|
|
* function: if add() returns true, the caller must assign the T value
|
|
|
|
* which produced p before using the hashtable again.
|
|
|
|
*/
|
|
|
|
bool add(AddPtr &p, T** pentry)
|
|
|
|
{
|
|
|
|
if (!add(p))
|
|
|
|
return false;
|
|
|
|
*pentry = &p.entry->t;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add(AddPtr &p, const T &t)
|
|
|
|
{
|
|
|
|
if (!add(p))
|
|
|
|
return false;
|
|
|
|
p.entry->t = t;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:27:53 -07:00
|
|
|
bool relookupOrAdd(AddPtr& p, const Lookup &l, const T& t)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
p.mutationCount = mutationCount;
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
|
|
|
p.entry = &lookup(l, p.keyHash, sCollisionBit);
|
|
|
|
}
|
|
|
|
return p.found() || add(p, t);
|
|
|
|
}
|
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
void remove(Ptr p)
|
|
|
|
{
|
|
|
|
ReentrancyGuard g(*this);
|
|
|
|
JS_ASSERT(p.found());
|
|
|
|
remove(*p.entry);
|
|
|
|
checkUnderloaded();
|
|
|
|
}
|
|
|
|
#undef METER
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash policy
|
|
|
|
*
|
|
|
|
* A hash policy P for a hash table with key-type Key must provide:
|
|
|
|
* - a type |P::Lookup| to use to lookup table entries;
|
|
|
|
* - a static member function |P::hash| with signature
|
|
|
|
*
|
|
|
|
* static js::HashNumber hash(Lookup)
|
|
|
|
*
|
|
|
|
* to use to hash the lookup type; and
|
|
|
|
* - a static member function |P::match| with signature
|
|
|
|
*
|
|
|
|
* static bool match(Key, Lookup)
|
|
|
|
*
|
|
|
|
* to use to test equality of key and lookup values.
|
|
|
|
*
|
2010-02-19 10:02:16 -08:00
|
|
|
* Normally, Lookup = Key. In general, though, different values and types of
|
|
|
|
* values can be used to lookup and store. If a Lookup value |l| is != to the
|
|
|
|
* added Key value |k|, the user must ensure that |P::match(k,l)|. E.g.:
|
2010-02-06 10:14:05 -08:00
|
|
|
*
|
|
|
|
* js::HashSet<Key, P>::AddPtr p = h.lookup(l);
|
|
|
|
* if (!p) {
|
|
|
|
* assert(P::match(k, l)); // must hold
|
|
|
|
* h.add(p, k);
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Default hashing policies. */
|
|
|
|
template <class Key>
|
|
|
|
struct DefaultHasher
|
|
|
|
{
|
|
|
|
typedef Key Lookup;
|
2010-05-10 23:27:53 -07:00
|
|
|
static HashNumber hash(const Lookup &l) {
|
2010-02-06 10:14:05 -08:00
|
|
|
/* Hash if can implicitly cast to hash number type. */
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
static bool match(const Key &k, const Lookup &l) {
|
|
|
|
/* Use builtin or overloaded operator==. */
|
|
|
|
return k == l;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Specialized hashing policy for pointer types. */
|
|
|
|
template <class T>
|
|
|
|
struct DefaultHasher<T *>
|
|
|
|
{
|
|
|
|
typedef T *Lookup;
|
2010-05-10 23:27:53 -07:00
|
|
|
static HashNumber hash(T *l) {
|
2010-02-06 10:14:05 -08:00
|
|
|
/*
|
|
|
|
* Strip often-0 lower bits for better distribution after multiplying
|
|
|
|
* by the sGoldenRatio.
|
|
|
|
*/
|
2010-05-10 23:27:53 -07:00
|
|
|
return HashNumber(reinterpret_cast<size_t>(l) >>
|
|
|
|
tl::FloorLog2<sizeof(void *)>::result);
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
static bool match(T *k, T *l) {
|
|
|
|
return k == l;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS-friendly, STL-like container providing a hash-based map from keys to
|
|
|
|
* values. In particular, HashMap calls constructors and destructors of all
|
|
|
|
* objects added so non-PODs may be used safely.
|
|
|
|
*
|
|
|
|
* Key/Value requirements:
|
|
|
|
* - default constructible, copyable, destructible, assignable
|
|
|
|
* HashPolicy requirements:
|
|
|
|
* - see "Hash policy" above (default js::DefaultHasher<Key>)
|
|
|
|
* AllocPolicy:
|
|
|
|
* - see "Allocation policies" in jstl.h (default js::ContextAllocPolicy)
|
|
|
|
*
|
|
|
|
* N.B: HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members
|
|
|
|
* called by HashMap must not call back into the same HashMap object.
|
|
|
|
* N.B: Due to the lack of exception handling, the user must call |init()|.
|
|
|
|
*/
|
|
|
|
template <class Key, class Value, class HashPolicy, class AllocPolicy>
|
|
|
|
class HashMap
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename HashPolicy::Lookup Lookup;
|
|
|
|
|
2010-02-19 10:02:16 -08:00
|
|
|
class Entry
|
2010-02-06 10:14:05 -08:00
|
|
|
{
|
2010-02-19 10:02:16 -08:00
|
|
|
template <class, class, class> friend class detail::HashTable;
|
|
|
|
void operator=(const Entry &rhs) {
|
|
|
|
const_cast<Key &>(key) = rhs.key;
|
|
|
|
value = rhs.value;
|
|
|
|
}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
2010-02-19 10:02:16 -08:00
|
|
|
public:
|
|
|
|
Entry() : key(), value() {}
|
|
|
|
Entry(const Key &k, const Value &v) : key(k), value(v) {}
|
|
|
|
|
|
|
|
const Key key;
|
|
|
|
Value value;
|
|
|
|
};
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/* Implement HashMap using HashTable. Lift |Key| operations to |Entry|. */
|
|
|
|
struct MapHashPolicy : HashPolicy
|
|
|
|
{
|
|
|
|
typedef Key KeyType;
|
|
|
|
static const Key &getKey(Entry &e) { return e.key; }
|
|
|
|
};
|
2010-02-19 10:02:16 -08:00
|
|
|
typedef detail::HashTable<Entry, MapHashPolicy, AllocPolicy> Impl;
|
2010-02-06 10:14:05 -08:00
|
|
|
|
2010-02-26 12:11:50 -08:00
|
|
|
friend class Impl::Enum;
|
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
/* Not implicitly copyable (expensive). May add explicit |clone| later. */
|
|
|
|
HashMap(const HashMap &);
|
|
|
|
HashMap &operator=(const HashMap &);
|
|
|
|
|
|
|
|
Impl impl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* HashMap construction is fallible (due to OOM); thus the user must call
|
|
|
|
* init after constructing a HashMap and check the return value.
|
|
|
|
*/
|
|
|
|
HashMap(AllocPolicy a = AllocPolicy()) : impl(a) {}
|
2010-05-06 09:14:43 -07:00
|
|
|
bool init(uint32 len = 0) { return impl.init(len); }
|
2010-05-13 10:50:43 -07:00
|
|
|
bool initialized() const { return impl.initialized(); }
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return whether the given lookup value is present in the map. E.g.:
|
|
|
|
*
|
|
|
|
* typedef HashMap<int,char> HM;
|
|
|
|
* HM h;
|
|
|
|
* if (HM::Ptr p = h.lookup(3)) {
|
|
|
|
* const HM::Entry &e = *p; // p acts like a pointer to Entry
|
|
|
|
* assert(p->key == 3); // Entry contains the key
|
|
|
|
* char val = p->value; // and value
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Also see the definition of Ptr in HashTable above (with T = Entry).
|
|
|
|
*/
|
|
|
|
typedef typename Impl::Ptr Ptr;
|
|
|
|
Ptr lookup(const Lookup &l) const { return impl.lookup(l); }
|
|
|
|
|
|
|
|
/* Assuming |p.found()|, remove |*p|. */
|
|
|
|
void remove(Ptr p) { impl.remove(p); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
|
|
|
|
* insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using
|
2010-02-19 10:02:16 -08:00
|
|
|
* |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new Entry. E.g.:
|
2010-02-06 10:14:05 -08:00
|
|
|
*
|
|
|
|
* typedef HashMap<int,char> HM;
|
|
|
|
* HM h;
|
|
|
|
* HM::AddPtr p = h.lookupForAdd(3);
|
|
|
|
* if (!p) {
|
|
|
|
* if (!h.add(p, 3, 'a'))
|
|
|
|
* return false;
|
|
|
|
* }
|
|
|
|
* const HM::Entry &e = *p; // p acts like a pointer to Entry
|
|
|
|
* assert(p->key == 3); // Entry contains the key
|
|
|
|
* char val = p->value; // and value
|
|
|
|
*
|
|
|
|
* Also see the definition of AddPtr in HashTable above (with T = Entry).
|
2010-05-10 23:27:53 -07:00
|
|
|
*
|
|
|
|
* N.B. The caller must ensure that no mutating hash table operations
|
|
|
|
* occur between a pair of |lookupForAdd| and |add| calls. To avoid
|
|
|
|
* looking up the key a second time, the caller may use the more efficient
|
2010-07-28 09:54:14 -07:00
|
|
|
* relookupOrAdd method. This method reuses part of the hashing computation
|
|
|
|
* to more efficiently insert the key if it has not been added. For
|
2010-05-10 23:27:53 -07:00
|
|
|
* example, a mutation-handling version of the previous example:
|
|
|
|
*
|
|
|
|
* HM::AddPtr p = h.lookupForAdd(3);
|
|
|
|
* if (!p) {
|
|
|
|
* call_that_may_mutate_h();
|
|
|
|
* if (!h.relookupOrAdd(p, 3, 'a'))
|
|
|
|
* return false;
|
|
|
|
* }
|
|
|
|
* const HM::Entry &e = *p;
|
|
|
|
* assert(p->key == 3);
|
|
|
|
* char val = p->value;
|
2010-02-06 10:14:05 -08:00
|
|
|
*/
|
|
|
|
typedef typename Impl::AddPtr AddPtr;
|
2010-07-21 19:01:33 -07:00
|
|
|
AddPtr lookupForAdd(const Lookup &l) const {
|
|
|
|
return impl.lookupForAdd(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add(AddPtr &p, const Key &k, const Value &v) {
|
|
|
|
Entry *pentry;
|
|
|
|
if (!impl.add(p, &pentry))
|
|
|
|
return false;
|
|
|
|
const_cast<Key &>(pentry->key) = k;
|
|
|
|
pentry->value = v;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add(AddPtr &p, const Key &k) {
|
|
|
|
Entry *pentry;
|
|
|
|
if (!impl.add(p, &pentry))
|
|
|
|
return false;
|
|
|
|
const_cast<Key &>(pentry->key) = k;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:27:53 -07:00
|
|
|
bool relookupOrAdd(AddPtr &p, const Key &k, const Value &v) {
|
|
|
|
return impl.relookupOrAdd(p, k, Entry(k, v));
|
|
|
|
}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* |all()| returns a Range containing |count()| elements. E.g.:
|
|
|
|
*
|
|
|
|
* typedef HashMap<int,char> HM;
|
|
|
|
* HM h;
|
|
|
|
* for (HM::Range r = h.all(); !r.empty(); r.popFront())
|
|
|
|
* char c = r.front().value;
|
|
|
|
*
|
|
|
|
* Also see the definition of Range in HashTable above (with T = Entry).
|
|
|
|
*/
|
|
|
|
typedef typename Impl::Range Range;
|
|
|
|
Range all() const { return impl.all(); }
|
|
|
|
size_t count() const { return impl.count(); }
|
|
|
|
|
|
|
|
/*
|
2010-02-26 12:11:50 -08:00
|
|
|
* Typedef for the enumeration class. An Enum may be used to examine and
|
|
|
|
* remove table entries:
|
2010-02-06 10:14:05 -08:00
|
|
|
*
|
|
|
|
* typedef HashMap<int,char> HM;
|
|
|
|
* HM s;
|
2010-02-26 12:11:50 -08:00
|
|
|
* for (HM::Enum e(s); !e.empty(); e.popFront())
|
2010-02-06 10:14:05 -08:00
|
|
|
* if (e.front().value == 'l')
|
|
|
|
* e.removeFront();
|
|
|
|
*
|
|
|
|
* Table resize may occur in Enum's destructor. Also see the definition of
|
|
|
|
* Enum in HashTable above (with T = Entry).
|
|
|
|
*/
|
|
|
|
typedef typename Impl::Enum Enum;
|
|
|
|
|
|
|
|
/* Remove all entries. */
|
|
|
|
void clear() { impl.clear(); }
|
|
|
|
|
|
|
|
/* Does the table contain any entries? */
|
|
|
|
bool empty() const { return impl.empty(); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If |generation()| is the same before and after a HashMap operation,
|
|
|
|
* pointers into the table remain valid.
|
|
|
|
*/
|
|
|
|
unsigned generation() const { return impl.generation(); }
|
|
|
|
|
|
|
|
/* Shorthand operations: */
|
|
|
|
|
|
|
|
bool has(const Lookup &l) const {
|
|
|
|
return impl.lookup(l) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry *put(const Key &k, const Value &v) {
|
|
|
|
AddPtr p = lookupForAdd(k);
|
2010-02-17 10:47:26 -08:00
|
|
|
if (p) {
|
|
|
|
p->value = v;
|
|
|
|
return &*p;
|
|
|
|
}
|
|
|
|
return add(p, k, v) ? &*p : NULL;
|
2010-02-06 10:14:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove(const Lookup &l) {
|
|
|
|
if (Ptr p = lookup(l))
|
|
|
|
remove(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS-friendly, STL-like container providing a hash-based set of values. In
|
|
|
|
* particular, HashSet calls constructors and destructors of all objects added
|
|
|
|
* so non-PODs may be used safely.
|
|
|
|
*
|
|
|
|
* T requirements:
|
|
|
|
* - default constructible, copyable, destructible, assignable
|
|
|
|
* HashPolicy requirements:
|
|
|
|
* - see "Hash policy" above (default js::DefaultHasher<Key>)
|
|
|
|
* AllocPolicy:
|
|
|
|
* - see "Allocation policies" in jstl.h (default js::ContextAllocPolicy)
|
|
|
|
*
|
|
|
|
* N.B: HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by
|
|
|
|
* HashSet must not call back into the same HashSet object.
|
|
|
|
* N.B: Due to the lack of exception handling, the user must call |init()|.
|
|
|
|
*/
|
|
|
|
template <class T, class HashPolicy, class AllocPolicy>
|
|
|
|
class HashSet
|
|
|
|
{
|
|
|
|
typedef typename HashPolicy::Lookup Lookup;
|
|
|
|
|
|
|
|
/* Implement HashSet in terms of HashTable. */
|
|
|
|
struct SetOps : HashPolicy {
|
|
|
|
typedef T KeyType;
|
|
|
|
static const KeyType &getKey(const T &t) { return t; }
|
|
|
|
};
|
2010-02-19 10:02:16 -08:00
|
|
|
typedef detail::HashTable<const T, SetOps, AllocPolicy> Impl;
|
2010-02-06 10:14:05 -08:00
|
|
|
|
2010-02-26 12:11:50 -08:00
|
|
|
friend class Impl::Enum;
|
|
|
|
|
2010-02-06 10:14:05 -08:00
|
|
|
/* Not implicitly copyable (expensive). May add explicit |clone| later. */
|
|
|
|
HashSet(const HashSet &);
|
|
|
|
HashSet &operator=(const HashSet &);
|
|
|
|
|
|
|
|
Impl impl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* HashSet construction is fallible (due to OOM); thus the user must call
|
|
|
|
* init after constructing a HashSet and check the return value.
|
|
|
|
*/
|
|
|
|
HashSet(AllocPolicy a = AllocPolicy()) : impl(a) {}
|
2010-05-06 09:14:43 -07:00
|
|
|
bool init(uint32 len = 0) { return impl.init(len); }
|
2010-05-13 10:50:43 -07:00
|
|
|
bool initialized() const { return impl.initialized(); }
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return whether the given lookup value is present in the map. E.g.:
|
|
|
|
*
|
|
|
|
* typedef HashSet<int> HS;
|
|
|
|
* HS h;
|
|
|
|
* if (HS::Ptr p = h.lookup(3)) {
|
|
|
|
* assert(*p == 3); // p acts like a pointer to int
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Also see the definition of Ptr in HashTable above.
|
|
|
|
*/
|
|
|
|
typedef typename Impl::Ptr Ptr;
|
|
|
|
Ptr lookup(const Lookup &l) const { return impl.lookup(l); }
|
|
|
|
|
|
|
|
/* Assuming |p.found()|, remove |*p|. */
|
|
|
|
void remove(Ptr p) { impl.remove(p); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
|
|
|
|
* insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using
|
2010-02-19 10:02:16 -08:00
|
|
|
* |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.:
|
2010-02-06 10:14:05 -08:00
|
|
|
*
|
|
|
|
* typedef HashSet<int> HS;
|
|
|
|
* HS h;
|
|
|
|
* HS::AddPtr p = h.lookupForAdd(3);
|
|
|
|
* if (!p) {
|
|
|
|
* if (!h.add(p, 3))
|
|
|
|
* return false;
|
|
|
|
* }
|
|
|
|
* assert(*p == 3); // p acts like a pointer to int
|
|
|
|
*
|
|
|
|
* Also see the definition of AddPtr in HashTable above.
|
2010-07-28 09:54:14 -07:00
|
|
|
*
|
|
|
|
* N.B. The caller must ensure that no mutating hash table operations
|
|
|
|
* occur between a pair of |lookupForAdd| and |add| calls. To avoid
|
|
|
|
* looking up the key a second time, the caller may use the more efficient
|
|
|
|
* relookupOrAdd method. This method reuses part of the hashing computation
|
|
|
|
* to more efficiently insert the key if it has not been added. For
|
|
|
|
* example, a mutation-handling version of the previous example:
|
|
|
|
*
|
|
|
|
* HS::AddPtr p = h.lookupForAdd(3);
|
|
|
|
* if (!p) {
|
|
|
|
* call_that_may_mutate_h();
|
|
|
|
* if (!h.relookupOrAdd(p, 3, 3))
|
|
|
|
* return false;
|
|
|
|
* }
|
|
|
|
* assert(*p == 3);
|
|
|
|
*
|
|
|
|
* Note that relookupOrAdd(p,l,t) performs Lookup using l and adds the
|
|
|
|
* entry t, where the caller ensures match(l,t).
|
2010-02-06 10:14:05 -08:00
|
|
|
*/
|
|
|
|
typedef typename Impl::AddPtr AddPtr;
|
2010-07-21 19:01:33 -07:00
|
|
|
AddPtr lookupForAdd(const Lookup &l) const {
|
|
|
|
return impl.lookupForAdd(l);
|
|
|
|
}
|
|
|
|
|
2010-07-28 09:54:14 -07:00
|
|
|
bool add(AddPtr &p, const T &t) {
|
|
|
|
return impl.add(p, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool relookupOrAdd(AddPtr &p, const Lookup &l, const T &t) {
|
|
|
|
return impl.relookupOrAdd(p, l, t);
|
|
|
|
}
|
2010-02-06 10:14:05 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* |all()| returns a Range containing |count()| elements:
|
|
|
|
*
|
|
|
|
* typedef HashSet<int> HS;
|
|
|
|
* HS h;
|
|
|
|
* for (HS::Range r = h.all(); !r.empty(); r.popFront())
|
|
|
|
* int i = r.front();
|
|
|
|
*
|
|
|
|
* Also see the definition of Range in HashTable above.
|
|
|
|
*/
|
|
|
|
typedef typename Impl::Range Range;
|
|
|
|
Range all() const { return impl.all(); }
|
|
|
|
size_t count() const { return impl.count(); }
|
|
|
|
|
|
|
|
/*
|
2010-02-26 12:11:50 -08:00
|
|
|
* Typedef for the enumeration class. An Enum may be used to examine and
|
|
|
|
* remove table entries:
|
2010-02-06 10:14:05 -08:00
|
|
|
*
|
|
|
|
* typedef HashSet<int> HS;
|
|
|
|
* HS s;
|
2010-02-26 12:11:50 -08:00
|
|
|
* for (HS::Enum e(s); !e.empty(); e.popFront())
|
2010-02-06 10:14:05 -08:00
|
|
|
* if (e.front() == 42)
|
|
|
|
* e.removeFront();
|
|
|
|
*
|
|
|
|
* Table resize may occur in Enum's destructor. Also see the definition of
|
|
|
|
* Enum in HashTable above.
|
|
|
|
*/
|
|
|
|
typedef typename Impl::Enum Enum;
|
|
|
|
|
|
|
|
/* Remove all entries. */
|
|
|
|
void clear() { impl.clear(); }
|
|
|
|
|
|
|
|
/* Does the table contain any entries? */
|
|
|
|
bool empty() const { return impl.empty(); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If |generation()| is the same before and after a HashSet operation,
|
|
|
|
* pointers into the table remain valid.
|
|
|
|
*/
|
|
|
|
unsigned generation() const { return impl.generation(); }
|
|
|
|
|
|
|
|
/* Shorthand operations: */
|
|
|
|
|
|
|
|
bool has(const Lookup &l) const {
|
|
|
|
return impl.lookup(l) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *put(const T &t) {
|
|
|
|
AddPtr p = lookupForAdd(t);
|
|
|
|
return p ? &*p : (add(p, t) ? &*p : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(const Lookup &l) {
|
|
|
|
if (Ptr p = lookup(l))
|
|
|
|
remove(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
#endif
|