Skip to content

Commit

Permalink
Auto merge of #74010 - pierwill:pierwill-o-notation, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Use italics for O notation

In documentation, I think it makes sense to italicize O notation (*O(n)*) as opposed to using back-ticks (`O(n)`). Visually, back-ticks focus the reader on the literal characters being used, making them ideal for representing code. Using italics, as far I can tell, more closely follows typographic conventions in mathematics and computer science.

Just a suggestion, of course! 😇
  • Loading branch information
bors committed Jul 20, 2020
2 parents 05630b0 + 76b8420 commit 7138410
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 51 deletions.
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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 }) }
Expand All @@ -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| {
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 10 additions & 10 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand Down Expand Up @@ -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
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand All @@ -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
///
Expand Down Expand Up @@ -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
Expand Down
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.

Expand Down Expand Up @@ -1512,7 +1512,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.
///
Expand Down Expand Up @@ -1547,7 +1547,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.
///
Expand Down Expand Up @@ -2331,7 +2331,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
///
Expand Down Expand Up @@ -2374,7 +2374,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
///
Expand Down Expand Up @@ -3076,15 +3076,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);
Expand Down
8 changes: 4 additions & 4 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,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
Expand Down Expand Up @@ -1262,7 +1262,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
Expand Down Expand Up @@ -1309,7 +1309,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
Expand Down Expand Up @@ -1971,7 +1971,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
Expand Down
14 changes: 7 additions & 7 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,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
///
Expand Down Expand Up @@ -1708,7 +1708,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
Expand Down Expand Up @@ -1763,8 +1763,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
///
Expand Down Expand Up @@ -1803,7 +1803,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.
Expand Down Expand Up @@ -1852,7 +1852,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
Expand Down Expand Up @@ -1906,7 +1906,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
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/slice/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
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
Expand Up @@ -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.
Expand All @@ -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.
///
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_index/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,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> {
Expand Down
Loading

0 comments on commit 7138410

Please sign in to comment.