Add 'rfind' function to Array

This commit is contained in:
George Marques 2016-06-10 17:28:09 -03:00
parent 269d570420
commit 46b6bb9dc4
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
3 changed files with 20 additions and 3 deletions

View file

@ -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)

View file

@ -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;

View file

@ -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());