diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/array.cpp | 24 | ||||
-rw-r--r-- | core/array.h | 3 | ||||
-rw-r--r-- | core/variant_call.cpp | 6 | ||||
-rw-r--r-- | core/vector.h | 10 |
4 files changed, 30 insertions, 13 deletions
diff --git a/core/array.cpp b/core/array.cpp index 1d283a14aa..bb8e527304 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -150,17 +150,26 @@ void Array::erase(const Variant& p_value) { _p->array.erase(p_value); } -int Array::find(const Variant& p_value) const { +int Array::find(const Variant& p_value, int p_from) const { - return _p->array.find(p_value); + return _p->array.find(p_value, p_from); } -int Array::find_last(const Variant& p_value) const { +int Array::rfind(const Variant& p_value, int p_from) const { - if(_p->array.size() == 0) + if (_p->array.size() == 0) return -1; - for (int i=_p->array.size()-1; i>=0; i--) { + 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; @@ -170,6 +179,11 @@ int Array::find_last(const Variant& p_value) const { 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) diff --git a/core/array.h b/core/array.h index 9472a6dd21..096660653e 100644 --- a/core/array.h +++ b/core/array.h @@ -71,7 +71,8 @@ public: void sort_custom(Object *p_obj,const StringName& p_function); void invert(); - int find(const Variant& p_value) const; + int find(const Variant& p_value, int p_from=0) const; + int rfind(const Variant& p_value, int p_from=-1) const; int find_last(const Variant& p_value) const; int count(const Variant& p_value) const; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index d427a80541..cc2d15b88c 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -464,7 +464,8 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM1(Array,resize); VCALL_LOCALMEM2(Array,insert); VCALL_LOCALMEM1(Array,remove); - VCALL_LOCALMEM1R(Array,find); + VCALL_LOCALMEM2R(Array,find); + VCALL_LOCALMEM2R(Array,rfind); VCALL_LOCALMEM1R(Array,find_last); VCALL_LOCALMEM1R(Array,count); VCALL_LOCALMEM1(Array,erase); @@ -1453,7 +1454,8 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC2(ARRAY,NIL,Array,insert,INT,"pos",NIL,"value",varray()); ADDFUNC1(ARRAY,NIL,Array,remove,INT,"pos",varray()); ADDFUNC1(ARRAY,NIL,Array,erase,NIL,"value",varray()); - ADDFUNC1(ARRAY,INT,Array,find,NIL,"value",varray()); + ADDFUNC2(ARRAY,INT,Array,find,NIL,"what",INT,"from",varray(0)); + ADDFUNC2(ARRAY,INT,Array,rfind,NIL,"what",INT,"from",varray(-1)); ADDFUNC1(ARRAY,INT,Array,find_last,NIL,"value",varray()); ADDFUNC1(ARRAY,INT,Array,count,NIL,"value",varray()); ADDFUNC0(ARRAY,NIL,Array,pop_back,varray()); diff --git a/core/vector.h b/core/vector.h index 87248ccf68..cafb4a4aa3 100644 --- a/core/vector.h +++ b/core/vector.h @@ -120,7 +120,7 @@ public: template <class T_val> - int find(const T_val& p_val) const; + int find(const T_val& p_val, int p_from=0) const; void set(int p_index,T p_elem); T get(int p_index) const; @@ -238,13 +238,13 @@ void Vector<T>::_copy_on_write() { } template<class T> template<class T_val> -int Vector<T>::find(const T_val &p_val) const { +int Vector<T>::find(const T_val &p_val, int p_from) const { int ret = -1; - if (size() == 0) + if (p_from < 0 || size() == 0) return ret; - for (int i=0; i<size(); i++) { + for (int i=p_from; i<size(); i++) { if (operator[](i) == p_val) { ret = i; @@ -253,7 +253,7 @@ int Vector<T>::find(const T_val &p_val) const { }; return ret; -}; +} template<class T> Error Vector<T>::resize(int p_size) { |