Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_BASE_HASH_SET_H_ |
| 18 | #define ART_RUNTIME_BASE_HASH_SET_H_ |
| 19 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 22 | #include <functional> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 23 | #include <iterator> |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 24 | #include <memory> |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 25 | #include <utility> |
| 26 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 27 | #include "bit_utils.h" |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 28 | #include "logging.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | // Returns true if an item is empty. |
| 33 | template <class T> |
| 34 | class DefaultEmptyFn { |
| 35 | public: |
| 36 | void MakeEmpty(T& item) const { |
| 37 | item = T(); |
| 38 | } |
| 39 | bool IsEmpty(const T& item) const { |
| 40 | return item == T(); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | template <class T> |
| 45 | class DefaultEmptyFn<T*> { |
| 46 | public: |
| 47 | void MakeEmpty(T*& item) const { |
| 48 | item = nullptr; |
| 49 | } |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 50 | bool IsEmpty(T* const& item) const { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 51 | return item == nullptr; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | // Low memory version of a hash set, uses less memory than std::unordered_set since elements aren't |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 56 | // boxed. Uses linear probing to resolve collisions. |
| 57 | // EmptyFn needs to implement two functions MakeEmpty(T& item) and IsEmpty(const T& item). |
| 58 | // TODO: We could get rid of this requirement by using a bitmap, though maybe this would be slower |
| 59 | // and more complicated. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 60 | template <class T, class EmptyFn = DefaultEmptyFn<T>, class HashFn = std::hash<T>, |
| 61 | class Pred = std::equal_to<T>, class Alloc = std::allocator<T>> |
| 62 | class HashSet { |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 63 | template <class Elem, class HashSetType> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 64 | class BaseIterator : std::iterator<std::forward_iterator_tag, Elem> { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 65 | public: |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 66 | BaseIterator(const BaseIterator&) = default; |
| 67 | BaseIterator(BaseIterator&&) = default; |
| 68 | BaseIterator(HashSetType* hash_set, size_t index) : index_(index), hash_set_(hash_set) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 69 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 70 | BaseIterator& operator=(const BaseIterator&) = default; |
| 71 | BaseIterator& operator=(BaseIterator&&) = default; |
| 72 | |
| 73 | bool operator==(const BaseIterator& other) const { |
| 74 | return hash_set_ == other.hash_set_ && this->index_ == other.index_; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 75 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 76 | |
| 77 | bool operator!=(const BaseIterator& other) const { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 78 | return !(*this == other); |
| 79 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 80 | |
| 81 | BaseIterator operator++() { // Value after modification. |
| 82 | this->index_ = this->NextNonEmptySlot(this->index_, hash_set_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 83 | return *this; |
| 84 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 85 | |
| 86 | BaseIterator operator++(int) { |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 87 | BaseIterator temp = *this; |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 88 | this->index_ = this->NextNonEmptySlot(this->index_, hash_set_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 89 | return temp; |
| 90 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 91 | |
| 92 | Elem& operator*() const { |
| 93 | DCHECK(!hash_set_->IsFreeSlot(this->index_)); |
| 94 | return hash_set_->ElementForIndex(this->index_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 95 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 96 | |
| 97 | Elem* operator->() const { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 98 | return &**this; |
| 99 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 100 | |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 101 | // TODO: Operator -- --(int) (and use std::bidirectional_iterator_tag) |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 102 | |
| 103 | private: |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 104 | size_t index_; |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 105 | HashSetType* hash_set_; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 106 | |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 107 | size_t NextNonEmptySlot(size_t index, const HashSet* hash_set) const { |
| 108 | const size_t num_buckets = hash_set->NumBuckets(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 109 | DCHECK_LT(index, num_buckets); |
| 110 | do { |
| 111 | ++index; |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 112 | } while (index < num_buckets && hash_set->IsFreeSlot(index)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 113 | return index; |
| 114 | } |
| 115 | |
| 116 | friend class HashSet; |
| 117 | }; |
| 118 | |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 119 | public: |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 120 | using value_type = T; |
| 121 | using allocator_type = Alloc; |
| 122 | using reference = T&; |
| 123 | using const_reference = const T&; |
| 124 | using pointer = T*; |
| 125 | using const_pointer = const T*; |
| 126 | using iterator = BaseIterator<T, HashSet>; |
| 127 | using const_iterator = BaseIterator<const T, const HashSet>; |
| 128 | using size_type = size_t; |
| 129 | using difference_type = ptrdiff_t; |
| 130 | |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 131 | static constexpr double kDefaultMinLoadFactor = 0.4; |
| 132 | static constexpr double kDefaultMaxLoadFactor = 0.7; |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 133 | static constexpr size_t kMinBuckets = 1000; |
| 134 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 135 | // If we don't own the data, this will create a new array which owns the data. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 136 | void Clear() { |
| 137 | DeallocateStorage(); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 138 | num_elements_ = 0; |
| 139 | elements_until_expand_ = 0; |
| 140 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 141 | |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 142 | HashSet() : HashSet(kDefaultMinLoadFactor, kDefaultMaxLoadFactor) {} |
| 143 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 144 | HashSet(double min_load_factor, double max_load_factor) noexcept |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 145 | : num_elements_(0u), |
| 146 | num_buckets_(0u), |
| 147 | elements_until_expand_(0u), |
| 148 | owns_data_(false), |
| 149 | data_(nullptr), |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 150 | min_load_factor_(min_load_factor), |
| 151 | max_load_factor_(max_load_factor) { |
| 152 | DCHECK_GT(min_load_factor, 0.0); |
| 153 | DCHECK_LT(max_load_factor, 1.0); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 154 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 156 | explicit HashSet(const allocator_type& alloc) noexcept |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 157 | : allocfn_(alloc), |
| 158 | hashfn_(), |
| 159 | emptyfn_(), |
| 160 | pred_(), |
| 161 | num_elements_(0u), |
| 162 | num_buckets_(0u), |
| 163 | elements_until_expand_(0u), |
| 164 | owns_data_(false), |
| 165 | data_(nullptr), |
| 166 | min_load_factor_(kDefaultMinLoadFactor), |
| 167 | max_load_factor_(kDefaultMaxLoadFactor) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 168 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 169 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 170 | HashSet(const HashSet& other) noexcept |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 171 | : allocfn_(other.allocfn_), |
| 172 | hashfn_(other.hashfn_), |
| 173 | emptyfn_(other.emptyfn_), |
| 174 | pred_(other.pred_), |
| 175 | num_elements_(other.num_elements_), |
| 176 | num_buckets_(0), |
| 177 | elements_until_expand_(other.elements_until_expand_), |
| 178 | owns_data_(false), |
| 179 | data_(nullptr), |
| 180 | min_load_factor_(other.min_load_factor_), |
| 181 | max_load_factor_(other.max_load_factor_) { |
| 182 | AllocateStorage(other.NumBuckets()); |
| 183 | for (size_t i = 0; i < num_buckets_; ++i) { |
| 184 | ElementForIndex(i) = other.data_[i]; |
| 185 | } |
| 186 | } |
| 187 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 188 | // noexcept required so that the move constructor is used instead of copy constructor. |
| 189 | // b/27860101 |
| 190 | HashSet(HashSet&& other) noexcept |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 191 | : allocfn_(std::move(other.allocfn_)), |
| 192 | hashfn_(std::move(other.hashfn_)), |
| 193 | emptyfn_(std::move(other.emptyfn_)), |
| 194 | pred_(std::move(other.pred_)), |
| 195 | num_elements_(other.num_elements_), |
| 196 | num_buckets_(other.num_buckets_), |
| 197 | elements_until_expand_(other.elements_until_expand_), |
| 198 | owns_data_(other.owns_data_), |
| 199 | data_(other.data_), |
| 200 | min_load_factor_(other.min_load_factor_), |
| 201 | max_load_factor_(other.max_load_factor_) { |
| 202 | other.num_elements_ = 0u; |
| 203 | other.num_buckets_ = 0u; |
| 204 | other.elements_until_expand_ = 0u; |
| 205 | other.owns_data_ = false; |
| 206 | other.data_ = nullptr; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 207 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 208 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 209 | // Construct from existing data. |
| 210 | // Read from a block of memory, if make_copy_of_data is false, then data_ points to within the |
| 211 | // passed in ptr_. |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 212 | HashSet(const uint8_t* ptr, bool make_copy_of_data, size_t* read_count) noexcept { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 213 | uint64_t temp; |
| 214 | size_t offset = 0; |
| 215 | offset = ReadFromBytes(ptr, offset, &temp); |
| 216 | num_elements_ = static_cast<uint64_t>(temp); |
| 217 | offset = ReadFromBytes(ptr, offset, &temp); |
| 218 | num_buckets_ = static_cast<uint64_t>(temp); |
| 219 | CHECK_LE(num_elements_, num_buckets_); |
| 220 | offset = ReadFromBytes(ptr, offset, &temp); |
| 221 | elements_until_expand_ = static_cast<uint64_t>(temp); |
| 222 | offset = ReadFromBytes(ptr, offset, &min_load_factor_); |
| 223 | offset = ReadFromBytes(ptr, offset, &max_load_factor_); |
| 224 | if (!make_copy_of_data) { |
| 225 | owns_data_ = false; |
| 226 | data_ = const_cast<T*>(reinterpret_cast<const T*>(ptr + offset)); |
| 227 | offset += sizeof(*data_) * num_buckets_; |
| 228 | } else { |
| 229 | AllocateStorage(num_buckets_); |
| 230 | // Write elements, not that this may not be safe for cross compilation if the elements are |
| 231 | // pointer sized. |
| 232 | for (size_t i = 0; i < num_buckets_; ++i) { |
| 233 | offset = ReadFromBytes(ptr, offset, &data_[i]); |
| 234 | } |
| 235 | } |
| 236 | // Caller responsible for aligning. |
| 237 | *read_count = offset; |
| 238 | } |
| 239 | |
| 240 | // Returns how large the table is after being written. If target is null, then no writing happens |
| 241 | // but the size is still returned. Target must be 8 byte aligned. |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 242 | size_t WriteToMemory(uint8_t* ptr) const { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 243 | size_t offset = 0; |
| 244 | offset = WriteToBytes(ptr, offset, static_cast<uint64_t>(num_elements_)); |
| 245 | offset = WriteToBytes(ptr, offset, static_cast<uint64_t>(num_buckets_)); |
| 246 | offset = WriteToBytes(ptr, offset, static_cast<uint64_t>(elements_until_expand_)); |
| 247 | offset = WriteToBytes(ptr, offset, min_load_factor_); |
| 248 | offset = WriteToBytes(ptr, offset, max_load_factor_); |
| 249 | // Write elements, not that this may not be safe for cross compilation if the elements are |
| 250 | // pointer sized. |
| 251 | for (size_t i = 0; i < num_buckets_; ++i) { |
| 252 | offset = WriteToBytes(ptr, offset, data_[i]); |
| 253 | } |
| 254 | // Caller responsible for aligning. |
| 255 | return offset; |
| 256 | } |
| 257 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 258 | ~HashSet() { |
| 259 | DeallocateStorage(); |
| 260 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 261 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 262 | HashSet& operator=(HashSet&& other) noexcept { |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 263 | HashSet(std::move(other)).swap(*this); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 264 | return *this; |
| 265 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 266 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 267 | HashSet& operator=(const HashSet& other) noexcept { |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 268 | HashSet(other).swap(*this); // NOLINT(runtime/explicit) - a case of lint gone mad. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 269 | return *this; |
| 270 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 271 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 272 | // Lower case for c++11 for each. |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 273 | iterator begin() { |
| 274 | iterator ret(this, 0); |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 275 | if (num_buckets_ != 0 && IsFreeSlot(ret.index_)) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 276 | ++ret; // Skip all the empty slots. |
| 277 | } |
| 278 | return ret; |
| 279 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 280 | |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 281 | // Lower case for c++11 for each. const version. |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 282 | const_iterator begin() const { |
| 283 | const_iterator ret(this, 0); |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 284 | if (num_buckets_ != 0 && IsFreeSlot(ret.index_)) { |
| 285 | ++ret; // Skip all the empty slots. |
| 286 | } |
| 287 | return ret; |
| 288 | } |
| 289 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 290 | // Lower case for c++11 for each. |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 291 | iterator end() { |
| 292 | return iterator(this, NumBuckets()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 293 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 294 | |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 295 | // Lower case for c++11 for each. const version. |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 296 | const_iterator end() const { |
| 297 | return const_iterator(this, NumBuckets()); |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 300 | bool Empty() const { |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 301 | return Size() == 0; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 302 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 303 | |
Mathieu Chartier | 5ef868c | 2016-04-05 19:13:37 -0700 | [diff] [blame] | 304 | // Return true if the hash set has ownership of the underlying data. |
| 305 | bool OwnsData() const { |
| 306 | return owns_data_; |
| 307 | } |
| 308 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 309 | // Erase algorithm: |
| 310 | // Make an empty slot where the iterator is pointing. |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 311 | // Scan forwards until we hit another empty slot. |
| 312 | // If an element in between doesn't rehash to the range from the current empty slot to the |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 313 | // iterator. It must be before the empty slot, in that case we can move it to the empty slot |
| 314 | // and set the empty slot to be the location we just moved from. |
| 315 | // Relies on maintaining the invariant that there's no empty slots from the 'ideal' index of an |
| 316 | // element to its actual location/index. |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 317 | iterator Erase(iterator it) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 318 | // empty_index is the index that will become empty. |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 319 | size_t empty_index = it.index_; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 320 | DCHECK(!IsFreeSlot(empty_index)); |
| 321 | size_t next_index = empty_index; |
| 322 | bool filled = false; // True if we filled the empty index. |
| 323 | while (true) { |
| 324 | next_index = NextIndex(next_index); |
| 325 | T& next_element = ElementForIndex(next_index); |
| 326 | // If the next element is empty, we are done. Make sure to clear the current empty index. |
| 327 | if (emptyfn_.IsEmpty(next_element)) { |
| 328 | emptyfn_.MakeEmpty(ElementForIndex(empty_index)); |
| 329 | break; |
| 330 | } |
| 331 | // Otherwise try to see if the next element can fill the current empty index. |
| 332 | const size_t next_hash = hashfn_(next_element); |
| 333 | // Calculate the ideal index, if it is within empty_index + 1 to next_index then there is |
| 334 | // nothing we can do. |
| 335 | size_t next_ideal_index = IndexForHash(next_hash); |
| 336 | // Loop around if needed for our check. |
| 337 | size_t unwrapped_next_index = next_index; |
| 338 | if (unwrapped_next_index < empty_index) { |
| 339 | unwrapped_next_index += NumBuckets(); |
| 340 | } |
| 341 | // Loop around if needed for our check. |
| 342 | size_t unwrapped_next_ideal_index = next_ideal_index; |
| 343 | if (unwrapped_next_ideal_index < empty_index) { |
| 344 | unwrapped_next_ideal_index += NumBuckets(); |
| 345 | } |
| 346 | if (unwrapped_next_ideal_index <= empty_index || |
| 347 | unwrapped_next_ideal_index > unwrapped_next_index) { |
| 348 | // If the target index isn't within our current range it must have been probed from before |
| 349 | // the empty index. |
| 350 | ElementForIndex(empty_index) = std::move(next_element); |
| 351 | filled = true; // TODO: Optimize |
| 352 | empty_index = next_index; |
| 353 | } |
| 354 | } |
| 355 | --num_elements_; |
| 356 | // If we didn't fill the slot then we need go to the next non free slot. |
| 357 | if (!filled) { |
| 358 | ++it; |
| 359 | } |
| 360 | return it; |
| 361 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 362 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 363 | // Find an element, returns end() if not found. |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 364 | // Allows custom key (K) types, example of when this is useful: |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 365 | // Set of Class* sorted by name, want to find a class with a name but can't allocate a dummy |
| 366 | // object in the heap for performance solution. |
| 367 | template <typename K> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 368 | iterator Find(const K& key) { |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 369 | return FindWithHash(key, hashfn_(key)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 370 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 371 | |
| 372 | template <typename K> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 373 | const_iterator Find(const K& key) const { |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 374 | return FindWithHash(key, hashfn_(key)); |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 377 | template <typename K> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 378 | iterator FindWithHash(const K& key, size_t hash) { |
| 379 | return iterator(this, FindIndex(key, hash)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 380 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 381 | |
| 382 | template <typename K> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 383 | const_iterator FindWithHash(const K& key, size_t hash) const { |
| 384 | return const_iterator(this, FindIndex(key, hash)); |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 387 | // Insert an element, allows duplicates. |
| 388 | void Insert(const T& element) { |
| 389 | InsertWithHash(element, hashfn_(element)); |
| 390 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 391 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 392 | void InsertWithHash(const T& element, size_t hash) { |
| 393 | DCHECK_EQ(hash, hashfn_(element)); |
| 394 | if (num_elements_ >= elements_until_expand_) { |
| 395 | Expand(); |
| 396 | DCHECK_LT(num_elements_, elements_until_expand_); |
| 397 | } |
| 398 | const size_t index = FirstAvailableSlot(IndexForHash(hash)); |
| 399 | data_[index] = element; |
| 400 | ++num_elements_; |
| 401 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 402 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 403 | size_t Size() const { |
| 404 | return num_elements_; |
| 405 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 406 | |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 407 | void swap(HashSet& other) { |
| 408 | // Use argument-dependent lookup with fall-back to std::swap() for function objects. |
| 409 | using std::swap; |
| 410 | swap(allocfn_, other.allocfn_); |
| 411 | swap(hashfn_, other.hashfn_); |
| 412 | swap(emptyfn_, other.emptyfn_); |
| 413 | swap(pred_, other.pred_); |
| 414 | std::swap(data_, other.data_); |
| 415 | std::swap(num_buckets_, other.num_buckets_); |
| 416 | std::swap(num_elements_, other.num_elements_); |
| 417 | std::swap(elements_until_expand_, other.elements_until_expand_); |
| 418 | std::swap(min_load_factor_, other.min_load_factor_); |
| 419 | std::swap(max_load_factor_, other.max_load_factor_); |
| 420 | std::swap(owns_data_, other.owns_data_); |
| 421 | } |
| 422 | |
| 423 | allocator_type get_allocator() const { |
| 424 | return allocfn_; |
| 425 | } |
| 426 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 427 | void ShrinkToMaximumLoad() { |
| 428 | Resize(Size() / max_load_factor_); |
| 429 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 430 | |
Mathieu Chartier | c482d38 | 2015-10-26 11:20:18 -0700 | [diff] [blame] | 431 | // Reserve enough room to insert until Size() == num_elements without requiring to grow the hash |
| 432 | // set. No-op if the hash set is already large enough to do this. |
| 433 | void Reserve(size_t num_elements) { |
| 434 | size_t num_buckets = num_elements / max_load_factor_; |
| 435 | // Deal with rounding errors. Add one for rounding. |
| 436 | while (static_cast<size_t>(num_buckets * max_load_factor_) <= num_elements + 1u) { |
| 437 | ++num_buckets; |
| 438 | } |
| 439 | if (num_buckets > NumBuckets()) { |
| 440 | Resize(num_buckets); |
| 441 | } |
| 442 | } |
| 443 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 444 | // To distance that inserted elements were probed. Used for measuring how good hash functions |
| 445 | // are. |
| 446 | size_t TotalProbeDistance() const { |
| 447 | size_t total = 0; |
| 448 | for (size_t i = 0; i < NumBuckets(); ++i) { |
| 449 | const T& element = ElementForIndex(i); |
| 450 | if (!emptyfn_.IsEmpty(element)) { |
| 451 | size_t ideal_location = IndexForHash(hashfn_(element)); |
| 452 | if (ideal_location > i) { |
| 453 | total += i + NumBuckets() - ideal_location; |
| 454 | } else { |
| 455 | total += i - ideal_location; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | return total; |
| 460 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 461 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 462 | // Calculate the current load factor and return it. |
| 463 | double CalculateLoadFactor() const { |
| 464 | return static_cast<double>(Size()) / static_cast<double>(NumBuckets()); |
| 465 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 466 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 467 | // Make sure that everything reinserts in the right spot. Returns the number of errors. |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 468 | size_t Verify() NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 469 | size_t errors = 0; |
| 470 | for (size_t i = 0; i < num_buckets_; ++i) { |
| 471 | T& element = data_[i]; |
| 472 | if (!emptyfn_.IsEmpty(element)) { |
| 473 | T temp; |
| 474 | emptyfn_.MakeEmpty(temp); |
| 475 | std::swap(temp, element); |
| 476 | size_t first_slot = FirstAvailableSlot(IndexForHash(hashfn_(temp))); |
| 477 | if (i != first_slot) { |
| 478 | LOG(ERROR) << "Element " << i << " should be in slot " << first_slot; |
| 479 | ++errors; |
| 480 | } |
| 481 | std::swap(temp, element); |
| 482 | } |
| 483 | } |
| 484 | return errors; |
| 485 | } |
| 486 | |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 487 | double GetMinLoadFactor() const { |
| 488 | return min_load_factor_; |
| 489 | } |
| 490 | |
| 491 | double GetMaxLoadFactor() const { |
| 492 | return max_load_factor_; |
| 493 | } |
| 494 | |
| 495 | // Change the load factor of the hash set. If the current load factor is greater than the max |
| 496 | // specified, then we resize the hash table storage. |
| 497 | void SetLoadFactor(double min_load_factor, double max_load_factor) { |
| 498 | DCHECK_LT(min_load_factor, max_load_factor); |
| 499 | DCHECK_GT(min_load_factor, 0.0); |
| 500 | DCHECK_LT(max_load_factor, 1.0); |
| 501 | min_load_factor_ = min_load_factor; |
| 502 | max_load_factor_ = max_load_factor; |
| 503 | elements_until_expand_ = NumBuckets() * max_load_factor_; |
| 504 | // If the current load factor isn't in the range, then resize to the mean of the minimum and |
| 505 | // maximum load factor. |
| 506 | const double load_factor = CalculateLoadFactor(); |
| 507 | if (load_factor > max_load_factor_) { |
| 508 | Resize(Size() / ((min_load_factor_ + max_load_factor_) * 0.5)); |
| 509 | } |
| 510 | } |
| 511 | |
Mathieu Chartier | c482d38 | 2015-10-26 11:20:18 -0700 | [diff] [blame] | 512 | // The hash set expands when Size() reaches ElementsUntilExpand(). |
| 513 | size_t ElementsUntilExpand() const { |
| 514 | return elements_until_expand_; |
| 515 | } |
| 516 | |
| 517 | size_t NumBuckets() const { |
| 518 | return num_buckets_; |
| 519 | } |
| 520 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 521 | private: |
| 522 | T& ElementForIndex(size_t index) { |
| 523 | DCHECK_LT(index, NumBuckets()); |
| 524 | DCHECK(data_ != nullptr); |
| 525 | return data_[index]; |
| 526 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 527 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 528 | const T& ElementForIndex(size_t index) const { |
| 529 | DCHECK_LT(index, NumBuckets()); |
| 530 | DCHECK(data_ != nullptr); |
| 531 | return data_[index]; |
| 532 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 533 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 534 | size_t IndexForHash(size_t hash) const { |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 535 | // Protect against undefined behavior (division by zero). |
| 536 | if (UNLIKELY(num_buckets_ == 0)) { |
| 537 | return 0; |
| 538 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 539 | return hash % num_buckets_; |
| 540 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 541 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 542 | size_t NextIndex(size_t index) const { |
| 543 | if (UNLIKELY(++index >= num_buckets_)) { |
| 544 | DCHECK_EQ(index, NumBuckets()); |
| 545 | return 0; |
| 546 | } |
| 547 | return index; |
| 548 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 549 | |
| 550 | // Find the hash table slot for an element, or return NumBuckets() if not found. |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 551 | // This value for not found is important so that iterator(this, FindIndex(...)) == end(). |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 552 | template <typename K> |
| 553 | size_t FindIndex(const K& element, size_t hash) const { |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 554 | // Guard against failing to get an element for a non-existing index. |
| 555 | if (UNLIKELY(NumBuckets() == 0)) { |
| 556 | return 0; |
| 557 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 558 | DCHECK_EQ(hashfn_(element), hash); |
| 559 | size_t index = IndexForHash(hash); |
| 560 | while (true) { |
| 561 | const T& slot = ElementForIndex(index); |
| 562 | if (emptyfn_.IsEmpty(slot)) { |
| 563 | return NumBuckets(); |
| 564 | } |
| 565 | if (pred_(slot, element)) { |
| 566 | return index; |
| 567 | } |
| 568 | index = NextIndex(index); |
| 569 | } |
| 570 | } |
| 571 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 572 | bool IsFreeSlot(size_t index) const { |
| 573 | return emptyfn_.IsEmpty(ElementForIndex(index)); |
| 574 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 575 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 576 | // Allocate a number of buckets. |
| 577 | void AllocateStorage(size_t num_buckets) { |
| 578 | num_buckets_ = num_buckets; |
| 579 | data_ = allocfn_.allocate(num_buckets_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 580 | owns_data_ = true; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 581 | for (size_t i = 0; i < num_buckets_; ++i) { |
| 582 | allocfn_.construct(allocfn_.address(data_[i])); |
| 583 | emptyfn_.MakeEmpty(data_[i]); |
| 584 | } |
| 585 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 586 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 587 | void DeallocateStorage() { |
Mathieu Chartier | 88b6b05 | 2015-07-22 19:39:56 -0700 | [diff] [blame] | 588 | if (owns_data_) { |
| 589 | for (size_t i = 0; i < NumBuckets(); ++i) { |
| 590 | allocfn_.destroy(allocfn_.address(data_[i])); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 591 | } |
Mathieu Chartier | 88b6b05 | 2015-07-22 19:39:56 -0700 | [diff] [blame] | 592 | if (data_ != nullptr) { |
| 593 | allocfn_.deallocate(data_, NumBuckets()); |
| 594 | } |
| 595 | owns_data_ = false; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 596 | } |
Mathieu Chartier | 88b6b05 | 2015-07-22 19:39:56 -0700 | [diff] [blame] | 597 | data_ = nullptr; |
| 598 | num_buckets_ = 0; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 599 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 600 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 601 | // Expand the set based on the load factors. |
| 602 | void Expand() { |
| 603 | size_t min_index = static_cast<size_t>(Size() / min_load_factor_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 604 | // Resize based on the minimum load factor. |
| 605 | Resize(min_index); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 606 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 607 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 608 | // Expand / shrink the table to the new specified size. |
| 609 | void Resize(size_t new_size) { |
Mathieu Chartier | 88b6b05 | 2015-07-22 19:39:56 -0700 | [diff] [blame] | 610 | if (new_size < kMinBuckets) { |
| 611 | new_size = kMinBuckets; |
| 612 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 613 | DCHECK_GE(new_size, Size()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 614 | T* const old_data = data_; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 615 | size_t old_num_buckets = num_buckets_; |
| 616 | // Reinsert all of the old elements. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 617 | const bool owned_data = owns_data_; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 618 | AllocateStorage(new_size); |
| 619 | for (size_t i = 0; i < old_num_buckets; ++i) { |
| 620 | T& element = old_data[i]; |
| 621 | if (!emptyfn_.IsEmpty(element)) { |
| 622 | data_[FirstAvailableSlot(IndexForHash(hashfn_(element)))] = std::move(element); |
| 623 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 624 | if (owned_data) { |
| 625 | allocfn_.destroy(allocfn_.address(element)); |
| 626 | } |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 627 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 628 | if (owned_data) { |
| 629 | allocfn_.deallocate(old_data, old_num_buckets); |
| 630 | } |
Igor Murashkin | 3552d96 | 2015-06-22 15:57:38 -0700 | [diff] [blame] | 631 | |
| 632 | // When we hit elements_until_expand_, we are at the max load factor and must expand again. |
| 633 | elements_until_expand_ = NumBuckets() * max_load_factor_; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 634 | } |
Mathieu Chartier | 47f867a | 2015-03-18 10:39:00 -0700 | [diff] [blame] | 635 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 636 | ALWAYS_INLINE size_t FirstAvailableSlot(size_t index) const { |
Igor Murashkin | 3552d96 | 2015-06-22 15:57:38 -0700 | [diff] [blame] | 637 | DCHECK_LT(index, NumBuckets()); // Don't try to get a slot out of range. |
| 638 | size_t non_empty_count = 0; |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 639 | while (!emptyfn_.IsEmpty(data_[index])) { |
| 640 | index = NextIndex(index); |
Igor Murashkin | 3552d96 | 2015-06-22 15:57:38 -0700 | [diff] [blame] | 641 | non_empty_count++; |
| 642 | DCHECK_LE(non_empty_count, NumBuckets()); // Don't loop forever. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 643 | } |
| 644 | return index; |
| 645 | } |
| 646 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 647 | // Return new offset. |
| 648 | template <typename Elem> |
| 649 | static size_t WriteToBytes(uint8_t* ptr, size_t offset, Elem n) { |
| 650 | DCHECK_ALIGNED(ptr + offset, sizeof(n)); |
| 651 | if (ptr != nullptr) { |
| 652 | *reinterpret_cast<Elem*>(ptr + offset) = n; |
| 653 | } |
| 654 | return offset + sizeof(n); |
| 655 | } |
| 656 | |
| 657 | template <typename Elem> |
| 658 | static size_t ReadFromBytes(const uint8_t* ptr, size_t offset, Elem* out) { |
| 659 | DCHECK(ptr != nullptr); |
| 660 | DCHECK_ALIGNED(ptr + offset, sizeof(*out)); |
| 661 | *out = *reinterpret_cast<const Elem*>(ptr + offset); |
| 662 | return offset + sizeof(*out); |
| 663 | } |
| 664 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 665 | Alloc allocfn_; // Allocator function. |
| 666 | HashFn hashfn_; // Hashing function. |
| 667 | EmptyFn emptyfn_; // IsEmpty/SetEmpty function. |
| 668 | Pred pred_; // Equals function. |
| 669 | size_t num_elements_; // Number of inserted elements. |
| 670 | size_t num_buckets_; // Number of hash table buckets. |
Igor Murashkin | 3552d96 | 2015-06-22 15:57:38 -0700 | [diff] [blame] | 671 | size_t elements_until_expand_; // Maximum number of elements until we expand the table. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 672 | bool owns_data_; // If we own data_ and are responsible for freeing it. |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 673 | T* data_; // Backing storage. |
| 674 | double min_load_factor_; |
| 675 | double max_load_factor_; |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 676 | |
| 677 | ART_FRIEND_TEST(InternTableTest, CrossHash); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 678 | }; |
| 679 | |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 680 | template <class T, class EmptyFn, class HashFn, class Pred, class Alloc> |
| 681 | void swap(HashSet<T, EmptyFn, HashFn, Pred, Alloc>& lhs, |
| 682 | HashSet<T, EmptyFn, HashFn, Pred, Alloc>& rhs) { |
| 683 | lhs.swap(rhs); |
| 684 | } |
| 685 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 686 | } // namespace art |
| 687 | |
| 688 | #endif // ART_RUNTIME_BASE_HASH_SET_H_ |