-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Added Binary Search for Sorted Collections #3207
base: master
Are you sure you want to change the base?
Conversation
Added `search` method to collection prototype. automatically used by `where` and `findWhere` when it is possible to do so. user may provide: - a comparison function (comparator unused) - a value to search for (the value of the model[comparator] sought - requires comparator to be a string) - a model to search for (where `comparator` is a function) Users may also request that it is the index of the model that is returned or that the model of highest/lowest index which matches the comparison is sought. One point that may need to be considered is that the behaviour when a model is input as `toFind` but the `comparator` is a string is different from that when `comparator` is a function.
Fixed small bugs with `where` and `findWhere` and the erroneous tests which didn't catch these issues.
} else if (_.isFunction(this.comparator)) { | ||
// Use the `comparator` function, `toFind` is a model. | ||
if (this.comparator.length === 2) { | ||
compare = _.bind(this.comparator, this, toFind); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clever!
I do think |
If `toFind` is a function, the `compare` method will be bound to the model. Removed option to return model and always return index.
@megawac Could you possibly elaborate on this a bit more? I think using |
@megawac In fact, looking at the underscore source code, there is no obvious, efficient way to use |
This function has too many unnecessary options and params. I'd ditch those options and make a second functions |
In any case, I would quite like to hear a bit more about why you think |
|
might be worthwhile, although I would quite like to hear why you think this method could ever become incompatible. I'm not sure that your suggested change would necessarily improve code redundancy or simplicity. As far as I can see, the binary search code would still need to be there for the case of a two parameter comparator and so all we would be doing is adding more code for the other cases, effectively achieving the same thing by two possible means, rather that just one. Furthermore, I think whether or not optimising for the case where multiple models compare equally need to be considered. I think this is actually quite a common situation - perhaps even occurring in the majority of cases and I suppose it would be slightly disappointing to not optimise efficiency where it is possible, since optimising efficiency where it is possible was the goal of this method. If you have an alternative strategy for optimising the duplicate cases whilst maintaining the use of |
|
It would be great to get a few more opinions on this. If others agree with you, I would be happy to implement your ideas. That being said, if this gets included, I will gladly also write up documentation to ensure that the use of this method is clear - it would surely take less time than that we've already spent discussing it. I'm also not fully sure what you mean by 'options args' or 'right sorted' but I hope I have interpreted them correctly. An overview of different viewpoints
I think that @megawac's main issue with the code as it stands is the inconsistency with |
It might improve the interface to take the second parameter as |
now only have `getMax` boolean, if this is not truthy then the min is fetched bump test underscore to 1.7.0 (unreleased)
bumped test underscore to 1.7.0 to include @megawac's new |
updated code to match changes to backbone.js
@joshbambrick that's not 1.7 but the master working branch. |
you're right. I wasn't quite sure what the protocol was in this situation. |
@megawac @joshbambrick where are we at with this? Worth adding to 1.2.0 or holding off a bit more? |
@akre54 I feel we should off on this until underscores methods are improved. It could reduce the bloat from this implementation |
Ok I'll leave it be for now. Thanks. |
we could update the pull request we a local copy of |
see most recent pull request for version with local copy of comparator (and also reset underscore.js version - was a mistake last time) |
could consider renaming to |
to avoid having to update this file at all, adding the newline back in at the end
Any updates on the status of this? If the |
Added
search
method to collection prototype as per #3169. This is automatically used bywhere
andfindWhere
when it is possible to do so to provide speedups without user needing to do anything.For
toFind
, the user may provide:comparator
unused)model[comparator]
sought - requirescomparator
to be a string)comparator
is a function)Users may also request that it is the index of the model that is returned or that the model of highest/lowest index which matches the comparison is sought.
When no match is found,
undefined
is returned, or ifreturnIndex
is true then-1
is returned instead.One point that may need to be considered is that the behaviour when a model is input as
toFind
but thecomparator
is a string is different from that whencomparator
is a function. Furthermore, the name of this method may also need some more thought to emphasise that it is for binary searches or that it only works on sorted collections sobinarySearch
or the more user-friendlysortedSearched
might be preferable.