2020-05-01 14:34:23 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* local_vector.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef LOCAL_VECTOR_H
|
|
|
|
#define LOCAL_VECTOR_H
|
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/error/error_macros.h"
|
2020-05-01 14:34:23 +02:00
|
|
|
#include "core/os/memory.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/templates/sort_array.h"
|
|
|
|
#include "core/templates/vector.h"
|
2020-05-01 14:34:23 +02:00
|
|
|
|
2021-07-15 18:28:05 +02:00
|
|
|
#include <initializer_list>
|
2022-08-04 13:53:03 +02:00
|
|
|
#include <type_traits>
|
2021-07-15 18:28:05 +02:00
|
|
|
|
2022-05-16 18:52:39 +02:00
|
|
|
// If tight, it grows strictly as much as needed.
|
|
|
|
// Otherwise, it grows exponentially (the default and what you want in most cases).
|
|
|
|
template <class T, class U = uint32_t, bool force_trivial = false, bool tight = false>
|
2020-05-01 14:34:23 +02:00
|
|
|
class LocalVector {
|
|
|
|
private:
|
|
|
|
U count = 0;
|
|
|
|
U capacity = 0;
|
|
|
|
T *data = nullptr;
|
|
|
|
|
|
|
|
public:
|
2020-06-25 15:33:28 +02:00
|
|
|
T *ptr() {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *ptr() const {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
_FORCE_INLINE_ void push_back(T p_elem) {
|
|
|
|
if (unlikely(count == capacity)) {
|
2022-08-08 20:54:17 +02:00
|
|
|
capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1);
|
2020-05-01 14:34:23 +02:00
|
|
|
data = (T *)memrealloc(data, capacity * sizeof(T));
|
|
|
|
CRASH_COND_MSG(!data, "Out of memory");
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:43:21 +01:00
|
|
|
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
|
2020-05-01 14:34:23 +02:00
|
|
|
memnew_placement(&data[count++], T(p_elem));
|
|
|
|
} else {
|
|
|
|
data[count++] = p_elem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 00:17:03 +02:00
|
|
|
void remove_at(U p_index) {
|
2020-05-01 14:34:23 +02:00
|
|
|
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
|
2020-07-31 13:49:15 +02:00
|
|
|
count--;
|
2020-05-01 14:34:23 +02:00
|
|
|
for (U i = p_index; i < count; i++) {
|
|
|
|
data[i] = data[i + 1];
|
|
|
|
}
|
2024-02-02 15:43:21 +01:00
|
|
|
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
|
2020-05-01 14:34:23 +02:00
|
|
|
data[count].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 13:25:55 +01:00
|
|
|
/// Removes the item copying the last value into the position of the one to
|
2022-08-08 20:54:17 +02:00
|
|
|
/// remove. It's generally faster than `remove_at`.
|
2021-07-04 00:17:03 +02:00
|
|
|
void remove_at_unordered(U p_index) {
|
2021-01-11 13:25:55 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, count);
|
|
|
|
count--;
|
|
|
|
if (count > p_index) {
|
|
|
|
data[p_index] = data[count];
|
|
|
|
}
|
2024-02-02 15:43:21 +01:00
|
|
|
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
|
2021-01-11 13:25:55 +01:00
|
|
|
data[count].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:45:53 +02:00
|
|
|
_FORCE_INLINE_ bool erase(const T &p_val) {
|
2020-05-27 15:39:51 +02:00
|
|
|
int64_t idx = find(p_val);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (idx >= 0) {
|
2021-07-04 00:17:03 +02:00
|
|
|
remove_at(idx);
|
2023-04-10 18:45:53 +02:00
|
|
|
return true;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-04-10 18:45:53 +02:00
|
|
|
return false;
|
2020-05-01 14:34:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void invert() {
|
|
|
|
for (U i = 0; i < count / 2; i++) {
|
|
|
|
SWAP(data[i], data[count - i - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void clear() { resize(0); }
|
|
|
|
_FORCE_INLINE_ void reset() {
|
|
|
|
clear();
|
|
|
|
if (data) {
|
|
|
|
memfree(data);
|
|
|
|
data = nullptr;
|
|
|
|
capacity = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 13:04:21 +01:00
|
|
|
_FORCE_INLINE_ bool is_empty() const { return count == 0; }
|
2021-01-11 13:25:55 +01:00
|
|
|
_FORCE_INLINE_ U get_capacity() const { return capacity; }
|
2020-05-01 14:34:23 +02:00
|
|
|
_FORCE_INLINE_ void reserve(U p_size) {
|
2022-05-16 18:52:39 +02:00
|
|
|
p_size = tight ? p_size : nearest_power_of_2_templated(p_size);
|
2020-05-01 14:34:23 +02:00
|
|
|
if (p_size > capacity) {
|
|
|
|
capacity = p_size;
|
|
|
|
data = (T *)memrealloc(data, capacity * sizeof(T));
|
|
|
|
CRASH_COND_MSG(!data, "Out of memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ U size() const { return count; }
|
|
|
|
void resize(U p_size) {
|
|
|
|
if (p_size < count) {
|
2024-02-02 15:43:21 +01:00
|
|
|
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
|
2020-05-01 14:34:23 +02:00
|
|
|
for (U i = p_size; i < count; i++) {
|
|
|
|
data[i].~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = p_size;
|
|
|
|
} else if (p_size > count) {
|
|
|
|
if (unlikely(p_size > capacity)) {
|
2022-08-08 20:54:17 +02:00
|
|
|
capacity = tight ? p_size : nearest_power_of_2_templated(p_size);
|
2020-05-01 14:34:23 +02:00
|
|
|
data = (T *)memrealloc(data, capacity * sizeof(T));
|
|
|
|
CRASH_COND_MSG(!data, "Out of memory");
|
|
|
|
}
|
2024-02-02 15:43:21 +01:00
|
|
|
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
|
2020-05-01 14:34:23 +02:00
|
|
|
for (U i = count; i < p_size; i++) {
|
|
|
|
memnew_placement(&data[i], T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = p_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const T &operator[](U p_index) const {
|
|
|
|
CRASH_BAD_UNSIGNED_INDEX(p_index, count);
|
|
|
|
return data[p_index];
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ T &operator[](U p_index) {
|
|
|
|
CRASH_BAD_UNSIGNED_INDEX(p_index, count);
|
|
|
|
return data[p_index];
|
|
|
|
}
|
|
|
|
|
2022-12-29 01:24:14 +01:00
|
|
|
struct Iterator {
|
|
|
|
_FORCE_INLINE_ T &operator*() const {
|
|
|
|
return *elem_ptr;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ T *operator->() const { return elem_ptr; }
|
|
|
|
_FORCE_INLINE_ Iterator &operator++() {
|
|
|
|
elem_ptr++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator &operator--() {
|
|
|
|
elem_ptr--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
|
|
|
|
|
|
|
|
Iterator(T *p_ptr) { elem_ptr = p_ptr; }
|
|
|
|
Iterator() {}
|
|
|
|
Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *elem_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstIterator {
|
|
|
|
_FORCE_INLINE_ const T &operator*() const {
|
|
|
|
return *elem_ptr;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator++() {
|
|
|
|
elem_ptr++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator--() {
|
|
|
|
elem_ptr--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
|
|
|
|
|
|
|
|
ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
|
|
|
|
ConstIterator() {}
|
|
|
|
ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T *elem_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Iterator begin() {
|
|
|
|
return Iterator(data);
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator end() {
|
|
|
|
return Iterator(data + size());
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ ConstIterator begin() const {
|
|
|
|
return ConstIterator(ptr());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator end() const {
|
|
|
|
return ConstIterator(ptr() + size());
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
void insert(U p_pos, T p_val) {
|
|
|
|
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
|
|
|
|
if (p_pos == count) {
|
|
|
|
push_back(p_val);
|
|
|
|
} else {
|
|
|
|
resize(count + 1);
|
2021-08-26 11:54:56 +02:00
|
|
|
for (U i = count - 1; i > p_pos; i--) {
|
2020-05-01 14:34:23 +02:00
|
|
|
data[i] = data[i - 1];
|
|
|
|
}
|
|
|
|
data[p_pos] = p_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t find(const T &p_val, U p_from = 0) const {
|
2021-06-29 13:37:02 +02:00
|
|
|
for (U i = p_from; i < count; i++) {
|
2020-05-01 14:34:23 +02:00
|
|
|
if (data[i] == p_val) {
|
|
|
|
return int64_t(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class C>
|
|
|
|
void sort_custom() {
|
|
|
|
U len = count;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (len == 0) {
|
2020-05-01 14:34:23 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-05-01 14:34:23 +02:00
|
|
|
|
|
|
|
SortArray<T, C> sorter;
|
|
|
|
sorter.sort(data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sort() {
|
|
|
|
sort_custom<_DefaultComparator<T>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ordered_insert(T p_val) {
|
|
|
|
U i;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (p_val < data[i]) {
|
|
|
|
break;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 14:34:23 +02:00
|
|
|
insert(i, p_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Vector<T>() const {
|
|
|
|
Vector<T> ret;
|
|
|
|
ret.resize(size());
|
|
|
|
T *w = ret.ptrw();
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(w, data, sizeof(T) * count);
|
2020-05-01 14:34:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
|
|
|
|
Vector<uint8_t> ret;
|
|
|
|
ret.resize(count * sizeof(T));
|
|
|
|
uint8_t *w = ret.ptrw();
|
2021-04-27 16:19:21 +02:00
|
|
|
memcpy(w, data, sizeof(T) * count);
|
2020-05-01 14:34:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ LocalVector() {}
|
2021-07-15 18:28:05 +02:00
|
|
|
_FORCE_INLINE_ LocalVector(std::initializer_list<T> p_init) {
|
|
|
|
reserve(p_init.size());
|
|
|
|
for (const T &element : p_init) {
|
|
|
|
push_back(element);
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 14:34:23 +02:00
|
|
|
_FORCE_INLINE_ LocalVector(const LocalVector &p_from) {
|
|
|
|
resize(p_from.size());
|
|
|
|
for (U i = 0; i < p_from.count; i++) {
|
|
|
|
data[i] = p_from.data[i];
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 15:19:26 +01:00
|
|
|
inline void operator=(const LocalVector &p_from) {
|
2020-05-01 14:34:23 +02:00
|
|
|
resize(p_from.size());
|
|
|
|
for (U i = 0; i < p_from.count; i++) {
|
|
|
|
data[i] = p_from.data[i];
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 15:19:26 +01:00
|
|
|
inline void operator=(const Vector<T> &p_from) {
|
2020-05-01 14:34:23 +02:00
|
|
|
resize(p_from.size());
|
|
|
|
for (U i = 0; i < count; i++) {
|
|
|
|
data[i] = p_from[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ ~LocalVector() {
|
|
|
|
if (data) {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-16 18:52:39 +02:00
|
|
|
template <class T, class U = uint32_t, bool force_trivial = false>
|
|
|
|
using TightLocalVector = LocalVector<T, U, force_trivial, true>;
|
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
#endif // LOCAL_VECTOR_H
|