Skip to content

SI-9605 Searching does not use binary search for Array#4902

Merged
SethTisue merged 1 commit intoscala:2.11.xfrom
ruippeixotog:issue/9605
Jan 14, 2016
Merged

SI-9605 Searching does not use binary search for Array#4902
SethTisue merged 1 commit intoscala:2.11.xfrom
ruippeixotog:issue/9605

Conversation

@ruippeixotog
Copy link
Contributor

Binary search should be used for every IndexedSeqLike instance and not only for IndexedSeq. According the Scaladoc, it is IndexedSeqLike that guarantees "constant-time or near constant-time element access and length computation".

@scala-jenkins scala-jenkins added this to the 2.11.8 milestone Jan 12, 2016
@Ichoran
Copy link
Contributor

Ichoran commented Jan 12, 2016

Thanks, that looks like it will probably do the trick! But could you write a test that verifies the correct behavior?

You can do this e.g. by creating a custom class that throws an exception if it is compared to a particular value (e.g. the 3rd one in the array), and then search for something in the second half so that comparison should be skipped if the correct implementation is used. (Make sure you explode both if 3 does the comparison or if something is compared to 3, so it doesn't matter which order the algorithm does the comparison in.)

@Ichoran
Copy link
Contributor

Ichoran commented Jan 12, 2016

I accidentally put L/G/T/(you fill in this part) here. Not quite yet!

@Ichoran
Copy link
Contributor

Ichoran commented Jan 12, 2016

Ack, wrong PR. I meant the ArrayStack one.

Binary search should be used for every `IndexedSeqLike` instance and not only for `IndexedSeq`. According the Scaladoc, it is `IndexedSeqLike` that guarantees "constant-time or near constant-time element access and length computation".
@ruippeixotog
Copy link
Contributor Author

Done, I added a test that verifies if apply is being called in the search or not (a sign of direct indexing / binary search). That does the trick, doesn't it?

@Ichoran
Copy link
Contributor

Ichoran commented Jan 13, 2016

@ruippeixotog - In general, no, it doesn't, because linear operations (e.g. iterator) may be implemented via apply.

@Ichoran Ichoran removed the reviewed label Jan 13, 2016
@ruippeixotog
Copy link
Contributor Author

Doesn't that defeat the purpose of linear search? If iterating over a collection is implemented by directly indexing elements with apply, then either apply is constant-time and the collection should be an ´IndexedSeq` instead or it isn't constant-time and binary search is actually more efficient than linear search.

Nonetheless, it doesn't seem to matter anyway. Even if testing this way may not apply to every conceivable collection, in this test I'm not using arbitrary collections - I'm using collections I created inside each test and I know the behavior of their operations. As I delegate all operations other than apply to the inner collection (either List or Vector, not abstract ones), I know that if apply is being called somewhere, it's inside search and that would prove either binary search is being used or linear search is badly implemented.

@Ichoran
Copy link
Contributor

Ichoran commented Jan 13, 2016

It's a little overly-specific about which elements are hit, but sure, this way is fine also. Just to be clear, though, it's not whether apply is being called at all, it's which elements are being hit, that indicates that it's binary search. (The default implementation of iterator for IndexedSeqLike uses an internal counter and apply.)

@SethTisue
Copy link
Member

/nothingtoseehere

integrate-ide failure is unrelated to the PR

@Ichoran
Copy link
Contributor

Ichoran commented Jan 14, 2016

LGTM

SethTisue added a commit that referenced this pull request Jan 14, 2016
SI-9605 Searching does not use binary search for Array
@SethTisue SethTisue merged commit 9a2e712 into scala:2.11.x Jan 14, 2016
@SethTisue
Copy link
Member

thank you Rui!

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.

4 participants