2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* array.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
2020-01-01 10:16:22 +00:00
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 01:10:30 +00:00
/* */
/* 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. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2014-02-10 01:10:30 +00:00
# include "array.h"
2017-01-16 07:04:19 +00:00
2020-04-20 22:06:00 +00:00
# include "container_type_validate.h"
2018-09-11 16:13:45 +00:00
# include "core/hashfuncs.h"
# include "core/object.h"
2020-04-20 22:06:00 +00:00
# include "core/script_language.h"
2018-09-11 16:13:45 +00:00
# include "core/variant.h"
# include "core/vector.h"
2014-02-10 01:10:30 +00:00
2018-03-09 19:16:08 +00:00
class ArrayPrivate {
public :
2014-02-10 01:10:30 +00:00
SafeRefCount refcount ;
Vector < Variant > array ;
2020-04-20 22:06:00 +00:00
ContainerTypeValidate typed ;
2014-02-10 01:10:30 +00:00
} ;
2017-03-05 15:44:50 +00:00
void Array : : _ref ( const Array & p_from ) const {
2014-02-10 01:10:30 +00:00
ArrayPrivate * _fp = p_from . _p ;
ERR_FAIL_COND ( ! _fp ) ; // should NOT happen.
if ( _fp = = _p )
2017-09-22 03:58:29 +00:00
return ; // whatever it is, nothing to do here move along
2014-02-10 01:10:30 +00:00
bool success = _fp - > refcount . ref ( ) ;
2017-09-22 03:58:29 +00:00
ERR_FAIL_COND ( ! success ) ; // should really not happen either
2014-02-10 01:10:30 +00:00
_unref ( ) ;
2017-01-11 11:53:31 +00:00
_p = p_from . _p ;
2014-02-10 01:10:30 +00:00
}
void Array : : _unref ( ) const {
if ( ! _p )
return ;
if ( _p - > refcount . unref ( ) ) {
memdelete ( _p ) ;
}
2020-04-01 23:20:12 +00:00
_p = nullptr ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
Variant & Array : : operator [ ] ( int p_idx ) {
2014-02-10 01:10:30 +00:00
2018-07-25 01:11:03 +00:00
return _p - > array . write [ p_idx ] ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
const Variant & Array : : operator [ ] ( int p_idx ) const {
2014-02-10 01:10:30 +00:00
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 ( ) ;
}
2017-03-05 15:44:50 +00:00
bool Array : : operator = = ( const Array & p_array ) const {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
return _p = = p_array . _p ;
2014-02-10 01:10:30 +00:00
}
uint32_t Array : : hash ( ) const {
2017-03-05 15:44:50 +00:00
uint32_t h = hash_djb2_one_32 ( 0 ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < _p - > array . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
h = hash_djb2_one_32 ( _p - > array [ i ] . hash ( ) , h ) ;
2014-02-10 01:10:30 +00:00
}
return h ;
}
2020-04-20 22:06:00 +00:00
void Array : : _assign ( const Array & p_array ) {
if ( _p - > typed . type ! = Variant : : OBJECT & & _p - > typed . type = = p_array . _p - > typed . type ) {
//same type or untyped, just reference, shuold be fine
_ref ( p_array ) ;
} else if ( _p - > typed . type = = Variant : : NIL ) { //from typed to untyped, must copy, but this is cheap anyway
_p - > array = p_array . _p - > array ;
} else if ( p_array . _p - > typed . type = = Variant : : NIL ) { //from untyped to typed, must try to check if they are all valid
2020-04-21 15:16:45 +00:00
if ( _p - > typed . type = = Variant : : OBJECT ) {
//for objects, it needs full validation, either can be converted or fail
for ( int i = 0 ; i < p_array . _p - > array . size ( ) ; i + + ) {
if ( ! _p - > typed . validate ( p_array . _p - > array [ i ] , " assign " ) ) {
return ;
}
2020-04-20 22:06:00 +00:00
}
2020-04-21 15:16:45 +00:00
_p - > array = p_array . _p - > array ; //then just copy, which is cheap anyway
} else {
//for non objects, we need to check if there is a valid conversion, which needs to happen one by one, so this is the worst case.
Vector < Variant > new_array ;
new_array . resize ( p_array . _p - > array . size ( ) ) ;
for ( int i = 0 ; i < p_array . _p - > array . size ( ) ; i + + ) {
Variant src_val = p_array . _p - > array [ i ] ;
if ( src_val . get_type ( ) = = _p - > typed . type ) {
new_array . write [ i ] = src_val ;
} else if ( Variant : : can_convert_strict ( src_val . get_type ( ) , _p - > typed . type ) ) {
Variant * ptr = & src_val ;
Callable : : CallError ce ;
new_array . write [ i ] = Variant : : construct ( _p - > typed . type , ( const Variant * * ) & ptr , 1 , ce , true ) ;
if ( ce . error ! = Callable : : CallError : : CALL_OK ) {
ERR_FAIL_MSG ( " Unable to convert array index " + itos ( i ) + " from ' " + Variant : : get_type_name ( src_val . get_type ( ) ) + " ' to ' " + Variant : : get_type_name ( _p - > typed . type ) + " '. " ) ;
}
} else {
ERR_FAIL_MSG ( " Unable to convert array index " + itos ( i ) + " from ' " + Variant : : get_type_name ( src_val . get_type ( ) ) + " ' to ' " + Variant : : get_type_name ( _p - > typed . type ) + " '. " ) ;
}
}
_p - > array = new_array ;
2020-04-20 22:06:00 +00:00
}
} else if ( _p - > typed . can_reference ( p_array . _p - > typed ) ) { //same type or compatible
_ref ( p_array ) ;
} else {
ERR_FAIL_MSG ( " Assignment of arrays of incompatible types. " ) ;
}
}
void Array : : operator = ( const Array & p_array ) {
_assign ( p_array ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void Array : : push_back ( const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND ( ! _p - > typed . validate ( p_value , " push_back " ) ) ;
2014-02-10 01:10:30 +00:00
_p - > array . push_back ( p_value ) ;
}
Error Array : : resize ( int p_new_size ) {
return _p - > array . resize ( p_new_size ) ;
}
2017-03-05 15:44:50 +00:00
void Array : : insert ( int p_pos , const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND ( ! _p - > typed . validate ( p_value , " insert " ) ) ;
2017-03-05 15:44:50 +00:00
_p - > array . insert ( p_pos , p_value ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void Array : : erase ( const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND ( ! _p - > typed . validate ( p_value , " erase " ) ) ;
2014-02-10 01:10:30 +00:00
_p - > array . erase ( p_value ) ;
}
2016-11-18 20:30:16 +00:00
Variant Array : : front ( ) const {
2019-08-15 02:57:49 +00:00
ERR_FAIL_COND_V_MSG ( _p - > array . size ( ) = = 0 , Variant ( ) , " Can't take value from empty array. " ) ;
2016-11-18 20:30:16 +00:00
return operator [ ] ( 0 ) ;
}
Variant Array : : back ( ) const {
2019-08-15 02:57:49 +00:00
ERR_FAIL_COND_V_MSG ( _p - > array . size ( ) = = 0 , Variant ( ) , " Can't take value from empty array. " ) ;
2016-11-18 20:30:16 +00:00
return operator [ ] ( _p - > array . size ( ) - 1 ) ;
}
2017-03-05 15:44:50 +00:00
int Array : : find ( const Variant & p_value , int p_from ) const {
2014-02-10 01:10:30 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " find " ) , - 1 ) ;
2016-06-10 17:57:56 +00:00
return _p - > array . find ( p_value , p_from ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
int Array : : rfind ( const Variant & p_value , int p_from ) const {
2016-06-03 21:10:43 +00:00
2016-06-10 20:28:09 +00:00
if ( _p - > array . size ( ) = = 0 )
2016-06-03 21:10:43 +00:00
return - 1 ;
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " rfind " ) , - 1 ) ;
2016-06-03 21:10:43 +00:00
2016-06-10 20:28:09 +00:00
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 ;
}
2017-03-05 15:44:50 +00:00
for ( int i = p_from ; i > = 0 ; i - - ) {
2016-06-03 21:10:43 +00:00
2017-03-05 15:44:50 +00:00
if ( _p - > array [ i ] = = p_value ) {
2016-06-03 21:10:43 +00:00
return i ;
2019-07-01 10:59:42 +00:00
}
}
2016-06-03 21:10:43 +00:00
return - 1 ;
}
2017-03-05 15:44:50 +00:00
int Array : : find_last ( const Variant & p_value ) const {
2016-06-10 20:28:09 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " find_last " ) , - 1 ) ;
2016-06-10 20:28:09 +00:00
return rfind ( p_value ) ;
}
2017-03-05 15:44:50 +00:00
int Array : : count ( const Variant & p_value ) const {
2016-06-03 21:10:43 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " count " ) , 0 ) ;
2017-03-05 15:44:50 +00:00
if ( _p - > array . size ( ) = = 0 )
2016-06-03 21:10:43 +00:00
return 0 ;
2017-03-05 15:44:50 +00:00
int amount = 0 ;
for ( int i = 0 ; i < _p - > array . size ( ) ; i + + ) {
2016-06-03 21:10:43 +00:00
2017-03-05 15:44:50 +00:00
if ( _p - > array [ i ] = = p_value ) {
2016-06-03 21:10:43 +00:00
amount + + ;
2019-07-01 10:59:42 +00:00
}
}
2016-06-03 21:10:43 +00:00
return amount ;
}
2017-03-05 15:44:50 +00:00
bool Array : : has ( const Variant & p_value ) const {
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " use 'has' " ) , false ) ;
2016-07-02 17:03:35 +00:00
return _p - > array . find ( p_value , 0 ) ! = - 1 ;
}
2014-02-10 01:10:30 +00:00
void Array : : remove ( int p_pos ) {
_p - > array . remove ( p_pos ) ;
}
2017-03-05 15:44:50 +00:00
void Array : : set ( int p_idx , const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND ( ! _p - > typed . validate ( p_value , " set " ) ) ;
2017-03-05 15:44:50 +00:00
operator [ ] ( p_idx ) = p_value ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
const Variant & Array : : get ( int p_idx ) const {
2014-02-10 01:10:30 +00:00
return operator [ ] ( p_idx ) ;
}
2018-03-09 19:16:08 +00:00
Array Array : : duplicate ( bool p_deep ) const {
2017-09-06 21:13:05 +00:00
Array new_arr ;
int element_count = size ( ) ;
new_arr . resize ( element_count ) ;
2020-04-20 22:06:00 +00:00
new_arr . _p - > typed = _p - > typed ;
2017-09-06 21:13:05 +00:00
for ( int i = 0 ; i < element_count ; i + + ) {
2018-03-09 19:16:08 +00:00
new_arr [ i ] = p_deep ? get ( i ) . duplicate ( p_deep ) : get ( i ) ;
2017-09-06 21:13:05 +00:00
}
return new_arr ;
}
2019-07-17 01:31:58 +00:00
2020-05-01 16:06:36 +00:00
int Array : : _clamp_slice_index ( int p_index ) const {
2019-07-17 01:31:58 +00:00
2020-05-01 16:06:36 +00:00
int arr_size = size ( ) ;
int fixed_index = CLAMP ( p_index , - arr_size , arr_size - 1 ) ;
if ( fixed_index < 0 ) {
fixed_index = arr_size + fixed_index ;
}
return fixed_index ;
2019-07-17 01:31:58 +00:00
}
Array Array : : slice ( int p_begin , int p_end , int p_step , bool p_deep ) const { // like python, but inclusive on upper bound
2019-09-24 19:33:09 +00:00
2020-05-01 16:06:36 +00:00
Array new_arr ;
2019-07-17 01:31:58 +00:00
2020-05-01 16:06:36 +00:00
ERR_FAIL_COND_V_MSG ( p_step = = 0 , new_arr , " Array slice step size cannot be zero. " ) ;
2019-07-17 01:31:58 +00:00
2020-05-01 16:06:36 +00:00
if ( empty ( ) ) // Don't try to slice empty arrays.
2019-07-17 01:31:58 +00:00
return new_arr ;
2020-05-01 16:06:36 +00:00
if ( p_step > 0 ) {
if ( p_begin > = size ( ) | | p_end < - size ( ) )
return new_arr ;
} else { // p_step < 0
if ( p_begin < - size ( ) | | p_end > = size ( ) )
2019-07-17 01:31:58 +00:00
return new_arr ;
}
2020-05-01 16:06:36 +00:00
int begin = _clamp_slice_index ( p_begin ) ;
int end = _clamp_slice_index ( p_end ) ;
int new_arr_size = MAX ( ( ( end - begin + p_step ) / p_step ) , 0 ) ;
new_arr . resize ( new_arr_size ) ;
if ( p_step > 0 ) {
int dest_idx = 0 ;
for ( int idx = begin ; idx < = end ; idx + = p_step ) {
ERR_FAIL_COND_V_MSG ( dest_idx < 0 | | dest_idx > = new_arr_size , Array ( ) , " Bug in Array slice() " ) ;
new_arr [ dest_idx + + ] = p_deep ? get ( idx ) . duplicate ( p_deep ) : get ( idx ) ;
2019-07-17 01:31:58 +00:00
}
2020-05-01 16:06:36 +00:00
} else { // p_step < 0
int dest_idx = 0 ;
for ( int idx = begin ; idx > = end ; idx + = p_step ) {
ERR_FAIL_COND_V_MSG ( dest_idx < 0 | | dest_idx > = new_arr_size , Array ( ) , " Bug in Array slice() " ) ;
new_arr [ dest_idx + + ] = p_deep ? get ( idx ) . duplicate ( p_deep ) : get ( idx ) ;
2019-07-17 01:31:58 +00:00
}
}
return new_arr ;
}
2014-02-10 01:10:30 +00:00
struct _ArrayVariantSort {
2017-03-05 15:44:50 +00:00
_FORCE_INLINE_ bool operator ( ) ( const Variant & p_l , const Variant & p_r ) const {
bool valid = false ;
2014-02-10 01:10:30 +00:00
Variant res ;
2017-03-05 15:44:50 +00:00
Variant : : evaluate ( Variant : : OP_LESS , p_l , p_r , res , valid ) ;
2014-02-10 01:10:30 +00:00
if ( ! valid )
2017-03-05 15:44:50 +00:00
res = false ;
2014-02-10 01:10:30 +00:00
return res ;
}
} ;
2017-10-07 15:49:23 +00:00
Array & Array : : sort ( ) {
2014-02-10 01:10:30 +00:00
_p - > array . sort_custom < _ArrayVariantSort > ( ) ;
2017-10-07 15:49:23 +00:00
return * this ;
2014-02-10 01:10:30 +00:00
}
struct _ArrayVariantSortCustom {
Object * obj ;
StringName func ;
2017-03-05 15:44:50 +00:00
_FORCE_INLINE_ bool operator ( ) ( const Variant & p_l , const Variant & p_r ) const {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
const Variant * args [ 2 ] = { & p_l , & p_r } ;
2020-02-19 19:27:19 +00:00
Callable : : CallError err ;
2017-03-05 15:44:50 +00:00
bool res = obj - > call ( func , args , 2 , err ) ;
2020-02-19 19:27:19 +00:00
if ( err . error ! = Callable : : CallError : : CALL_OK )
2017-03-05 15:44:50 +00:00
res = false ;
2014-02-10 01:10:30 +00:00
return res ;
}
} ;
2017-10-07 15:49:23 +00:00
Array & Array : : sort_custom ( Object * p_obj , const StringName & p_function ) {
2014-02-10 01:10:30 +00:00
2017-10-07 15:49:23 +00:00
ERR_FAIL_NULL_V ( p_obj , * this ) ;
2014-02-10 01:10:30 +00:00
2018-01-09 18:03:51 +00:00
SortArray < Variant , _ArrayVariantSortCustom , true > avs ;
2017-03-05 15:44:50 +00:00
avs . compare . obj = p_obj ;
avs . compare . func = p_function ;
2017-11-25 03:07:54 +00:00
avs . sort ( _p - > array . ptrw ( ) , _p - > array . size ( ) ) ;
2017-10-07 15:49:23 +00:00
return * this ;
2014-02-10 01:10:30 +00:00
}
2018-01-10 18:36:53 +00:00
void Array : : shuffle ( ) {
const int n = _p - > array . size ( ) ;
if ( n < 2 )
return ;
Variant * data = _p - > array . ptrw ( ) ;
for ( int i = n - 1 ; i > = 1 ; i - - ) {
const int j = Math : : rand ( ) % ( i + 1 ) ;
const Variant tmp = data [ j ] ;
data [ j ] = data [ i ] ;
data [ i ] = tmp ;
}
}
2017-11-02 18:04:38 +00:00
template < typename Less >
_FORCE_INLINE_ int bisect ( const Vector < Variant > & p_array , const Variant & p_value , bool p_before , const Less & p_less ) {
int lo = 0 ;
int hi = p_array . size ( ) ;
if ( p_before ) {
while ( lo < hi ) {
const int mid = ( lo + hi ) / 2 ;
if ( p_less ( p_array . get ( mid ) , p_value ) ) {
lo = mid + 1 ;
} else {
hi = mid ;
}
}
} else {
while ( lo < hi ) {
const int mid = ( lo + hi ) / 2 ;
if ( p_less ( p_value , p_array . get ( mid ) ) ) {
hi = mid ;
} else {
lo = mid + 1 ;
}
}
}
return lo ;
}
int Array : : bsearch ( const Variant & p_value , bool p_before ) {
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " binary search " ) , - 1 ) ;
2017-11-02 18:04:38 +00:00
return bisect ( _p - > array , p_value , p_before , _ArrayVariantSort ( ) ) ;
}
int Array : : bsearch_custom ( const Variant & p_value , Object * p_obj , const StringName & p_function , bool p_before ) {
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND_V ( ! _p - > typed . validate ( p_value , " custom binary search " ) , - 1 ) ;
2017-11-02 18:04:38 +00:00
ERR_FAIL_NULL_V ( p_obj , 0 ) ;
_ArrayVariantSortCustom less ;
less . obj = p_obj ;
less . func = p_function ;
return bisect ( _p - > array , p_value , p_before , less ) ;
}
2017-10-07 15:49:23 +00:00
Array & Array : : invert ( ) {
2014-02-10 01:10:30 +00:00
_p - > array . invert ( ) ;
2017-10-07 15:49:23 +00:00
return * this ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void Array : : push_front ( const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
2020-04-20 22:06:00 +00:00
ERR_FAIL_COND ( ! _p - > typed . validate ( p_value , " push_front " ) ) ;
2017-03-05 15:44:50 +00:00
_p - > array . insert ( 0 , p_value ) ;
2015-12-12 11:27:30 +00:00
}
2017-03-05 15:44:50 +00:00
Variant Array : : pop_back ( ) {
2015-12-12 11:27:30 +00:00
2016-08-27 15:33:45 +00:00
if ( ! _p - > array . empty ( ) ) {
int n = _p - > array . size ( ) - 1 ;
Variant ret = _p - > array . get ( n ) ;
_p - > array . resize ( n ) ;
return ret ;
}
return Variant ( ) ;
2015-12-12 11:27:30 +00:00
}
2017-03-05 15:44:50 +00:00
Variant Array : : pop_front ( ) {
2016-08-27 15:33:45 +00:00
if ( ! _p - > array . empty ( ) ) {
Variant ret = _p - > array . get ( 0 ) ;
2015-12-12 11:27:30 +00:00
_p - > array . remove ( 0 ) ;
2016-08-27 15:33:45 +00:00
return ret ;
}
return Variant ( ) ;
2015-12-12 11:27:30 +00:00
}
2018-08-23 18:31:02 +00:00
Variant Array : : min ( ) const {
Variant minval ;
for ( int i = 0 ; i < size ( ) ; i + + ) {
if ( i = = 0 ) {
minval = get ( i ) ;
} else {
bool valid ;
Variant ret ;
Variant test = get ( i ) ;
Variant : : evaluate ( Variant : : OP_LESS , test , minval , ret , valid ) ;
if ( ! valid ) {
return Variant ( ) ; //not a valid comparison
}
if ( bool ( ret ) ) {
//is less
minval = test ;
}
}
}
return minval ;
}
Variant Array : : max ( ) const {
Variant maxval ;
for ( int i = 0 ; i < size ( ) ; i + + ) {
if ( i = = 0 ) {
maxval = get ( i ) ;
} else {
bool valid ;
Variant ret ;
Variant test = get ( i ) ;
Variant : : evaluate ( Variant : : OP_GREATER , test , maxval , ret , valid ) ;
if ( ! valid ) {
return Variant ( ) ; //not a valid comparison
}
if ( bool ( ret ) ) {
//is less
maxval = test ;
}
}
}
return maxval ;
}
2019-04-19 23:57:29 +00:00
const void * Array : : id ( ) const {
return _p - > array . ptr ( ) ;
}
2020-04-20 22:06:00 +00:00
Array : : Array ( const Array & p_from , uint32_t p_type , const StringName & p_class_name , const Variant & p_script ) {
_p = memnew ( ArrayPrivate ) ;
_p - > refcount . init ( ) ;
set_typed ( p_type , p_class_name , p_script ) ;
_assign ( p_from ) ;
}
void Array : : set_typed ( uint32_t p_type , const StringName & p_class_name , const Variant & p_script ) {
ERR_FAIL_COND_MSG ( _p - > array . size ( ) > 0 , " Type can only be set when array is empty. " ) ;
ERR_FAIL_COND_MSG ( _p - > refcount . get ( ) > 1 , " Type can only be set when array has no more than one user. " ) ;
ERR_FAIL_COND_MSG ( _p - > typed . type ! = Variant : : NIL , " Type can only be set once. " ) ;
ERR_FAIL_COND_MSG ( p_class_name ! = StringName ( ) & & p_type ! = Variant : : OBJECT , " Class names can only be set for type OBJECT " ) ;
Ref < Script > script = p_script ;
ERR_FAIL_COND_MSG ( script . is_valid ( ) & & p_class_name = = StringName ( ) , " Script class can only be set together with base class name " ) ;
_p - > typed . type = Variant : : Type ( p_type ) ;
_p - > typed . class_name = p_class_name ;
_p - > typed . script = script ;
_p - > typed . where = " TypedArray " ;
}
2017-03-05 15:44:50 +00:00
Array : : Array ( const Array & p_from ) {
2014-02-10 01:10:30 +00:00
2020-04-01 23:20:12 +00:00
_p = nullptr ;
2014-02-10 01:10:30 +00:00
_ref ( p_from ) ;
}
2018-08-23 18:31:02 +00:00
2017-01-11 11:53:31 +00:00
Array : : Array ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
_p = memnew ( ArrayPrivate ) ;
2014-02-10 01:10:30 +00:00
_p - > refcount . init ( ) ;
}
Array : : ~ Array ( ) {
_unref ( ) ;
}