2013-04-16 20:47:10 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-06-20 00:59:09 +00:00
|
|
|
#ifndef js_HashTable_h
|
|
|
|
#define js_HashTable_h
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-08-14 07:00:52 +00:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-03-22 01:26:15 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-12-14 23:58:45 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-20 18:55:41 +00:00
|
|
|
#include "mozilla/Casting.h"
|
2013-06-23 11:21:01 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-07-03 00:25:13 +00:00
|
|
|
#include "mozilla/Move.h"
|
2013-04-02 05:43:19 +00:00
|
|
|
#include "mozilla/PodOperations.h"
|
2013-07-03 00:47:08 +00:00
|
|
|
#include "mozilla/ReentrancyGuard.h"
|
2013-07-08 19:42:13 +00:00
|
|
|
#include "mozilla/TemplateLib.h"
|
2013-02-08 21:18:49 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2012-12-14 23:58:45 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
#include "js/Utility.h"
|
2012-09-10 20:27:04 +00:00
|
|
|
|
2010-02-06 18:14:05 +00:00
|
|
|
namespace js {
|
|
|
|
|
2012-01-02 22:34:25 +00:00
|
|
|
class TempAllocPolicy;
|
2012-11-12 23:30:39 +00:00
|
|
|
template <class> struct DefaultHasher;
|
|
|
|
template <class, class> class HashMapEntry;
|
|
|
|
namespace detail {
|
|
|
|
template <class T> class HashTableEntry;
|
|
|
|
template <class T, class HashPolicy, class AllocPolicy> class HashTable;
|
|
|
|
}
|
2012-01-02 22:34:25 +00:00
|
|
|
|
2011-03-24 00:40:11 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// A 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:
|
|
|
|
// - movable, destructible, assignable
|
|
|
|
// HashPolicy requirements:
|
|
|
|
// - see Hash Policy section below
|
|
|
|
// AllocPolicy:
|
|
|
|
// - see jsalloc.h
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members
|
|
|
|
// called by HashMap must not call back into the same HashMap object.
|
|
|
|
// - Due to the lack of exception handling, the user must call |init()|.
|
|
|
|
template <class Key,
|
|
|
|
class Value,
|
|
|
|
class HashPolicy = DefaultHasher<Key>,
|
|
|
|
class AllocPolicy = TempAllocPolicy>
|
|
|
|
class HashMap
|
|
|
|
{
|
|
|
|
typedef HashMapEntry<Key, Value> TableEntry;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
struct MapHashPolicy : HashPolicy
|
|
|
|
{
|
|
|
|
typedef Key KeyType;
|
2015-03-28 22:22:11 +00:00
|
|
|
static const Key& getKey(TableEntry& e) { return e.key(); }
|
|
|
|
static void setKey(TableEntry& e, Key& k) { HashPolicy::rekey(e.mutableKey(), k); }
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
typedef detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy> Impl;
|
|
|
|
Impl impl;
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
typedef typename HashPolicy::Lookup Lookup;
|
|
|
|
typedef TableEntry Entry;
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// HashMap construction is fallible (due to OOM); thus the user must call
|
|
|
|
// init after constructing a HashMap and check the return value.
|
2014-05-26 01:46:24 +00:00
|
|
|
explicit HashMap(AllocPolicy a = AllocPolicy()) : impl(a) {}
|
2012-11-12 23:30:39 +00:00
|
|
|
bool init(uint32_t len = 16) { return impl.init(len); }
|
|
|
|
bool initialized() const { return impl.initialized(); }
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00: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)) {
|
2015-03-28 22:22:11 +00:00
|
|
|
// const HM::Entry& e = *p; // p acts like a pointer to Entry
|
2012-11-12 23:30:39 +00:00
|
|
|
// 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;
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr lookup(const Lookup& l) const { return impl.lookup(l); }
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2013-02-26 12:49:44 +00:00
|
|
|
// Like lookup, but does not assert if two threads call lookup at the same
|
|
|
|
// time. Only use this method when none of the threads will modify the map.
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr readonlyThreadsafeLookup(const Lookup& l) const { return impl.readonlyThreadsafeLookup(l); }
|
2013-02-26 12:49:44 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// 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
|
|
|
|
// |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new Entry. E.g.:
|
|
|
|
//
|
|
|
|
// typedef HashMap<int,char> HM;
|
|
|
|
// HM h;
|
|
|
|
// HM::AddPtr p = h.lookupForAdd(3);
|
|
|
|
// if (!p) {
|
|
|
|
// if (!h.add(p, 3, 'a'))
|
|
|
|
// return false;
|
|
|
|
// }
|
2015-03-28 22:22:11 +00:00
|
|
|
// const HM::Entry& e = *p; // p acts like a pointer to Entry
|
2012-11-12 23:30:39 +00:00
|
|
|
// 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).
|
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
|
|
|
// HM::AddPtr p = h.lookupForAdd(3);
|
|
|
|
// if (!p) {
|
|
|
|
// call_that_may_mutate_h();
|
|
|
|
// if (!h.relookupOrAdd(p, 3, 'a'))
|
|
|
|
// return false;
|
|
|
|
// }
|
2015-03-28 22:22:11 +00:00
|
|
|
// const HM::Entry& e = *p;
|
2012-11-12 23:30:39 +00:00
|
|
|
// assert(p->key == 3);
|
|
|
|
// char val = p->value;
|
|
|
|
typedef typename Impl::AddPtr AddPtr;
|
2015-03-28 22:22:11 +00:00
|
|
|
AddPtr lookupForAdd(const Lookup& l) const {
|
2012-11-12 23:30:39 +00:00
|
|
|
return impl.lookupForAdd(l);
|
2011-06-24 21:22:30 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template<typename KeyInput, typename ValueInput>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool add(AddPtr& p, KeyInput&& k, ValueInput&& v) {
|
2015-04-01 09:11:06 +00:00
|
|
|
return impl.add(p,
|
|
|
|
mozilla::Forward<KeyInput>(k),
|
|
|
|
mozilla::Forward<ValueInput>(v));
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
template<typename KeyInput>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool add(AddPtr& p, KeyInput&& k) {
|
2015-04-01 09:11:06 +00:00
|
|
|
return impl.add(p, mozilla::Forward<KeyInput>(k), Value());
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2011-03-24 00:40:11 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template<typename KeyInput, typename ValueInput>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool relookupOrAdd(AddPtr& p, KeyInput&& k, ValueInput&& v) {
|
2015-04-01 09:11:06 +00:00
|
|
|
return impl.relookupOrAdd(p, k,
|
|
|
|
mozilla::Forward<KeyInput>(k),
|
|
|
|
mozilla::Forward<ValueInput>(v));
|
2012-11-12 23:30:39 +00: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())
|
2014-01-21 03:52:52 +00:00
|
|
|
// char c = r.front().value();
|
2012-11-12 23:30:39 +00:00
|
|
|
//
|
|
|
|
// Also see the definition of Range in HashTable above (with T = Entry).
|
|
|
|
typedef typename Impl::Range Range;
|
|
|
|
Range all() const { return impl.all(); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Typedef for the enumeration class. An Enum may be used to examine and
|
|
|
|
// remove table entries:
|
|
|
|
//
|
|
|
|
// typedef HashMap<int,char> HM;
|
|
|
|
// HM s;
|
|
|
|
// for (HM::Enum e(s); !e.empty(); e.popFront())
|
2014-01-21 03:52:52 +00:00
|
|
|
// if (e.front().value() == 'l')
|
2012-11-12 23:30:39 +00:00
|
|
|
// 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;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Remove all entries. This does not shrink the table. For that consider
|
|
|
|
// using the finish() method.
|
|
|
|
void clear() { impl.clear(); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Remove all the entries and release all internal buffers. The map must
|
|
|
|
// be initialized again before any use.
|
|
|
|
void finish() { impl.finish(); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Does the table contain any entries?
|
|
|
|
bool empty() const { return impl.empty(); }
|
2010-02-19 19:03:18 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Number of live elements in the map.
|
|
|
|
uint32_t count() const { return impl.count(); }
|
2011-03-29 22:46:51 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Total number of allocation in the dynamic table. Note: resize will
|
|
|
|
// happen well before count() == capacity().
|
|
|
|
size_t capacity() const { return impl.capacity(); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Don't just call |impl.sizeOfExcludingThis()| because there's no
|
|
|
|
// guarantee that |impl| is the first field in HashMap.
|
2013-06-23 11:21:01 +00:00
|
|
|
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
|
2012-11-12 23:30:39 +00:00
|
|
|
return impl.sizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
2013-06-23 11:21:01 +00:00
|
|
|
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
|
2012-11-12 23:30:39 +00:00
|
|
|
return mallocSizeOf(this) + impl.sizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// If |generation()| is the same before and after a HashMap operation,
|
|
|
|
// pointers into the table remain valid.
|
2014-07-16 23:51:09 +00:00
|
|
|
uint32_t generation() const { return impl.generation(); }
|
2010-05-11 06:27:53 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
/************************************************** Shorthand operations */
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
bool has(const Lookup& l) const {
|
2015-02-03 16:52:36 +00:00
|
|
|
return impl.lookup(l).found();
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Overwrite existing value with v. Return false on oom.
|
|
|
|
template<typename KeyInput, typename ValueInput>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool put(KeyInput&& k, ValueInput&& v) {
|
2012-11-12 23:30:39 +00:00
|
|
|
AddPtr p = lookupForAdd(k);
|
|
|
|
if (p) {
|
2013-11-26 01:35:09 +00:00
|
|
|
p->value() = mozilla::Forward<ValueInput>(v);
|
2012-11-12 23:30:39 +00:00
|
|
|
return true;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2013-11-19 17:05:36 +00:00
|
|
|
return add(p, mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Like put, but assert that the given key is not already present.
|
|
|
|
template<typename KeyInput, typename ValueInput>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool putNew(KeyInput&& k, ValueInput&& v) {
|
2015-04-01 09:11:06 +00:00
|
|
|
return impl.putNew(k, mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Add (k,defaultValue) if |k| is not found. Return a false-y Ptr on oom.
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr lookupWithDefault(const Key& k, const Value& defaultValue) {
|
2012-11-12 23:30:39 +00:00
|
|
|
AddPtr p = lookupForAdd(k);
|
|
|
|
if (p)
|
|
|
|
return p;
|
|
|
|
(void)add(p, k, defaultValue); // p is left false-y on oom.
|
|
|
|
return p;
|
|
|
|
}
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Remove if present.
|
2015-03-28 22:22:11 +00:00
|
|
|
void remove(const Lookup& l) {
|
2012-11-12 23:30:39 +00:00
|
|
|
if (Ptr p = lookup(l))
|
|
|
|
remove(p);
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-09-24 09:03:32 +00:00
|
|
|
// Infallibly rekey one entry, if necessary.
|
|
|
|
// Requires template parameters Key and HashPolicy::Lookup to be the same type.
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyIfMoved(const Key& old_key, const Key& new_key) {
|
2013-09-24 09:03:32 +00:00
|
|
|
if (old_key != new_key)
|
|
|
|
rekeyAs(old_key, new_key, new_key);
|
|
|
|
}
|
|
|
|
|
2015-02-02 10:11:23 +00:00
|
|
|
// Infallibly rekey one entry if present, and return whether that happened.
|
2015-03-28 22:22:11 +00:00
|
|
|
bool rekeyAs(const Lookup& old_lookup, const Lookup& new_lookup, const Key& new_key) {
|
2015-02-02 10:11:23 +00:00
|
|
|
if (Ptr p = lookup(old_lookup)) {
|
2013-09-24 09:03:32 +00:00
|
|
|
impl.rekeyAndMaybeRehash(p, new_lookup, new_key);
|
2015-02-02 10:11:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-07-02 08:43:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 23:19:39 +00:00
|
|
|
// HashMap is movable
|
2015-03-28 22:22:11 +00:00
|
|
|
HashMap(HashMap&& rhs) : impl(mozilla::Move(rhs.impl)) {}
|
|
|
|
void operator=(HashMap&& rhs) {
|
2013-11-19 17:05:36 +00:00
|
|
|
MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
|
|
|
|
impl = mozilla::Move(rhs.impl);
|
|
|
|
}
|
2012-11-28 23:19:39 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
private:
|
2012-11-28 23:19:39 +00:00
|
|
|
// HashMap is not copyable or assignable
|
2015-03-28 22:22:11 +00:00
|
|
|
HashMap(const HashMap& hm) = delete;
|
|
|
|
HashMap& operator=(const HashMap& hm) = delete;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
friend class Impl::Enum;
|
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
/*****************************************************************************/
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// A 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:
|
|
|
|
// - movable, destructible, assignable
|
|
|
|
// HashPolicy requirements:
|
|
|
|
// - see Hash Policy section below
|
|
|
|
// AllocPolicy:
|
|
|
|
// - see jsalloc.h
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by
|
|
|
|
// HashSet must not call back into the same HashSet object.
|
|
|
|
// - Due to the lack of exception handling, the user must call |init()|.
|
|
|
|
template <class T,
|
|
|
|
class HashPolicy = DefaultHasher<T>,
|
|
|
|
class AllocPolicy = TempAllocPolicy>
|
|
|
|
class HashSet
|
|
|
|
{
|
|
|
|
struct SetOps : HashPolicy
|
|
|
|
{
|
|
|
|
typedef T KeyType;
|
2015-03-28 22:22:11 +00:00
|
|
|
static const KeyType& getKey(const T& t) { return t; }
|
|
|
|
static void setKey(T& t, KeyType& k) { HashPolicy::rekey(t, k); }
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
typedef detail::HashTable<const T, SetOps, AllocPolicy> Impl;
|
|
|
|
Impl impl;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
typedef typename HashPolicy::Lookup Lookup;
|
|
|
|
typedef T Entry;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// HashSet construction is fallible (due to OOM); thus the user must call
|
|
|
|
// init after constructing a HashSet and check the return value.
|
2014-05-26 01:46:24 +00:00
|
|
|
explicit HashSet(AllocPolicy a = AllocPolicy()) : impl(a) {}
|
2012-11-12 23:30:39 +00:00
|
|
|
bool init(uint32_t len = 16) { return impl.init(len); }
|
|
|
|
bool initialized() const { return impl.initialized(); }
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00: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;
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr lookup(const Lookup& l) const { return impl.lookup(l); }
|
2012-05-04 21:18:28 +00:00
|
|
|
|
2013-10-08 22:14:03 +00:00
|
|
|
// Like lookup, but does not assert if two threads call lookup at the same
|
|
|
|
// time. Only use this method when none of the threads will modify the map.
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr readonlyThreadsafeLookup(const Lookup& l) const { return impl.readonlyThreadsafeLookup(l); }
|
2013-10-08 22:14:03 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Assuming |p.found()|, remove |*p|.
|
|
|
|
void remove(Ptr p) { impl.remove(p); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
|
|
|
|
// insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using
|
|
|
|
// |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.:
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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).
|
|
|
|
typedef typename Impl::AddPtr AddPtr;
|
2015-03-28 22:22:11 +00:00
|
|
|
AddPtr lookupForAdd(const Lookup& l) const { return impl.lookupForAdd(l); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
template <typename U>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool add(AddPtr& p, U&& u) {
|
2013-11-19 17:05:36 +00:00
|
|
|
return impl.add(p, mozilla::Forward<U>(u));
|
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
template <typename U>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool relookupOrAdd(AddPtr& p, const Lookup& l, U&& u) {
|
2013-11-19 17:05:36 +00:00
|
|
|
return impl.relookupOrAdd(p, l, mozilla::Forward<U>(u));
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00: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(); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Typedef for the enumeration class. An Enum may be used to examine and
|
|
|
|
// remove table entries:
|
|
|
|
//
|
|
|
|
// typedef HashSet<int> HS;
|
|
|
|
// HS s;
|
|
|
|
// for (HS::Enum e(s); !e.empty(); e.popFront())
|
|
|
|
// 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;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Remove all entries. This does not shrink the table. For that consider
|
|
|
|
// using the finish() method.
|
|
|
|
void clear() { impl.clear(); }
|
2010-05-11 06:27:53 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Remove all the entries and release all internal buffers. The set must
|
|
|
|
// be initialized again before any use.
|
|
|
|
void finish() { impl.finish(); }
|
2011-08-12 01:10:16 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Does the table contain any entries?
|
|
|
|
bool empty() const { return impl.empty(); }
|
2010-05-11 06:27:53 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Number of live elements in the map.
|
|
|
|
uint32_t count() const { return impl.count(); }
|
2010-05-11 06:27:53 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Total number of allocation in the dynamic table. Note: resize will
|
|
|
|
// happen well before count() == capacity().
|
|
|
|
size_t capacity() const { return impl.capacity(); }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Don't just call |impl.sizeOfExcludingThis()| because there's no
|
|
|
|
// guarantee that |impl| is the first field in HashSet.
|
2013-06-23 11:21:01 +00:00
|
|
|
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
|
2012-11-12 23:30:39 +00:00
|
|
|
return impl.sizeOfExcludingThis(mallocSizeOf);
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2013-06-23 11:21:01 +00:00
|
|
|
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
|
2012-11-12 23:30:39 +00:00
|
|
|
return mallocSizeOf(this) + impl.sizeOfExcludingThis(mallocSizeOf);
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// If |generation()| is the same before and after a HashSet operation,
|
|
|
|
// pointers into the table remain valid.
|
2014-07-16 23:51:09 +00:00
|
|
|
uint32_t generation() const { return impl.generation(); }
|
2010-02-13 20:02:12 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
/************************************************** Shorthand operations */
|
2010-02-13 20:02:12 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
bool has(const Lookup& l) const {
|
2015-02-03 16:52:36 +00:00
|
|
|
return impl.lookup(l).found();
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
// Add |u| if it is not present already. Return false on oom.
|
|
|
|
template <typename U>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool put(U&& u) {
|
2013-11-19 17:05:36 +00:00
|
|
|
AddPtr p = lookupForAdd(u);
|
|
|
|
return p ? true : add(p, mozilla::Forward<U>(u));
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Like put, but assert that the given key is not already present.
|
2013-11-19 17:05:36 +00:00
|
|
|
template <typename U>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool putNew(U&& u) {
|
2013-11-19 17:05:36 +00:00
|
|
|
return impl.putNew(u, mozilla::Forward<U>(u));
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
template <typename U>
|
2015-03-28 22:22:11 +00:00
|
|
|
bool putNew(const Lookup& l, U&& u) {
|
2013-11-19 17:05:36 +00:00
|
|
|
return impl.putNew(l, mozilla::Forward<U>(u));
|
2010-05-13 17:50:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void remove(const Lookup& l) {
|
2012-11-12 23:30:39 +00:00
|
|
|
if (Ptr p = lookup(l))
|
|
|
|
remove(p);
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 01:12:54 +00:00
|
|
|
// Infallibly rekey one entry, if present.
|
2013-09-24 09:03:32 +00:00
|
|
|
// Requires template parameters T and HashPolicy::Lookup to be the same type.
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyIfMoved(const Lookup& old_value, const T& new_value) {
|
2013-09-24 09:03:32 +00:00
|
|
|
if (old_value != new_value)
|
|
|
|
rekeyAs(old_value, new_value, new_value);
|
|
|
|
}
|
|
|
|
|
2015-02-02 10:11:23 +00:00
|
|
|
// Infallibly rekey one entry if present, and return whether that happened.
|
2015-03-28 22:22:11 +00:00
|
|
|
bool rekeyAs(const Lookup& old_lookup, const Lookup& new_lookup, const T& new_value) {
|
2015-02-02 10:11:23 +00:00
|
|
|
if (Ptr p = lookup(old_lookup)) {
|
2013-09-24 09:03:32 +00:00
|
|
|
impl.rekeyAndMaybeRehash(p, new_lookup, new_value);
|
2015-02-02 10:11:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-06-28 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 17:38:44 +00:00
|
|
|
// Infallibly rekey one entry with a new key that is equivalent.
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyInPlace(Ptr p, const T& new_value)
|
2014-06-27 17:38:44 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(HashPolicy::match(*p, new_value));
|
|
|
|
impl.rekeyInPlace(p, new_value);
|
|
|
|
}
|
|
|
|
|
2012-11-28 23:19:39 +00:00
|
|
|
// HashSet is movable
|
2015-03-28 22:22:11 +00:00
|
|
|
HashSet(HashSet&& rhs) : impl(mozilla::Move(rhs.impl)) {}
|
|
|
|
void operator=(HashSet&& rhs) {
|
2013-11-19 17:05:36 +00:00
|
|
|
MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
|
|
|
|
impl = mozilla::Move(rhs.impl);
|
|
|
|
}
|
2012-11-28 23:19:39 +00:00
|
|
|
|
2010-02-06 18:14:05 +00:00
|
|
|
private:
|
2012-11-28 23:19:39 +00:00
|
|
|
// HashSet is not copyable or assignable
|
2015-03-28 22:22:11 +00:00
|
|
|
HashSet(const HashSet& hs) = delete;
|
|
|
|
HashSet& operator=(const HashSet& hs) = delete;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
friend class Impl::Enum;
|
|
|
|
};
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
/*****************************************************************************/
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.:
|
|
|
|
//
|
|
|
|
// js::HashSet<Key, P>::AddPtr p = h.lookup(l);
|
|
|
|
// if (!p) {
|
|
|
|
// assert(P::match(k, l)); // must hold
|
|
|
|
// h.add(p, k);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Pointer hashing policy that strips the lowest zeroBits when calculating the
|
|
|
|
// hash to improve key distribution.
|
|
|
|
template <typename Key, size_t zeroBits>
|
|
|
|
struct PointerHasher
|
|
|
|
{
|
|
|
|
typedef Key Lookup;
|
2015-03-28 22:22:11 +00:00
|
|
|
static HashNumber hash(const Lookup& l) {
|
2012-11-12 23:30:39 +00:00
|
|
|
size_t word = reinterpret_cast<size_t>(l) >> zeroBits;
|
2014-10-22 22:17:38 +00:00
|
|
|
static_assert(sizeof(HashNumber) == 4,
|
|
|
|
"subsequent code assumes a four-byte hash");
|
2013-10-15 14:11:03 +00:00
|
|
|
#if JS_BITS_PER_WORD == 32
|
2012-11-12 23:30:39 +00:00
|
|
|
return HashNumber(word);
|
|
|
|
#else
|
2014-10-22 22:17:38 +00:00
|
|
|
static_assert(sizeof(word) == 8,
|
|
|
|
"unexpected word size, new hashing strategy required to "
|
|
|
|
"properly incorporate all bits");
|
2012-11-12 23:30:39 +00:00
|
|
|
return HashNumber((word >> 32) ^ word);
|
|
|
|
#endif
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2015-03-28 22:22:11 +00:00
|
|
|
static bool match(const Key& k, const Lookup& l) {
|
2012-11-12 23:30:39 +00:00
|
|
|
return k == l;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2015-03-28 22:22:11 +00:00
|
|
|
static void rekey(Key& k, const Key& newKey) {
|
2013-02-15 17:59:19 +00:00
|
|
|
k = newKey;
|
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Default hash policy: just use the 'lookup' value. This of course only
|
|
|
|
// works if the lookup value is integral. HashTable applies ScrambleHashCode to
|
|
|
|
// the result of the 'hash' which means that it is 'ok' if the lookup value is
|
|
|
|
// not well distributed over the HashNumber domain.
|
|
|
|
template <class Key>
|
|
|
|
struct DefaultHasher
|
|
|
|
{
|
|
|
|
typedef Key Lookup;
|
2015-03-28 22:22:11 +00:00
|
|
|
static HashNumber hash(const Lookup& l) {
|
2012-11-12 23:30:39 +00:00
|
|
|
// Hash if can implicitly cast to hash number type.
|
|
|
|
return l;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2015-03-28 22:22:11 +00:00
|
|
|
static bool match(const Key& k, const Lookup& l) {
|
2012-11-12 23:30:39 +00:00
|
|
|
// Use builtin or overloaded operator==.
|
|
|
|
return k == l;
|
|
|
|
}
|
2015-03-28 22:22:11 +00:00
|
|
|
static void rekey(Key& k, const Key& newKey) {
|
2013-02-15 17:59:19 +00:00
|
|
|
k = newKey;
|
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Specialize hashing policy for pointer types. It assumes that the type is
|
|
|
|
// at least word-aligned. For types with smaller size use PointerHasher.
|
|
|
|
template <class T>
|
2015-03-28 22:22:11 +00:00
|
|
|
struct DefaultHasher<T*> : PointerHasher<T*, mozilla::tl::FloorLog2<sizeof(void*)>::value>
|
2012-11-12 23:30:39 +00:00
|
|
|
{};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-02-27 00:56:30 +00:00
|
|
|
// For doubles, we can xor the two uint32s.
|
|
|
|
template <>
|
|
|
|
struct DefaultHasher<double>
|
|
|
|
{
|
|
|
|
typedef double Lookup;
|
|
|
|
static HashNumber hash(double d) {
|
2014-10-22 22:17:38 +00:00
|
|
|
static_assert(sizeof(HashNumber) == 4,
|
|
|
|
"subsequent code assumes a four-byte hash");
|
2013-06-20 18:55:41 +00:00
|
|
|
uint64_t u = mozilla::BitwiseCast<uint64_t>(d);
|
|
|
|
return HashNumber(u ^ (u >> 32));
|
2013-02-27 00:56:30 +00:00
|
|
|
}
|
|
|
|
static bool match(double lhs, double rhs) {
|
2013-06-20 18:55:41 +00:00
|
|
|
return mozilla::BitwiseCast<uint64_t>(lhs) == mozilla::BitwiseCast<uint64_t>(rhs);
|
2013-02-27 00:56:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-03 00:11:18 +00:00
|
|
|
template <>
|
|
|
|
struct DefaultHasher<float>
|
|
|
|
{
|
|
|
|
typedef float Lookup;
|
|
|
|
static HashNumber hash(float f) {
|
2014-10-22 22:17:38 +00:00
|
|
|
static_assert(sizeof(HashNumber) == 4,
|
|
|
|
"subsequent code assumes a four-byte hash");
|
2013-10-03 00:11:18 +00:00
|
|
|
return HashNumber(mozilla::BitwiseCast<uint32_t>(f));
|
|
|
|
}
|
|
|
|
static bool match(float lhs, float rhs) {
|
|
|
|
return mozilla::BitwiseCast<uint32_t>(lhs) == mozilla::BitwiseCast<uint32_t>(rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
/*****************************************************************************/
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Both HashMap and HashSet are implemented by a single HashTable that is even
|
|
|
|
// more heavily parameterized than the other two. This leaves HashTable gnarly
|
|
|
|
// and extremely coupled to HashMap and HashSet; thus code should not use
|
|
|
|
// HashTable directly.
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template <class Key, class Value>
|
|
|
|
class HashMapEntry
|
|
|
|
{
|
2013-11-26 01:35:09 +00:00
|
|
|
Key key_;
|
|
|
|
Value value_;
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template <class, class, class> friend class detail::HashTable;
|
|
|
|
template <class> friend class detail::HashTableEntry;
|
2013-11-26 01:35:09 +00:00
|
|
|
template <class, class, class, class> friend class HashMap;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-11-26 01:35:09 +00:00
|
|
|
Key & mutableKey() { return key_; }
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
template<typename KeyInput, typename ValueInput>
|
2015-03-28 22:22:11 +00:00
|
|
|
HashMapEntry(KeyInput&& k, ValueInput&& v)
|
2013-11-26 01:35:09 +00:00
|
|
|
: key_(mozilla::Forward<KeyInput>(k)),
|
|
|
|
value_(mozilla::Forward<ValueInput>(v))
|
|
|
|
{}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
HashMapEntry(HashMapEntry&& rhs)
|
2013-11-26 01:35:09 +00:00
|
|
|
: key_(mozilla::Move(rhs.key_)),
|
|
|
|
value_(mozilla::Move(rhs.value_))
|
|
|
|
{}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-03-14 17:26:06 +00:00
|
|
|
typedef Key KeyType;
|
|
|
|
typedef Value ValueType;
|
|
|
|
|
2013-11-26 01:35:09 +00:00
|
|
|
const Key & key() const { return key_; }
|
|
|
|
const Value & value() const { return value_; }
|
|
|
|
Value & value() { return value_; }
|
|
|
|
|
|
|
|
private:
|
2015-03-28 22:22:11 +00:00
|
|
|
HashMapEntry(const HashMapEntry&) = delete;
|
|
|
|
void operator=(const HashMapEntry&) = delete;
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-02-08 21:18:49 +00:00
|
|
|
} // namespace js
|
|
|
|
|
|
|
|
namespace mozilla {
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-12-16 23:20:17 +00:00
|
|
|
template <typename T>
|
|
|
|
struct IsPod<js::detail::HashTableEntry<T> > : IsPod<T> {};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-12-16 23:20:17 +00:00
|
|
|
template <typename K, typename V>
|
2013-02-08 21:18:49 +00:00
|
|
|
struct IsPod<js::HashMapEntry<K, V> >
|
2012-12-16 23:20:17 +00:00
|
|
|
: IntegralConstant<bool, IsPod<K>::value && IsPod<V>::value>
|
|
|
|
{};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-02-08 21:18:49 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
namespace js {
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
namespace detail {
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template <class T, class HashPolicy, class AllocPolicy>
|
|
|
|
class HashTable;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template <class T>
|
|
|
|
class HashTableEntry
|
|
|
|
{
|
|
|
|
template <class, class, class> friend class HashTable;
|
2013-04-03 15:24:11 +00:00
|
|
|
typedef typename mozilla::RemoveConst<T>::Type NonConstT;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
HashNumber keyHash;
|
|
|
|
mozilla::AlignedStorage2<NonConstT> mem;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
static const HashNumber sFreeKey = 0;
|
|
|
|
static const HashNumber sRemovedKey = 1;
|
|
|
|
static const HashNumber sCollisionBit = 1;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
static bool isLiveHash(HashNumber hash)
|
2010-02-06 18:14:05 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
return hash > sRemovedKey;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
HashTableEntry(const HashTableEntry&) = delete;
|
|
|
|
void operator=(const HashTableEntry&) = delete;
|
2015-01-06 23:35:02 +00:00
|
|
|
~HashTableEntry() = delete;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
// NB: HashTableEntry is treated as a POD: no constructor or destructor calls.
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void destroyIfLive() {
|
|
|
|
if (isLive())
|
|
|
|
mem.addr()->~T();
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void destroy() {
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(isLive());
|
2012-11-12 23:30:39 +00:00
|
|
|
mem.addr()->~T();
|
2012-03-14 20:48:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void swap(HashTableEntry* other) {
|
2013-07-03 22:57:33 +00:00
|
|
|
mozilla::Swap(keyHash, other->keyHash);
|
|
|
|
mozilla::Swap(mem, other->mem);
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
T& get() { MOZ_ASSERT(isLive()); return *mem.addr(); }
|
2012-11-12 23:30:39 +00:00
|
|
|
|
|
|
|
bool isFree() const { return keyHash == sFreeKey; }
|
2014-02-18 06:24:15 +00:00
|
|
|
void clearLive() { MOZ_ASSERT(isLive()); keyHash = sFreeKey; mem.addr()->~T(); }
|
2012-11-12 23:30:39 +00:00
|
|
|
void clear() { if (isLive()) mem.addr()->~T(); keyHash = sFreeKey; }
|
|
|
|
bool isRemoved() const { return keyHash == sRemovedKey; }
|
2014-02-18 06:24:15 +00:00
|
|
|
void removeLive() { MOZ_ASSERT(isLive()); keyHash = sRemovedKey; mem.addr()->~T(); }
|
2012-11-12 23:30:39 +00:00
|
|
|
bool isLive() const { return isLiveHash(keyHash); }
|
2014-02-18 06:24:15 +00:00
|
|
|
void setCollision() { MOZ_ASSERT(isLive()); keyHash |= sCollisionBit; }
|
2012-11-12 23:30:39 +00:00
|
|
|
void unsetCollision() { keyHash &= ~sCollisionBit; }
|
|
|
|
bool hasCollision() const { return keyHash & sCollisionBit; }
|
|
|
|
bool matchHash(HashNumber hn) { return (keyHash & ~sCollisionBit) == hn; }
|
|
|
|
HashNumber getKeyHash() const { return keyHash & ~sCollisionBit; }
|
|
|
|
|
2015-04-01 09:11:06 +00:00
|
|
|
template <typename... Args>
|
|
|
|
void setLive(HashNumber hn, Args&&... args)
|
2012-03-14 20:48:59 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(!isLive());
|
2012-11-12 23:30:39 +00:00
|
|
|
keyHash = hn;
|
2015-04-01 09:11:06 +00:00
|
|
|
new(mem.addr()) T(mozilla::Forward<Args>(args)...);
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(isLive());
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
|
|
|
};
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
template <class T, class HashPolicy, class AllocPolicy>
|
|
|
|
class HashTable : private AllocPolicy
|
|
|
|
{
|
2014-07-16 23:51:09 +00:00
|
|
|
friend class mozilla::ReentrancyGuard;
|
|
|
|
|
2013-04-03 15:24:11 +00:00
|
|
|
typedef typename mozilla::RemoveConst<T>::Type NonConstT;
|
2012-11-12 23:30:39 +00:00
|
|
|
typedef typename HashPolicy::KeyType Key;
|
|
|
|
typedef typename HashPolicy::Lookup Lookup;
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
typedef HashTableEntry<T> Entry;
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// 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
|
2012-06-15 20:21:24 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
friend class HashTable;
|
2012-06-15 20:21:24 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* entry_;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2015-03-28 22:22:11 +00:00
|
|
|
const HashTable* table_;
|
2013-12-03 20:51:02 +00:00
|
|
|
uint32_t generation;
|
|
|
|
#endif
|
2012-06-04 07:00:24 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
protected:
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr(Entry& entry, const HashTable& tableArg)
|
2013-12-03 20:51:02 +00:00
|
|
|
: entry_(&entry)
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2013-12-03 20:51:02 +00:00
|
|
|
, table_(&tableArg)
|
|
|
|
, generation(tableArg.generation())
|
|
|
|
#endif
|
|
|
|
{}
|
2012-11-12 23:30:39 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Leaves Ptr uninitialized.
|
|
|
|
Ptr() {
|
2013-12-06 23:03:08 +00:00
|
|
|
#ifdef JS_DEBUG
|
2015-03-28 22:22:11 +00:00
|
|
|
entry_ = (Entry*)0xbad;
|
2012-11-12 23:30:39 +00:00
|
|
|
#endif
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 20:51:02 +00:00
|
|
|
bool found() const {
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2013-12-03 20:51:02 +00:00
|
|
|
return entry_->isLive();
|
|
|
|
}
|
|
|
|
|
2015-02-03 16:52:36 +00:00
|
|
|
explicit operator bool() const {
|
|
|
|
return found();
|
2013-12-03 20:51:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
bool operator==(const Ptr& rhs) const {
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(found() && rhs.found());
|
2013-12-03 20:51:02 +00:00
|
|
|
return entry_ == rhs.entry_;
|
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
bool operator!=(const Ptr& rhs) const {
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2013-12-03 20:51:02 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
T& operator*() const {
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2013-12-03 20:51:02 +00:00
|
|
|
return entry_->get();
|
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
T* operator->() const {
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2013-12-03 20:51:02 +00:00
|
|
|
return &entry_->get();
|
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A Ptr that can be used to add a key after a failed lookup.
|
|
|
|
class AddPtr : public Ptr
|
2010-02-06 18:14:05 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
friend class HashTable;
|
|
|
|
HashNumber keyHash;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-07-16 23:51:09 +00:00
|
|
|
uint64_t mutationCount;
|
|
|
|
#endif
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
AddPtr(Entry& entry, const HashTable& tableArg, HashNumber hn)
|
2014-07-16 23:51:09 +00:00
|
|
|
: Ptr(entry, tableArg)
|
|
|
|
, keyHash(hn)
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-07-16 23:51:09 +00:00
|
|
|
, mutationCount(tableArg.mutationCount)
|
|
|
|
#endif
|
2013-12-03 20:51:02 +00:00
|
|
|
{}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
// Leaves AddPtr uninitialized.
|
|
|
|
AddPtr() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2012-06-15 20:21:24 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
protected:
|
|
|
|
friend class HashTable;
|
2012-06-15 20:21:24 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
Range(const HashTable& tableArg, Entry* c, Entry* e)
|
2013-12-03 20:51:02 +00:00
|
|
|
: cur(c)
|
|
|
|
, end(e)
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2013-12-03 20:51:02 +00:00
|
|
|
, table_(&tableArg)
|
|
|
|
, mutationCount(tableArg.mutationCount)
|
|
|
|
, generation(tableArg.generation())
|
|
|
|
, validEntry(true)
|
|
|
|
#endif
|
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
while (cur < end && !cur->isLive())
|
|
|
|
++cur;
|
|
|
|
}
|
2012-06-15 20:21:24 +00:00
|
|
|
|
2015-04-02 09:22:21 +00:00
|
|
|
Entry* cur;
|
|
|
|
Entry* end;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2015-03-28 22:22:11 +00:00
|
|
|
const HashTable* table_;
|
2013-12-03 20:51:02 +00:00
|
|
|
uint64_t mutationCount;
|
|
|
|
uint32_t generation;
|
|
|
|
bool validEntry;
|
|
|
|
#endif
|
2012-06-15 20:21:24 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
2013-12-03 20:51:02 +00:00
|
|
|
Range()
|
|
|
|
: cur(nullptr)
|
|
|
|
, end(nullptr)
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2013-12-03 20:51:02 +00:00
|
|
|
, table_(nullptr)
|
|
|
|
, mutationCount(0)
|
|
|
|
, generation(0)
|
|
|
|
, validEntry(false)
|
|
|
|
#endif
|
|
|
|
{}
|
2012-06-15 20:21:24 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
bool empty() const {
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
|
|
|
MOZ_ASSERT(mutationCount == table_->mutationCount);
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
return cur == end;
|
2012-06-15 20:21:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
T& front() const {
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(!empty());
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
|
|
|
MOZ_ASSERT(validEntry);
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
|
|
|
MOZ_ASSERT(mutationCount == table_->mutationCount);
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
return cur->get();
|
|
|
|
}
|
2012-06-15 20:21:24 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void popFront() {
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(!empty());
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(generation == table_->generation());
|
|
|
|
MOZ_ASSERT(mutationCount == table_->mutationCount);
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
while (++cur < end && !cur->isLive())
|
|
|
|
continue;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
validEntry = true;
|
2013-12-03 20:51:02 +00:00
|
|
|
#endif
|
2011-06-24 21:22:30 +00:00
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// A Range whose lifetime delimits a mutating enumeration of a hash table.
|
|
|
|
// Since rehashing when elements were removed during enumeration would be
|
2013-01-08 10:04:19 +00:00
|
|
|
// bad, it is postponed until the Enum is destructed. Since the Enum's
|
|
|
|
// destructor touches the hash table, the user must ensure that the hash
|
|
|
|
// table is still alive when the destructor runs.
|
2012-11-12 23:30:39 +00:00
|
|
|
class Enum : public Range
|
2011-03-13 14:45:02 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
friend class HashTable;
|
2011-03-13 14:45:02 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
HashTable& table_;
|
2012-11-12 23:30:39 +00:00
|
|
|
bool rekeyed;
|
|
|
|
bool removed;
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
/* Not copyable. */
|
2015-03-28 22:22:11 +00:00
|
|
|
Enum(const Enum&) = delete;
|
|
|
|
void operator=(const Enum&) = delete;
|
2011-03-13 14:45:02 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
|
|
|
template<class Map> explicit
|
2015-03-28 22:22:11 +00:00
|
|
|
Enum(Map& map) : Range(map.all()), table_(map.impl), rekeyed(false), removed(false) {}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Removes the |front()| element from the table, leaving |front()|
|
|
|
|
// invalid until the next call to |popFront()|. For example:
|
|
|
|
//
|
|
|
|
// HashSet<int> s;
|
|
|
|
// for (HashSet<int>::Enum e(s); !e.empty(); e.popFront())
|
|
|
|
// if (e.front() == 42)
|
|
|
|
// e.removeFront();
|
|
|
|
void removeFront() {
|
2013-12-03 20:51:02 +00:00
|
|
|
table_.remove(*this->cur);
|
2012-11-12 23:30:39 +00:00
|
|
|
removed = true;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
this->validEntry = false;
|
2013-12-03 20:51:02 +00:00
|
|
|
this->mutationCount = table_.mutationCount;
|
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Removes the |front()| element and re-inserts it into the table with
|
|
|
|
// a new key at the new Lookup position. |front()| is invalid after
|
|
|
|
// this operation until the next call to |popFront()|.
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyFront(const Lookup& l, const Key& k) {
|
2014-10-01 17:17:51 +00:00
|
|
|
MOZ_ASSERT(&k != &HashPolicy::getKey(this->cur->get()));
|
2013-12-03 20:51:02 +00:00
|
|
|
Ptr p(*this->cur, table_);
|
|
|
|
table_.rekeyWithoutRehash(p, l, k);
|
2012-11-12 23:30:39 +00:00
|
|
|
rekeyed = true;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
this->validEntry = false;
|
2013-12-03 20:51:02 +00:00
|
|
|
this->mutationCount = table_.mutationCount;
|
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyFront(const Key& k) {
|
2012-11-12 23:30:39 +00:00
|
|
|
rekeyFront(k, k);
|
|
|
|
}
|
2011-10-28 06:06:17 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Potentially rehashes the table.
|
|
|
|
~Enum() {
|
2013-02-25 18:37:36 +00:00
|
|
|
if (rekeyed) {
|
2013-12-03 20:51:02 +00:00
|
|
|
table_.gen++;
|
|
|
|
table_.checkOverRemoved();
|
2013-02-25 18:37:36 +00:00
|
|
|
}
|
2013-01-08 10:04:19 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
if (removed)
|
2013-12-03 20:51:02 +00:00
|
|
|
table_.compactIfUnderloaded();
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-28 23:19:39 +00:00
|
|
|
// HashTable is movable
|
2015-03-28 22:22:11 +00:00
|
|
|
HashTable(HashTable&& rhs)
|
2013-11-19 17:05:36 +00:00
|
|
|
: AllocPolicy(rhs)
|
2012-11-28 23:19:39 +00:00
|
|
|
{
|
2013-11-19 17:05:36 +00:00
|
|
|
mozilla::PodAssign(this, &rhs);
|
|
|
|
rhs.table = nullptr;
|
2012-11-28 23:19:39 +00:00
|
|
|
}
|
2015-03-28 22:22:11 +00:00
|
|
|
void operator=(HashTable&& rhs) {
|
2013-11-19 17:05:36 +00:00
|
|
|
MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
|
2012-11-28 23:19:39 +00:00
|
|
|
if (table)
|
|
|
|
destroyTable(*this, table, capacity());
|
2013-11-19 17:05:36 +00:00
|
|
|
mozilla::PodAssign(this, &rhs);
|
|
|
|
rhs.table = nullptr;
|
2012-11-28 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// HashTable is not copyable or assignable
|
2015-03-28 22:22:11 +00:00
|
|
|
HashTable(const HashTable&) = delete;
|
|
|
|
void operator=(const HashTable&) = delete;
|
2012-11-28 23:19:39 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
private:
|
2015-02-26 09:02:13 +00:00
|
|
|
static const size_t CAP_BITS = 30;
|
2011-11-28 03:03:14 +00:00
|
|
|
|
2014-07-16 23:51:09 +00:00
|
|
|
public:
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* table; // entry storage
|
2015-02-26 09:02:13 +00:00
|
|
|
uint32_t gen:24; // entry storage generation number
|
2014-07-16 23:51:09 +00:00
|
|
|
uint32_t hashShift:8; // multiplicative hash shift
|
2015-02-26 09:02:13 +00:00
|
|
|
uint32_t entryCount; // number of entries in table
|
|
|
|
uint32_t removedCount; // removed entry sentinels in table
|
2011-08-10 05:33:27 +00:00
|
|
|
|
2013-12-06 23:03:08 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-08-11 06:17:44 +00:00
|
|
|
uint64_t mutationCount;
|
|
|
|
mutable bool mEntered;
|
2015-02-25 18:39:46 +00:00
|
|
|
// Note that some updates to these stats are not thread-safe. See the
|
|
|
|
// comment on the three-argument overloading of HashTable::lookup().
|
2012-11-12 23:30:39 +00:00
|
|
|
mutable struct Stats
|
|
|
|
{
|
|
|
|
uint32_t searches; // total number of table searches
|
|
|
|
uint32_t steps; // hash chain links traversed
|
|
|
|
uint32_t hits; // searches that found key
|
|
|
|
uint32_t misses; // searches that didn't find key
|
|
|
|
uint32_t addOverRemoved; // adds that recycled a removed entry
|
|
|
|
uint32_t removes; // calls to remove
|
|
|
|
uint32_t removeFrees; // calls to remove that freed the entry
|
|
|
|
uint32_t grows; // table expansions
|
|
|
|
uint32_t shrinks; // table contractions
|
|
|
|
uint32_t compresses; // table compressions
|
|
|
|
uint32_t rehashes; // tombstone decontaminations
|
|
|
|
} stats;
|
|
|
|
# define METER(x) x
|
|
|
|
#else
|
|
|
|
# define METER(x)
|
|
|
|
#endif
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-02-14 19:11:15 +00:00
|
|
|
// The default initial capacity is 32 (enough to hold 16 elements), but it
|
|
|
|
// can be as low as 4.
|
|
|
|
static const unsigned sMinCapacityLog2 = 2;
|
|
|
|
static const unsigned sMinCapacity = 1 << sMinCapacityLog2;
|
2014-07-16 23:51:09 +00:00
|
|
|
static const unsigned sMaxInit = JS_BIT(CAP_BITS - 1);
|
|
|
|
static const unsigned sMaxCapacity = JS_BIT(CAP_BITS);
|
2013-07-08 19:42:13 +00:00
|
|
|
static const unsigned sHashBits = mozilla::tl::BitSize<HashNumber>::value;
|
2013-10-24 16:11:32 +00:00
|
|
|
|
|
|
|
// Hash-table alpha is conceptually a fraction, but to avoid floating-point
|
|
|
|
// math we implement it as a ratio of integers.
|
|
|
|
static const uint8_t sAlphaDenominator = 4;
|
|
|
|
static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4
|
|
|
|
static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
static const HashNumber sFreeKey = Entry::sFreeKey;
|
|
|
|
static const HashNumber sRemovedKey = Entry::sRemovedKey;
|
|
|
|
static const HashNumber sCollisionBit = Entry::sCollisionBit;
|
|
|
|
|
2014-07-16 23:51:09 +00:00
|
|
|
void setTableSizeLog2(unsigned sizeLog2)
|
|
|
|
{
|
|
|
|
hashShift = sHashBits - sizeLog2;
|
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
static bool isLiveHash(HashNumber hash)
|
2010-02-06 18:14:05 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
return Entry::isLiveHash(hash);
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
static HashNumber prepareHash(const Lookup& l)
|
|
|
|
{
|
|
|
|
HashNumber keyHash = ScrambleHashCode(HashPolicy::hash(l));
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Avoid reserved hash codes.
|
|
|
|
if (!isLiveHash(keyHash))
|
|
|
|
keyHash -= (sRemovedKey + 1);
|
|
|
|
return keyHash & ~sCollisionBit;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
static Entry* createTable(AllocPolicy& alloc, uint32_t capacity)
|
2010-07-22 02:47:02 +00:00
|
|
|
{
|
2013-10-24 16:11:32 +00:00
|
|
|
static_assert(sFreeKey == 0,
|
|
|
|
"newly-calloc'd tables have to be considered empty");
|
2014-08-05 21:06:35 +00:00
|
|
|
return alloc.template pod_calloc<Entry>(capacity);
|
2010-07-22 02:47:02 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
static void destroyTable(AllocPolicy& alloc, Entry* oldTable, uint32_t capacity)
|
2010-07-22 02:47:02 +00:00
|
|
|
{
|
2015-04-02 09:22:21 +00:00
|
|
|
Entry* end = oldTable + capacity;
|
|
|
|
for (Entry* e = oldTable; e < end; ++e)
|
2012-11-12 23:30:39 +00:00
|
|
|
e->destroyIfLive();
|
|
|
|
alloc.free_(oldTable);
|
2010-07-22 02:47:02 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
public:
|
2014-05-26 01:46:24 +00:00
|
|
|
explicit HashTable(AllocPolicy ap)
|
2014-07-16 23:51:09 +00:00
|
|
|
: AllocPolicy(ap)
|
|
|
|
, table(nullptr)
|
|
|
|
, gen(0)
|
2015-02-26 09:02:13 +00:00
|
|
|
, hashShift(sHashBits)
|
2014-07-16 23:51:09 +00:00
|
|
|
, entryCount(0)
|
|
|
|
, removedCount(0)
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-07-16 23:51:09 +00:00
|
|
|
, mutationCount(0)
|
|
|
|
, mEntered(false)
|
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
MOZ_WARN_UNUSED_RESULT bool init(uint32_t length)
|
2012-06-06 23:40:56 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(!initialized());
|
2012-06-06 23:40:56 +00:00
|
|
|
|
2013-10-24 16:11:32 +00:00
|
|
|
// Reject all lengths whose initial computed capacity would exceed
|
|
|
|
// sMaxCapacity. Round that maximum length down to the nearest power
|
|
|
|
// of two for speedier code.
|
2015-02-12 16:37:01 +00:00
|
|
|
if (MOZ_UNLIKELY(length > sMaxInit)) {
|
2012-11-12 23:30:39 +00:00
|
|
|
this->reportAllocOverflow();
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-06 23:40:56 +00:00
|
|
|
|
2013-10-24 16:11:32 +00:00
|
|
|
static_assert((sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit,
|
|
|
|
"multiplication in numerator below could overflow");
|
|
|
|
static_assert(sMaxInit * sAlphaDenominator <= UINT32_MAX - sMaxAlphaNumerator,
|
|
|
|
"numerator calculation below could potentially overflow");
|
|
|
|
|
|
|
|
// Compute the smallest capacity allowing |length| elements to be
|
|
|
|
// inserted without rehashing: ceil(length / max-alpha). (Ceiling
|
|
|
|
// integral division: <http://stackoverflow.com/a/2745086>.)
|
|
|
|
uint32_t newCapacity =
|
|
|
|
(length * sAlphaDenominator + sMaxAlphaNumerator - 1) / sMaxAlphaNumerator;
|
2013-02-18 06:56:32 +00:00
|
|
|
if (newCapacity < sMinCapacity)
|
|
|
|
newCapacity = sMinCapacity;
|
2012-11-12 23:30:39 +00:00
|
|
|
|
|
|
|
// FIXME: use JS_CEILING_LOG2 when PGO stops crashing (bug 543034).
|
2013-02-14 19:11:15 +00:00
|
|
|
uint32_t roundUp = sMinCapacity, roundUpLog2 = sMinCapacityLog2;
|
2013-02-18 06:56:32 +00:00
|
|
|
while (roundUp < newCapacity) {
|
2012-11-12 23:30:39 +00:00
|
|
|
roundUp <<= 1;
|
|
|
|
++roundUpLog2;
|
2012-06-06 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 06:56:32 +00:00
|
|
|
newCapacity = roundUp;
|
2013-10-24 16:11:32 +00:00
|
|
|
MOZ_ASSERT(newCapacity >= length);
|
|
|
|
MOZ_ASSERT(newCapacity <= sMaxCapacity);
|
2012-06-06 23:40:56 +00:00
|
|
|
|
2013-02-18 06:56:32 +00:00
|
|
|
table = createTable(*this, newCapacity);
|
2012-11-12 23:30:39 +00:00
|
|
|
if (!table)
|
2012-06-06 23:40:56 +00:00
|
|
|
return false;
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
setTableSizeLog2(roundUpLog2);
|
|
|
|
METER(memset(&stats, 0, sizeof(stats)));
|
2012-06-06 23:40:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
bool initialized() const
|
2010-05-11 06:27:53 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
return !!table;
|
2010-05-11 06:27:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
~HashTable()
|
2010-02-06 18:14:05 +00:00
|
|
|
{
|
2012-11-12 23:30:39 +00:00
|
|
|
if (table)
|
|
|
|
destroyTable(*this, table, capacity());
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2012-03-14 20:48:59 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
private:
|
|
|
|
HashNumber hash1(HashNumber hash0) const
|
|
|
|
{
|
|
|
|
return hash0 >> hashShift;
|
|
|
|
}
|
2011-03-24 00:40:11 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
struct DoubleHash
|
|
|
|
{
|
|
|
|
HashNumber h2;
|
|
|
|
HashNumber sizeMask;
|
|
|
|
};
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
DoubleHash hash2(HashNumber curKeyHash) const
|
|
|
|
{
|
|
|
|
unsigned sizeLog2 = sHashBits - hashShift;
|
|
|
|
DoubleHash dh = {
|
|
|
|
((curKeyHash << sizeLog2) >> hashShift) | 1,
|
|
|
|
(HashNumber(1) << sizeLog2) - 1
|
|
|
|
};
|
|
|
|
return dh;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
static HashNumber applyDoubleHash(HashNumber h1, const DoubleHash& dh)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
|
|
|
return (h1 - dh.h2) & dh.sizeMask;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2011-07-19 23:37:11 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
bool overloaded()
|
|
|
|
{
|
2013-10-24 16:11:32 +00:00
|
|
|
static_assert(sMaxCapacity <= UINT32_MAX / sMaxAlphaNumerator,
|
|
|
|
"multiplication below could overflow");
|
|
|
|
return entryCount + removedCount >=
|
|
|
|
capacity() * sMaxAlphaNumerator / sAlphaDenominator;
|
2011-07-19 23:37:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 10:04:19 +00:00
|
|
|
// Would the table be underloaded if it had the given capacity and entryCount?
|
|
|
|
static bool wouldBeUnderloaded(uint32_t capacity, uint32_t entryCount)
|
|
|
|
{
|
2013-10-24 16:11:32 +00:00
|
|
|
static_assert(sMaxCapacity <= UINT32_MAX / sMinAlphaNumerator,
|
|
|
|
"multiplication below could overflow");
|
|
|
|
return capacity > sMinCapacity &&
|
|
|
|
entryCount <= capacity * sMinAlphaNumerator / sAlphaDenominator;
|
2013-01-08 10:04:19 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
bool underloaded()
|
|
|
|
{
|
2013-01-08 10:04:19 +00:00
|
|
|
return wouldBeUnderloaded(capacity(), entryCount);
|
2011-07-19 23:37:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
static bool match(Entry& e, const Lookup& l)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
|
|
|
return HashPolicy::match(HashPolicy::getKey(e.get()), l);
|
2011-06-24 21:22:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 18:39:46 +00:00
|
|
|
// Warning: in order for readonlyThreadsafeLookup() to be safe this
|
|
|
|
// function must not modify the table in any way when |collisionBit| is 0.
|
|
|
|
// (The use of the METER() macro to increment stats violates this
|
|
|
|
// restriction but we will live with that for now because it's enabled so
|
|
|
|
// rarely.)
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry& lookup(const Lookup& l, HashNumber keyHash, unsigned collisionBit) const
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(isLiveHash(keyHash));
|
|
|
|
MOZ_ASSERT(!(keyHash & sCollisionBit));
|
|
|
|
MOZ_ASSERT(collisionBit == 0 || collisionBit == sCollisionBit);
|
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
METER(stats.searches++);
|
2011-10-25 23:07:42 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Compute the primary hash address.
|
|
|
|
HashNumber h1 = hash1(keyHash);
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* entry = &table[h1];
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Miss: return space for a new entry.
|
|
|
|
if (entry->isFree()) {
|
|
|
|
METER(stats.misses++);
|
|
|
|
return *entry;
|
|
|
|
}
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Hit: return entry.
|
|
|
|
if (entry->matchHash(keyHash) && match(*entry, l)) {
|
|
|
|
METER(stats.hits++);
|
|
|
|
return *entry;
|
|
|
|
}
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Collision: double hash.
|
|
|
|
DoubleHash dh = hash2(keyHash);
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Save the first removed entry pointer so we can recycle later.
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* firstRemoved = nullptr;
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2015-02-25 18:39:46 +00:00
|
|
|
while (true) {
|
2014-01-25 03:57:58 +00:00
|
|
|
if (MOZ_UNLIKELY(entry->isRemoved())) {
|
2012-11-12 23:30:39 +00:00
|
|
|
if (!firstRemoved)
|
|
|
|
firstRemoved = entry;
|
|
|
|
} else {
|
2015-02-25 18:39:46 +00:00
|
|
|
if (collisionBit == sCollisionBit)
|
|
|
|
entry->setCollision();
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2011-06-24 21:22:30 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
METER(stats.steps++);
|
|
|
|
h1 = applyDoubleHash(h1, dh);
|
2012-07-16 17:54:56 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
entry = &table[h1];
|
|
|
|
if (entry->isFree()) {
|
|
|
|
METER(stats.misses++);
|
|
|
|
return firstRemoved ? *firstRemoved : *entry;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
if (entry->matchHash(keyHash) && match(*entry, l)) {
|
|
|
|
METER(stats.hits++);
|
|
|
|
return *entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// This is a copy of lookup hardcoded to the assumptions:
|
|
|
|
// 1. the lookup is a lookupForAdd
|
|
|
|
// 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.
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry& findFreeEntry(HashNumber keyHash)
|
2010-02-06 18:14:05 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(!(keyHash & sCollisionBit));
|
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
METER(stats.searches++);
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// We assume 'keyHash' has already been distributed.
|
2012-03-30 18:01:58 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Compute the primary hash address.
|
|
|
|
HashNumber h1 = hash1(keyHash);
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* entry = &table[h1];
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Miss: return space for a new entry.
|
|
|
|
if (!entry->isLive()) {
|
|
|
|
METER(stats.misses++);
|
|
|
|
return *entry;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Collision: double hash.
|
|
|
|
DoubleHash dh = hash2(keyHash);
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-02-25 18:39:46 +00:00
|
|
|
while (true) {
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(!entry->isRemoved());
|
2012-11-12 23:30:39 +00:00
|
|
|
entry->setCollision();
|
2010-07-22 02:01:33 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
METER(stats.steps++);
|
|
|
|
h1 = applyDoubleHash(h1, dh);
|
2010-07-22 02:01:33 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
entry = &table[h1];
|
|
|
|
if (!entry->isLive()) {
|
|
|
|
METER(stats.misses++);
|
|
|
|
return *entry;
|
|
|
|
}
|
|
|
|
}
|
2011-08-02 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed };
|
2010-07-22 02:01:33 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
RebuildStatus changeTableSize(int deltaLog2)
|
|
|
|
{
|
|
|
|
// Look, but don't touch, until we succeed in getting new entry store.
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* oldTable = table;
|
2012-11-12 23:30:39 +00:00
|
|
|
uint32_t oldCap = capacity();
|
|
|
|
uint32_t newLog2 = sHashBits - hashShift + deltaLog2;
|
|
|
|
uint32_t newCapacity = JS_BIT(newLog2);
|
2015-02-12 16:37:01 +00:00
|
|
|
if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)) {
|
2012-11-12 23:30:39 +00:00
|
|
|
this->reportAllocOverflow();
|
|
|
|
return RehashFailed;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* newTable = createTable(*this, newCapacity);
|
2012-11-12 23:30:39 +00:00
|
|
|
if (!newTable)
|
|
|
|
return RehashFailed;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// We can't fail from here on, so update table parameters.
|
|
|
|
setTableSizeLog2(newLog2);
|
|
|
|
removedCount = 0;
|
|
|
|
gen++;
|
|
|
|
table = newTable;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Copy only live entries, leaving removed ones behind.
|
2015-04-02 09:22:21 +00:00
|
|
|
Entry* end = oldTable + oldCap;
|
|
|
|
for (Entry* src = oldTable; src < end; ++src) {
|
2012-11-12 23:30:39 +00:00
|
|
|
if (src->isLive()) {
|
|
|
|
HashNumber hn = src->getKeyHash();
|
2015-03-03 16:54:00 +00:00
|
|
|
findFreeEntry(hn).setLive(
|
|
|
|
hn, mozilla::Move(const_cast<typename Entry::NonConstT&>(src->get())));
|
2012-11-12 23:30:39 +00:00
|
|
|
src->destroy();
|
|
|
|
}
|
|
|
|
}
|
2011-03-13 14:45:02 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// All entries have been destroyed, no need to destroyTable.
|
|
|
|
this->free_(oldTable);
|
|
|
|
return Rehashed;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
RebuildStatus checkOverloaded()
|
|
|
|
{
|
|
|
|
if (!overloaded())
|
|
|
|
return NotOverloaded;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Compress if a quarter or more of all entries are removed.
|
|
|
|
int deltaLog2;
|
|
|
|
if (removedCount >= (capacity() >> 2)) {
|
|
|
|
METER(stats.compresses++);
|
|
|
|
deltaLog2 = 0;
|
|
|
|
} else {
|
|
|
|
METER(stats.grows++);
|
|
|
|
deltaLog2 = 1;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
return changeTableSize(deltaLog2);
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Infallibly rehash the table if we are overloaded with removals.
|
|
|
|
void checkOverRemoved()
|
|
|
|
{
|
|
|
|
if (overloaded()) {
|
2013-02-25 18:37:36 +00:00
|
|
|
if (checkOverloaded() == RehashFailed)
|
|
|
|
rehashTableInPlace();
|
2010-02-17 18:47:26 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void remove(Entry& e)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
METER(stats.removes++);
|
2011-03-24 00:40:11 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
if (e.hasCollision()) {
|
|
|
|
e.removeLive();
|
|
|
|
removedCount++;
|
|
|
|
} else {
|
|
|
|
METER(stats.removeFrees++);
|
|
|
|
e.clearLive();
|
|
|
|
}
|
|
|
|
entryCount--;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
mutationCount++;
|
2014-07-16 23:51:09 +00:00
|
|
|
#endif
|
2011-03-24 00:40:11 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void checkUnderloaded()
|
|
|
|
{
|
|
|
|
if (underloaded()) {
|
|
|
|
METER(stats.shrinks++);
|
|
|
|
(void) changeTableSize(-1);
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2012-09-10 20:27:04 +00:00
|
|
|
|
2013-01-08 10:04:19 +00:00
|
|
|
// Resize the table down to the largest capacity which doesn't underload the
|
|
|
|
// table. Since we call checkUnderloaded() on every remove, you only need
|
|
|
|
// to call this after a bulk removal of items done without calling remove().
|
|
|
|
void compactIfUnderloaded()
|
|
|
|
{
|
|
|
|
int32_t resizeLog2 = 0;
|
|
|
|
uint32_t newCapacity = capacity();
|
|
|
|
while (wouldBeUnderloaded(newCapacity, entryCount)) {
|
|
|
|
newCapacity = newCapacity >> 1;
|
|
|
|
resizeLog2--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resizeLog2 != 0) {
|
|
|
|
changeTableSize(resizeLog2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// This is identical to changeTableSize(currentSize), but without requiring
|
|
|
|
// a second table. We do this by recycling the collision bits to tell us if
|
|
|
|
// the element is already inserted or still waiting to be inserted. Since
|
|
|
|
// already-inserted elements win any conflicts, we get the same table as we
|
|
|
|
// would have gotten through random insertion order.
|
2013-02-25 18:37:36 +00:00
|
|
|
void rehashTableInPlace()
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2013-02-25 18:37:36 +00:00
|
|
|
METER(stats.rehashes++);
|
2012-11-12 23:30:39 +00:00
|
|
|
removedCount = 0;
|
|
|
|
for (size_t i = 0; i < capacity(); ++i)
|
|
|
|
table[i].unsetCollision();
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
for (size_t i = 0; i < capacity();) {
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* src = &table[i];
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
if (!src->isLive() || src->hasCollision()) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
HashNumber keyHash = src->getKeyHash();
|
|
|
|
HashNumber h1 = hash1(keyHash);
|
|
|
|
DoubleHash dh = hash2(keyHash);
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* tgt = &table[h1];
|
2012-11-12 23:30:39 +00:00
|
|
|
while (true) {
|
|
|
|
if (!tgt->hasCollision()) {
|
|
|
|
src->swap(tgt);
|
|
|
|
tgt->setCollision();
|
|
|
|
break;
|
|
|
|
}
|
2010-02-26 20:11:50 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
h1 = applyDoubleHash(h1, dh);
|
|
|
|
tgt = &table[h1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this algorithm leaves collision bits on *all* elements, even if
|
|
|
|
// they are on no collision path. We have the option of setting the
|
|
|
|
// collision bits correctly on a subsequent pass or skipping the rehash
|
|
|
|
// unless we are totally filled with tombstones: benchmark to find out
|
|
|
|
// which approach is best.
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
|
|
|
public:
|
2012-11-12 23:30:39 +00:00
|
|
|
void clear()
|
|
|
|
{
|
2013-02-09 06:59:54 +00:00
|
|
|
if (mozilla::IsPod<Entry>::value) {
|
2012-11-12 23:30:39 +00:00
|
|
|
memset(table, 0, sizeof(*table) * capacity());
|
|
|
|
} else {
|
|
|
|
uint32_t tableCapacity = capacity();
|
2015-04-02 09:22:21 +00:00
|
|
|
Entry* end = table + tableCapacity;
|
|
|
|
for (Entry* e = table; e < end; ++e)
|
2012-11-12 23:30:39 +00:00
|
|
|
e->clear();
|
|
|
|
}
|
|
|
|
removedCount = 0;
|
|
|
|
entryCount = 0;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
mutationCount++;
|
2014-07-16 23:51:09 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2012-03-30 18:01:58 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void finish()
|
|
|
|
{
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(!mEntered);
|
2014-08-11 06:17:44 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
if (!table)
|
|
|
|
return;
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
destroyTable(*this, table, capacity());
|
2013-09-19 19:24:53 +00:00
|
|
|
table = nullptr;
|
2012-11-12 23:30:39 +00:00
|
|
|
gen++;
|
|
|
|
entryCount = 0;
|
|
|
|
removedCount = 0;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
mutationCount++;
|
2014-07-16 23:51:09 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
Range all() const
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2013-12-03 20:51:02 +00:00
|
|
|
return Range(*this, table, table + capacity());
|
2010-07-22 02:01:33 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
bool empty() const
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
return !entryCount;
|
2010-07-28 16:54:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
uint32_t count() const
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
return entryCount;
|
2010-07-28 16:54:14 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
uint32_t capacity() const
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
return JS_BIT(sHashBits - hashShift);
|
2011-11-28 03:03:14 +00:00
|
|
|
}
|
2012-11-12 23:30:39 +00:00
|
|
|
|
|
|
|
uint32_t generation() const
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
return gen;
|
2011-11-28 03:03:14 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-06-23 11:21:01 +00:00
|
|
|
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
|
|
|
return mallocSizeOf(table);
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2013-06-23 11:21:01 +00:00
|
|
|
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
|
|
|
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr lookup(const Lookup& l) const
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2013-07-03 00:47:08 +00:00
|
|
|
mozilla::ReentrancyGuard g(*this);
|
2012-11-12 23:30:39 +00:00
|
|
|
HashNumber keyHash = prepareHash(l);
|
2013-12-03 20:51:02 +00:00
|
|
|
return Ptr(lookup(l, keyHash, 0), *this);
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2011-03-13 14:45:02 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
Ptr readonlyThreadsafeLookup(const Lookup& l) const
|
2013-02-26 12:49:44 +00:00
|
|
|
{
|
|
|
|
HashNumber keyHash = prepareHash(l);
|
2013-12-03 20:51:02 +00:00
|
|
|
return Ptr(lookup(l, keyHash, 0), *this);
|
2013-02-26 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
AddPtr lookupForAdd(const Lookup& l) const
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2013-07-03 00:47:08 +00:00
|
|
|
mozilla::ReentrancyGuard g(*this);
|
2012-11-12 23:30:39 +00:00
|
|
|
HashNumber keyHash = prepareHash(l);
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry& entry = lookup(l, keyHash, sCollisionBit);
|
2013-12-03 20:51:02 +00:00
|
|
|
AddPtr p(entry, *this, keyHash);
|
2012-11-12 23:30:39 +00:00
|
|
|
return p;
|
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-04-01 09:11:06 +00:00
|
|
|
template <typename... Args>
|
|
|
|
bool add(AddPtr& p, Args&&... args)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2013-07-03 00:47:08 +00:00
|
|
|
mozilla::ReentrancyGuard g(*this);
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
|
|
|
MOZ_ASSERT(!p.found());
|
|
|
|
MOZ_ASSERT(!(p.keyHash & sCollisionBit));
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
// Changing an entry from removed to live does not affect whether we
|
|
|
|
// are overloaded and can be handled separately.
|
2013-02-18 06:56:32 +00:00
|
|
|
if (p.entry_->isRemoved()) {
|
2012-11-12 23:30:39 +00:00
|
|
|
METER(stats.addOverRemoved++);
|
|
|
|
removedCount--;
|
|
|
|
p.keyHash |= sCollisionBit;
|
|
|
|
} else {
|
2013-02-18 06:56:32 +00:00
|
|
|
// Preserve the validity of |p.entry_|.
|
2012-11-12 23:30:39 +00:00
|
|
|
RebuildStatus status = checkOverloaded();
|
|
|
|
if (status == RehashFailed)
|
|
|
|
return false;
|
|
|
|
if (status == Rehashed)
|
2013-02-18 06:56:32 +00:00
|
|
|
p.entry_ = &findFreeEntry(p.keyHash);
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2010-02-06 18:14:05 +00:00
|
|
|
|
2015-04-01 09:11:06 +00:00
|
|
|
p.entry_->setLive(p.keyHash, mozilla::Forward<Args>(args)...);
|
2012-11-12 23:30:39 +00:00
|
|
|
entryCount++;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2014-07-16 23:51:09 +00:00
|
|
|
mutationCount++;
|
2013-12-03 20:51:02 +00:00
|
|
|
p.generation = generation();
|
|
|
|
p.mutationCount = mutationCount;
|
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
return true;
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
// Note: |l| may be a reference to a piece of |u|, so this function
|
|
|
|
// must take care not to use |l| after moving |u|.
|
2015-04-01 09:11:06 +00:00
|
|
|
template <typename... Args>
|
|
|
|
void putNewInfallible(const Lookup& l, Args&&... args)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2012-11-12 23:30:39 +00:00
|
|
|
|
|
|
|
HashNumber keyHash = prepareHash(l);
|
2015-03-28 22:22:11 +00:00
|
|
|
Entry* entry = &findFreeEntry(keyHash);
|
2012-11-12 23:30:39 +00:00
|
|
|
|
|
|
|
if (entry->isRemoved()) {
|
|
|
|
METER(stats.addOverRemoved++);
|
|
|
|
removedCount--;
|
|
|
|
keyHash |= sCollisionBit;
|
|
|
|
}
|
|
|
|
|
2015-04-01 09:11:06 +00:00
|
|
|
entry->setLive(keyHash, mozilla::Forward<Args>(args)...);
|
2012-11-12 23:30:39 +00:00
|
|
|
entryCount++;
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2012-11-12 23:30:39 +00:00
|
|
|
mutationCount++;
|
2014-07-16 23:51:09 +00:00
|
|
|
#endif
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 09:11:06 +00:00
|
|
|
// Note: |l| may be alias arguments in |args|, so this function must take
|
|
|
|
// care not to use |l| after moving |args|.
|
|
|
|
template <typename... Args>
|
|
|
|
bool putNew(const Lookup& l, Args&&... args)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
|
|
|
if (checkOverloaded() == RehashFailed)
|
|
|
|
return false;
|
|
|
|
|
2015-04-01 09:11:06 +00:00
|
|
|
putNewInfallible(l, mozilla::Forward<Args>(args)...);
|
2012-11-12 23:30:39 +00:00
|
|
|
return true;
|
2011-03-24 00:40:11 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 17:05:36 +00:00
|
|
|
// Note: |l| may be a reference to a piece of |u|, so this function
|
|
|
|
// must take care not to use |l| after moving |u|.
|
2015-04-01 09:11:06 +00:00
|
|
|
template <typename... Args>
|
|
|
|
bool relookupOrAdd(AddPtr& p, const Lookup& l, Args&&... args)
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2014-08-11 06:17:44 +00:00
|
|
|
#ifdef JS_DEBUG
|
2013-12-03 20:51:02 +00:00
|
|
|
p.generation = generation();
|
2012-11-12 23:30:39 +00:00
|
|
|
p.mutationCount = mutationCount;
|
2013-12-03 20:51:02 +00:00
|
|
|
#endif
|
2012-11-12 23:30:39 +00:00
|
|
|
{
|
2013-07-03 00:47:08 +00:00
|
|
|
mozilla::ReentrancyGuard g(*this);
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(prepareHash(l) == p.keyHash); // l has not been destroyed
|
2013-02-18 06:56:32 +00:00
|
|
|
p.entry_ = &lookup(l, p.keyHash, sCollisionBit);
|
2012-11-12 23:30:39 +00:00
|
|
|
}
|
2015-04-01 09:11:06 +00:00
|
|
|
return p.found() || add(p, mozilla::Forward<Args>(args)...);
|
2011-12-31 14:32:04 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void remove(Ptr p)
|
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2013-07-03 00:47:08 +00:00
|
|
|
mozilla::ReentrancyGuard g(*this);
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(p.found());
|
2013-02-18 06:56:32 +00:00
|
|
|
remove(*p.entry_);
|
2012-11-12 23:30:39 +00:00
|
|
|
checkUnderloaded();
|
2010-02-06 18:14:05 +00:00
|
|
|
}
|
2012-09-10 20:27:04 +00:00
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyWithoutRehash(Ptr p, const Lookup& l, const Key& k)
|
2013-07-02 08:43:45 +00:00
|
|
|
{
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(table);
|
2013-07-03 00:47:08 +00:00
|
|
|
mozilla::ReentrancyGuard g(*this);
|
2014-02-18 06:24:15 +00:00
|
|
|
MOZ_ASSERT(p.found());
|
2013-11-19 17:05:36 +00:00
|
|
|
typename HashTableEntry<T>::NonConstT t(mozilla::Move(*p));
|
2015-03-28 22:22:11 +00:00
|
|
|
HashPolicy::setKey(t, const_cast<Key&>(k));
|
2013-07-02 08:43:45 +00:00
|
|
|
remove(*p.entry_);
|
2013-11-19 17:05:36 +00:00
|
|
|
putNewInfallible(l, mozilla::Move(t));
|
2013-07-02 08:43:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyAndMaybeRehash(Ptr p, const Lookup& l, const Key& k)
|
2013-08-29 09:27:50 +00:00
|
|
|
{
|
|
|
|
rekeyWithoutRehash(p, l, k);
|
|
|
|
checkOverRemoved();
|
|
|
|
}
|
|
|
|
|
2015-03-28 22:22:11 +00:00
|
|
|
void rekeyInPlace(Ptr p, const Key& k)
|
2014-06-27 17:38:44 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(table);
|
|
|
|
mozilla::ReentrancyGuard g(*this);
|
|
|
|
MOZ_ASSERT(p.found());
|
2015-03-28 22:22:11 +00:00
|
|
|
HashPolicy::rekey(const_cast<Key&>(*p), const_cast<Key&>(k));
|
2014-06-27 17:38:44 +00:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
#undef METER
|
2010-02-06 18:14:05 +00:00
|
|
|
};
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace js
|
|
|
|
|
2013-06-20 00:59:09 +00:00
|
|
|
#endif /* js_HashTable_h */
|