about summary refs log tree commit diff
path: root/src/libcore/slice
AgeCommit message (Collapse)AuthorLines
2019-07-16fix double wordingDodo-1/+1
2019-07-15Add debug assertions to write_bytes and copy*Valentin Tolmer-3/+3
2019-07-14better commentsRalf Jung-3/+4
2019-07-13explain how to search without owned dataRalf Jung-0/+8
2019-07-11Rollup merge of #61665 - aschampion:slice-eq-ptr, r=sfacklerMazdak Farrokhzad-6/+17
core: check for pointer equality when comparing Eq slices Because `Eq` types must be reflexively equal, an equal-length slice to the same memory location must be equal. This is related to #33892 (and #32699) answering this comment from that PR: > Great! One more easy question: why does this optimization not apply in the non-BytewiseEquality implementation directly above? Because slices of non-reflexively equal types (like `f64`) are not equal even if it's the same slice. But if the types are `Eq`, we can use this same-address optimization, which this PR implements. Obviously this changes behavior if types violate the reflexivity condition of `Eq`, because their impls of `PartialEq` will no longer be called per-item, but 🤷‍♂ . It's not clear how often this optimization comes up in the real world outside of the same-`&str` case covered by #33892, so **I'm requesting a perf run** (on MacOS today, so can't run `rustc_perf` myself). I'm going ahead and making the PR on the basis of being surprised things didn't already work this way. This is my first time hacking rust itself, so as a perf sanity check I ran `./x.py bench --stage 0 src/lib{std,alloc}`, but the differences were noisy. To make the existing specialization for `BytewiseEquality` explicit, it's now a supertrait of `Eq + Copy`. `Eq` should be sufficient, but `Copy` was included for clarity.
2019-07-08Auto merge of #62473 - timvermeulen:is_sorted_by_key, r=scottmcmbors-2/+2
Only call the closure parameter of Iterator::is_sorted_by_key once per item See https://github.com/rust-lang/rust/issues/53485#issuecomment-472314004. This changes `Iterator::is_sorted_by_key` to only call the given closure once for each item, which allows us to pass the items to the closure by value instead of by reference. **Important**: `is_sorted_by_key` for slices and slice iterators is now no longer implemented in terms of the custom `slice::Iter::is_sorted_by` implementation. It's a trade-off: we could forward `slice::Iter::is_sorted_by_key` to it directly for potential SIMD benefits, but that would mean that the closure is potentially called twice for (almost) every element of the slice.
2019-07-07Only call the closure parameter of Iterator::is_sorted_by_key once per itemTim Vermeulen-2/+2
2019-06-23squash of all commits for nth_back on ChunksMut@amit.chandra-0/+19
wip nth_back for chunks_mut working chunksmut fixed nth_back for chunksmut Signed-off-by: wizAmit <amitforfriends_dns@yahoo.com>
2019-06-23squash commit for nth_back on chunks exact@amit.chandra-0/+15
wip nth_back for chunks_exact working nth_back for chunks exact Signed-off-by: wizAmit <amitforfriends_dns@yahoo.com>
2019-06-20Rollup merge of #60772 - timvermeulen:slice_iter_nth_back, r=scottmcmMazdak Farrokhzad-21/+55
Implement nth_back for slice::{Iter, IterMut} Part of #54054. I implemented `nth_back` as straightforwardly as I could, and then slightly changed `nth` to match `nth_back`. I believe I did so correctly, but please double-check 🙂 I also added the helper methods `zst_shrink`, `next_unchecked`, and `next_back_unchecked` to get rid of some duplicated code. These changes hopefully make this code easier to understand for new contributors like me. I noticed the `is_empty!` and `len!` macros which sole purpose seems to be inlining, according to the comment right above them, but the `is_empty` and `len` methods are already marked with `#[inline(always)]`. Does that mean we could replace these macros with method calls, without affecting anything? I'd love to get rid of them.
2019-06-19Add functions to build raw slicesOliver Scherer-16/+3
2019-06-15Help LLVM better optimize slice::Iter(Mut)::lenScott McMurray-4/+17
2019-06-13Rollup merge of #61398 - kennytm:stabilize-copy-within, r=SimonSapinMazdak Farrokhzad-4/+3
Stabilize copy_within Closes #54236.
2019-06-10core: use memcmp optimization for 128 bit integer slicesAndrew Champion-1/+1
2019-06-08core: use iterators for slice equality comparisonAndrew Champion-14/+2
2019-06-08core: check for pointer equality when comparing Eq slicesAndrew Champion-1/+24
Because Eq types must be reflexively equal, an equal-length slice to the same memory location must be equal.
2019-06-02copy_within: replace element access by pointer arithmetic to avoid UBkennytm-2/+2
This ensures we won't accidentally read *src or *dest even when count = 0.
2019-05-31Stabilize copy_withinkennytm-2/+1
2019-05-29Rollup merge of #61048 - wizAmit:feature/nth_back_chunks, r=scottmcmMazdak Farrokhzad-0/+18
Feature/nth back chunks A succinct implementation for nth_back on chunks. Thank you @timvermeulen for the guidance. r? @timvermeulen
2019-05-29Rollup merge of #60555 - timvermeulen:rchunks_nth_back, r=scottmcmMazdak Farrokhzad-0/+72
Implement nth_back for RChunks(Exact)(Mut) Part of #54054. These implementations may not be optimal because of the use of `self.len()`, but it's quite cheap and simplifies the code a lot. There's quite some duplication going on here, I wouldn't mind cleaning this up later. A good next step would probably be to add private `split_off_up_to`/`split_off_from` helper methods for slices since their behavior is commonly useful throughout the `Chunks` types. r? @scottmcm
2019-05-25Implement nth_back for slice::{Iter, IterMut}Tim Vermeulen-21/+55
2019-05-22Revert "Add implementations of last in terms of next_back on a bunch of ↵Steven Fackler-20/+0
DoubleEndedIterators." This reverts commit 3e86cf36b5114f201868bf459934fe346a76a2d4.
2019-05-22succint implementationwizAmit-0/+18
2019-05-22wip nth_back on chunks@amit.chandra-17/+0
Signed-off-by: wizAmit <amitforfriends_dns@yahoo.com>
2019-05-22hopefully working nth_back on chunks@amit.chandra-8/+9
Signed-off-by: wizAmit <amitforfriends_dns@yahoo.com>
2019-05-22wip nth_back on chunks@amit.chandra-0/+16
Signed-off-by: wizAmit <amitforfriends_dns@yahoo.com>
2019-05-14Rollup merge of #60443 - RalfJung:as_ptr, r=SimonSapinMazdak Farrokhzad-0/+6
as_ptr returns a read-only pointer Add comments to `as_ptr` methods to warn that these are read-only pointers, and writing to them is UB. [It was pointed out](https://internals.rust-lang.org/t/as-ptr-vs-as-mut-ptr/9940) that `CStr` does not even have an `as_mut_ptr`. I originally was going to add one, but there is no method at all that would mutate a `CStr`. Was that a deliberate choice or should I add an `as_mut_ptr` (similar to [what I did for `str`](https://github.com/rust-lang/rust/pull/58200))?
2019-05-14Rollup merge of #60130 - khuey:efficient_last, r=sfacklerMazdak Farrokhzad-0/+20
Add implementations of last in terms of next_back on a bunch of DoubleEndedIterators Provided a `DoubleEndedIterator` has finite length, `Iterator::last` is equivalent to `DoubleEndedIterator::next_back`. But searching forwards through the iterator when it's unnecessary is obviously not good for performance. I ran into this on one of the collection iterators. I tried adding appropriate overloads for a bunch of the iterator adapters like filter, map, etc, but I ran into a lot of type inference failures after doing so. The other interesting case is what to do with `Repeat`. Do we consider it part of the contract that `Iterator::last` will loop forever on it? The docs do say that the iterator will be evaluated until it returns None. This is also relevant for the adapters, it's trivially easy to observe whether a `Map` adapter invoked its closure a zillion times or just once for the last element.
2019-05-05Implement nth_back for RChunks(Exact)(Mut)Tim Vermeulen-0/+72
2019-05-02clarify wordingRalf Jung-2/+2
2019-05-01as_ptr returns a read-only pointerRalf Jung-0/+6
2019-04-27Rename .cap() methods to .capacity()Matthias Geier-2/+2
... but leave the old names in there for backwards compatibility.
2019-04-25ignore-tidy-filelength on all files with greater than 3000 linesvarkor-0/+2
2019-04-19Add implementations of last in terms of next_back on a bunch of ↵Kyle Huey-0/+20
DoubleEndedIterators. r?Manishearth
2019-04-19Rollup merge of #60098 - Centril:libcore-deny-more, r=varkorMazdak Farrokhzad-29/+29
libcore: deny `elided_lifetimes_in_paths` r? @varkor
2019-04-19Rollup merge of #60080 - nathankleyn:fix-issue-60068, r=CentrilMazdak Farrokhzad-3/+3
Fix small errors in docs for `rchunks_exact` and `rchunks_exact_mut`. The documentation for `rchunks_exact` said it started at the beginning of the slice, bit it actually starts at the end of the slice. In addition, there were a couple of "of the slice of the slice" duplicate phrases going on for `rchunks_exact` and `rchunks_exact_mut`. This fixes #60068.
2019-04-19libcore: deny more...Mazdak Farrokhzad-29/+29
2019-04-18Fix small errors in docs for `rchunks_exact` and `rchunks_exact_mut`.Nathan Kleyn-3/+3
The documentation for `rchunks_exact` said it started at the beginning of the slice, bit it actually starts at the end of the slice. In addition, there were a couple of "of the slice of the slice" duplicate phrases going on for `rchunks_exact` and `rchunks_exact_mut`. This fixes #60068.
2019-04-18libcore => 2018Taiki Endo-33/+34
2019-04-03Rollup merge of #55448 - Mokosha:SortAtIndex, r=blussMazdak Farrokhzad-0/+236
Add 'partition_at_index/_by/_by_key' for slices. This is an analog to C++'s std::nth_element (a.k.a. quickselect). Corresponds to tracking bug #55300.
2019-03-26adjust MaybeUninit API to discussionsRalf Jung-1/+1
uninitialized -> uninit into_initialized -> assume_init read_initialized -> read set -> write
2019-03-24Rollup merge of #59328 - koalatux:iter-nth-back, r=scottmcmkennytm-0/+13
Implement specialized nth_back() for Box and Windows. Hi there, this is my first pull request to rust :-) I started implementing some specializations for DoubleEndedIterator::nth_back() and these are the first two. The problem has been discussed in #54054 and nth_back() is tracked in #56995. I'm stuck with the next implementation so I though I do a PR for the ones I'm confident with to get some feedback.
2019-03-11Add initial implementation of 'sort_at_index' for slices -- analog to C++'s ↵Pavel Krajcevski-0/+236
std::nth_element (a.k.a. quickselect) Add some more notes to the documentation: - Mention that the median can be found if we used `len() / 2`. - Mention that this function is usually called "kth element" in other libraries. Address some comments in PR: - Change wording on some of the documentation - Change recursive function into a loop Update name to `partition_at_index` and add convenience return values. Address reviewer comments: - Don't swap on each iteration when searching for min/max element. - Add some docs about when we panic. - Test that the sum of the lengths of the output matches the length of the input. - Style fix for for-loop. Address more reviewer comments Fix Rng stuff for test Fix doc test build Don't run the partition_at_index test on wasm targets Miri does not support entropy for test partition_at_index
2019-03-05Add a tracking issue for new as_slice methodsJosh Stone-1/+1
2019-03-05Clean up the example on slice::IterMut::as_slice()Josh Stone-6/+2
2019-03-05Apply suggestions from code reviewMazdak Farrokhzad-4/+4
Co-Authored-By: cuviper <cuviper@gmail.com>
2019-03-04Add as_slice() to slice::IterMut and vec::DrainJosh Stone-0/+32
In bluss/indexmap#88, we found that there was no easy way to implement `Debug` for our `IterMut` and `Drain` iterators. Those are built on `slice::IterMut` and `vec::Drain`, which implement `Debug` themselves, but have no other way to access their data. With a new `as_slice()` method, we can read the data and customize its presentation.
2019-02-25heading # Unsafety => # Safety in stdlib docs.Mazdak Farrokhzad-2/+2
2019-02-24implement nth_back for WindowsAdrian Friedli-0/+13
2019-02-20Rollup merge of #58553 - scottmcm:more-ihle, r=Centrilkennytm-16/+16
Use more impl header lifetime elision Inspired by seeing explicit lifetimes on these two: - https://doc.rust-lang.org/nightly/std/slice/struct.Iter.html#impl-FusedIterator - https://doc.rust-lang.org/nightly/std/primitive.u32.html#impl-Not And a follow-up to https://github.com/rust-lang/rust/pull/54687, that started using IHLE in libcore. Most of the changes in here fall into two big categories: - Removing lifetimes from common traits that can essentially never user a lifetime from an input (particularly `Drop`, `Debug`, and `Clone`) - Forwarding impls that are only possible because the lifetime doesn't matter (like `impl<R: Read + ?Sized> Read for &mut R`) I omitted things that seemed like they could be more controversial, like the handful of iterators that have a `Item: 'static` despite the iterator having a lifetime or the `PartialEq` implementations [where the flipped one cannot elide the lifetime](https://internals.rust-lang.org/t/impl-type-parameter-aliases/9403/2?u=scottmcm). I also removed two lifetimes that turned out to be completely unused; see https://github.com/rust-lang/rust/issues/41960#issuecomment-464557423