js/src/ds/FixedSizeHash.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef jsfixedsizehash_h_
michael@0 8 #define jsfixedsizehash_h_
michael@0 9
michael@0 10 #include "ds/LifoAlloc.h"
michael@0 11
michael@0 12 namespace js {
michael@0 13
michael@0 14 /*
michael@0 15 * Class representing a hash set with fixed capacity, with newer entries
michael@0 16 * evicting older entries. Each entry has several hashes and can be stored in
michael@0 17 * different buckets, with the choice of which to evict on insertion being
michael@0 18 * managed via LRU. For tables with a relatively small size, using different
michael@0 19 * hashes increases utilization and makes it less likely that entries will keep
michael@0 20 * evicting each other due to wanting to use the same bucket.
michael@0 21 *
michael@0 22 * T indicates the type of hash elements, HashPolicy must have the following
michael@0 23 * contents:
michael@0 24 *
michael@0 25 * Lookup - As for HashMap / HashSet.
michael@0 26 *
michael@0 27 * bool match(T, Lookup) - As for HashMap / HashSet.
michael@0 28 *
michael@0 29 * NumHashes - Number of different hashes generated for each entry.
michael@0 30 *
michael@0 31 * void hash(Lookup, HashNumber[NumHashes]) - Compute all hashes for an entry.
michael@0 32 *
michael@0 33 * void clear(T *) - Clear an entry, such that isCleared() holds afterwards.
michael@0 34 *
michael@0 35 * bool isCleared(T) - Test whether an entry has been cleared.
michael@0 36 */
michael@0 37 template <class T, class HashPolicy, size_t Capacity>
michael@0 38 class FixedSizeHashSet
michael@0 39 {
michael@0 40 T entries[Capacity];
michael@0 41 uint32_t lastOperations[Capacity];
michael@0 42 uint32_t numOperations;
michael@0 43
michael@0 44 static const size_t NumHashes = HashPolicy::NumHashes;
michael@0 45
michael@0 46 public:
michael@0 47 typedef typename HashPolicy::Lookup Lookup;
michael@0 48
michael@0 49 FixedSizeHashSet()
michael@0 50 : entries(), lastOperations(), numOperations(0)
michael@0 51 {
michael@0 52 JS_STATIC_ASSERT(Capacity > 0);
michael@0 53 JS_ASSERT(HashPolicy::isCleared(entries[0]));
michael@0 54 }
michael@0 55
michael@0 56 bool lookup(const Lookup &lookup, T *pentry)
michael@0 57 {
michael@0 58 size_t bucket;
michael@0 59 if (lookupReference(lookup, &bucket)) {
michael@0 60 *pentry = entries[bucket];
michael@0 61 lastOperations[bucket] = numOperations++;
michael@0 62 return true;
michael@0 63 }
michael@0 64 return false;
michael@0 65 }
michael@0 66
michael@0 67 void insert(const Lookup &lookup, const T &entry)
michael@0 68 {
michael@0 69 size_t buckets[NumHashes];
michael@0 70 getBuckets(lookup, buckets);
michael@0 71
michael@0 72 size_t min = buckets[0];
michael@0 73 for (size_t i = 0; i < NumHashes; i++) {
michael@0 74 const T &entry = entries[buckets[i]];
michael@0 75 if (HashPolicy::isCleared(entry)) {
michael@0 76 entries[buckets[i]] = entry;
michael@0 77 lastOperations[buckets[i]] = numOperations++;
michael@0 78 return;
michael@0 79 }
michael@0 80 if (i && lastOperations[min] > lastOperations[buckets[i]])
michael@0 81 min = buckets[i];
michael@0 82 }
michael@0 83
michael@0 84 entries[min] = entry;
michael@0 85 lastOperations[min] = numOperations++;
michael@0 86 }
michael@0 87
michael@0 88 template <typename S>
michael@0 89 void remove(const S &s)
michael@0 90 {
michael@0 91 size_t bucket;
michael@0 92 if (lookupReference(s, &bucket))
michael@0 93 HashPolicy::clear(&entries[bucket]);
michael@0 94 }
michael@0 95
michael@0 96 private:
michael@0 97 template <typename S>
michael@0 98 bool lookupReference(const S &s, size_t *pbucket)
michael@0 99 {
michael@0 100 size_t buckets[NumHashes];
michael@0 101 getBuckets(s, buckets);
michael@0 102
michael@0 103 for (size_t i = 0; i < NumHashes; i++) {
michael@0 104 const T &entry = entries[buckets[i]];
michael@0 105 if (!HashPolicy::isCleared(entry) && HashPolicy::match(entry, s)) {
michael@0 106 *pbucket = buckets[i];
michael@0 107 return true;
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 return false;
michael@0 112 }
michael@0 113
michael@0 114 template <typename S>
michael@0 115 void getBuckets(const S &s, size_t buckets[NumHashes])
michael@0 116 {
michael@0 117 HashNumber hashes[NumHashes];
michael@0 118 HashPolicy::hash(s, hashes);
michael@0 119
michael@0 120 for (size_t i = 0; i < NumHashes; i++)
michael@0 121 buckets[i] = hashes[i] % Capacity;
michael@0 122 }
michael@0 123 };
michael@0 124
michael@0 125 } /* namespace js */
michael@0 126
michael@0 127 #endif /* jsfixedsizehash_h_ */

mercurial