-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add vector time complexity #121262
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
Add vector time complexity #121262
Changes from 7 commits
cb8ce9d
bb6dca0
0a5d684
d2f825f
ef1a584
a9cfeb3
bc52e5d
74151cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1490,6 +1490,12 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// vec.insert(4, 5); | ||
/// assert_eq!(vec, [1, 4, 2, 3, 5]); | ||
/// ``` | ||
/// | ||
/// # Time complexity | ||
/// | ||
/// Takes *O*([`Vec::len`]) time. All items after the insertion index must be | ||
/// shifted to the right. In the worst case, all elements are shifted when | ||
/// the insertion index is 0. | ||
#[cfg(not(no_global_oom_handling))] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn insert(&mut self, index: usize, element: T) { | ||
|
@@ -1912,6 +1918,14 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// vec.push(3); | ||
/// assert_eq!(vec, [1, 2, 3]); | ||
/// ``` | ||
/// | ||
/// # Time complexity | ||
/// | ||
/// Takes amortized *O*(1) time. If the vector's length would exceed its | ||
/// capacity after the push, the capacity is doubled by allocating | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While true for now, I don't think doubling is something we want to promise in documentation, just amortized growth. For example, some languages only double up to a certain size and then use a smaller growth factor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! I'll make it more vague There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of this? /// Takes amortized *O*(1) time. If the vector's length would exceed its
/// capacity after the push, *O*(*capacity*) time is taken to copy the
/// vector's elements to a larger allocation. This expensive operation is
/// offset by the *capacity* *O*(1) insertions it allows.
|
||
/// *O*(*capacity*) space, then *O*(*capacity*) time to copy the vector's | ||
/// elements. This expensive operation is offset by the *capacity* *O*(1) | ||
/// insertions it allows. | ||
#[cfg(not(no_global_oom_handling))] | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
@@ -1959,6 +1973,10 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// } | ||
/// assert_eq!(from_iter_fallible(0..100), Ok(Vec::from_iter(0..100))); | ||
/// ``` | ||
/// | ||
/// # Time complexity | ||
/// | ||
/// Takes *O*(1) time. | ||
#[inline] | ||
#[unstable(feature = "vec_push_within_capacity", issue = "100486")] | ||
pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> { | ||
|
@@ -1988,6 +2006,10 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// assert_eq!(vec.pop(), Some(3)); | ||
/// assert_eq!(vec, [1, 2]); | ||
/// ``` | ||
/// | ||
/// # Time complexity | ||
/// | ||
/// Takes *O*(1) time. | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn pop(&mut self) -> Option<T> { | ||
|
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.
And perhaps specify that if index = n then it's O(1).
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 it's fine -- Big O notation doesn't have to be precise, and this does represent the average (len/2) number of items that have to move, since constants like 1/2 are usually dropped.