summaryrefslogtreecommitdiff
path: root/core/array.cpp
blob: 683a43e3d02dcbce88ac318c1787d10c461ce348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*************************************************************************/
/*  array.cpp                                                            */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 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.                */
/*************************************************************************/
#include "array.h"
#include "vector.h"
#include "hashfuncs.h"
#include "variant.h"
#include "object.h"

struct ArrayPrivate {

	SafeRefCount refcount;
	Vector<Variant> array;
	bool shared;
};

void Array::_ref(const Array& p_from) const {

	ArrayPrivate *_fp = p_from._p;

	ERR_FAIL_COND(!_fp); // should NOT happen.

	if (_fp == _p)
		return; //wathever it is, nothing to do here move along

	bool success = _fp->refcount.ref();

	ERR_FAIL_COND(!success); //should really not happen either

	_unref();

	if (_fp->shared) {

		_p = p_from._p;

	} else {

		_p = memnew( ArrayPrivate );
		_p->shared=false;
		_p->refcount.init();
		_p->array=_fp->array;

		if (_fp->refcount.unref())
			memdelete(_fp);
	}
}

void Array::_unref() const {

	if (!_p)
		return;

	if (_p->refcount.unref()) {
		memdelete(_p);
	}
	_p=NULL;
}


Variant& Array::operator[](int p_idx) {

	return _p->array[p_idx];
}

const Variant& Array::operator[](int p_idx) const {

	return _p->array[p_idx];

}

int Array::size() const {

	return _p->array.size();
}
bool Array::empty() const {

	return _p->array.empty();
}
void Array::clear() {

	_p->array.clear();
}

bool Array::is_shared() const {

    return _p->shared;
}

bool Array::operator==(const Array& p_array) const {

	return _p==p_array._p;
}

uint32_t Array::hash() const {

	uint32_t h=hash_djb2_one_32(0);

	for (int i=0;i<_p->array.size();i++) {

		h = hash_djb2_one_32( _p->array[i].hash(), h);
	}
	return h;
}
void Array::operator=(const Array& p_array) {

	_ref(p_array);
}
void Array::push_back(const Variant& p_value) {

	_p->array.push_back(p_value);
}

Error Array::resize(int p_new_size) {

	return _p->array.resize(p_new_size);
}

void Array::insert(int p_pos, const Variant& p_value) {

	_p->array.insert(p_pos,p_value);
}

void Array::erase(const Variant& p_value) {

	_p->array.erase(p_value);
}

int Array::find(const Variant& p_value, int p_from) const {

	return _p->array.find(p_value, p_from);
}

int Array::rfind(const Variant& p_value, int p_from) const {

	if (_p->array.size() == 0)
		return -1;

	if (p_from < 0) {
		// Relative offset from the end
		p_from = _p->array.size() + p_from;
	}
	if (p_from < 0 || p_from >= _p->array.size()) {
		// Limit to array boundaries
		p_from = _p->array.size() - 1;
	}

	for (int i=p_from; i>=0; i--) {

		if(_p->array[i] == p_value){
			return i;
		};
	};

	return -1;
}

int Array::find_last(const Variant& p_value) const {

	return rfind(p_value);
}

int Array::count(const Variant& p_value) const {

	if(_p->array.size() == 0)
		return 0;

	int amount=0;
	for (int i=0; i<_p->array.size(); i++) {

		if(_p->array[i] == p_value){
			amount++;
		};
	};

	return amount;
}

bool Array::has(const Variant& p_value) const {
	return _p->array.find(p_value, 0) != -1;
}

void Array::remove(int p_pos) {

	_p->array.remove(p_pos);
}


void Array::set(int p_idx,const Variant& p_value) {

	operator[](p_idx)=p_value;
}

const Variant& Array::get(int p_idx) const {

	return operator[](p_idx);
}

struct _ArrayVariantSort {

	_FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {
		bool valid=false;
		Variant res;
		Variant::evaluate(Variant::OP_LESS,p_l,p_r,res,valid);
		if (!valid)
			res=false;
		return res;
	}
};

void Array::sort() {

	_p->array.sort_custom<_ArrayVariantSort>();

}

struct _ArrayVariantSortCustom {

	Object *obj;
	StringName func;

	_FORCE_INLINE_ bool operator()(const Variant& p_l, const Variant& p_r) const {

		const Variant*args[2]={&p_l,&p_r};
		Variant::CallError err;
		bool res = obj->call(func,args,2,err);
		if (err.error!=Variant::CallError::CALL_OK)
			res=false;
		return res;

	}
};
void Array::sort_custom(Object *p_obj,const StringName& p_function){

	ERR_FAIL_NULL(p_obj);

	SortArray<Variant,_ArrayVariantSortCustom> avs;
	avs.compare.obj=p_obj;
	avs.compare.func=p_function;
	avs.sort(_p->array.ptr(),_p->array.size());

}

void Array::invert(){

	_p->array.invert();
}


void Array::push_front(const Variant& p_value) {

	_p->array.insert(0,p_value);
}

Variant Array::pop_back(){

	if (!_p->array.empty()) {
		int n = _p->array.size() - 1;
		Variant ret = _p->array.get(n);
		_p->array.resize(n);
		return ret;
	}
	return Variant();

}

Variant Array::pop_front(){

	if (!_p->array.empty()) {
		Variant ret = _p->array.get(0);
		_p->array.remove(0);
		return ret;
	}
	return Variant();

}


Array::Array(const Array& p_from) {

	_p=NULL;
	_ref(p_from);

}
Array::Array(bool p_shared) {

	_p = memnew( ArrayPrivate );
	_p->refcount.init();
	_p->shared=p_shared;
}
Array::~Array() {

	_unref();
}