-
Notifications
You must be signed in to change notification settings - Fork 224
Add support to merge sort with a limit #222
Conversation
Codecov Report
@@ Coverage Diff @@
## main #222 +/- ##
==========================================
- Coverage 76.94% 76.84% -0.10%
==========================================
Files 229 229
Lines 19536 19610 +74
==========================================
+ Hits 15031 15070 +39
- Misses 4505 4540 +35
Continue to review full report at Codecov.
|
src/compute/merge_sort/mod.rs
Outdated
for (index, start, len) in slices { | ||
growable.extend(index, start, len) | ||
if len + current_len >= limit { |
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.
I think this probably makes the code without a limit (limit=None) slower.
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.
This can be avoided by having two different implementations/bodies.
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.
done
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.
I would prefer to not expose a new function just for this; wouldn't it make sense to move this if
to outside of the loop and keep the function with the limit
argument?
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.
I'm afraid the if can't move outside of the loop because the slice is an iterator.
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.
If you do slices.into_iter()
in two code blocks instead of reusing it, it shouldn't be a compile error?
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.
What I mean is thatslices
is lazy iterated, we don't know the item of len
unless we call next(), so the if
check must be inside the loop when limit is some value.
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.
I was thinking in something like this:
...
if let Some(limit) = limit {
iterate with the limit condition
} else {
iterate without the limit condition
}
...
growable.to_box()
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.
Sounds great.
Closes #221
Backward incompatible changes
The functions
compute::merge_sort::take_arrays
andcompute::merge_sort::merge_sort
now expects a third argument,Option<usize>
, denoting an optional limit. To migrate, addNone
to their calls.