Skip to content

Commit 89491f4

Browse files
committed
Add callable support for find and rfind Array methods
1 parent 88f3b5f commit 89491f4

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

core/variant/array.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,34 @@ int Array::find(const Variant &p_value, int p_from) const {
369369
return ret;
370370
}
371371

372+
int Array::find_custom(const Callable &p_callable, int p_from) const {
373+
int ret = -1;
374+
375+
if (p_from < 0 || size() == 0) {
376+
return ret;
377+
}
378+
379+
const Variant *argptrs[1];
380+
381+
for (int i = p_from; i < size(); i++) {
382+
const Variant &val = _p->array[i];
383+
argptrs[0] = &val;
384+
Variant res;
385+
Callable::CallError ce;
386+
p_callable.callp(argptrs, 1, res, ce);
387+
if (unlikely(ce.error != Callable::CallError::CALL_OK)) {
388+
ERR_FAIL_V_MSG(ret, "Error calling method from 'find_custom': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
389+
}
390+
391+
ERR_FAIL_COND_V_MSG(res.get_type() != Variant::Type::BOOL, ret, "Error on method from 'find_custom': Return type of callable must be boolean.");
392+
if (res.operator bool()) {
393+
return i;
394+
}
395+
}
396+
397+
return ret;
398+
}
399+
372400
int Array::rfind(const Variant &p_value, int p_from) const {
373401
if (_p->array.size() == 0) {
374402
return -1;
@@ -394,6 +422,41 @@ int Array::rfind(const Variant &p_value, int p_from) const {
394422
return -1;
395423
}
396424

425+
int Array::rfind_custom(const Callable &p_callable, int p_from) const {
426+
if (_p->array.size() == 0) {
427+
return -1;
428+
}
429+
430+
if (p_from < 0) {
431+
// Relative offset from the end.
432+
p_from = _p->array.size() + p_from;
433+
}
434+
if (p_from < 0 || p_from >= _p->array.size()) {
435+
// Limit to array boundaries.
436+
p_from = _p->array.size() - 1;
437+
}
438+
439+
const Variant *argptrs[1];
440+
441+
for (int i = p_from; i >= 0; i--) {
442+
const Variant &val = _p->array[i];
443+
argptrs[0] = &val;
444+
Variant res;
445+
Callable::CallError ce;
446+
p_callable.callp(argptrs, 1, res, ce);
447+
if (unlikely(ce.error != Callable::CallError::CALL_OK)) {
448+
ERR_FAIL_V_MSG(-1, "Error calling method from 'rfind_custom': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
449+
}
450+
451+
ERR_FAIL_COND_V_MSG(res.get_type() != Variant::Type::BOOL, -1, "Error on method from 'rfind_custom': Return type of callable must be boolean.");
452+
if (res.operator bool()) {
453+
return i;
454+
}
455+
}
456+
457+
return -1;
458+
}
459+
397460
int Array::count(const Variant &p_value) const {
398461
Variant value = p_value;
399462
ERR_FAIL_COND_V(!_p->typed.validate(value, "count"), 0);
@@ -761,7 +824,7 @@ Variant Array::max() const {
761824
return Variant(); //not a valid comparison
762825
}
763826
if (bool(ret)) {
764-
//is less
827+
//is greater
765828
maxval = test;
766829
}
767830
}

core/variant/array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ class Array {
152152
void reverse();
153153

154154
int find(const Variant &p_value, int p_from = 0) const;
155+
int find_custom(const Callable &p_callable, int p_from = 0) const;
155156
int rfind(const Variant &p_value, int p_from = -1) const;
157+
int rfind_custom(const Callable &p_callable, int p_from = -1) const;
156158
int count(const Variant &p_value) const;
157159
bool has(const Variant &p_value) const;
158160

core/variant/variant_call.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,7 +2289,9 @@ static void _register_variant_builtin_methods_array() {
22892289
bind_method(Array, back, sarray(), varray());
22902290
bind_method(Array, pick_random, sarray(), varray());
22912291
bind_method(Array, find, sarray("what", "from"), varray(0));
2292+
bind_method(Array, find_custom, sarray("method", "from"), varray(0));
22922293
bind_method(Array, rfind, sarray("what", "from"), varray(-1));
2294+
bind_method(Array, rfind_custom, sarray("method", "from"), varray(-1));
22932295
bind_method(Array, count, sarray("value"), varray());
22942296
bind_method(Array, has, sarray("value"), varray());
22952297
bind_method(Array, pop_back, sarray(), varray());

doc/classes/Array.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
<param index="0" name="value" type="Variant" />
326326
<description>
327327
Returns the number of times an element is in the array.
328+
To count how many elements in an array satisfy a condition, see [method reduce].
328329
</description>
329330
</method>
330331
<method name="duplicate" qualifiers="const">
@@ -396,6 +397,25 @@
396397
[b]Note:[/b] For performance reasons, the search is affected by [param what]'s [enum Variant.Type]. For example, [code]7[/code] ([int]) and [code]7.0[/code] ([float]) are not considered equal for this method.
397398
</description>
398399
</method>
400+
<method name="find_custom" qualifiers="const">
401+
<return type="int" />
402+
<param index="0" name="method" type="Callable" />
403+
<param index="1" name="from" type="int" default="0" />
404+
<description>
405+
Returns the index of the [b]first[/b] element in the array that causes [param method] to return [code]true[/code], or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the end of the array.
406+
[param method] is a callable that takes an element of the array, and returns a [bool].
407+
[b]Note:[/b] If you just want to know whether the array contains [i]anything[/i] that satisfies [param method], use [method any].
408+
[codeblocks]
409+
[gdscript]
410+
func is_even(number):
411+
return number % 2 == 0
412+
413+
func _ready():
414+
print([1, 3, 4, 7].find_custom(is_even.bind())) # prints 2
415+
[/gdscript]
416+
[/codeblocks]
417+
</description>
418+
</method>
399419
<method name="front" qualifiers="const">
400420
<return type="Variant" />
401421
<description>
@@ -619,6 +639,17 @@
619639
func is_length_greater(a, b):
620640
return a.length() &gt; b.length()
621641
[/codeblock]
642+
This method can also be used to count how many elements in an array satisfy a certain condition, similar to [method count]:
643+
[codeblock]
644+
func is_even(number):
645+
return number % 2 == 0
646+
647+
func _ready():
648+
var arr = [1, 2, 3, 4, 5]
649+
# Increment count if it's even, else leaves count the same.
650+
var even_count = arr.reduce(func(count, next): return count + 1 if is_even(next) else count, 0)
651+
print(even_count) # Prints 2
652+
[/codeblock]
622653
See also [method map], [method filter], [method any] and [method all].
623654
</description>
624655
</method>
@@ -655,6 +686,14 @@
655686
Returns the index of the [b]last[/b] occurrence of [param what] in this array, or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the beginning of the array. This method is the reverse of [method find].
656687
</description>
657688
</method>
689+
<method name="rfind_custom" qualifiers="const">
690+
<return type="int" />
691+
<param index="0" name="method" type="Callable" />
692+
<param index="1" name="from" type="int" default="-1" />
693+
<description>
694+
Returns the index of the [b]last[/b] element of the array that causes [param method] to return [code]true[/code], or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the beginning of the array. This method is the reverse of [method find_custom].
695+
</description>
696+
</method>
658697
<method name="shuffle">
659698
<return type="void" />
660699
<description>

tests/core/variant/test_array.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,24 @@ TEST_CASE("[Array] Typed copying") {
634634
a6.clear();
635635
}
636636

637+
static bool _find_custom_callable(const Variant &p_val) {
638+
return (int)p_val % 2 == 0;
639+
}
640+
641+
TEST_CASE("[Array] Test find_custom") {
642+
Array a1 = build_array(1, 3, 4, 5, 8, 9);
643+
// Find first even number.
644+
int index = a1.find_custom(callable_mp_static(_find_custom_callable));
645+
CHECK_EQ(index, 2);
646+
}
647+
648+
TEST_CASE("[Array] Test rfind_custom") {
649+
Array a1 = build_array(1, 3, 4, 5, 8, 9);
650+
// Find last even number.
651+
int index = a1.rfind_custom(callable_mp_static(_find_custom_callable));
652+
CHECK_EQ(index, 4);
653+
}
654+
637655
} // namespace TestArray
638656

639657
#endif // TEST_ARRAY_H

0 commit comments

Comments
 (0)