Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Godot's native sort and binary search functions on VariantArray #989

Open
chitoyuu opened this issue Dec 7, 2022 · 1 comment
Open
Labels
c: core Component: core (mod core_types, object, log, init, ...) feature Adds functionality to the library

Comments

@chitoyuu
Copy link
Contributor

chitoyuu commented Dec 7, 2022

Created during survey of commented code (#377).

The following methods of VariantArray are not exposed to Rust:

  • sort_custom
  • bsearch
  • bsearch_custom

These methods may have some fringe uses, such as for replicating GDScript behavior exactly, but are clumsy to use and generally slower (due to FFI overhead) than collecting the values to Vec and performing the corresponding operations purely on the Rust side.

Bindings for these methods can be added for completeness's sake, but it should be made clear in the documentation what the performance implications are.

Unresolved questions

Should bsearch be included under its original Godot name, or binary_search?

@chitoyuu chitoyuu added feature Adds functionality to the library c: core Component: core (mod core_types, object, log, init, ...) labels Dec 7, 2022
@Bromeon
Copy link
Member

Bromeon commented Dec 7, 2022

Should bsearch be included under its original Godot name, or binary_search?

I would use bsearch -- it also has a completely different signature than [T]::binary_search in Rust:

int bsearch ( Variant value, bool before=true )

Since we have a default parameter, we might add an overload bsearch_after. Regarding naming, before=false (i.e. not the default) because "the returned index comes after all existing entries of the value in the array".

Or even better, we just provide one method always taking before parameter -- as you say, it's obscure enough to be rarely used.

Also maybe worth documenting (assert might be too much):

Calling bsearch on an unsorted array results in unexpected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Component: core (mod core_types, object, log, init, ...) feature Adds functionality to the library
Projects
None yet
Development

No branches or pull requests

2 participants