Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2010 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | |
michael@0 | 11 | #ifndef GrTHashTable_DEFINED |
michael@0 | 12 | #define GrTHashTable_DEFINED |
michael@0 | 13 | |
michael@0 | 14 | #include "GrTypes.h" |
michael@0 | 15 | #include "SkTDArray.h" |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Key needs |
michael@0 | 19 | * static bool Equals(const Entry&, const Key&); |
michael@0 | 20 | * static bool LessThan(const Entry&, const Key&); |
michael@0 | 21 | * static bool Equals(const Entry&, const Entry&); for SK_DEBUG if GrTHashTable::validate() is called |
michael@0 | 22 | * static bool LessThan(const Entry&, const Entry&); for SK_DEBUG if GrTHashTable::validate() is called |
michael@0 | 23 | * uint32_t getHash() const; |
michael@0 | 24 | * |
michael@0 | 25 | * Allows duplicate key entries but on find you may get |
michael@0 | 26 | * any of the duplicate entries returned. |
michael@0 | 27 | */ |
michael@0 | 28 | template <typename T, typename Key, size_t kHashBits> class GrTHashTable { |
michael@0 | 29 | public: |
michael@0 | 30 | GrTHashTable() { this->clearHash(); } |
michael@0 | 31 | ~GrTHashTable() {} |
michael@0 | 32 | |
michael@0 | 33 | int count() const { return fSorted.count(); } |
michael@0 | 34 | |
michael@0 | 35 | struct Any { |
michael@0 | 36 | // Return the first resource that matches the key. |
michael@0 | 37 | bool operator()(const T*) const { return true; } |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | T* find(const Key& key) const { return this->find(key, Any()); } |
michael@0 | 41 | template <typename Filter> T* find(const Key&, Filter filter) const; |
michael@0 | 42 | |
michael@0 | 43 | // return true if key was unique when inserted. |
michael@0 | 44 | bool insert(const Key&, T*); |
michael@0 | 45 | void remove(const Key&, const T*); |
michael@0 | 46 | |
michael@0 | 47 | void deleteAll(); |
michael@0 | 48 | |
michael@0 | 49 | #ifdef SK_DEBUG |
michael@0 | 50 | void validate() const; |
michael@0 | 51 | bool contains(T*) const; |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | // testing |
michael@0 | 55 | const SkTDArray<T*>& getArray() const { return fSorted; } |
michael@0 | 56 | SkTDArray<T*>& getArray() { return fSorted; } |
michael@0 | 57 | private: |
michael@0 | 58 | void clearHash() { sk_bzero(fHash, sizeof(fHash)); } |
michael@0 | 59 | |
michael@0 | 60 | enum { |
michael@0 | 61 | kHashCount = 1 << kHashBits, |
michael@0 | 62 | kHashMask = kHashCount - 1 |
michael@0 | 63 | }; |
michael@0 | 64 | static unsigned hash2Index(uint32_t hash) { |
michael@0 | 65 | hash ^= hash >> 16; |
michael@0 | 66 | if (kHashBits <= 8) { |
michael@0 | 67 | hash ^= hash >> 8; |
michael@0 | 68 | } |
michael@0 | 69 | return hash & kHashMask; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | mutable T* fHash[kHashCount]; |
michael@0 | 73 | SkTDArray<T*> fSorted; |
michael@0 | 74 | |
michael@0 | 75 | // search fSorted, and return the found index, or ~index of where it |
michael@0 | 76 | // should be inserted |
michael@0 | 77 | int searchArray(const Key&) const; |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 81 | |
michael@0 | 82 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 83 | int GrTHashTable<T, Key, kHashBits>::searchArray(const Key& key) const { |
michael@0 | 84 | int count = fSorted.count(); |
michael@0 | 85 | if (0 == count) { |
michael@0 | 86 | // we should insert it at 0 |
michael@0 | 87 | return ~0; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | const T* const* array = fSorted.begin(); |
michael@0 | 91 | int high = count - 1; |
michael@0 | 92 | int low = 0; |
michael@0 | 93 | while (high > low) { |
michael@0 | 94 | int index = (low + high) >> 1; |
michael@0 | 95 | if (Key::LessThan(*array[index], key)) { |
michael@0 | 96 | low = index + 1; |
michael@0 | 97 | } else { |
michael@0 | 98 | high = index; |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | // check if we found it |
michael@0 | 103 | if (Key::Equals(*array[high], key)) { |
michael@0 | 104 | // above search should have found the first occurrence if there |
michael@0 | 105 | // are multiple. |
michael@0 | 106 | SkASSERT(0 == high || Key::LessThan(*array[high - 1], key)); |
michael@0 | 107 | return high; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | // now return the ~ of where we should insert it |
michael@0 | 111 | if (Key::LessThan(*array[high], key)) { |
michael@0 | 112 | high += 1; |
michael@0 | 113 | } |
michael@0 | 114 | return ~high; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 118 | template <typename Filter> |
michael@0 | 119 | T* GrTHashTable<T, Key, kHashBits>::find(const Key& key, Filter filter) const { |
michael@0 | 120 | |
michael@0 | 121 | int hashIndex = hash2Index(key.getHash()); |
michael@0 | 122 | T* elem = fHash[hashIndex]; |
michael@0 | 123 | |
michael@0 | 124 | if (NULL != elem && Key::Equals(*elem, key) && filter(elem)) { |
michael@0 | 125 | return elem; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // bsearch for the key in our sorted array |
michael@0 | 129 | int index = this->searchArray(key); |
michael@0 | 130 | if (index < 0) { |
michael@0 | 131 | return NULL; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | const T* const* array = fSorted.begin(); |
michael@0 | 135 | |
michael@0 | 136 | // above search should have found the first occurrence if there |
michael@0 | 137 | // are multiple. |
michael@0 | 138 | SkASSERT(0 == index || Key::LessThan(*array[index - 1], key)); |
michael@0 | 139 | |
michael@0 | 140 | for ( ; index < count() && Key::Equals(*array[index], key); ++index) { |
michael@0 | 141 | if (filter(fSorted[index])) { |
michael@0 | 142 | // update the hash |
michael@0 | 143 | fHash[hashIndex] = fSorted[index]; |
michael@0 | 144 | return fSorted[index]; |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | return NULL; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 152 | bool GrTHashTable<T, Key, kHashBits>::insert(const Key& key, T* elem) { |
michael@0 | 153 | int index = this->searchArray(key); |
michael@0 | 154 | bool first = index < 0; |
michael@0 | 155 | if (first) { |
michael@0 | 156 | // turn it into the actual index |
michael@0 | 157 | index = ~index; |
michael@0 | 158 | } |
michael@0 | 159 | // add it to our array |
michael@0 | 160 | *fSorted.insert(index) = elem; |
michael@0 | 161 | // update our hash table (overwrites any dupe's position in the hash) |
michael@0 | 162 | fHash[hash2Index(key.getHash())] = elem; |
michael@0 | 163 | return first; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 167 | void GrTHashTable<T, Key, kHashBits>::remove(const Key& key, const T* elem) { |
michael@0 | 168 | int index = hash2Index(key.getHash()); |
michael@0 | 169 | if (fHash[index] == elem) { |
michael@0 | 170 | fHash[index] = NULL; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | // remove from our sorted array |
michael@0 | 174 | index = this->searchArray(key); |
michael@0 | 175 | SkASSERT(index >= 0); |
michael@0 | 176 | // if there are multiple matches searchArray will give us the first match |
michael@0 | 177 | // march forward until we find elem. |
michael@0 | 178 | while (elem != fSorted[index]) { |
michael@0 | 179 | ++index; |
michael@0 | 180 | SkASSERT(index < fSorted.count()); |
michael@0 | 181 | } |
michael@0 | 182 | SkASSERT(elem == fSorted[index]); |
michael@0 | 183 | fSorted.remove(index); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 187 | void GrTHashTable<T, Key, kHashBits>::deleteAll() { |
michael@0 | 188 | fSorted.deleteAll(); |
michael@0 | 189 | this->clearHash(); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | #ifdef SK_DEBUG |
michael@0 | 193 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 194 | void GrTHashTable<T, Key, kHashBits>::validate() const { |
michael@0 | 195 | int count = fSorted.count(); |
michael@0 | 196 | for (int i = 1; i < count; i++) { |
michael@0 | 197 | SkASSERT(Key::LessThan(*fSorted[i - 1], *fSorted[i]) || |
michael@0 | 198 | Key::Equals(*fSorted[i - 1], *fSorted[i])); |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | template <typename T, typename Key, size_t kHashBits> |
michael@0 | 203 | bool GrTHashTable<T, Key, kHashBits>::contains(T* elem) const { |
michael@0 | 204 | int index = fSorted.find(elem); |
michael@0 | 205 | return index >= 0; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | #endif |
michael@0 | 209 | |
michael@0 | 210 | #endif |