5#ifndef RUNTIME_VM_HASH_MAP_H_
6#define RUNTIME_VM_HASH_MAP_H_
16template <
typename KeyValueTrait,
typename B,
typename Allocator = Zone>
36 void Insert(
typename KeyValueTrait::Pair kv);
46 void Update(
typename KeyValueTrait::Pair kv);
64 pairs_[
i] =
typename KeyValueTrait::Pair();
72 typename KeyValueTrait::Pair*
Next();
74 void Reset() { pair_index_ = 0; }
78 : map_(
map), pair_index_(0) {}
83 template <
typename T,
typename Bs,
typename A>
96 typename KeyValueTrait::Pair*
pairs_ =
nullptr;
110template <
typename KeyValueTrait,
typename B,
typename Allocator>
114 allocator_(other.allocator_),
116 other.allocator_->template Alloc<uint32_t>(other.hash_table_size_)),
117 pairs_(other.allocator_->template Alloc<typename KeyValueTrait::
Pair>(
119 hash_table_size_(other.hash_table_size_),
120 pairs_size_(other.pairs_size_),
121 next_pair_index_(other.next_pair_index_),
122 deleted_count_(other.deleted_count_) {
125 pairs_size_ *
sizeof(
typename KeyValueTrait::Pair));
128template <
typename KeyValueTrait,
typename B,
typename Allocator>
129typename KeyValueTrait::Pair*
133 uint32_t mask = hash_table_size_ - 1;
134 uint32_t hash_index =
hash & mask;
135 uint32_t
start = hash_index;
138 uint32_t pair_index = hash_table_[hash_index];
139 if (pair_index ==
kEmpty) {
142 if (pair_index != kDeleted) {
143 ASSERT(pair_index < pairs_size_);
145 if (KeyValueTrait::IsKeyEqual(pairs_[pair_index],
key)) {
146 return &pairs_[pair_index];
149 hash_index = (hash_index + 1) & mask;
157template <
typename KeyValueTrait,
typename B,
typename Allocator>
158typename KeyValueTrait::Value
161 const typename KeyValueTrait::Value kNoValue =
162 KeyValueTrait::ValueOf(
typename KeyValueTrait::Pair());
163 typename KeyValueTrait::Pair* pair = Lookup(
key);
164 return (pair ==
nullptr) ? kNoValue : KeyValueTrait::ValueOf(*pair);
167template <
typename KeyValueTrait,
typename B,
typename Allocator>
168typename KeyValueTrait::Pair*
170 const typename KeyValueTrait::Value kNoValue =
171 KeyValueTrait::ValueOf(
typename KeyValueTrait::Pair());
173 if (KeyValueTrait::ValueOf(map_.
pairs_[pair_index_]) != kNoValue) {
174 intptr_t old_index = pair_index_;
176 return &map_.
pairs_[old_index];
183template <
typename KeyValueTrait,
typename B,
typename Allocator>
197 typename KeyValueTrait::Pair* old_pairs =
pairs_;
207 pairs_[
i] =
typename KeyValueTrait::Pair();
210 const typename KeyValueTrait::Value kNoValue =
211 KeyValueTrait::ValueOf(
typename KeyValueTrait::Pair());
213 uint32_t deleted = 0;
214 for (uint32_t
i = 0;
i < old_next_pair_index;
i++) {
215 if (KeyValueTrait::ValueOf(old_pairs[
i]) == kNoValue) {
223 ASSERT_EQUAL(used, old_next_pair_index - old_deleted_count);
225 allocator_->template Free<typename KeyValueTrait::Pair>(old_pairs,
229template <
typename KeyValueTrait,
typename B,
typename Allocator>
231 typename KeyValueTrait::Pair kv) {
237 uint32_t hash_index =
hash & mask;
238 uint32_t
start = hash_index;
250 hash_index = (hash_index + 1) & mask;
260template <
typename KeyValueTrait,
typename B,
typename Allocator>
262 typename KeyValueTrait::Pair kv) {
263 const typename KeyValueTrait::Value kNoValue =
264 KeyValueTrait::ValueOf(
typename KeyValueTrait::Pair());
266 ASSERT(KeyValueTrait::ValueOf(kv) != kNoValue);
267 if (
auto const old_kv =
Lookup(KeyValueTrait::KeyOf(kv))) {
274template <
typename KeyValueTrait,
typename B,
typename Allocator>
279 uint32_t hash_index =
hash & mask;
280 uint32_t
start = hash_index;
284 if (pair_index ==
kEmpty) {
290 if (KeyValueTrait::IsKeyEqual(
pairs_[pair_index],
key)) {
292 pairs_[pair_index] =
typename KeyValueTrait::Pair();
297 hash_index = (hash_index + 1) & mask;
305template <
typename KeyValueTrait>
330template <
typename KeyValueTrait>
350template <
typename KeyValueTrait>
397template <
typename K,
typename V>
432 return kv ==
key || strcmp(kv,
key) == 0;
436template <
typename B,
typename Allocator>
486template <
typename B,
typename Allocator>
551 if (pair ==
nullptr) {
static uint32_t hash(const SkShaderBase::GradientInfo &v)
#define ASSERT_EQUAL(expected, actual)
#define RELEASE_ASSERT(cond)
#define ASSERT_NOTNULL(ptr)
BaseCStringIntMap(Allocator *allocator)
BaseCStringSet(Allocator *allocator)
KeyValueTrait::Pair * Next()
bool Remove(typename KeyValueTrait::Key key)
uint32_t hash_table_size_
KeyValueTrait::Pair * pairs_
static constexpr uint32_t kMaxPairs
Allocator *const allocator_
void Update(typename KeyValueTrait::Pair kv)
static constexpr uint32_t kDeleted
KeyValueTrait::Value LookupValue(typename KeyValueTrait::Key key) const
void Insert(typename KeyValueTrait::Pair kv)
BaseDirectChainedHashMap(Allocator *allocator, intptr_t initial_size=kInitialSize)
static constexpr intptr_t kInitialSize
BaseDirectChainedHashMap(const BaseDirectChainedHashMap &other)
~BaseDirectChainedHashMap()
uint32_t next_pair_index_
static constexpr uint32_t kEmpty
bool HasKey(typename KeyValueTrait::Key key) const
KeyValueTrait::Pair * Lookup(typename KeyValueTrait::Key key) const
Iterator GetIterator() const
void Resize(intptr_t new_size)
CStringIntMap(Zone *zone)
static uword Hash(Key key)
static bool IsKeyEqual(Pair kv, Key key)
static Key KeyOf(Pair kv)
static Value ValueOf(Pair kv)
DirectChainedHashMap(Zone *zone, intptr_t initial_size=DirectChainedHashMap::kInitialSize)
DirectChainedHashMap(const DirectChainedHashMap &other)
static Key KeyOf(Pair kv)
static uword Hash(Key key)
static Value ValueOf(Pair kv)
static bool IsKeyEqual(Pair pair, Key key)
static uword Hash(Key key)
static Value ValueOf(Pair kv)
static bool IsKeyEqual(Pair kv, Key key)
static Key KeyOf(Pair kv)
IntKeyRawPointerValueTrait< V >::Key Key
V Lookup(const Key &key) const
IntKeyRawPointerValueTrait< V >::Value Value
Pair * LookupPair(const Key &key) const
void Insert(const Key &key, const Value &value)
IntKeyRawPointerValueTrait< V >::Pair Pair
MallocDirectChainedHashMap(intptr_t initial_size=MallocDirectChainedHashMap::kInitialSize)
MallocDirectChainedHashMap(const MallocDirectChainedHashMap &other)
static uword Hash(Key key)
static bool IsKeyEqual(Pair kv, Key key)
static T ValueOf(Pair kv)
static intptr_t KeyOf(Pair kv)
static Value ValueOf(Pair kv)
static Key KeyOf(Pair kv)
static uword Hash(Key key)
static bool IsKeyEqual(Pair kv, Key key)
static uword Hash(Key key)
static Value ValueOf(Pair kv)
static Key KeyOf(Pair kv)
static bool IsKeyEqual(Pair kv, Key key)
static constexpr uintptr_t RoundUpToPowerOfTwo(uintptr_t x)
static uint32_t StringHash(const void *data, int length)
ZoneCStringSet(Zone *zone)
ZoneDirectChainedHashMap(Zone *zone, intptr_t initial_size=ZoneDirectChainedHashMap::kInitialSize)
ZoneDirectChainedHashMap()
T __attribute__((ext_vector_type(N))) V
constexpr intptr_t kIntptrMin
constexpr uint32_t kMaxUint32
static uint32_t Hash(uint32_t key)
SI auto map(std::index_sequence< I... >, Fn &&fn, const Args &... args) -> skvx::Vec< sizeof...(I), decltype(fn(args[0]...))>
Pair(const Key key, const Value &value)
Pair & operator=(const Pair &)=default
static bool IsKeyEqual(const Pair &kv, const Key &key)
static Value ValueOf(const Pair &pair)
static Key KeyOf(const Pair &pair)
static uword Hash(const Key &key)
Pair & operator=(const Pair &)=default
Pair(const Key key, const Value &value)
Pair(const Key key, const Value &value)
Pair & operator=(const Pair &)=default