| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef ds_InlineTable_h |
| 8 |
#define ds_InlineTable_h |
| 9 |
|
| 10 |
#include "mozilla/Maybe.h" |
| 11 |
#include "mozilla/Variant.h" |
| 12 |
|
| 13 |
#include <utility> |
| 14 |
|
| 15 |
#include "js/AllocPolicy.h" |
| 16 |
#include "js/HashTable.h" |
| 17 |
|
| 18 |
namespace js { |
| 19 |
|
| 20 |
namespace detail { |
| 21 |
|
| 22 |
template <typename InlineEntry, typename Entry, typename Table, |
| 23 |
typename HashPolicy, typename AllocPolicy, size_t InlineEntries> |
| 24 |
class InlineTable : private AllocPolicy { |
| 25 |
private: |
| 26 |
using TablePtr = typename Table::Ptr; |
| 27 |
using TableAddPtr = typename Table::AddPtr; |
| 28 |
using TableRange = typename Table::Range; |
| 29 |
using Lookup = typename HashPolicy::Lookup; |
| 30 |
|
| 31 |
struct InlineArray { |
| 32 |
uint32_t count = 0; |
| 33 |
InlineEntry inl[InlineEntries]; |
| 34 |
}; |
| 35 |
mozilla::Variant<InlineArray, Table> data_{InlineArray()}; |
| 36 |
#ifdef DEBUG |
| 37 |
// Used to check that entries aren't added/removed while using Ptr/AddPtr or |
| 38 |
// Range. Similar to HashTable::mMutationCount. |
| 39 |
uint64_t mutationCount_ = 0; |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifndef DEBUG |
| 43 |
// If this assertion fails, you should probably increase InlineEntries because |
| 44 |
// an extra inline entry could likely be added "for free". |
| 45 |
static_assert(sizeof(InlineArray) + sizeof(InlineEntry) >= sizeof(Table), |
| 46 |
"Space for additional inline elements in InlineTable?"); |
| 47 |
#endif |
| 48 |
|
| 49 |
InlineArray& inlineArray() { return data_.template as<InlineArray>(); } |
| 50 |
const InlineArray& inlineArray() const { |
| 51 |
return data_.template as<InlineArray>(); |
| 52 |
} |
| 53 |
Table& table() { return data_.template as<Table>(); } |
| 54 |
const Table& table() const { return data_.template as<Table>(); } |
| 55 |
|
| 56 |
InlineEntry* inlineStart() { |
| 57 |
MOZ_ASSERT(!usingTable()); |
| 58 |
return inlineArray().inl; |
| 59 |
} |
| 60 |
|
| 61 |
const InlineEntry* inlineStart() const { |
| 62 |
MOZ_ASSERT(!usingTable()); |
| 63 |
return inlineArray().inl; |
| 64 |
} |
| 65 |
|
| 66 |
InlineEntry* inlineEnd() { |
| 67 |
MOZ_ASSERT(!usingTable()); |
| 68 |
return inlineArray().inl + inlineArray().count; |
| 69 |
} |
| 70 |
|
| 71 |
const InlineEntry* inlineEnd() const { |
| 72 |
MOZ_ASSERT(!usingTable()); |
| 73 |
return inlineArray().inl + inlineArray().count; |
| 74 |
} |
| 75 |
|
| 76 |
bool usingTable() const { return data_.template is<Table>(); } |
| 77 |
|
| 78 |
void bumpMutationCount() { |
| 79 |
#ifdef DEBUG |
| 80 |
mutationCount_++; |
| 81 |
#endif |
| 82 |
} |
| 83 |
|
| 84 |
[[nodiscard]] bool switchToTable() { |
| 85 |
MOZ_ASSERT(inlineArray().count == InlineEntries); |
| 86 |
|
| 87 |
Table table(*static_cast<AllocPolicy*>(this)); |
| 88 |
|
| 89 |
// This is called before adding the next element, so reserve space for it |
| 90 |
// too. |
| 91 |
if (!table.reserve(InlineEntries + 1)) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
InlineEntry* end = inlineStart() + InlineEntries; |
| 96 |
for (InlineEntry* it = inlineStart(); it != end; ++it) { |
| 97 |
// Note: don't use putNewInfallible because hashing can be fallible too. |
| 98 |
if (!it->moveTo(table)) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
MOZ_ASSERT(table.count() == InlineEntries); |
| 104 |
data_.template emplace<Table>(std::move(table)); |
| 105 |
MOZ_ASSERT(usingTable()); |
| 106 |
bumpMutationCount(); |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
public: |
| 111 |
static const size_t SizeOfInlineEntries = sizeof(InlineEntry) * InlineEntries; |
| 112 |
|
| 113 |
explicit InlineTable(AllocPolicy a = AllocPolicy()) |
| 114 |
: AllocPolicy(std::move(a)) {} |
| 115 |
|
| 116 |
class Ptr { |
| 117 |
friend class InlineTable; |
| 118 |
|
| 119 |
MOZ_INIT_OUTSIDE_CTOR Entry entry_; |
| 120 |
MOZ_INIT_OUTSIDE_CTOR TablePtr tablePtr_; |
| 121 |
MOZ_INIT_OUTSIDE_CTOR InlineEntry* inlPtr_; |
| 122 |
MOZ_INIT_OUTSIDE_CTOR bool isInlinePtr_; |
| 123 |
#ifdef DEBUG |
| 124 |
uint64_t mutationCount_ = 0xbadbad; |
| 125 |
#endif |
| 126 |
|
| 127 |
Ptr(const InlineTable& table, TablePtr p) |
| 128 |
: entry_(p.found() ? &*p : nullptr), tablePtr_(p), isInlinePtr_(false) { |
| 129 |
#ifdef DEBUG |
| 130 |
mutationCount_ = table.mutationCount_; |
| 131 |
#endif |
| 132 |
} |
| 133 |
|
| 134 |
Ptr(const InlineTable& table, InlineEntry* inlineEntry) |
| 135 |
: entry_(inlineEntry), inlPtr_(inlineEntry), isInlinePtr_(true) { |
| 136 |
#ifdef DEBUG |
| 137 |
mutationCount_ = table.mutationCount_; |
| 138 |
#endif |
| 139 |
} |
| 140 |
|
| 141 |
public: |
| 142 |
// Leaves Ptr uninitialized. |
| 143 |
Ptr() { |
| 144 |
#ifdef DEBUG |
| 145 |
inlPtr_ = (InlineEntry*)0xbad; |
| 146 |
isInlinePtr_ = true; |
| 147 |
#endif |
| 148 |
} |
| 149 |
|
| 150 |
// Default copy constructor works for this structure. |
| 151 |
|
| 152 |
bool found() const { |
| 153 |
return isInlinePtr_ ? bool(inlPtr_) : tablePtr_.found(); |
| 154 |
} |
| 155 |
|
| 156 |
explicit operator bool() const { return found(); } |
| 157 |
|
| 158 |
Entry& operator*() { |
| 159 |
MOZ_ASSERT(found()); |
| 160 |
return entry_; |
| 161 |
} |
| 162 |
|
| 163 |
Entry* operator->() { |
| 164 |
MOZ_ASSERT(found()); |
| 165 |
return &entry_; |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
class AddPtr { |
| 170 |
friend class InlineTable; |
| 171 |
|
| 172 |
MOZ_INIT_OUTSIDE_CTOR Entry entry_; |
| 173 |
MOZ_INIT_OUTSIDE_CTOR TableAddPtr tableAddPtr_; |
| 174 |
MOZ_INIT_OUTSIDE_CTOR InlineEntry* inlAddPtr_; |
| 175 |
MOZ_INIT_OUTSIDE_CTOR bool isInlinePtr_; |
| 176 |
// Indicates whether inlAddPtr is a found result or an add pointer. |
| 177 |
MOZ_INIT_OUTSIDE_CTOR bool inlPtrFound_; |
| 178 |
#ifdef DEBUG |
| 179 |
uint64_t mutationCount_ = 0xbadbad; |
| 180 |
#endif |
| 181 |
|
| 182 |
AddPtr(const InlineTable& table, InlineEntry* ptr, bool found) |
| 183 |
: entry_(ptr), |
| 184 |
inlAddPtr_(ptr), |
| 185 |
isInlinePtr_(true), |
| 186 |
inlPtrFound_(found) { |
| 187 |
#ifdef DEBUG |
| 188 |
mutationCount_ = table.mutationCount_; |
| 189 |
#endif |
| 190 |
} |
| 191 |
|
| 192 |
AddPtr(const InlineTable& table, const TableAddPtr& p) |
| 193 |
: entry_(p.found() ? &*p : nullptr), |
| 194 |
tableAddPtr_(p), |
| 195 |
isInlinePtr_(false) { |
| 196 |
#ifdef DEBUG |
| 197 |
mutationCount_ = table.mutationCount_; |
| 198 |
#endif |
| 199 |
} |
| 200 |
|
| 201 |
public: |
| 202 |
AddPtr() = default; |
| 203 |
|
| 204 |
bool found() const { |
| 205 |
return isInlinePtr_ ? inlPtrFound_ : tableAddPtr_.found(); |
| 206 |
} |
| 207 |
|
| 208 |
explicit operator bool() const { return found(); } |
| 209 |
|
| 210 |
Entry& operator*() { |
| 211 |
MOZ_ASSERT(found()); |
| 212 |
return entry_; |
| 213 |
} |
| 214 |
|
| 215 |
Entry* operator->() { |
| 216 |
MOZ_ASSERT(found()); |
| 217 |
return &entry_; |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
size_t count() const { |
| 222 |
return usingTable() ? table().count() : inlineArray().count; |
| 223 |
} |
| 224 |
|
| 225 |
bool empty() const { |
| 226 |
return usingTable() ? table().empty() : !inlineArray().count; |
| 227 |
} |
| 228 |
|
| 229 |
void clear() { |
| 230 |
data_.template emplace<InlineArray>(); |
| 231 |
bumpMutationCount(); |
| 232 |
} |
| 233 |
|
| 234 |
MOZ_ALWAYS_INLINE |
| 235 |
Ptr lookup(const Lookup& l) { |
| 236 |
if (usingTable()) { |
| 237 |
return Ptr(*this, table().lookup(l)); |
| 238 |
} |
| 239 |
|
| 240 |
InlineEntry* end = inlineEnd(); |
| 241 |
for (InlineEntry* it = inlineStart(); it != end; ++it) { |
| 242 |
if (HashPolicy::match(it->key, l)) { |
| 243 |
return Ptr(*this, it); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return Ptr(*this, nullptr); |
| 248 |
} |
| 249 |
|
| 250 |
MOZ_ALWAYS_INLINE |
| 251 |
AddPtr lookupForAdd(const Lookup& l) { |
| 252 |
if (usingTable()) { |
| 253 |
return AddPtr(*this, table().lookupForAdd(l)); |
| 254 |
} |
| 255 |
|
| 256 |
InlineEntry* end = inlineEnd(); |
| 257 |
for (InlineEntry* it = inlineStart(); it != end; ++it) { |
| 258 |
if (HashPolicy::match(it->key, l)) { |
| 259 |
return AddPtr(*this, it, true); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
// The add pointer that's returned here may indicate the limit entry of |
| 264 |
// the linear space, in which case the |add| operation will initialize |
| 265 |
// the table if necessary and add the entry there. |
| 266 |
return AddPtr(*this, inlineEnd(), false); |
| 267 |
} |
| 268 |
|
| 269 |
template <typename KeyInput, typename... Args> |
| 270 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool add(AddPtr& p, KeyInput&& key, |
| 271 |
Args&&... args) { |
| 272 |
MOZ_ASSERT(!p); |
| 273 |
MOZ_ASSERT(p.mutationCount_ == mutationCount_); |
| 274 |
|
| 275 |
if (p.isInlinePtr_) { |
| 276 |
InlineEntry* addPtr = p.inlAddPtr_; |
| 277 |
MOZ_ASSERT(addPtr == inlineEnd()); |
| 278 |
|
| 279 |
// Switching to table mode before we add this pointer. |
| 280 |
if (addPtr == inlineStart() + InlineEntries) { |
| 281 |
if (!switchToTable()) { |
| 282 |
return false; |
| 283 |
} |
| 284 |
if (!table().putNew(std::forward<KeyInput>(key), |
| 285 |
std::forward<Args>(args)...)) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
bumpMutationCount(); |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
MOZ_ASSERT(!p.found()); |
| 293 |
MOZ_ASSERT(uintptr_t(inlineEnd()) == uintptr_t(p.inlAddPtr_)); |
| 294 |
|
| 295 |
if (!this->checkSimulatedOOM()) { |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
addPtr->update(std::forward<KeyInput>(key), std::forward<Args>(args)...); |
| 300 |
inlineArray().count++; |
| 301 |
bumpMutationCount(); |
| 302 |
return true; |
| 303 |
} |
| 304 |
|
| 305 |
if (!table().add(p.tableAddPtr_, std::forward<KeyInput>(key), |
| 306 |
std::forward<Args>(args)...)) { |
| 307 |
return false; |
| 308 |
} |
| 309 |
bumpMutationCount(); |
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
void remove(Ptr& p) { |
| 314 |
MOZ_ASSERT(p); |
| 315 |
MOZ_ASSERT(p.mutationCount_ == mutationCount_); |
| 316 |
if (p.isInlinePtr_) { |
| 317 |
InlineArray& arr = inlineArray(); |
| 318 |
MOZ_ASSERT(arr.count > 0); |
| 319 |
InlineEntry* last = &arr.inl[arr.count - 1]; |
| 320 |
MOZ_ASSERT(p.inlPtr_ <= last); |
| 321 |
if (p.inlPtr_ != last) { |
| 322 |
// Removing an entry that's not the last one. Move the last entry. |
| 323 |
*p.inlPtr_ = std::move(*last); |
| 324 |
} |
| 325 |
arr.count--; |
| 326 |
} else { |
| 327 |
MOZ_ASSERT(usingTable()); |
| 328 |
table().remove(p.tablePtr_); |
| 329 |
} |
| 330 |
bumpMutationCount(); |
| 331 |
} |
| 332 |
|
| 333 |
void remove(const Lookup& l) { |
| 334 |
if (Ptr p = lookup(l)) { |
| 335 |
remove(p); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
class Range { |
| 340 |
friend class InlineTable; |
| 341 |
|
| 342 |
mozilla::Maybe<TableRange> tableRange_; // `Nothing` if `isInline_==true` |
| 343 |
InlineEntry* cur_; |
| 344 |
InlineEntry* end_; |
| 345 |
bool isInline_; |
| 346 |
#ifdef DEBUG |
| 347 |
const InlineTable* table_ = nullptr; |
| 348 |
uint64_t mutationCount_ = 0xbadbad; |
| 349 |
#endif |
| 350 |
|
| 351 |
Range(const InlineTable& table, TableRange r) |
| 352 |
: tableRange_(mozilla::Some(r)), |
| 353 |
cur_(nullptr), |
| 354 |
end_(nullptr), |
| 355 |
isInline_(false) { |
| 356 |
MOZ_ASSERT(!isInlineRange()); |
| 357 |
#ifdef DEBUG |
| 358 |
table_ = &table; |
| 359 |
mutationCount_ = table.mutationCount_; |
| 360 |
#endif |
| 361 |
} |
| 362 |
|
| 363 |
Range(const InlineTable& table, const InlineEntry* begin, |
| 364 |
const InlineEntry* end) |
| 365 |
: tableRange_(mozilla::Nothing()), |
| 366 |
cur_(const_cast<InlineEntry*>(begin)), |
| 367 |
end_(const_cast<InlineEntry*>(end)), |
| 368 |
isInline_(true) { |
| 369 |
MOZ_ASSERT(isInlineRange()); |
| 370 |
#ifdef DEBUG |
| 371 |
table_ = &table; |
| 372 |
mutationCount_ = table.mutationCount_; |
| 373 |
#endif |
| 374 |
} |
| 375 |
|
| 376 |
bool assertInlineRangeInvariants() const { |
| 377 |
MOZ_ASSERT(uintptr_t(cur_) <= uintptr_t(end_)); |
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
bool isInlineRange() const { |
| 382 |
MOZ_ASSERT_IF(isInline_, assertInlineRangeInvariants()); |
| 383 |
return isInline_; |
| 384 |
} |
| 385 |
|
| 386 |
void bumpCurPtr() { |
| 387 |
MOZ_ASSERT(isInlineRange()); |
| 388 |
cur_++; |
| 389 |
} |
| 390 |
|
| 391 |
public: |
| 392 |
bool empty() const { |
| 393 |
MOZ_ASSERT(table_->mutationCount_ == mutationCount_); |
| 394 |
return isInlineRange() ? cur_ == end_ : tableRange_->empty(); |
| 395 |
} |
| 396 |
|
| 397 |
Entry front() { |
| 398 |
MOZ_ASSERT(!empty()); |
| 399 |
MOZ_ASSERT(table_->mutationCount_ == mutationCount_); |
| 400 |
if (isInlineRange()) { |
| 401 |
return Entry(cur_); |
| 402 |
} |
| 403 |
return Entry(&tableRange_->front()); |
| 404 |
} |
| 405 |
|
| 406 |
void popFront() { |
| 407 |
MOZ_ASSERT(!empty()); |
| 408 |
MOZ_ASSERT(table_->mutationCount_ == mutationCount_); |
| 409 |
if (isInlineRange()) { |
| 410 |
bumpCurPtr(); |
| 411 |
} else { |
| 412 |
tableRange_->popFront(); |
| 413 |
} |
| 414 |
} |
| 415 |
}; |
| 416 |
|
| 417 |
Range all() const { |
| 418 |
return usingTable() ? Range(*this, table().all()) |
| 419 |
: Range(*this, inlineStart(), inlineEnd()); |
| 420 |
} |
| 421 |
}; |
| 422 |
|
| 423 |
} // namespace detail |
| 424 |
|
| 425 |
// A map with InlineEntries number of entries kept inline in an array. |
| 426 |
// |
| 427 |
// The Value type must have a default constructor. |
| 428 |
// |
| 429 |
// The API is very much like HashMap's. |
| 430 |
template <typename Key, typename Value, size_t InlineEntries, |
| 431 |
typename HashPolicy = DefaultHasher<Key>, |
| 432 |
typename AllocPolicy = TempAllocPolicy> |
| 433 |
class InlineMap { |
| 434 |
using Map = HashMap<Key, Value, HashPolicy, AllocPolicy>; |
| 435 |
|
| 436 |
struct InlineEntry { |
| 437 |
Key key; |
| 438 |
Value value; |
| 439 |
|
| 440 |
template <typename KeyInput, typename ValueInput> |
| 441 |
void update(KeyInput&& key, ValueInput&& value) { |
| 442 |
this->key = std::forward<KeyInput>(key); |
| 443 |
this->value = std::forward<ValueInput>(value); |
| 444 |
} |
| 445 |
|
| 446 |
[[nodiscard]] bool moveTo(Map& map) { |
| 447 |
return map.putNew(std::move(key), std::move(value)); |
| 448 |
} |
| 449 |
}; |
| 450 |
|
| 451 |
class Entry { |
| 452 |
using MapEntry = typename Map::Entry; |
| 453 |
|
| 454 |
MapEntry* mapEntry_; |
| 455 |
InlineEntry* inlineEntry_; |
| 456 |
|
| 457 |
public: |
| 458 |
Entry() = default; |
| 459 |
|
| 460 |
explicit Entry(MapEntry* mapEntry) |
| 461 |
: mapEntry_(mapEntry), inlineEntry_(nullptr) {} |
| 462 |
|
| 463 |
explicit Entry(InlineEntry* inlineEntry) |
| 464 |
: mapEntry_(nullptr), inlineEntry_(inlineEntry) {} |
| 465 |
|
| 466 |
const Key& key() const { |
| 467 |
MOZ_ASSERT(!!mapEntry_ != !!inlineEntry_); |
| 468 |
if (mapEntry_) { |
| 469 |
return mapEntry_->key(); |
| 470 |
} |
| 471 |
return inlineEntry_->key; |
| 472 |
} |
| 473 |
|
| 474 |
Value& value() { |
| 475 |
MOZ_ASSERT(!!mapEntry_ != !!inlineEntry_); |
| 476 |
if (mapEntry_) { |
| 477 |
return mapEntry_->value(); |
| 478 |
} |
| 479 |
return inlineEntry_->value; |
| 480 |
} |
| 481 |
}; |
| 482 |
|
| 483 |
using Impl = detail::InlineTable<InlineEntry, Entry, Map, HashPolicy, |
| 484 |
AllocPolicy, InlineEntries>; |
| 485 |
|
| 486 |
Impl impl_; |
| 487 |
|
| 488 |
public: |
| 489 |
using Table = Map; |
| 490 |
using Ptr = typename Impl::Ptr; |
| 491 |
using AddPtr = typename Impl::AddPtr; |
| 492 |
using Range = typename Impl::Range; |
| 493 |
using Lookup = typename HashPolicy::Lookup; |
| 494 |
|
| 495 |
static const size_t SizeOfInlineEntries = Impl::SizeOfInlineEntries; |
| 496 |
|
| 497 |
explicit InlineMap(AllocPolicy a = AllocPolicy()) : impl_(std::move(a)) {} |
| 498 |
|
| 499 |
size_t count() const { return impl_.count(); } |
| 500 |
|
| 501 |
bool empty() const { return impl_.empty(); } |
| 502 |
|
| 503 |
void clear() { impl_.clear(); } |
| 504 |
|
| 505 |
Range all() const { return impl_.all(); } |
| 506 |
|
| 507 |
MOZ_ALWAYS_INLINE |
| 508 |
Ptr lookup(const Lookup& l) { return impl_.lookup(l); } |
| 509 |
|
| 510 |
MOZ_ALWAYS_INLINE |
| 511 |
bool has(const Lookup& l) const { |
| 512 |
return const_cast<InlineMap*>(this)->lookup(l).found(); |
| 513 |
} |
| 514 |
|
| 515 |
MOZ_ALWAYS_INLINE |
| 516 |
AddPtr lookupForAdd(const Lookup& l) { return impl_.lookupForAdd(l); } |
| 517 |
|
| 518 |
template <typename KeyInput, typename ValueInput> |
| 519 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool add(AddPtr& p, KeyInput&& key, |
| 520 |
ValueInput&& value) { |
| 521 |
return impl_.add(p, std::forward<KeyInput>(key), |
| 522 |
std::forward<ValueInput>(value)); |
| 523 |
} |
| 524 |
|
| 525 |
template <typename KeyInput, typename ValueInput> |
| 526 |
[[nodiscard]] bool put(KeyInput&& key, ValueInput&& value) { |
| 527 |
AddPtr p = lookupForAdd(key); |
| 528 |
if (p) { |
| 529 |
p->value() = std::forward<ValueInput>(value); |
| 530 |
return true; |
| 531 |
} |
| 532 |
return add(p, std::forward<KeyInput>(key), std::forward<ValueInput>(value)); |
| 533 |
} |
| 534 |
|
| 535 |
void remove(Ptr& p) { impl_.remove(p); } |
| 536 |
|
| 537 |
void remove(const Lookup& l) { impl_.remove(l); } |
| 538 |
}; |
| 539 |
|
| 540 |
// A set with InlineEntries number of entries kept inline in an array. |
| 541 |
// |
| 542 |
// The T type must have a default constructor. |
| 543 |
// |
| 544 |
// The API is very much like HashSet's. |
| 545 |
template <typename T, size_t InlineEntries, |
| 546 |
typename HashPolicy = DefaultHasher<T>, |
| 547 |
typename AllocPolicy = TempAllocPolicy> |
| 548 |
class InlineSet { |
| 549 |
using Set = HashSet<T, HashPolicy, AllocPolicy>; |
| 550 |
|
| 551 |
struct InlineEntry { |
| 552 |
T key; |
| 553 |
|
| 554 |
template <typename TInput> |
| 555 |
void update(TInput&& key) { |
| 556 |
this->key = std::forward<TInput>(key); |
| 557 |
} |
| 558 |
|
| 559 |
[[nodiscard]] bool moveTo(Set& set) { return set.putNew(std::move(key)); } |
| 560 |
}; |
| 561 |
|
| 562 |
class Entry { |
| 563 |
using SetEntry = typename Set::Entry; |
| 564 |
|
| 565 |
SetEntry* setEntry_; |
| 566 |
InlineEntry* inlineEntry_; |
| 567 |
|
| 568 |
public: |
| 569 |
Entry() = default; |
| 570 |
|
| 571 |
explicit Entry(const SetEntry* setEntry) |
| 572 |
: setEntry_(const_cast<SetEntry*>(setEntry)), inlineEntry_(nullptr) {} |
| 573 |
|
| 574 |
explicit Entry(InlineEntry* inlineEntry) |
| 575 |
: setEntry_(nullptr), inlineEntry_(inlineEntry) {} |
| 576 |
|
| 577 |
operator T() const { |
| 578 |
MOZ_ASSERT(!!setEntry_ != !!inlineEntry_); |
| 579 |
if (setEntry_) { |
| 580 |
return *setEntry_; |
| 581 |
} |
| 582 |
return inlineEntry_->key; |
| 583 |
} |
| 584 |
}; |
| 585 |
|
| 586 |
using Impl = detail::InlineTable<InlineEntry, Entry, Set, HashPolicy, |
| 587 |
AllocPolicy, InlineEntries>; |
| 588 |
|
| 589 |
Impl impl_; |
| 590 |
|
| 591 |
public: |
| 592 |
using Table = Set; |
| 593 |
using Ptr = typename Impl::Ptr; |
| 594 |
using AddPtr = typename Impl::AddPtr; |
| 595 |
using Range = typename Impl::Range; |
| 596 |
using Lookup = typename HashPolicy::Lookup; |
| 597 |
|
| 598 |
static const size_t SizeOfInlineEntries = Impl::SizeOfInlineEntries; |
| 599 |
|
| 600 |
explicit InlineSet(AllocPolicy a = AllocPolicy()) : impl_(std::move(a)) {} |
| 601 |
|
| 602 |
size_t count() const { return impl_.count(); } |
| 603 |
|
| 604 |
bool empty() const { return impl_.empty(); } |
| 605 |
|
| 606 |
void clear() { impl_.clear(); } |
| 607 |
|
| 608 |
Range all() const { return impl_.all(); } |
| 609 |
|
| 610 |
MOZ_ALWAYS_INLINE |
| 611 |
Ptr lookup(const Lookup& l) { return impl_.lookup(l); } |
| 612 |
|
| 613 |
MOZ_ALWAYS_INLINE |
| 614 |
bool has(const Lookup& l) const { |
| 615 |
return const_cast<InlineSet*>(this)->lookup(l).found(); |
| 616 |
} |
| 617 |
|
| 618 |
MOZ_ALWAYS_INLINE |
| 619 |
AddPtr lookupForAdd(const Lookup& l) { return impl_.lookupForAdd(l); } |
| 620 |
|
| 621 |
template <typename TInput> |
| 622 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool add(AddPtr& p, TInput&& key) { |
| 623 |
return impl_.add(p, std::forward<TInput>(key)); |
| 624 |
} |
| 625 |
|
| 626 |
template <typename TInput> |
| 627 |
[[nodiscard]] bool put(TInput&& key) { |
| 628 |
AddPtr p = lookupForAdd(key); |
| 629 |
return p ? true : add(p, std::forward<TInput>(key)); |
| 630 |
} |
| 631 |
|
| 632 |
void remove(Ptr& p) { impl_.remove(p); } |
| 633 |
|
| 634 |
void remove(const Lookup& l) { impl_.remove(l); } |
| 635 |
}; |
| 636 |
|
| 637 |
} // namespace js |
| 638 |
|
| 639 |
#endif // ds_InlineTable_h |