summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/array.cpp24
-rw-r--r--core/array.h3
-rw-r--r--core/variant_call.cpp6
-rw-r--r--core/vector.h10
-rw-r--r--doc/base/classes.xml15
5 files changed, 43 insertions, 15 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) {
diff --git a/doc/base/classes.xml b/doc/base/classes.xml
index b74ac1c144..b72f082afa 100644
--- a/doc/base/classes.xml
+++ b/doc/base/classes.xml
@@ -4390,10 +4390,12 @@
<method name="find">
<return type="int">
</return>
- <argument index="0" name="value" type="var">
+ <argument index="0" name="what" type="var">
+ </argument>
+ <argument index="1" name="from" type="int" default="0">
</argument>
<description>
- 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.
</description>
</method>
<method name="find_last">
@@ -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.
</description>
</method>
+ <method name="rfind">
+ <argument index="0" name="what" type="var">
+ </argument>
+ <argument index="1" name="from" type="int">
+ </argument>
+ <description>
+ 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.
+ </description>
+ </method>
<method name="size">
<return type="int">
</return>