about summary refs log tree commit diff
path: root/library/alloc/src/collections
AgeCommit message (Collapse)AuthorLines
2023-04-03Auto merge of #108448 - ishitatsuyuki:binary-heap, r=Mark-Simulacrumbors-51/+19
binary_heap: Optimize Extend implementation. This PR makes the `Extend` implementation for `BinaryHeap` no longer rely on specialization, so that it always use the bulk rebuild optimization that was previously only available for the `Vec` specialization.
2023-04-02Auto merge of #109701 - Amanieu:binaryheap_retain, r=ChrisDentonbors-2/+1
Stabilize `binary_heap_retain` FCP finished in tracking issue: #71503
2023-03-30Rollup merge of #106985 - jofas:106746-fix, r=ChrisDentonYuki Okushi-10/+10
Enhanced doucmentation of binary search methods for `slice` and `VecDeque` for unsorted instances Fixes #106746. Issue #106746 raises the concern that the binary search methods for slices and deques aren't explicit enough about the fact that they are only applicable to sorted slices/deques. I changed the explanation for these methods. I took the relatively harsh description of the behaviour of binary search on unsorted collections ("unspecified and meaningless") from the description of the [`partition_point`](https://doc.rust-lang.org/std/primitive.slice.html#method.partition_point) method: > If this slice is not partitioned, the returned result is unspecified and meaningless, as this method performs a kind of binary search.
2023-03-30removed deprecated markdown links from documentationjofas-3/+0
2023-03-29enhanced documentation of binary search methods for slice and VecDeque for ↵jofas-7/+10
unsorted instances
2023-03-28Stabilize `binary_heap_retain`Amanieu d'Antras-2/+1
FCP finished in tracking issue: #71503
2023-03-27fix advance_by impl for vec_deque and add testsThe 8472-7/+7
2023-03-27replace advance_by returning usize with Result<(), NonZeroUsize>The 8472-29/+39
2023-03-27Change advance(_back)_by to return `usize` instead of `Result<(), usize>`The 8472-32/+30
A successful advance is now signalled by returning `0` and other values now represent the remaining number of steps that couldn't be advanced as opposed to the amount of steps that have been advanced during a partial advance_by. This simplifies adapters a bit, replacing some `match`/`if` with arithmetic. Whether this is beneficial overall depends on whether `advance_by` is mostly used as a building-block for other iterator methods and adapters or whether we also see uses by users where `Result` might be more useful.
2023-03-25Auto merge of #99929 - the8472:default-iters, r=scottmcmbors-0/+228
Implement Default for some alloc/core iterators Add `Default` impls to the following collection iterators: * slice::{Iter, IterMut} * binary_heap::IntoIter * btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues} * btree::set::{Iter, IntoIter, Range} * linked_list::IntoIter * vec::IntoIter and these adapters: * adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev} For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change. These changes will be insta-stable. ACP: https://github.com/rust-lang/libs-team/issues/77
2023-03-20Remove outdated commentsMaybe Waffle-6/+0
2023-03-11Rollup merge of #106276 - Sp00ph:unify_slice_ranges, r=the8472Matthias Krüger-36/+31
Fix `vec_deque::Drain` FIXME In my original `VecDeque` rewrite, I didn't use `VecDeque::slice_ranges` in `Drain::as_slices`, even though that's basically the exact use case for `slice_ranges`. The reason for this was that a `VecDeque` wrapped in a `Drain` actually has its length set to `drain_start`, so that there's no potential use after free if you `mem::forget` the `Drain`. I modified `slice_ranges` to accept an explicit `len` parameter instead, which it now uses to bounds check the given range. This way, `Drain::as_slices` can use `slice_ranges` internally instead of having to basically just copy paste the `slice_ranges` code. Since `slice_ranges` is just an internal helper function, this shouldn't change the user facing behavior in any way.
2023-03-01Rollup merge of #108462 - pommicket:fix-vecdeque-zst-overflow, r=AmanieuDylan DPC-1/+1
Fix `VecDeque::append` capacity overflow for ZSTs Fixes #108454.
2023-02-28Support allocators in various Default for IntoIter implsThe 8472-5/+17
Global implements Default so we can use that as bound for all allocators
2023-02-28rewrite iterator `Default` tests as doctestsThe 8472-22/+98
2023-02-28Implement Default for some alloc/core iteratorsThe 8472-0/+140
This way one can `mem::take()` them out of structs or #[derive(Default)] on structs containing them. These changes will be insta-stable.
2023-02-26Disambiguate commentsMarkus Everling-2/+2
2023-02-26Fix `VecDeque::shrink_to` and add tests.Markus Everling-55/+104
This adds both a test specific to #108453 as well as an exhaustive test that goes through all possible combinations of head index, length and target capacity for a deque with capacity 16.
2023-02-25Use checked_add in VecDeque::append for ZSTs to avoid overflowpommicket-1/+1
2023-02-25binary_heap: Unify Extend implementation.Tatsuyuki Ishi-34/+2
Previously the bulk rebuild specialization was only available with Vec, and for general iterators Extend only provided pre-allocation through reserve(). By using a drop guard, we can safely bulk rebuild even if the iterator may panic. This allows benefiting from the bulk rebuild optimization without collecting iterator elements into a Vec beforehand, which would nullify any performance gains from bulk rebuild.
2023-02-25binary_heap: Make RebuildOnDrop a common helper.Tatsuyuki Ishi-17/+17
This helper was written for retain() but will also be useful for extend().
2023-02-24Rollup merge of #106918 - dtolnay:heapretain, r=the8472Dylan DPC-6/+37
Rebuild BinaryHeap on unwind from retain This closes the hole identified in https://github.com/rust-lang/rust/issues/71503#issuecomment-1383251315 which had made it possible for the caller to end up with a heap in invalid state. As of #105851, heaps in invalid state are not supposed to exist.
2023-02-20Changes according to reviewMarkus Everling-11/+11
2023-02-18Auto merge of #106241 - Sp00ph:vec_deque_iter_methods, r=the8472bors-1/+184
Implement more methods for `vec_deque::IntoIter` This implements a couple `Iterator` methods on `vec_deque::IntoIter` (`(try_)fold`, `(try_)rfold` `advance_(back_)by`, `next_chunk`, `count` and `last`) to allow these to be more efficient than their default implementations, also allowing many other `Iterator` methods that use these under the hood to take advantage of these manual implementations. `vec::IntoIter` has similar implementations for many of these methods. This PR does not yet implement `TrustedRandomAccess` and friends, as I'm not very familiar with the required safety guarantees. r? `@the8472` (since you also took over my last PR)
2023-02-08Rollup merge of #105641 - Amanieu:btree_cursor, r=m-ou-seMatthias Krüger-41/+957
Implement cursors for BTreeMap See the ACP for an overview of the API: https://github.com/rust-lang/libs-team/issues/141 The implementation is split into 2 commits: - The first changes the internal insertion functions to return a handle to the newly inserted element. The lifetimes involved are a bit hairy since we need a mutable handle to both the `BTreeMap` itself (which holds the root) and the nodes allocated in memory. I have tested that this passes the standard library testsuite under miri. - The second commit implements the cursor API itself. This is more straightforward to follow but still involves some unsafe code to deal with simultaneous mutable borrows of the tree root and the node that is currently being iterated.
2023-02-05Add `slice_ranges` safety commentMarkus Everling-5/+12
2023-02-01BTreeMap: Add Cursor and CursorMutAmanieu d'Antras-5/+839
2023-02-01BTreeMap: Change internal insert function to return a handleAmanieu d'Antras-37/+119
This is a prerequisite for cursor support for `BTreeMap`.
2023-01-31Fix `vec_deque::Drain` FIXMEMarkus Everling-31/+19
2023-01-25Set version placeholders to 1.68Mark Rousskov-1/+1
2023-01-15Rebuild BinaryHeap on unwind from retainDavid Tolnay-7/+21
2023-01-15Add test showing broken behavior of BinaryHeap::retainDavid Tolnay-0/+17
2023-01-14Document guarantees about BinaryHeap invariantDavid Tolnay-1/+9
2023-01-14Leak amplification for peek_mut() to ensure BinaryHeap's invariant is always metDavid Tolnay-9/+46
2023-01-14Add test of leaking a binary_heap PeekMutDavid Tolnay-0/+19
2023-01-10mv binary_heap.rs binary_heap/mod.rsAlan Egerton-0/+0
2023-01-08Rollup merge of #106562 - clubby789:vec-deque-example, r=Mark-SimulacrumYuki Okushi-1/+3
Clarify examples for `VecDeque::get/get_mut` Closes #106114 ``@rustbot`` label +A-docs
2023-01-08Auto merge of #104658 - thomcc:rand-update-and-usable-no_std, r=Mark-Simulacrumbors-8/+9
Update `rand` in the stdlib tests, and remove the `getrandom` feature from it. The main goal is actually removing `getrandom`, so that eventually we can allow running the stdlib test suite on tier3 targets which don't have `getrandom` support. Currently those targets can only run the subset of stdlib tests that exist in uitests, and (generally speaking), we prefer not to test libstd functionality in uitests, which came up recently in https://github.com/rust-lang/rust/pull/104095 and https://github.com/rust-lang/rust/pull/104185. Additionally, the fact that we can't update `rand`/`getrandom` means we're stuck with the old set of tier3 targets, so can't test new ones. ~~Anyway, I haven't checked that this actually does allow use on tier3 targets (I think it does not, as some work is needed in stdlib submodules) but it moves us slightly closer to this, and seems to allow at least finally updating our `rand` dep, which definitely improves the status quo.~~ Checked and works now. For the most part, our tests and benchmarks are fine using hard-coded seeds. A couple tests seem to fail with this (stuff manipulating the environment expecting no collisions, for example), or become pointless (all inputs to a function become equivalent). In these cases I've done a (gross) dance (ab)using `RandomState` and `Location::caller()` for some extra "entropy". Trying to share that code seems *way* more painful than it's worth given that the duplication is a 7-line function, even if the lines are quite gross. (Keeping in mind that sharing it would require adding `rand` as a non-dev dep to std, and exposing a type from it publicly, all of which sounds truly awful, even if done behind a perma-unstable feature). See also some previous attempts: - https://github.com/rust-lang/rust/pull/86963 (in particular https://github.com/rust-lang/rust/pull/86963#issuecomment-885438936 which explains why this is non-trivial) - https://github.com/rust-lang/rust/pull/89131 - https://github.com/rust-lang/rust/pull/96626#issuecomment-1114562857 (I tried in that PR at the same time, but settled for just removing the usage of `thread_rng()` from the benchmarks, since that was the main goal). - https://github.com/rust-lang/rust/pull/104185 - Probably more. It's very tempting of a thing to "just update". r? `@Mark-Simulacrum`
2023-01-07Rollup merge of #105128 - Sp00ph:vec_vec_deque_conversion, r=dtolnayMatthias Krüger-3/+3
Add O(1) `Vec -> VecDeque` conversion guarantee (See #105072)
2023-01-07Clarify examples for `VecDeque::get/get_mut`clubby789-1/+3
2023-01-04Update rand in the stdlib tests, and remove the getrandom feature from itThom Chiovoloni-8/+9
2022-12-29Implement more methods for `vec_deque::IntoIter`Markus Everling-1/+184
2022-12-28fix documenting private items of standard libraryLukas Markeffsky-11/+17
2022-12-28Rollup merge of #94145 - ssomers:binary_heap_tests, r=jyn514fee1-dead-291/+107
Test leaking of BinaryHeap Drain iterators Add test cases about forgetting the `BinaryHeap::Drain` iterator, and slightly fortifies some other test cases. Consists of separate commits that I don't think are relevant on their own (but I'll happily turn these into more PRs if desired).
2022-12-20Auto merge of #105127 - Sp00ph:const_new, r=dtolnaybors-3/+4
Make `VecDeque::new` const (See #105072)
2022-12-15doc: Fix a few small issuesHannes Körber-1/+1
* A few typos around generic types (`;` vs `,`) * Use inline code formatting for code fragments * One instance of wrong wording
2022-12-08Apply review feedback; Fix no_global_oom_handling buildScott McMurray-0/+3
2022-12-08Make `VecDeque::from_iter` O(1) from `vec(_deque)::IntoIter`Scott McMurray-11/+71
2022-12-05Add O(1) `Vec -> VecDeque` conversion guaranteeMarkus Everling-3/+3
2022-12-05Auto merge of #105046 - scottmcm:vecdeque-vs-vec, r=Mark-Simulacrumbors-5/+12
Send `VecDeque::from_iter` via `Vec::from_iter` Since it's O(1) to convert between them now, might as well reuse the logic. Mostly for the various specializations it does, but might also save some monomorphization work if, say, people collect slice iterators into both `Vec`s and `VecDeque`s.