Skip to content

Commit 7138410

Browse files
committed
Auto merge of #74010 - pierwill:pierwill-o-notation, r=GuillaumeGomez
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! 😇
2 parents 05630b0 + 76b8420 commit 7138410

File tree

9 files changed

+51
-51
lines changed

9 files changed

+51
-51
lines changed

src/liballoc/collections/binary_heap.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! A priority queue implemented with a binary heap.
22
//!
3-
//! Insertion and popping the largest element have `O(log(n))` time complexity.
4-
//! Checking the largest element is `O(1)`. Converting a vector to a binary heap
5-
//! can be done in-place, and has `O(n)` complexity. A binary heap can also be
6-
//! converted to a sorted vector in-place, allowing it to be used for an `O(n * log(n))`
3+
//! Insertion and popping the largest element have *O*(log(*n*)) time complexity.
4+
//! Checking the largest element is *O*(1). Converting a vector to a binary heap
5+
//! can be done in-place, and has *O*(*n*) complexity. A binary heap can also be
6+
//! converted to a sorted vector in-place, allowing it to be used for an *O*(*n* \* log(*n*))
77
//! in-place heapsort.
88
//!
99
//! # Examples
@@ -235,7 +235,7 @@ use super::SpecExtend;
235235
///
236236
/// | [push] | [pop] | [peek]/[peek\_mut] |
237237
/// |--------|-----------|--------------------|
238-
/// | O(1)~ | O(log(n)) | O(1) |
238+
/// | O(1)~ | *O*(log(*n*)) | *O*(1) |
239239
///
240240
/// The value for `push` is an expected cost; the method documentation gives a
241241
/// more detailed analysis.
@@ -398,7 +398,7 @@ impl<T: Ord> BinaryHeap<T> {
398398
///
399399
/// # Time complexity
400400
///
401-
/// Cost is `O(1)` in the worst case.
401+
/// Cost is *O*(1) in the worst case.
402402
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
403403
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
404404
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: true }) }
@@ -422,7 +422,7 @@ impl<T: Ord> BinaryHeap<T> {
422422
///
423423
/// # Time complexity
424424
///
425-
/// The worst case cost of `pop` on a heap containing *n* elements is `O(log(n))`.
425+
/// The worst case cost of `pop` on a heap containing *n* elements is *O*(log(*n*)).
426426
#[stable(feature = "rust1", since = "1.0.0")]
427427
pub fn pop(&mut self) -> Option<T> {
428428
self.data.pop().map(|mut item| {
@@ -455,15 +455,15 @@ impl<T: Ord> BinaryHeap<T> {
455455
///
456456
/// The expected cost of `push`, averaged over every possible ordering of
457457
/// the elements being pushed, and over a sufficiently large number of
458-
/// pushes, is `O(1)`. This is the most meaningful cost metric when pushing
458+
/// pushes, is *O*(1). This is the most meaningful cost metric when pushing
459459
/// elements that are *not* already in any sorted pattern.
460460
///
461461
/// The time complexity degrades if elements are pushed in predominantly
462462
/// ascending order. In the worst case, elements are pushed in ascending
463-
/// sorted order and the amortized cost per push is `O(log(n))` against a heap
463+
/// sorted order and the amortized cost per push is *O*(log(*n*)) against a heap
464464
/// containing *n* elements.
465465
///
466-
/// The worst case cost of a *single* call to `push` is `O(n)`. The worst case
466+
/// The worst case cost of a *single* call to `push` is *O*(*n*). The worst case
467467
/// occurs when capacity is exhausted and needs a resize. The resize cost
468468
/// has been amortized in the previous figures.
469469
#[stable(feature = "rust1", since = "1.0.0")]
@@ -643,7 +643,7 @@ impl<T: Ord> BinaryHeap<T> {
643643
/// The remaining elements will be removed on drop in heap order.
644644
///
645645
/// Note:
646-
/// * `.drain_sorted()` is `O(n * log(n))`; much slower than `.drain()`.
646+
/// * `.drain_sorted()` is *O*(*n* \* log(*n*)); much slower than `.drain()`.
647647
/// You should use the latter for most cases.
648648
///
649649
/// # Examples
@@ -756,7 +756,7 @@ impl<T> BinaryHeap<T> {
756756
///
757757
/// # Time complexity
758758
///
759-
/// Cost is `O(1)` in the worst case.
759+
/// Cost is *O*(1) in the worst case.
760760
#[stable(feature = "rust1", since = "1.0.0")]
761761
pub fn peek(&self) -> Option<&T> {
762762
self.data.get(0)
@@ -1312,7 +1312,7 @@ unsafe impl<T: Ord> TrustedLen for DrainSorted<'_, T> {}
13121312
impl<T: Ord> From<Vec<T>> for BinaryHeap<T> {
13131313
/// Converts a `Vec<T>` into a `BinaryHeap<T>`.
13141314
///
1315-
/// This conversion happens in-place, and has `O(n)` time complexity.
1315+
/// This conversion happens in-place, and has *O*(*n*) time complexity.
13161316
fn from(vec: Vec<T>) -> BinaryHeap<T> {
13171317
let mut heap = BinaryHeap { data: vec };
13181318
heap.rebuild();

src/liballoc/collections/linked_list.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<T> LinkedList<T> {
404404
/// This reuses all the nodes from `other` and moves them into `self`. After
405405
/// this operation, `other` becomes empty.
406406
///
407-
/// This operation should compute in `O(1)` time and `O(1)` memory.
407+
/// This operation should compute in *O*(1) time and *O*(1) memory.
408408
///
409409
/// # Examples
410410
///
@@ -561,7 +561,7 @@ impl<T> LinkedList<T> {
561561

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

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

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

752752
/// Adds an element first in the list.
753753
///
754-
/// This operation should compute in `O(1)` time.
754+
/// This operation should compute in *O*(1) time.
755755
///
756756
/// # Examples
757757
///
@@ -774,7 +774,7 @@ impl<T> LinkedList<T> {
774774
/// Removes the first element and returns it, or `None` if the list is
775775
/// empty.
776776
///
777-
/// This operation should compute in `O(1)` time.
777+
/// This operation should compute in *O*(1) time.
778778
///
779779
/// # Examples
780780
///
@@ -797,7 +797,7 @@ impl<T> LinkedList<T> {
797797

798798
/// Appends an element to the back of a list.
799799
///
800-
/// This operation should compute in `O(1)` time.
800+
/// This operation should compute in *O*(1) time.
801801
///
802802
/// # Examples
803803
///
@@ -817,7 +817,7 @@ impl<T> LinkedList<T> {
817817
/// Removes the last element from a list and returns it, or `None` if
818818
/// it is empty.
819819
///
820-
/// This operation should compute in `O(1)` time.
820+
/// This operation should compute in *O*(1) time.
821821
///
822822
/// # Examples
823823
///
@@ -838,7 +838,7 @@ impl<T> LinkedList<T> {
838838
/// Splits the list into two at the given index. Returns everything after the given index,
839839
/// including the index.
840840
///
841-
/// This operation should compute in `O(n)` time.
841+
/// This operation should compute in *O*(*n*) time.
842842
///
843843
/// # Panics
844844
///
@@ -894,7 +894,7 @@ impl<T> LinkedList<T> {
894894

895895
/// Removes the element at the given index and returns it.
896896
///
897-
/// This operation should compute in `O(n)` time.
897+
/// This operation should compute in *O*(*n*) time.
898898
///
899899
/// # Panics
900900
/// Panics if at >= len

src/liballoc/collections/vec_deque.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A double-ended queue implemented with a growable ring buffer.
22
//!
3-
//! This queue has `O(1)` amortized inserts and removals from both ends of the
4-
//! container. It also has `O(1)` indexing like a vector. The contained elements
3+
//! This queue has *O*(1) amortized inserts and removals from both ends of the
4+
//! container. It also has *O*(1) indexing like a vector. The contained elements
55
//! are not required to be copyable, and the queue will be sendable if the
66
//! contained type is sendable.
77
@@ -1512,7 +1512,7 @@ impl<T> VecDeque<T> {
15121512
/// Removes an element from anywhere in the `VecDeque` and returns it,
15131513
/// replacing it with the first element.
15141514
///
1515-
/// This does not preserve ordering, but is `O(1)`.
1515+
/// This does not preserve ordering, but is *O*(1).
15161516
///
15171517
/// Returns `None` if `index` is out of bounds.
15181518
///
@@ -1547,7 +1547,7 @@ impl<T> VecDeque<T> {
15471547
/// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
15481548
/// last element.
15491549
///
1550-
/// This does not preserve ordering, but is `O(1)`.
1550+
/// This does not preserve ordering, but is *O*(1).
15511551
///
15521552
/// Returns `None` if `index` is out of bounds.
15531553
///
@@ -2331,7 +2331,7 @@ impl<T> VecDeque<T> {
23312331
///
23322332
/// # Complexity
23332333
///
2334-
/// Takes `O(min(mid, len() - mid))` time and no extra space.
2334+
/// Takes `*O*(min(mid, len() - mid))` time and no extra space.
23352335
///
23362336
/// # Examples
23372337
///
@@ -2374,7 +2374,7 @@ impl<T> VecDeque<T> {
23742374
///
23752375
/// # Complexity
23762376
///
2377-
/// Takes `O(min(k, len() - k))` time and no extra space.
2377+
/// Takes `*O*(min(k, len() - k))` time and no extra space.
23782378
///
23792379
/// # Examples
23802380
///
@@ -3076,15 +3076,15 @@ impl<T> From<VecDeque<T>> for Vec<T> {
30763076
/// [`Vec<T>`]: crate::vec::Vec
30773077
/// [`VecDeque<T>`]: crate::collections::VecDeque
30783078
///
3079-
/// This never needs to re-allocate, but does need to do `O(n)` data movement if
3079+
/// This never needs to re-allocate, but does need to do *O*(*n*) data movement if
30803080
/// the circular buffer doesn't happen to be at the beginning of the allocation.
30813081
///
30823082
/// # Examples
30833083
///
30843084
/// ```
30853085
/// use std::collections::VecDeque;
30863086
///
3087-
/// // This one is O(1).
3087+
/// // This one is *O*(1).
30883088
/// let deque: VecDeque<_> = (1..5).collect();
30893089
/// let ptr = deque.as_slices().0.as_ptr();
30903090
/// let vec = Vec::from(deque);

src/liballoc/string.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ impl String {
11621162

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

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

13101310
/// Inserts a string slice into this `String` at a byte position.
13111311
///
1312-
/// This is an `O(n)` operation as it requires copying every element in the
1312+
/// This is an *O*(*n*) operation as it requires copying every element in the
13131313
/// buffer.
13141314
///
13151315
/// # Panics
@@ -1971,7 +1971,7 @@ impl hash::Hash for String {
19711971
///
19721972
/// This consumes the `String` on the left-hand side and re-uses its buffer (growing it if
19731973
/// necessary). This is done to avoid allocating a new `String` and copying the entire contents on
1974-
/// every operation, which would lead to `O(n^2)` running time when building an `n`-byte string by
1974+
/// every operation, which would lead to *O*(*n*^2) running time when building an *n*-byte string by
19751975
/// repeated concatenation.
19761976
///
19771977
/// The string on the right-hand side is only borrowed; its contents are copied into the returned

src/libcore/slice/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ impl<T> [T] {
16721672
/// Sorts the slice, but may not preserve the order of equal elements.
16731673
///
16741674
/// This sort is unstable (i.e., may reorder equal elements), in-place
1675-
/// (i.e., does not allocate), and `O(n * log(n))` worst-case.
1675+
/// (i.e., does not allocate), and *O*(*n* \* log(*n*)) worst-case.
16761676
///
16771677
/// # Current implementation
16781678
///
@@ -1708,7 +1708,7 @@ impl<T> [T] {
17081708
/// elements.
17091709
///
17101710
/// This sort is unstable (i.e., may reorder equal elements), in-place
1711-
/// (i.e., does not allocate), and `O(n * log(n))` worst-case.
1711+
/// (i.e., does not allocate), and *O*(*n* \* log(*n*)) worst-case.
17121712
///
17131713
/// The comparator function must define a total ordering for the elements in the slice. If
17141714
/// the ordering is not total, the order of the elements is unspecified. An order is a
@@ -1763,8 +1763,8 @@ impl<T> [T] {
17631763
/// elements.
17641764
///
17651765
/// This sort is unstable (i.e., may reorder equal elements), in-place
1766-
/// (i.e., does not allocate), and `O(m * n * log(n))` worst-case, where the key function is
1767-
/// `O(m)`.
1766+
/// (i.e., does not allocate), and *O*(m \* *n* \* log(*n*)) worst-case, where the key function is
1767+
/// *O*(*m*).
17681768
///
17691769
/// # Current implementation
17701770
///
@@ -1803,7 +1803,7 @@ impl<T> [T] {
18031803
/// This reordering has the additional property that any value at position `i < index` will be
18041804
/// less than or equal to any value at a position `j > index`. Additionally, this reordering is
18051805
/// unstable (i.e. any number of equal elements may end up at position `index`), in-place
1806-
/// (i.e. does not allocate), and `O(n)` worst-case. This function is also/ known as "kth
1806+
/// (i.e. does not allocate), and *O*(*n*) worst-case. This function is also/ known as "kth
18071807
/// element" in other libraries. It returns a triplet of the following values: all elements less
18081808
/// than the one at the given index, the value at the given index, and all elements greater than
18091809
/// the one at the given index.
@@ -1852,7 +1852,7 @@ impl<T> [T] {
18521852
/// This reordering has the additional property that any value at position `i < index` will be
18531853
/// less than or equal to any value at a position `j > index` using the comparator function.
18541854
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
1855-
/// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
1855+
/// position `index`), in-place (i.e. does not allocate), and *O*(*n*) worst-case. This function
18561856
/// is also known as "kth element" in other libraries. It returns a triplet of the following
18571857
/// values: all elements less than the one at the given index, the value at the given index,
18581858
/// and all elements greater than the one at the given index, using the provided comparator
@@ -1906,7 +1906,7 @@ impl<T> [T] {
19061906
/// This reordering has the additional property that any value at position `i < index` will be
19071907
/// less than or equal to any value at a position `j > index` using the key extraction function.
19081908
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
1909-
/// position `index`), in-place (i.e. does not allocate), and `O(n)` worst-case. This function
1909+
/// position `index`), in-place (i.e. does not allocate), and *O*(*n*) worst-case. This function
19101910
/// is also known as "kth element" in other libraries. It returns a triplet of the following
19111911
/// values: all elements less than the one at the given index, the value at the given index, and
19121912
/// all elements greater than the one at the given index, using the provided key extraction

src/libcore/slice/sort.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ where
121121

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

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

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

754-
/// Sorts `v` using pattern-defeating quicksort, which is `O(n * log(n))` worst-case.
754+
/// Sorts `v` using pattern-defeating quicksort, which is *O*(*n* \* log(*n*)) worst-case.
755755
pub fn quicksort<T, F>(v: &mut [T], mut is_less: F)
756756
where
757757
F: FnMut(&T, &T) -> bool,

src/librustc_data_structures/sorted_map/index_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::iter::FromIterator;
77
use crate::stable_hasher::{HashStable, StableHasher};
88
use rustc_index::vec::{Idx, IndexVec};
99

10-
/// An indexed multi-map that preserves insertion order while permitting both `O(log n)` lookup of
11-
/// an item by key and `O(1)` lookup by index.
10+
/// An indexed multi-map that preserves insertion order while permitting both *O*(log *n*) lookup of
11+
/// an item by key and *O*(1) lookup by index.
1212
///
1313
/// This data structure is a hybrid of an [`IndexVec`] and a [`SortedMap`]. Like `IndexVec`,
1414
/// `SortedIndexMultiMap` assigns a typed index to each item while preserving insertion order.
@@ -20,7 +20,7 @@ use rustc_index::vec::{Idx, IndexVec};
2020
/// items will be yielded in insertion order.
2121
///
2222
/// Unlike a general-purpose map like `BTreeSet` or `HashSet`, `SortedMap` and
23-
/// `SortedIndexMultiMap` require `O(n)` time to insert a single item. This is because we may need
23+
/// `SortedIndexMultiMap` require *O*(*n*) time to insert a single item. This is because we may need
2424
/// to insert into the middle of the sorted array. Users should avoid mutating this data structure
2525
/// in-place.
2626
///

src/librustc_index/bit_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
772772
}
773773

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

0 commit comments

Comments
 (0)