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