Skip to content

Commit

Permalink
[Doc] Clarify bsearch(_custom) behavior
Browse files Browse the repository at this point in the history
* Added an example for the effect of `before`
* Clarified the arguments to the custom callable can be either order

(cherry picked from commit 06df4ab)
  • Loading branch information
AThousandShips authored and akien-mga committed Mar 11, 2024
1 parent c2ff91c commit aec956d
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@
<param index="1" name="before" type="bool" default="true" />
<description>
Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search. Optionally, a [param before] specifier can be passed. If [code]false[/code], the returned index comes after all existing entries of the value in the array.
[codeblock]
var array = ["a", "b", "c", "c", "d", "e"]
print(array.bsearch("c", true)) # Prints 2, at the first matching element.
print(array.bsearch("c", false)) # Prints 4, after the last matching element, pointing to "d".
[/codeblock]
[b]Note:[/b] Calling [method bsearch] on an unsorted array results in unexpected behavior.
</description>
</method>
Expand All @@ -228,6 +233,7 @@
<param index="2" name="before" type="bool" default="true" />
<description>
Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search and a custom comparison method. Optionally, a [param before] specifier can be passed. If [code]false[/code], the returned index comes after all existing entries of the value in the array. The custom method receives two arguments (an element from the array and the value searched for) and must return [code]true[/code] if the first argument is less than the second, and return [code]false[/code] otherwise.
[b]Note:[/b] The custom method must accept the two arguments in any order, you cannot rely on that the first argument will always be from the array.
[b]Note:[/b] Calling [method bsearch_custom] on an unsorted array results in unexpected behavior.
</description>
</method>
Expand Down

0 comments on commit aec956d

Please sign in to comment.