| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/hash_map.h" |
| 34 |
#include "core/templates/local_vector.h" |
| 35 |
|
| 36 |
#include <iterator> |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
template <typename KeyType, typename ValueType, size_t LinearCount, typename IndexType = uint16_t> |
| 46 |
class InflectionMap { |
| 47 |
public: |
| 48 |
using value_type = ValueType; |
| 49 |
class Iterator { |
| 50 |
InflectionMap *map; |
| 51 |
IndexType index; |
| 52 |
|
| 53 |
public: |
| 54 |
using iterator_category = std::forward_iterator_tag; |
| 55 |
using value_type = ValueType; |
| 56 |
using pointer = value_type *; |
| 57 |
using reference = value_type &; |
| 58 |
|
| 59 |
Iterator() : |
| 60 |
map(nullptr), index(0) {} |
| 61 |
Iterator(InflectionMap &p_m, const IndexType p_i) : |
| 62 |
map(&p_m), index(p_i) {} |
| 63 |
|
| 64 |
Iterator &operator=(const Iterator &p_it) { |
| 65 |
map = p_it.map; |
| 66 |
index = p_it.index; |
| 67 |
return *this; |
| 68 |
} |
| 69 |
|
| 70 |
ValueType *operator->() { return &map->_values[index]; } |
| 71 |
ValueType &operator*() { return map->_values[index]; } |
| 72 |
operator ValueType *() { return &map->_values[index]; } |
| 73 |
|
| 74 |
bool operator==(const Iterator &p_it) const { return map == p_it.map && index == p_it.index; } |
| 75 |
bool operator!=(const Iterator &p_it) const { return map != p_it.map || index != p_it.index; } |
| 76 |
|
| 77 |
Iterator &operator++() { |
| 78 |
index++; |
| 79 |
return *this; |
| 80 |
} |
| 81 |
Iterator operator++(int) { |
| 82 |
Iterator t = *this; |
| 83 |
index++; |
| 84 |
return t; |
| 85 |
} |
| 86 |
|
| 87 |
bool is_valid() const { return index < map->_values.size(); } |
| 88 |
}; |
| 89 |
|
| 90 |
const ValueType &operator[](const KeyType p_idx) const { return get_value(p_idx); } |
| 91 |
ValueType &operator[](const KeyType p_idx) { return get_value(p_idx); } |
| 92 |
|
| 93 |
Iterator begin() { return Iterator(*this, 0); } |
| 94 |
Iterator end() { return Iterator(*this, _values.size()); } |
| 95 |
|
| 96 |
bool is_empty() { return _values.is_empty(); } |
| 97 |
size_t size() { return _values.size(); } |
| 98 |
void reserve(size_t p_new_cap) { _values.reserve(p_new_cap); } |
| 99 |
|
| 100 |
protected: |
| 101 |
static constexpr IndexType INVALID = std::numeric_limits<IndexType>::max(); |
| 102 |
typedef struct IndexValue { |
| 103 |
IndexType value = INVALID; |
| 104 |
} IndexValue; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
ValueType &get_value(KeyType p_idx) { |
| 110 |
IndexValue *val_idx = p_idx < LinearCount ? &_linear_indexes[p_idx] : _inflection_indexes.getptr(p_idx); |
| 111 |
if (val_idx == nullptr || val_idx->value == INVALID) { |
| 112 |
_values.push_back({}); |
| 113 |
if (val_idx == nullptr) { |
| 114 |
val_idx = &_inflection_indexes.insert(p_idx, {})->value; |
| 115 |
} |
| 116 |
val_idx->value = _values.size() - 1; |
| 117 |
} |
| 118 |
return _values[val_idx->value]; |
| 119 |
} |
| 120 |
|
| 121 |
TightLocalVector<ValueType> _values; |
| 122 |
HashMap<KeyType, IndexValue> _inflection_indexes; |
| 123 |
IndexValue _linear_indexes[LinearCount]; |
| 124 |
}; |