Add exponential and vectorized search functions from Interpolations.jl#42
Merged
ChrisRackauckas merged 2 commits intoDec 29, 2025
Conversation
Implements search functions inspired by Interpolations.jl (issue SciML#8): - searchsortedfirstexp: Exponential search followed by binary search, efficient for correlated sequential lookups where the target is expected to be near the starting position. - searchsortedfirstvec: Batch search for multiple sorted values, leveraging monotonicity to avoid redundant searching. - searchsortedlastvec: Similar to searchsortedfirstvec but returns the last index where v[i] <= x. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
ChrisRackauckas
approved these changes
Dec 29, 2025
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses issue #8 by implementing search functions inspired by Interpolations.jl's specialized index finding routines.
New Functions Added:
searchsortedfirstexp(v, x, lo, hi): An exponential search followed by binary search, optimized for correlated sequential lookups where the target is expected to be near the starting positionlo. This is similar to Interpolations.jl'ssearchsortedfirst_exp_left.searchsortedfirstvec(v, x): Batch search for multiple sorted values. When the input vectorxis sorted, this leverages monotonicity to avoid redundant searching by reusing the previous search result as the starting point.searchsortedlastvec(v, x): Similar tosearchsortedfirstvecbut returns the last index wherev[i] <= x[i].How it works:
The exponential search starts with a linear scan of the first 5 elements (for cache efficiency), then switches to exponential doubling to quickly narrow the search range before falling back to binary search. This provides O(log k) complexity where k is the distance from
loto the target, instead of O(log n) for full binary search.The vectorized search functions check if
xis sorted and, if so, maintain a monotonically advancing lower bound for subsequent searches. This significantly reduces comparisons when searching for multiple values that are already ordered.Test plan
searchsortedfirstexpcovering basic functionality, edge cases, float vectors, empty/small vectors, repeated elements, and large vectorssearchsortedfirstvecandsearchsortedlastveccomparing against element-wisesearchsortedfirst/searchsortedlastFixes #8
cc @ChrisRackauckas
🤖 Generated with Claude Code