Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/metal/inflection_map.h
21318 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
#include <iterator>
37
38
/// An unordered map that splits elements between a fast-access vector of LinearCount consecutively
39
/// indexed elements, and a slower-access map holding sparse indexes larger than LinearCount.
40
///
41
/// \tparam KeyType is used to lookup values, and must be a type that is convertible to an unsigned integer.
42
/// \tparam ValueType must have an empty constructor (default or otherwise).
43
/// \tparam LinearCount
44
/// \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.
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
// Returns a reference to the value at the index.
107
// If the index has not been initialized, add an empty element at
108
// the end of the values array, and set the index to its position.
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
};
125
126