Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/array.h
20830 views
1
/**************************************************************************/
2
/* array.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
#include "core/typedefs.h"
35
#include "core/variant/variant_deep_duplicate.h"
36
37
#include <climits>
38
#include <initializer_list>
39
40
class Callable;
41
class StringName;
42
class Variant;
43
44
struct ArrayPrivate;
45
struct ContainerType;
46
47
class Array {
48
mutable ArrayPrivate *_p;
49
void _ref(const Array &p_from) const;
50
void _unref() const;
51
52
public:
53
struct ConstIterator {
54
_FORCE_INLINE_ const Variant &operator*() const { return *element_ptr; }
55
_FORCE_INLINE_ const Variant *operator->() const { return element_ptr; }
56
57
_FORCE_INLINE_ ConstIterator &operator++();
58
_FORCE_INLINE_ ConstIterator &operator--();
59
60
_FORCE_INLINE_ bool operator==(const ConstIterator &p_other) const { return element_ptr == p_other.element_ptr; }
61
_FORCE_INLINE_ bool operator!=(const ConstIterator &p_other) const { return element_ptr != p_other.element_ptr; }
62
63
ConstIterator() = default;
64
65
_FORCE_INLINE_ ConstIterator(const Variant *p_element_ptr) :
66
element_ptr(p_element_ptr) {}
67
68
private:
69
const Variant *element_ptr = nullptr;
70
};
71
static_assert(std::is_trivially_copyable_v<ConstIterator>, "ConstIterator must be trivially copyable");
72
73
struct Iterator {
74
_FORCE_INLINE_ Variant &operator*() const;
75
_FORCE_INLINE_ Variant *operator->() const;
76
77
_FORCE_INLINE_ Iterator &operator++();
78
_FORCE_INLINE_ Iterator &operator--();
79
80
_FORCE_INLINE_ bool operator==(const Iterator &p_other) const { return element_ptr == p_other.element_ptr; }
81
_FORCE_INLINE_ bool operator!=(const Iterator &p_other) const { return element_ptr != p_other.element_ptr; }
82
83
_FORCE_INLINE_ operator ConstIterator() const { return ConstIterator(element_ptr); }
84
85
Iterator() = default;
86
87
_FORCE_INLINE_ Iterator(Variant *p_element_ptr, Variant *p_read_only = nullptr) :
88
element_ptr(p_element_ptr), read_only(p_read_only) {}
89
90
private:
91
Variant *element_ptr = nullptr;
92
Variant *read_only = nullptr;
93
};
94
static_assert(std::is_trivially_copyable_v<Iterator>, "Iterator must be trivially copyable");
95
96
Iterator begin();
97
Iterator end();
98
99
ConstIterator begin() const;
100
ConstIterator end() const;
101
102
Variant &operator[](int p_idx);
103
const Variant &operator[](int p_idx) const;
104
105
void set(int p_idx, const Variant &p_value);
106
const Variant &get(int p_idx) const;
107
108
int size() const;
109
bool is_empty() const;
110
void clear();
111
112
bool operator==(const Array &p_array) const;
113
bool operator!=(const Array &p_array) const;
114
bool recursive_equal(const Array &p_array, int recursion_count) const;
115
116
uint32_t hash() const;
117
uint32_t recursive_hash(int recursion_count) const;
118
void operator=(const Array &p_array);
119
120
void assign(const Array &p_array);
121
void push_back(const Variant &p_value);
122
_FORCE_INLINE_ void append(const Variant &p_value) { push_back(p_value); } //for python compatibility
123
void append_array(const Array &p_array);
124
Error resize(int p_new_size);
125
Error reserve(int p_new_size);
126
127
Error insert(int p_pos, const Variant &p_value);
128
void remove_at(int p_pos);
129
void fill(const Variant &p_value);
130
131
Variant front() const;
132
Variant back() const;
133
Variant pick_random() const;
134
135
void sort();
136
void sort_custom(const Callable &p_callable);
137
void shuffle();
138
int bsearch(const Variant &p_value, bool p_before = true) const;
139
int bsearch_custom(const Variant &p_value, const Callable &p_callable, bool p_before = true) const;
140
void reverse();
141
142
int find(const Variant &p_value, int p_from = 0) const;
143
int find_custom(const Callable &p_callable, int p_from = 0) const;
144
int rfind(const Variant &p_value, int p_from = -1) const;
145
int rfind_custom(const Callable &p_callable, int p_from = -1) const;
146
int count(const Variant &p_value) const;
147
bool has(const Variant &p_value) const;
148
149
void erase(const Variant &p_value);
150
151
void push_front(const Variant &p_value);
152
Variant pop_back();
153
Variant pop_front();
154
Variant pop_at(int p_pos);
155
156
Array duplicate(bool p_deep = false) const;
157
Array duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
158
Array recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const;
159
160
Array slice(int p_begin, int p_end = INT_MAX, int p_step = 1, bool p_deep = false) const;
161
Array filter(const Callable &p_callable) const;
162
Array map(const Callable &p_callable) const;
163
Variant reduce(const Callable &p_callable, const Variant &p_accum) const;
164
bool any(const Callable &p_callable) const;
165
bool all(const Callable &p_callable) const;
166
167
bool operator<(const Array &p_array) const;
168
bool operator<=(const Array &p_array) const;
169
bool operator>(const Array &p_array) const;
170
bool operator>=(const Array &p_array) const;
171
172
Variant min() const;
173
Variant max() const;
174
175
const void *id() const;
176
177
void set_typed(const ContainerType &p_element_type);
178
void set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
179
180
bool is_typed() const;
181
bool is_same_typed(const Array &p_other) const;
182
bool is_same_instance(const Array &p_other) const;
183
184
ContainerType get_element_type() const;
185
uint32_t get_typed_builtin() const;
186
StringName get_typed_class_name() const;
187
Variant get_typed_script() const;
188
189
void make_read_only();
190
bool is_read_only() const;
191
static Array create_read_only();
192
193
Span<Variant> span() const;
194
operator Span<Variant>() const {
195
return this->span();
196
}
197
198
Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
199
Array(const Array &p_from);
200
Array(std::initializer_list<Variant> p_init);
201
Array();
202
~Array();
203
};
204
205