Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/fixed_vector.h
9973 views
1
/**************************************************************************/
2
/* fixed_vector.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/span.h"
34
35
/**
36
* A high performance Vector of fixed capacity.
37
* Especially useful if you need to create an array on the stack, to
38
* prevent dynamic allocations (especially in bottleneck code).
39
*
40
* Choose CAPACITY such that it is enough for all elements that could be added through all branches.
41
*
42
*/
43
template <class T, uint32_t CAPACITY>
44
class FixedVector {
45
// This declaration allows us to access other FixedVector's private members.
46
template <class T_, uint32_t CAPACITY_>
47
friend class FixedVector;
48
49
uint32_t _size = 0;
50
alignas(T) uint8_t _data[CAPACITY * sizeof(T)];
51
52
constexpr static uint32_t DATA_PADDING = MAX(alignof(T), alignof(uint32_t)) - alignof(uint32_t);
53
54
public:
55
_FORCE_INLINE_ constexpr FixedVector() = default;
56
constexpr FixedVector(std::initializer_list<T> p_init) {
57
ERR_FAIL_COND(p_init.size() > CAPACITY);
58
for (const T &element : p_init) {
59
memnew_placement(ptr() + _size++, T(element));
60
}
61
}
62
63
template <uint32_t p_capacity>
64
constexpr FixedVector(const FixedVector<T, p_capacity> &p_from) {
65
ERR_FAIL_COND(p_from.size() > CAPACITY);
66
if constexpr (std::is_trivially_copyable_v<T>) {
67
// Copy size and all provided elements at once.
68
memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
69
} else {
70
for (const T &element : p_from) {
71
memnew_placement(ptr() + _size++, T(element));
72
}
73
}
74
}
75
76
template <uint32_t p_capacity>
77
constexpr FixedVector(FixedVector<T, p_capacity> &&p_from) {
78
ERR_FAIL_COND(p_from.size() > CAPACITY);
79
// Copy size and all provided elements at once.
80
// Note: Assumes trivial relocatability.
81
memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
82
p_from._size = 0;
83
}
84
85
~FixedVector() {
86
if constexpr (!std::is_trivially_destructible_v<T>) {
87
for (uint32_t i = 0; i < _size; i++) {
88
ptr()[i].~T();
89
}
90
}
91
}
92
93
_FORCE_INLINE_ constexpr T *ptr() { return (T *)(_data); }
94
_FORCE_INLINE_ constexpr const T *ptr() const { return (const T *)(_data); }
95
96
_FORCE_INLINE_ constexpr operator Span<T>() const { return Span<T>(ptr(), size()); }
97
_FORCE_INLINE_ constexpr Span<T> span() const { return operator Span<T>(); }
98
99
_FORCE_INLINE_ constexpr uint32_t size() const { return _size; }
100
_FORCE_INLINE_ constexpr bool is_empty() const { return !_size; }
101
_FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; }
102
_FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; }
103
104
_FORCE_INLINE_ constexpr void clear() { resize_initialized(0); }
105
106
/// Changes the size of the vector.
107
/// If p_size > size(), constructs new elements.
108
/// If p_size < size(), destructs new elements.
109
constexpr Error resize_initialized(uint32_t p_size) {
110
if (p_size > _size) {
111
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
112
memnew_arr_placement(ptr() + _size, p_size - _size);
113
} else if (p_size < _size) {
114
if constexpr (!std::is_trivially_destructible_v<T>) {
115
for (uint32_t i = p_size; i < _size; i++) {
116
ptr()[i].~T();
117
}
118
}
119
}
120
121
_size = p_size;
122
return OK;
123
}
124
125
/// Changes the size of the vector.
126
/// The initializer of new elements is skipped, making this function faster than resize_initialized.
127
/// The caller is required to initialize the new values.
128
constexpr Error resize_uninitialized(uint32_t p_size) {
129
static_assert(std::is_trivially_destructible_v<T>, "resize_uninitialized is unsafe to call if T is not trivially destructible.");
130
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
131
_size = p_size;
132
return OK;
133
}
134
135
constexpr void push_back(const T &p_val) {
136
ERR_FAIL_COND(_size >= CAPACITY);
137
memnew_placement(ptr() + _size, T(p_val));
138
_size++;
139
}
140
141
constexpr void pop_back() {
142
ERR_FAIL_COND(_size == 0);
143
_size--;
144
ptr()[_size].~T();
145
}
146
147
// NOTE: Subscripts sanity check the bounds to avoid undefined behavior.
148
// This is slower than direct buffer access and can prevent autovectorization.
149
// If the bounds are known, use ptr() subscript instead.
150
constexpr const T &operator[](uint32_t p_index) const {
151
CRASH_COND(p_index >= _size);
152
return ptr()[p_index];
153
}
154
155
constexpr T &operator[](uint32_t p_index) {
156
CRASH_COND(p_index >= _size);
157
return ptr()[p_index];
158
}
159
160
_FORCE_INLINE_ constexpr T *begin() { return ptr(); }
161
_FORCE_INLINE_ constexpr T *end() { return ptr() + _size; }
162
163
_FORCE_INLINE_ constexpr const T *begin() const { return ptr(); }
164
_FORCE_INLINE_ constexpr const T *end() const { return ptr() + _size; }
165
};
166
167