From 269d5704202d29d1f367abfb2df44fa19997fd18 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 10 Jun 2016 14:57:56 -0300 Subject: [PATCH 1/3] Add 'from' argument to Array.find() --- core/array.cpp | 4 ++-- core/array.h | 2 +- core/variant_call.cpp | 4 ++-- core/vector.h | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/array.cpp b/core/array.cpp index 1d283a14aaa..146e56b7f8f 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -150,9 +150,9 @@ 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 { diff --git a/core/array.h b/core/array.h index 9472a6dd215..4fb22a4dcc8 100644 --- a/core/array.h +++ b/core/array.h @@ -71,7 +71,7 @@ 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 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 d427a805415..6c16d8cba0a 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -464,7 +464,7 @@ 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_LOCALMEM1R(Array,find_last); VCALL_LOCALMEM1R(Array,count); VCALL_LOCALMEM1(Array,erase); @@ -1453,7 +1453,7 @@ _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)); 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 87248ccf687..cafb4a4aa34 100644 --- a/core/vector.h +++ b/core/vector.h @@ -120,7 +120,7 @@ public: template - 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::_copy_on_write() { } template template -int Vector::find(const T_val &p_val) const { +int Vector::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::find(const T_val &p_val) const { }; return ret; -}; +} template Error Vector::resize(int p_size) { From 46b6bb9dc49a7b187a7bf6e19e5da732a5656e19 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 10 Jun 2016 17:28:09 -0300 Subject: [PATCH 2/3] Add 'rfind' function to Array --- core/array.cpp | 20 +++++++++++++++++--- core/array.h | 1 + core/variant_call.cpp | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/array.cpp b/core/array.cpp index 146e56b7f8f..bb8e527304f 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -155,12 +155,21 @@ int Array::find(const Variant& p_value, int p_from) const { 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 4fb22a4dcc8..096660653e6 100644 --- a/core/array.h +++ b/core/array.h @@ -72,6 +72,7 @@ public: void invert(); 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 6c16d8cba0a..cc2d15b88c5 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -465,6 +465,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM2(Array,insert); VCALL_LOCALMEM1(Array,remove); VCALL_LOCALMEM2R(Array,find); + VCALL_LOCALMEM2R(Array,rfind); VCALL_LOCALMEM1R(Array,find_last); VCALL_LOCALMEM1R(Array,count); VCALL_LOCALMEM1(Array,erase); @@ -1454,6 +1455,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC1(ARRAY,NIL,Array,remove,INT,"pos",varray()); ADDFUNC1(ARRAY,NIL,Array,erase,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()); From 6ce5876c63ffd7bc21b38197c4fa89713a77ceb5 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 10 Jun 2016 18:07:59 -0300 Subject: [PATCH 3/3] Add documentation for Array.find and Array.rfind --- doc/base/classes.xml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/base/classes.xml b/doc/base/classes.xml index b74ac1c1442..b72f082afa0 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -4390,10 +4390,12 @@ - + + + - Searches the array for a value and returns its index or -1 if not found. + Searches the array for a value and returns its index or -1 if not found. Optionally, the initial search index can be passed. @@ -4471,6 +4473,15 @@ Resize the array to contain a different number of elements. If the array size is smaller, elements are cleared, if bigger, new elements are Null. + + + + + + + Searches the array in reverse order. Optionally, a start search index can be passed. If negative, the start index is considered relative to the end of the array. + +