Skip to content
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

Use italics for O notation #74010

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/liballoc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A priority queue implemented with a binary heap.
//!
//! Insertion and popping the largest element have `O(log(n))` time complexity.
//! Checking the largest element is `O(1)`. Converting a vector to a binary heap
//! can be done in-place, and has `O(n)` complexity. A binary heap can also be
//! converted to a sorted vector in-place, allowing it to be used for an `O(n * log(n))`
//! Insertion and popping the largest element have *O*(log(*n*)) time complexity.
//! Checking the largest element is *O*(1). Converting a vector to a binary heap
//! can be done in-place, and has *O*(*n*) complexity. A binary heap can also be
//! converted to a sorted vector in-place, allowing it to be used for an *O*(*n* \* log(*n*))
//! in-place heapsort.
//!
//! # Examples
@@ -235,7 +235,7 @@ use super::SpecExtend;
///
/// | [push] | [pop] | [peek]/[peek\_mut] |
/// |--------|-----------|--------------------|
/// | O(1)~ | O(log(n)) | O(1) |
/// | O(1)~ | *O*(log(*n*)) | *O*(1) |
///
/// The value for `push` is an expected cost; the method documentation gives a
/// more detailed analysis.
@@ -398,7 +398,7 @@ impl<T: Ord> BinaryHeap<T> {
///
/// # Time complexity
///
/// Cost is `O(1)` in the worst case.
/// Cost is *O*(1) in the worst case.
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: true }) }
@@ -422,7 +422,7 @@ impl<T: Ord> BinaryHeap<T> {
///
/// # Time complexity
///
/// The worst case cost of `pop` on a heap containing *n* elements is `O(log(n))`.
/// The worst case cost of `pop` on a heap containing *n* elements is *O*(log(*n*)).
#[stable(feature = "rust1", since = "1.0.0")]
pub fn pop(&mut self) -> Option<T> {
self.data.pop().map(|mut item| {
@@ -455,15 +455,15 @@ impl<T: Ord> BinaryHeap<T> {
///
/// The expected cost of `push`, averaged over every possible ordering of
/// the elements being pushed, and over a sufficiently large number of
/// pushes, is `O(1)`. This is the most meaningful cost metric when pushing
/// pushes, is *O*(1). This is the most meaningful cost metric when pushing
/// elements that are *not* already in any sorted pattern.
///
/// The time complexity degrades if elements are pushed in predominantly
/// ascending order. In the worst case, elements are pushed in ascending
/// sorted order and the amortized cost per push is `O(log(n))` against a heap
/// sorted order and the amortized cost per push is *O*(log(*n*)) against a heap
/// containing *n* elements.
///
/// The worst case cost of a *single* call to `push` is `O(n)`. The worst case
/// The worst case cost of a *single* call to `push` is *O*(*n*). The worst case
/// occurs when capacity is exhausted and needs a resize. The resize cost
/// has been amortized in the previous figures.
#[stable(feature = "rust1", since = "1.0.0")]
@@ -643,7 +643,7 @@ impl<T: Ord> BinaryHeap<T> {
/// The remaining elements will be removed on drop in heap order.
///
/// Note:
/// * `.drain_sorted()` is `O(n * log(n))`; much slower than `.drain()`.
/// * `.drain_sorted()` is *O*(*n* \* log(*n*)); much slower than `.drain()`.
/// You should use the latter for most cases.
///
/// # Examples
@@ -756,7 +756,7 @@ impl<T> BinaryHeap<T> {
///
/// # Time complexity
///
/// Cost is `O(1)` in the worst case.
/// Cost is *O*(1) in the worst case.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn peek(&self) -> Option<&T> {
self.data.get(0)
@@ -1312,7 +1312,7 @@ unsafe impl<T: Ord> TrustedLen for DrainSorted<'_, T> {}
impl<T: Ord> From<Vec<T>> for BinaryHeap<T> {
/// Converts a `Vec<T>` into a `BinaryHeap<T>`.
///
/// This conversion happens in-place, and has `O(n)` time complexity.
/// This conversion happens in-place, and has *O*(*n*) time complexity.
fn from(vec: Vec<T>) -> BinaryHeap<T> {
let mut heap = BinaryHeap { data: vec };
heap.rebuild();
20 changes: 10 additions & 10 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
@@ -404,7 +404,7 @@ impl<T> LinkedList<T> {
/// This reuses all the nodes from `other` and moves them into `self`. After
/// this operation, `other` becomes empty.
///
/// This operation should compute in `O(1)` time and `O(1)` memory.
/// This operation should compute in *O*(1) time and *O*(1) memory.
///
/// # Examples
///
@@ -561,7 +561,7 @@ impl<T> LinkedList<T> {

/// Returns `true` if the `LinkedList` is empty.
///
/// This operation should compute in `O(1)` time.
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
@@ -582,7 +582,7 @@ impl<T> LinkedList<T> {

/// Returns the length of the `LinkedList`.
///
/// This operation should compute in `O(1)` time.
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
@@ -608,7 +608,7 @@ impl<T> LinkedList<T> {

/// Removes all elements from the `LinkedList`.
///
/// This operation should compute in `O(n)` time.
/// This operation should compute in *O*(*n*) time.
///
/// # Examples
///
@@ -751,7 +751,7 @@ impl<T> LinkedList<T> {

/// Adds an element first in the list.
///
/// This operation should compute in `O(1)` time.
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
@@ -774,7 +774,7 @@ impl<T> LinkedList<T> {
/// Removes the first element and returns it, or `None` if the list is
/// empty.
///
/// This operation should compute in `O(1)` time.
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
@@ -797,7 +797,7 @@ impl<T> LinkedList<T> {

/// Appends an element to the back of a list.
///
/// This operation should compute in `O(1)` time.
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
@@ -817,7 +817,7 @@ impl<T> LinkedList<T> {
/// Removes the last element from a list and returns it, or `None` if
/// it is empty.
///
/// This operation should compute in `O(1)` time.
/// This operation should compute in *O*(1) time.
///
/// # Examples
///
@@ -838,7 +838,7 @@ impl<T> LinkedList<T> {
/// Splits the list into two at the given index. Returns everything after the given index,
/// including the index.
///
/// This operation should compute in `O(n)` time.
/// This operation should compute in *O*(*n*) time.
///
/// # Panics
///
@@ -894,7 +894,7 @@ impl<T> LinkedList<T> {

/// Removes the element at the given index and returns it.
///
/// This operation should compute in `O(n)` time.
/// This operation should compute in *O*(*n*) time.
///
/// # Panics
/// Panics if at >= len
16 changes: 8 additions & 8 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A double-ended queue implemented with a growable ring buffer.
//!
//! This queue has `O(1)` amortized inserts and removals from both ends of the
//! container. It also has `O(1)` indexing like a vector. The contained elements
//! This queue has *O*(1) amortized inserts and removals from both ends of the
//! container. It also has *O*(1) indexing like a vector. The contained elements
//! are not required to be copyable, and the queue will be sendable if the
//! contained type is sendable.

@@ -1422,7 +1422,7 @@ impl<T> VecDeque<T> {
/// Removes an element from anywhere in the `VecDeque` and returns it,
/// replacing it with the first element.
///
/// This does not preserve ordering, but is `O(1)`.
/// This does not preserve ordering, but is *O*(1).
///
/// Returns `None` if `index` is out of bounds.
///
@@ -1457,7 +1457,7 @@ impl<T> VecDeque<T> {
/// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
/// last element.
///
/// This does not preserve ordering, but is `O(1)`.
/// This does not preserve ordering, but is *O*(1).
///
/// Returns `None` if `index` is out of bounds.
///
@@ -2241,7 +2241,7 @@ impl<T> VecDeque<T> {
///
/// # Complexity
///
/// Takes `O(min(mid, len() - mid))` time and no extra space.
/// Takes `*O*(min(mid, len() - mid))` time and no extra space.
///
/// # Examples
///
@@ -2284,7 +2284,7 @@ impl<T> VecDeque<T> {
///
/// # Complexity
///
/// Takes `O(min(k, len() - k))` time and no extra space.
/// Takes `*O*(min(k, len() - k))` time and no extra space.
///
/// # Examples
///
@@ -2986,15 +2986,15 @@ impl<T> From<VecDeque<T>> for Vec<T> {
/// [`Vec<T>`]: crate::vec::Vec
/// [`VecDeque<T>`]: crate::collections::VecDeque
///
/// This never needs to re-allocate, but does need to do `O(n)` data movement if
/// This never needs to re-allocate, but does need to do *O*(*n*) data movement if
/// the circular buffer doesn't happen to be at the beginning of the allocation.
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
///
/// // This one is O(1).
/// // This one is *O*(1).
/// let deque: VecDeque<_> = (1..5).collect();
/// let ptr = deque.as_slices().0.as_ptr();
/// let vec = Vec::from(deque);
8 changes: 4 additions & 4 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
@@ -1187,7 +1187,7 @@ impl String {

/// Removes a [`char`] from this `String` at a byte position and returns it.
///
/// This is an `O(n)` operation, as it requires copying every element in the
/// This is an *O*(*n*) operation, as it requires copying every element in the
/// buffer.
///
/// # Panics
@@ -1289,7 +1289,7 @@ impl String {

/// Inserts a character into this `String` at a byte position.
///
/// This is an `O(n)` operation as it requires copying every element in the
/// This is an *O*(*n*) operation as it requires copying every element in the
/// buffer.
///
/// # Panics
@@ -1338,7 +1338,7 @@ impl String {

/// Inserts a string slice into this `String` at a byte position.
///
/// This is an `O(n)` operation as it requires copying every element in the
/// This is an *O*(*n*) operation as it requires copying every element in the
/// buffer.
///
/// # Panics
@@ -1996,7 +1996,7 @@ impl hash::Hash for String {
///
/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
/// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
/// every operation, which would lead to *O*(*n*^2) running time when building an *n*-byte string by
/// repeated concatenation.
///
/// The string on the right-hand side is only borrowed; its contents are copied into the returned
14 changes: 7 additions & 7 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
@@ -1668,7 +1668,7 @@ impl<T> [T] {
/// Sorts the slice, but may not preserve the order of equal elements.
///
/// This sort is unstable (i.e., may reorder equal elements), in-place
/// (i.e., does not allocate), and `O(n * log(n))` worst-case.
/// (i.e., does not allocate), and *O*(*n* \* log(*n*)) worst-case.
///
/// # Current implementation
///
@@ -1704,7 +1704,7 @@ impl<T> [T] {
/// elements.
///
/// This sort is unstable (i.e., may reorder equal elements), in-place
/// (i.e., does not allocate), and `O(n * log(n))` worst-case.
/// (i.e., does not allocate), and *O*(*n* \* log(*n*)) worst-case.
///
/// The comparator function must define a total ordering for the elements in the slice. If
/// the ordering is not total, the order of the elements is unspecified. An order is a
@@ -1759,8 +1759,8 @@ impl<T> [T] {
/// elements.
///
/// This sort is unstable (i.e., may reorder equal elements), in-place
/// (i.e., does not allocate), and `O(m * n * log(n))` worst-case, where the key function is
/// `O(m)`.
/// (i.e., does not allocate), and *O*(m \* *n* \* log(*n*)) worst-case, where the key function is
/// *O*(*m*).
///
/// # Current implementation
///
@@ -1799,7 +1799,7 @@ impl<T> [T] {
/// This reordering has the additional property that any value at position `i < index` will be
/// less than or equal to any value at a position `j > index`. Additionally, this reordering is
/// unstable (i.e. any number of equal elements may end up at position `index`), in-place
/// (i.e. does not allocate), and `O(n)` worst-case. This function is also/ known as "kth
/// (i.e. does not allocate), and *O*(*n*) worst-case. This function is also/ known as "kth
/// element" in other libraries. It returns a triplet of the following values: all elements less
/// than the one at the given index, the value at the given index, and all elements greater than
/// the one at the given index.
@@ -1848,7 +1848,7 @@ impl<T> [T] {
/// This reordering has the additional property that any value at position `i < index` will be
/// less than or equal to any value at a position `j > index` using the comparator function.
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
/// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
/// position `index`), in-place (i.e. does not allocate), and *O*(*n*) worst-case. This function
/// is also known as "kth element" in other libraries. It returns a triplet of the following
/// values: all elements less than the one at the given index, the value at the given index,
/// and all elements greater than the one at the given index, using the provided comparator
@@ -1902,7 +1902,7 @@ impl<T> [T] {
/// This reordering has the additional property that any value at position `i < index` will be
/// less than or equal to any value at a position `j > index` using the key extraction function.
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
/// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
/// position `index`), in-place (i.e. does not allocate), and *O*(*n*) worst-case. This function
/// is also known as "kth element" in other libraries. It returns a triplet of the following
/// values: all elements less than the one at the given index, the value at the given index, and
/// all elements greater than the one at the given index, using the provided key extraction
8 changes: 4 additions & 4 deletions src/libcore/slice/sort.rs
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ where

/// Partially sorts a slice by shifting several out-of-order elements around.
///
/// Returns `true` if the slice is sorted at the end. This function is `O(n)` worst-case.
/// Returns `true` if the slice is sorted at the end. This function is *O*(*n*) worst-case.
#[cold]
fn partial_insertion_sort<T, F>(v: &mut [T], is_less: &mut F) -> bool
where
@@ -168,7 +168,7 @@ where
false
}

/// Sorts a slice using insertion sort, which is `O(n^2)` worst-case.
/// Sorts a slice using insertion sort, which is *O*(*n*^2) worst-case.
fn insertion_sort<T, F>(v: &mut [T], is_less: &mut F)
where
F: FnMut(&T, &T) -> bool,
@@ -178,7 +178,7 @@ where
}
}

/// Sorts `v` using heapsort, which guarantees `O(n * log(n))` worst-case.
/// Sorts `v` using heapsort, which guarantees *O*(*n* \* log(*n*)) worst-case.
#[cold]
pub fn heapsort<T, F>(v: &mut [T], is_less: &mut F)
where
@@ -751,7 +751,7 @@ where
}
}

/// Sorts `v` using pattern-defeating quicksort, which is `O(n * log(n))` worst-case.
/// Sorts `v` using pattern-defeating quicksort, which is *O*(*n* \* log(*n*)) worst-case.
pub fn quicksort<T, F>(v: &mut [T], mut is_less: F)
where
F: FnMut(&T, &T) -> bool,
6 changes: 3 additions & 3 deletions src/librustc_data_structures/sorted_map/index_map.rs
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ use std::iter::FromIterator;
use crate::stable_hasher::{HashStable, StableHasher};
use rustc_index::vec::{Idx, IndexVec};

/// An indexed multi-map that preserves insertion order while permitting both `O(log n)` lookup of
/// an item by key and `O(1)` lookup by index.
/// An indexed multi-map that preserves insertion order while permitting both *O*(log *n*) lookup of
/// an item by key and *O*(1) lookup by index.
///
/// This data structure is a hybrid of an [`IndexVec`] and a [`SortedMap`]. Like `IndexVec`,
/// `SortedIndexMultiMap` assigns a typed index to each item while preserving insertion order.
@@ -20,7 +20,7 @@ use rustc_index::vec::{Idx, IndexVec};
/// items will be yielded in insertion order.
///
/// Unlike a general-purpose map like `BTreeSet` or `HashSet`, `SortedMap` and
/// `SortedIndexMultiMap` require `O(n)` time to insert a single item. This is because we may need
/// `SortedIndexMultiMap` require *O*(*n*) time to insert a single item. This is because we may need
/// to insert into the middle of the sorted array. Users should avoid mutating this data structure
/// in-place.
///
2 changes: 1 addition & 1 deletion src/librustc_index/bit_set.rs
Original file line number Diff line number Diff line change
@@ -773,7 +773,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
}

/// Returns those indices that are true in rows `a` and `b`. This
/// is an O(n) operation where `n` is the number of elements
/// is an *O*(*n*) operation where *n* is the number of elements
/// (somewhat independent from the actual size of the
/// intersection, in particular).
pub fn intersect_rows(&self, row1: R, row2: R) -> Vec<C> {
Loading