#ifndef A_HASH_MAP_H
#define A_HASH_MAP_H
#include "core/templates/hash_map.h"
struct HashMapData { … };
static_assert …;
template <typename TKey, typename TValue,
typename Hasher = HashMapHasherDefault,
typename Comparator = HashMapComparatorDefault<TKey>>
class AHashMap {
public:
static constexpr uint32_t INITIAL_CAPACITY = 16;
static constexpr uint32_t EMPTY_HASH = 0;
static_assert(EMPTY_HASH == 0, "EMPTY_HASH must always be 0 for the memcpy() optimization.");
private:
typedef KeyValue<TKey, TValue> MapKeyValue;
MapKeyValue *elements = nullptr;
HashMapData *map_data = nullptr;
uint32_t capacity = 0;
uint32_t num_elements = 0;
uint32_t _hash(const TKey &p_key) const { … }
static _FORCE_INLINE_ uint32_t _get_resize_count(uint32_t p_capacity) { … }
static _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash, uint32_t p_local_capacity) { … }
bool _lookup_pos(const TKey &p_key, uint32_t &r_pos, uint32_t &r_hash_pos) const { … }
bool _lookup_pos_with_hash(const TKey &p_key, uint32_t &r_pos, uint32_t &r_hash_pos, uint32_t p_hash) const { … }
uint32_t _insert_with_hash(uint32_t p_hash, uint32_t p_index) { … }
void _resize_and_rehash(uint32_t p_new_capacity) { … }
int32_t _insert_element(const TKey &p_key, const TValue &p_value, uint32_t p_hash) { … }
void _init_from(const AHashMap &p_other) { … }
public:
_FORCE_INLINE_ uint32_t get_capacity() const { … }
_FORCE_INLINE_ uint32_t size() const { … }
_FORCE_INLINE_ bool is_empty() const { … }
void clear() { … }
TValue &get(const TKey &p_key) { … }
const TValue &get(const TKey &p_key) const { … }
const TValue *getptr(const TKey &p_key) const { … }
TValue *getptr(const TKey &p_key) { … }
bool has(const TKey &p_key) const { … }
bool erase(const TKey &p_key) { … }
bool replace_key(const TKey &p_old_key, const TKey &p_new_key) { … }
void reserve(uint32_t p_new_capacity) { … }
struct ConstIterator {
_FORCE_INLINE_ const MapKeyValue &operator*() const {
return *pair;
}
_FORCE_INLINE_ const MapKeyValue *operator->() const {
return pair;
}
_FORCE_INLINE_ ConstIterator &operator++() {
pair++;
return *this;
}
_FORCE_INLINE_ ConstIterator &operator--() {
pair--;
if (pair < begin) {
pair = end;
}
return *this;
}
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return pair == b.pair; }
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return pair != b.pair; }
_FORCE_INLINE_ explicit operator bool() const {
return pair != end;
}
_FORCE_INLINE_ ConstIterator(MapKeyValue *p_key, MapKeyValue *p_begin, MapKeyValue *p_end) {
pair = p_key;
begin = p_begin;
end = p_end;
}
_FORCE_INLINE_ ConstIterator() {}
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) {
pair = p_it.pair;
begin = p_it.begin;
end = p_it.end;
}
_FORCE_INLINE_ void operator=(const ConstIterator &p_it) {
pair = p_it.pair;
begin = p_it.begin;
end = p_it.end;
}
private:
MapKeyValue *pair = nullptr;
MapKeyValue *begin = nullptr;
MapKeyValue *end = nullptr;
};
struct Iterator {
_FORCE_INLINE_ MapKeyValue &operator*() const {
return *pair;
}
_FORCE_INLINE_ MapKeyValue *operator->() const {
return pair;
}
_FORCE_INLINE_ Iterator &operator++() {
pair++;
return *this;
}
_FORCE_INLINE_ Iterator &operator--() {
pair--;
if (pair < begin) {
pair = end;
}
return *this;
}
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return pair == b.pair; }
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return pair != b.pair; }
_FORCE_INLINE_ explicit operator bool() const {
return pair != end;
}
_FORCE_INLINE_ Iterator(MapKeyValue *p_key, MapKeyValue *p_begin, MapKeyValue *p_end) {
pair = p_key;
begin = p_begin;
end = p_end;
}
_FORCE_INLINE_ Iterator() {}
_FORCE_INLINE_ Iterator(const Iterator &p_it) {
pair = p_it.pair;
begin = p_it.begin;
end = p_it.end;
}
_FORCE_INLINE_ void operator=(const Iterator &p_it) {
pair = p_it.pair;
begin = p_it.begin;
end = p_it.end;
}
operator ConstIterator() const {
return ConstIterator(pair, begin, end);
}
private:
MapKeyValue *pair = nullptr;
MapKeyValue *begin = nullptr;
MapKeyValue *end = nullptr;
};
_FORCE_INLINE_ Iterator begin() { … }
_FORCE_INLINE_ Iterator end() { … }
_FORCE_INLINE_ Iterator last() { … }
Iterator find(const TKey &p_key) { … }
void remove(const Iterator &p_iter) { … }
_FORCE_INLINE_ ConstIterator begin() const { … }
_FORCE_INLINE_ ConstIterator end() const { … }
_FORCE_INLINE_ ConstIterator last() const { … }
ConstIterator find(const TKey &p_key) const { … }
const TValue &operator[](const TKey &p_key) const { … }
TValue &operator[](const TKey &p_key) { … }
Iterator insert(const TKey &p_key, const TValue &p_value) { … }
void insert_new(const TKey &p_key, const TValue &p_value) { … }
KeyValue<TKey, TValue> *get_elements_ptr() { … }
int get_index(const TKey &p_key) { … }
KeyValue<TKey, TValue> &get_by_index(uint32_t p_index) { … }
bool erase_by_index(uint32_t p_index) { … }
AHashMap(const AHashMap &p_other) { … }
AHashMap(const HashMap<TKey, TValue> &p_other) { … }
void operator=(const AHashMap &p_other) { … }
void operator=(const HashMap<TKey, TValue> &p_other) { … }
AHashMap(uint32_t p_initial_capacity) { … }
AHashMap() : … { … }
void reset() { … }
~AHashMap() { … }
};
extern template class AHashMap<int, int>;
extern template class AHashMap<String, int>;
extern template class AHashMap<StringName, StringName>;
extern template class AHashMap<StringName, Variant>;
extern template class AHashMap<StringName, int>;
#endif