Browse Source

Added Array.find_last() and Array.count()

J08nY 9 years ago
parent
commit
5f5ca8cd9b
4 changed files with 55 additions and 0 deletions
  1. 31 0
      core/array.cpp
  2. 2 0
      core/array.h
  3. 4 0
      core/variant_call.cpp
  4. 18 0
      doc/base/classes.xml

+ 31 - 0
core/array.cpp

@@ -155,6 +155,37 @@ int Array::find(const Variant& p_value) const {
 	return _p->array.find(p_value);
 }
 
+int Array::find_last(const Variant& p_value) const {
+
+	if(_p->array.size() == 0)
+		return -1;
+
+	for (int i=_p->array.size()-1; i>=0; i--) {
+
+		if(_p->array[i] == p_value){
+			return i;
+		};
+	};
+
+	return -1;
+}
+
+int Array::count(const Variant& p_value) const {
+
+	if(_p->array.size() == 0)
+		return 0;
+
+	int amount=0;
+	for (int i=0; i<_p->array.size(); i++) {
+
+		if(_p->array[i] == p_value){
+			amount++;
+		};
+	};
+
+	return amount;
+}
+
 void Array::remove(int p_pos) {
 
 	_p->array.remove(p_pos);

+ 2 - 0
core/array.h

@@ -72,6 +72,8 @@ public:
 	void invert();
 
 	int find(const Variant& p_value) const;
+	int find_last(const Variant& p_value) const;
+	int count(const Variant& p_value) const;
 
 	void erase(const Variant& p_value);
 

+ 4 - 0
core/variant_call.cpp

@@ -463,6 +463,8 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var
 	VCALL_LOCALMEM2(Array,insert);
 	VCALL_LOCALMEM1(Array,remove);
 	VCALL_LOCALMEM1R(Array,find);
+	VCALL_LOCALMEM1R(Array,find_last);
+	VCALL_LOCALMEM1R(Array,count);
 	VCALL_LOCALMEM1(Array,erase);
 	VCALL_LOCALMEM0(Array,sort);
 	VCALL_LOCALMEM2(Array,sort_custom);
@@ -1448,6 +1450,8 @@ _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());
 	ADDFUNC1(ARRAY,INT,Array,find,NIL,"value",varray());
+	ADDFUNC1(ARRAY,INT,Array,find_last,NIL,"value",varray());
+	ADDFUNC1(ARRAY,INT,Array,count,NIL,"value",varray());
 	ADDFUNC0(ARRAY,NIL,Array,pop_back,varray());
 	ADDFUNC0(ARRAY,NIL,Array,pop_front,varray());
 	ADDFUNC0(ARRAY,NIL,Array,sort,varray());

+ 18 - 0
doc/base/classes.xml

@@ -4340,6 +4340,15 @@
 			Clear the array (resize to 0).
 			</description>
 		</method>
+		<method name="count">
+			<return type="int">
+			</return>
+			<argument index="0" name="value" type="var">
+			</argument>
+			<description>
+			Return the amount of times an element is in the array.
+			</description>
+		</method>
 		<method name="empty">
 			<return type="bool">
 			</return>
@@ -4363,6 +4372,15 @@
 			Searches the array for a value and returns its index or -1 if not found.
 			</description>
 		</method>
+		<method name="find_last">
+			<return type="int">
+			</return>
+			<argument index="0" name="value" type="var">
+			</argument>
+			<description>
+			Searches the array in reverse order for a value and returns its index or -1 if not found.
+			</description>
+		</method>
 		<method name="hash">
 			<return type="int">
 			</return>