Skip to content

Commit 1839673

Browse files
authored
Rollup merge of rust-lang#121262 - 20jasper:add-vector-time-complexity, r=cuviper
Add vector time complexity Added time complexity for `Vec` methods `push`, `push_within_capacity`, `pop`, and `insert`. <details> <summary> Reference images </summary> ![`Vec::push` documentation](https://github.com/rust-lang/rust/assets/78604367/dc966bbd-e92e-45a6-af82-35afabfa79a9) ![`Vec::push_within_capacity` documentation](https://github.com/rust-lang/rust/assets/78604367/9aadaf48-46ed-4fad-bdd5-74b98a61f4bb) ![`Vec::pop` documentation](https://github.com/rust-lang/rust/assets/78604367/88ec0389-a346-4ea5-a3b7-17caf514dd8b) ![`Vec::insert` documentation](https://github.com/rust-lang/rust/assets/78604367/960c15c3-ef8e-4aa7-badc-35ce80f6f221) </details> I followed a convention to use `#Time complexity` that I found in [the `BinaryHeap` documentation](https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#time-complexity-1). Looking through the rest of standard library collections, there is not a consistent way to handle this. [`Vec::swap_remove`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.swap_remove) does not have a dedicated section for time complexity but does list it. [`VecDeque::rotate_left`](https://doc.rust-lang.org/std/collections/struct.VecDeque.html#complexity) uses a `#complexity` heading.
2 parents fd7bb60 + 74151cb commit 1839673

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

library/alloc/src/vec/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,12 @@ impl<T, A: Allocator> Vec<T, A> {
15241524
/// vec.insert(4, 5);
15251525
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
15261526
/// ```
1527+
///
1528+
/// # Time complexity
1529+
///
1530+
/// Takes *O*([`Vec::len`]) time. All items after the insertion index must be
1531+
/// shifted to the right. In the worst case, all elements are shifted when
1532+
/// the insertion index is 0.
15271533
#[cfg(not(no_global_oom_handling))]
15281534
#[stable(feature = "rust1", since = "1.0.0")]
15291535
pub fn insert(&mut self, index: usize, element: T) {
@@ -1947,6 +1953,13 @@ impl<T, A: Allocator> Vec<T, A> {
19471953
/// vec.push(3);
19481954
/// assert_eq!(vec, [1, 2, 3]);
19491955
/// ```
1956+
///
1957+
/// # Time complexity
1958+
///
1959+
/// Takes amortized *O*(1) time. If the vector's length would exceed its
1960+
/// capacity after the push, *O*(*capacity*) time is taken to copy the
1961+
/// vector's elements to a larger allocation. This expensive operation is
1962+
/// offset by the *capacity* *O*(1) insertions it allows.
19501963
#[cfg(not(no_global_oom_handling))]
19511964
#[inline]
19521965
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1995,6 +2008,10 @@ impl<T, A: Allocator> Vec<T, A> {
19952008
/// }
19962009
/// assert_eq!(from_iter_fallible(0..100), Ok(Vec::from_iter(0..100)));
19972010
/// ```
2011+
///
2012+
/// # Time complexity
2013+
///
2014+
/// Takes *O*(1) time.
19982015
#[inline]
19992016
#[unstable(feature = "vec_push_within_capacity", issue = "100486")]
20002017
pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> {
@@ -2024,6 +2041,10 @@ impl<T, A: Allocator> Vec<T, A> {
20242041
/// assert_eq!(vec.pop(), Some(3));
20252042
/// assert_eq!(vec, [1, 2]);
20262043
/// ```
2044+
///
2045+
/// # Time complexity
2046+
///
2047+
/// Takes *O*(1) time.
20272048
#[inline]
20282049
#[stable(feature = "rust1", since = "1.0.0")]
20292050
pub fn pop(&mut self) -> Option<T> {

0 commit comments

Comments
 (0)