Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/inflection_map.h
9973 views
1
/**************************************************************************/
2
/* inflection_map.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/templates/hash_map.h"
34
#include "core/templates/local_vector.h"
35
36
/// An unordered map that splits elements between a fast-access vector of LinearCount consecutively
37
/// indexed elements, and a slower-access map holding sparse indexes larger than LinearCount.
38
///
39
/// \tparam KeyType is used to lookup values, and must be a type that is convertible to an unsigned integer.
40
/// \tparam ValueType must have an empty constructor (default or otherwise).
41
/// \tparam LinearCount
42
/// \tparam IndexType must be a type that is convertible to an unsigned integer (eg. uint8_t...uint64_t), and which is large enough to represent the number of values in this map.
43
template <typename KeyType, typename ValueType, size_t LinearCount, typename IndexType = uint16_t>
44
class InflectionMap {
45
public:
46
using value_type = ValueType;
47
class Iterator {
48
InflectionMap *map;
49
IndexType index;
50
51
public:
52
using iterator_category = std::forward_iterator_tag;
53
using value_type = ValueType;
54
using pointer = value_type *;
55
using reference = value_type &;
56
57
Iterator() :
58
map(nullptr), index(0) {}
59
Iterator(InflectionMap &p_m, const IndexType p_i) :
60
map(&p_m), index(p_i) {}
61
62
Iterator &operator=(const Iterator &p_it) {
63
map = p_it.map;
64
index = p_it.index;
65
return *this;
66
}
67
68
ValueType *operator->() { return &map->_values[index]; }
69
ValueType &operator*() { return map->_values[index]; }
70
operator ValueType *() { return &map->_values[index]; }
71
72
bool operator==(const Iterator &p_it) const { return map == p_it.map && index == p_it.index; }
73
bool operator!=(const Iterator &p_it) const { return map != p_it.map || index != p_it.index; }
74
75
Iterator &operator++() {
76
index++;
77
return *this;
78
}
79
Iterator operator++(int) {
80
Iterator t = *this;
81
index++;
82
return t;
83
}
84
85
bool is_valid() const { return index < map->_values.size(); }
86
};
87
88
const ValueType &operator[](const KeyType p_idx) const { return get_value(p_idx); }
89
ValueType &operator[](const KeyType p_idx) { return get_value(p_idx); }
90
91
Iterator begin() { return Iterator(*this, 0); }
92
Iterator end() { return Iterator(*this, _values.size()); }
93
94
bool is_empty() { return _values.is_empty(); }
95
size_t size() { return _values.size(); }
96
void reserve(size_t p_new_cap) { _values.reserve(p_new_cap); }
97
98
protected:
99
static constexpr IndexType INVALID = std::numeric_limits<IndexType>::max();
100
typedef struct IndexValue {
101
IndexType value = INVALID;
102
} IndexValue;
103
104
// Returns a reference to the value at the index.
105
// If the index has not been initialized, add an empty element at
106
// the end of the values array, and set the index to its position.
107
ValueType &get_value(KeyType p_idx) {
108
IndexValue *val_idx = p_idx < LinearCount ? &_linear_indexes[p_idx] : _inflection_indexes.getptr(p_idx);
109
if (val_idx == nullptr || val_idx->value == INVALID) {
110
_values.push_back({});
111
if (val_idx == nullptr) {
112
val_idx = &_inflection_indexes.insert(p_idx, {})->value;
113
}
114
val_idx->value = _values.size() - 1;
115
}
116
return _values[val_idx->value];
117
}
118
119
TightLocalVector<ValueType> _values;
120
HashMap<KeyType, IndexValue> _inflection_indexes;
121
IndexValue _linear_indexes[LinearCount];
122
};
123
124