|
5 | 5 | * file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -use std::fmt; |
9 | 8 | use std::marker::PhantomData;
|
| 9 | +use std::{cmp, fmt}; |
10 | 10 |
|
11 | 11 | use crate::builtin::*;
|
12 | 12 | use crate::meta;
|
@@ -612,37 +612,80 @@ impl<T: ArrayElement> Array<T> {
|
612 | 612 | }
|
613 | 613 | }
|
614 | 614 |
|
615 |
| - /// Finds the index of an existing value in a sorted array using binary search. |
616 |
| - /// Equivalent of `bsearch` in GDScript. |
| 615 | + /// Finds the index of a value in a sorted array using binary search. |
617 | 616 | ///
|
618 |
| - /// If the value is not present in the array, returns the insertion index that |
619 |
| - /// would maintain sorting order. |
| 617 | + /// If the value is not present in the array, returns the insertion index that would maintain sorting order. |
620 | 618 | ///
|
621 |
| - /// Calling `bsearch` on an unsorted array results in unspecified behavior. |
| 619 | + /// Calling `bsearch` on an unsorted array results in unspecified behavior. Consider using `sort()` to ensure the sorting |
| 620 | + /// order is compatible with your callable's ordering. |
622 | 621 | pub fn bsearch(&self, value: impl AsArg<T>) -> usize {
|
623 | 622 | meta::arg_into_ref!(value: T);
|
624 | 623 |
|
625 | 624 | to_usize(self.as_inner().bsearch(&value.to_variant(), true))
|
626 | 625 | }
|
627 | 626 |
|
628 |
| - /// Finds the index of an existing value in a sorted array using binary search. |
629 |
| - /// Equivalent of `bsearch_custom` in GDScript. |
| 627 | + /// Finds the index of a value in a sorted array using binary search, with type-safe custom predicate. |
630 | 628 | ///
|
631 |
| - /// Takes a `Callable` and uses the return value of it to perform binary search. |
| 629 | + /// The comparator function should return an ordering that indicates whether its argument is `Less`, `Equal` or `Greater` the desired value. |
| 630 | + /// For example, for an ascending-ordered array, a simple predicate searching for a constant value would be `|elem| elem.cmp(&4)`. |
| 631 | + /// See also [`slice::binary_search_by()`]. |
632 | 632 | ///
|
633 |
| - /// If the value is not present in the array, returns the insertion index that |
634 |
| - /// would maintain sorting order. |
| 633 | + /// If the value is found, returns `Ok(index)` with its index. Otherwise, returns `Err(index)`, where `index` is the insertion index |
| 634 | + /// that would maintain sorting order. |
635 | 635 | ///
|
636 |
| - /// Calling `bsearch_custom` on an unsorted array results in unspecified behavior. |
| 636 | + /// Calling `bsearch_by` on an unsorted array results in unspecified behavior. Consider using [`sort_by()`] to ensure |
| 637 | + /// the sorting order is compatible with your callable's ordering. |
| 638 | + #[cfg(since_api = "4.2")] |
| 639 | + pub fn bsearch_by<F>(&self, mut func: F) -> Result<usize, usize> |
| 640 | + where |
| 641 | + F: FnMut(&T) -> cmp::Ordering + 'static, |
| 642 | + { |
| 643 | + // Early exit; later code relies on index 0 being present. |
| 644 | + if self.is_empty() { |
| 645 | + return Err(0); |
| 646 | + } |
| 647 | + |
| 648 | + // We need one dummy element of type T, because Godot's bsearch_custom() checks types (so Variant::nil() can't be passed). |
| 649 | + // Optimization: roundtrip Variant -> T -> Variant could be avoided, but anyone needing speed would use Rust binary search... |
| 650 | + let ignored_value = self.at(0); |
| 651 | + let ignored_value = <T as ParamType>::owned_to_arg(ignored_value); |
| 652 | + |
| 653 | + let godot_comparator = |args: &[&Variant]| { |
| 654 | + let value = T::from_variant(&args[0]); |
| 655 | + let is_less = matches!(func(&value), cmp::Ordering::Less); |
| 656 | + |
| 657 | + Ok(is_less.to_variant()) |
| 658 | + }; |
| 659 | + |
| 660 | + let debug_name = std::any::type_name::<F>(); |
| 661 | + let index = Callable::with_scoped_fn(debug_name, godot_comparator, |pred| { |
| 662 | + self.bsearch_custom(ignored_value, pred) |
| 663 | + }); |
| 664 | + |
| 665 | + if let Some(value_at_index) = self.get(index) { |
| 666 | + if func(&value_at_index) == cmp::Ordering::Equal { |
| 667 | + return Ok(index); |
| 668 | + } |
| 669 | + } |
| 670 | + |
| 671 | + Err(index) |
| 672 | + } |
| 673 | + |
| 674 | + /// Finds the index of a value in a sorted array using binary search, with `Callable` custom predicate. |
| 675 | + /// |
| 676 | + /// The callable `pred` takes two elements `(a, b)` and should return if `a < b` (strictly less). |
| 677 | + /// For a type-safe version, check out [`bsearch_by()`][Self::bsearch_by]. |
| 678 | + /// |
| 679 | + /// If the value is not present in the array, returns the insertion index that would maintain sorting order. |
637 | 680 | ///
|
638 |
| - /// Consider using `sort_custom()` to ensure the sorting order is compatible with |
639 |
| - /// your callable's ordering |
640 |
| - pub fn bsearch_custom(&self, value: impl AsArg<T>, func: &Callable) -> usize { |
| 681 | + /// Calling `bsearch_custom` on an unsorted array results in unspecified behavior. Consider using `sort_custom()` to ensure |
| 682 | + /// the sorting order is compatible with your callable's ordering. |
| 683 | + pub fn bsearch_custom(&self, value: impl AsArg<T>, pred: &Callable) -> usize { |
641 | 684 | meta::arg_into_ref!(value: T);
|
642 | 685 |
|
643 | 686 | to_usize(
|
644 | 687 | self.as_inner()
|
645 |
| - .bsearch_custom(&value.to_variant(), func, true), |
| 688 | + .bsearch_custom(&value.to_variant(), pred, true), |
646 | 689 | )
|
647 | 690 | }
|
648 | 691 |
|
|
0 commit comments