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

RFC: Clean up search_sorted* functions #1620

Closed
wants to merge 1 commit into from

Conversation

kmsquire
Copy link
Member

  • Use 1-based indexing for searching subarrays
  • Allow to be defined on AbstractVectors
  • Make search_sorted and alias for search_sorted_first
  • Round out functions with search_sorted_first_gt, search_sorted_last_lt (+ tests)
  • Added documentation

Discussion

Recent commit df7a323 exposed a more general interface to search_sorted*() functions, allowing the caller to optionally specify the range to call. This interface was inconsistent between search_sorted() (1-based) and search_sorted_{first,last}() (0-based, range 0:length(a)+1). The first item above address this issue, using 1-based indexing; internally, 0-based indexing is still used (and this is the 'proper' way to do binary search--see http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary)

Some additional deficiencies/idiosyncracies were identified (see df7a323600#commitcomment-2212563) and are addressed by this commit.

@ViralBShah
Copy link
Member

This looks good. Any thoughts on how to avoid the _gt and _lt versions of the functions? We need a better way to specify comparisons in a generic way across all the search and sort functions.

@StefanKarpinski did a bunch of work on the comparison functions for sort.

@StefanKarpinski
Copy link
Sponsor Member

I'm not clear on that the _gt and _lt versions are for here.

@kmsquire
Copy link
Member Author

We don't actually need to expose those functions, and they can also be
written easily in terms of the regular functions. I'll take them out here
and redefine them with timsort. The macro will probably be useful there as
well, but since there will be only two definitions here, I can remove it
for clarity for now. Thoughts?

On Wednesday, November 28, 2012, Stefan Karpinski wrote:

I'm not clear on that the _gt and _lt versions are for here.


Reply to this email directly or view it on GitHubhttps://github.com//pull/1620#issuecomment-10805002.

@kmsquire
Copy link
Member Author

Ok, I started thinking that search_sorted_first() and search_sorted_last() functions make more sense if you think about them as finding the first and last insertion point for x, and changed the documentation as such in this latest set of patches.

However, I just realized that that statement is not true for search_sorted_last(), because the last insertion point is the first value strictly greater than x, and the current definition is `>=x'.

I have a slight preference to change the definition of search_sorted_last() to reflect this, but I definitely would like some feedback on whether this is a good idea or not. The only in-tree user of this function is base/darray2.jl.

@ViralBShah
Copy link
Member

I think search_sorted_last should insert at the last insertion point using >. If you can make that change, we can merge this.

Also, are you planning on committing timsort into base, or keeping it in a package?

* Allow to be defined on AbstractVectors
* Use 1-based indexing for searching subarrays
* Make search_sorted and alias for search_sorted_first
* Added documentation
@kmsquire
Copy link
Member Author

kmsquire commented Dec 6, 2012

With regard to changing the definition of search_sorted_last: I chickened out. The definition is actually arbitrary, and the in-tree use of search_sorted_last was correct with the old definition. So I changed the docs back.

While it's unlikely, now that we have packages, one other small possibility is that there's a package which uses this function. I could probably install all packages and look, but I need to get some sleep. ;-)

Assuming it passes Travis-CI, I think this is mergable for now.

For timsort, I'm about to push a WIP version.

@kmsquire
Copy link
Member Author

Subsumed by #1691. Closing...

@kmsquire kmsquire closed this Dec 19, 2012
@kmsquire kmsquire deleted the search_sorted_update branch December 19, 2012 02:12
@kmsquire
Copy link
Member Author

Except I forgot to add the documentation...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants