Skip to content

Commit d5d9bf0

Browse files
authored
Rollup merge of #71167 - RalfJung:big-o, r=shepmaster
big-O notation: parenthesis for function calls, explicit multiplication I saw `O(n m log n)` in the docs and found that really hard to parse. In particular, I don't think we should use blank space as syntax for *both* multiplication and function calls, that is just confusing. This PR makes both multiplication and function calls explicit using Rust-like syntax. If you prefer, I can also leave one of them implicit, but I believe explicit is better here. While I was at it I also added backticks consistently.
2 parents b45f133 + 818bef5 commit d5d9bf0

File tree

9 files changed

+46
-47
lines changed

9 files changed

+46
-47
lines changed

src/liballoc/collections/binary_heap.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! A priority queue implemented with a binary heap.
22
//!
3-
//! Insertion and popping the largest element have `O(log n)` time complexity.
3+
//! Insertion and popping the largest element have `O(log(n))` time complexity.
44
//! Checking the largest element is `O(1)`. Converting a vector to a binary heap
55
//! 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
7-
//! log n)` in-place heapsort.
6+
//! converted to a sorted vector in-place, allowing it to be used for an `O(n * log(n))`
7+
//! in-place heapsort.
88
//!
99
//! # Examples
1010
//!
@@ -233,9 +233,9 @@ use super::SpecExtend;
233233
///
234234
/// # Time complexity
235235
///
236-
/// | [push] | [pop] | [peek]/[peek\_mut] |
237-
/// |--------|----------|--------------------|
238-
/// | O(1)~ | O(log n) | O(1) |
236+
/// | [push] | [pop] | [peek]/[peek\_mut] |
237+
/// |--------|-----------|--------------------|
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,8 +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
426-
/// n).
425+
/// The worst case cost of `pop` on a heap containing *n* elements is `O(log(n))`.
427426
#[stable(feature = "rust1", since = "1.0.0")]
428427
pub fn pop(&mut self) -> Option<T> {
429428
self.data.pop().map(|mut item| {
@@ -456,15 +455,15 @@ impl<T: Ord> BinaryHeap<T> {
456455
///
457456
/// The expected cost of `push`, averaged over every possible ordering of
458457
/// the elements being pushed, and over a sufficiently large number of
459-
/// 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
460459
/// elements that are *not* already in any sorted pattern.
461460
///
462461
/// The time complexity degrades if elements are pushed in predominantly
463462
/// ascending order. In the worst case, elements are pushed in ascending
464-
/// 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
465464
/// containing *n* elements.
466465
///
467-
/// 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
468467
/// occurs when capacity is exhausted and needs a resize. The resize cost
469468
/// has been amortized in the previous figures.
470469
#[stable(feature = "rust1", since = "1.0.0")]
@@ -623,7 +622,7 @@ impl<T: Ord> BinaryHeap<T> {
623622

624623
// `rebuild` takes O(len1 + len2) operations
625624
// and about 2 * (len1 + len2) comparisons in the worst case
626-
// while `extend` takes O(len2 * log_2(len1)) operations
625+
// while `extend` takes O(len2 * log(len1)) operations
627626
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
628627
// assuming len1 >= len2.
629628
#[inline]
@@ -644,7 +643,7 @@ impl<T: Ord> BinaryHeap<T> {
644643
/// The remaining elements will be removed on drop in heap order.
645644
///
646645
/// Note:
647-
/// * `.drain_sorted()` is O(n lg n); much slower than `.drain()`.
646+
/// * `.drain_sorted()` is `O(n * log(n))`; much slower than `.drain()`.
648647
/// You should use the latter for most cases.
649648
///
650649
/// # Examples
@@ -729,7 +728,7 @@ impl<T> BinaryHeap<T> {
729728
///
730729
/// # Time complexity
731730
///
732-
/// Cost is O(1) in the worst case.
731+
/// Cost is `O(1)` in the worst case.
733732
#[stable(feature = "rust1", since = "1.0.0")]
734733
pub fn peek(&self) -> Option<&T> {
735734
self.data.get(0)

src/liballoc/collections/btree/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use UnderflowResult::*;
4040
/// performance on *small* nodes of elements which are cheap to compare. However in the future we
4141
/// would like to further explore choosing the optimal search strategy based on the choice of B,
4242
/// and possibly other factors. Using linear search, searching for a random element is expected
43-
/// to take O(B log<sub>B</sub>n) comparisons, which is generally worse than a BST. In practice,
43+
/// to take O(B * log(n)) comparisons, which is generally worse than a BST. In practice,
4444
/// however, performance is excellent.
4545
///
4646
/// It is a logic error for a key to be modified in such a way that the key's ordering relative to

src/liballoc/collections/linked_list.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<T> LinkedList<T> {
390390
/// This reuses all the nodes from `other` and moves them into `self`. After
391391
/// this operation, `other` becomes empty.
392392
///
393-
/// This operation should compute in O(1) time and O(1) memory.
393+
/// This operation should compute in `O(1)` time and `O(1)` memory.
394394
///
395395
/// # Examples
396396
///
@@ -547,7 +547,7 @@ impl<T> LinkedList<T> {
547547

548548
/// Returns `true` if the `LinkedList` is empty.
549549
///
550-
/// This operation should compute in O(1) time.
550+
/// This operation should compute in `O(1)` time.
551551
///
552552
/// # Examples
553553
///
@@ -568,7 +568,7 @@ impl<T> LinkedList<T> {
568568

569569
/// Returns the length of the `LinkedList`.
570570
///
571-
/// This operation should compute in O(1) time.
571+
/// This operation should compute in `O(1)` time.
572572
///
573573
/// # Examples
574574
///
@@ -594,7 +594,7 @@ impl<T> LinkedList<T> {
594594

595595
/// Removes all elements from the `LinkedList`.
596596
///
597-
/// This operation should compute in O(n) time.
597+
/// This operation should compute in `O(n)` time.
598598
///
599599
/// # Examples
600600
///
@@ -737,7 +737,7 @@ impl<T> LinkedList<T> {
737737

738738
/// Adds an element first in the list.
739739
///
740-
/// This operation should compute in O(1) time.
740+
/// This operation should compute in `O(1)` time.
741741
///
742742
/// # Examples
743743
///
@@ -760,7 +760,7 @@ impl<T> LinkedList<T> {
760760
/// Removes the first element and returns it, or `None` if the list is
761761
/// empty.
762762
///
763-
/// This operation should compute in O(1) time.
763+
/// This operation should compute in `O(1)` time.
764764
///
765765
/// # Examples
766766
///
@@ -783,7 +783,7 @@ impl<T> LinkedList<T> {
783783

784784
/// Appends an element to the back of a list.
785785
///
786-
/// This operation should compute in O(1) time.
786+
/// This operation should compute in `O(1)` time.
787787
///
788788
/// # Examples
789789
///
@@ -803,7 +803,7 @@ impl<T> LinkedList<T> {
803803
/// Removes the last element from a list and returns it, or `None` if
804804
/// it is empty.
805805
///
806-
/// This operation should compute in O(1) time.
806+
/// This operation should compute in `O(1)` time.
807807
///
808808
/// # Examples
809809
///
@@ -824,7 +824,7 @@ impl<T> LinkedList<T> {
824824
/// Splits the list into two at the given index. Returns everything after the given index,
825825
/// including the index.
826826
///
827-
/// This operation should compute in O(n) time.
827+
/// This operation should compute in `O(n)` time.
828828
///
829829
/// # Panics
830830
///
@@ -880,7 +880,7 @@ impl<T> LinkedList<T> {
880880

881881
/// Removes the element at the given index and returns it.
882882
///
883-
/// This operation should compute in O(n) time.
883+
/// This operation should compute in `O(n)` time.
884884
///
885885
/// # Panics
886886
/// Panics if at >= len

src/liballoc/collections/vec_deque.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ impl<T> VecDeque<T> {
13911391
/// Removes an element from anywhere in the `VecDeque` and returns it,
13921392
/// replacing it with the first element.
13931393
///
1394-
/// This does not preserve ordering, but is O(1).
1394+
/// This does not preserve ordering, but is `O(1)`.
13951395
///
13961396
/// Returns `None` if `index` is out of bounds.
13971397
///
@@ -1426,7 +1426,7 @@ impl<T> VecDeque<T> {
14261426
/// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the
14271427
/// last element.
14281428
///
1429-
/// This does not preserve ordering, but is O(1).
1429+
/// This does not preserve ordering, but is `O(1)`.
14301430
///
14311431
/// Returns `None` if `index` is out of bounds.
14321432
///
@@ -2927,7 +2927,7 @@ impl<T> From<VecDeque<T>> for Vec<T> {
29272927
/// [`Vec<T>`]: crate::vec::Vec
29282928
/// [`VecDeque<T>`]: crate::collections::VecDeque
29292929
///
2930-
/// This never needs to re-allocate, but does need to do O(n) data movement if
2930+
/// This never needs to re-allocate, but does need to do `O(n)` data movement if
29312931
/// the circular buffer doesn't happen to be at the beginning of the allocation.
29322932
///
29332933
/// # Examples

src/liballoc/slice.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mod hack {
165165
impl<T> [T] {
166166
/// Sorts the slice.
167167
///
168-
/// This sort is stable (i.e., does not reorder equal elements) and `O(n log n)` worst-case.
168+
/// This sort is stable (i.e., does not reorder equal elements) and `O(n * log(n))` worst-case.
169169
///
170170
/// When applicable, unstable sorting is preferred because it is generally faster than stable
171171
/// sorting and it doesn't allocate auxiliary memory.
@@ -200,7 +200,7 @@ impl<T> [T] {
200200

201201
/// Sorts the slice with a comparator function.
202202
///
203-
/// This sort is stable (i.e., does not reorder equal elements) and `O(n log n)` worst-case.
203+
/// This sort is stable (i.e., does not reorder equal elements) and `O(n * log(n))` worst-case.
204204
///
205205
/// The comparator function must define a total ordering for the elements in the slice. If
206206
/// the ordering is not total, the order of the elements is unspecified. An order is a
@@ -254,7 +254,7 @@ impl<T> [T] {
254254

255255
/// Sorts the slice with a key extraction function.
256256
///
257-
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n log n)`
257+
/// This sort is stable (i.e., does not reorder equal elements) and `O(m * n * log(n))`
258258
/// worst-case, where the key function is `O(m)`.
259259
///
260260
/// For expensive key functions (e.g. functions that are not simple property accesses or
@@ -297,7 +297,7 @@ impl<T> [T] {
297297
///
298298
/// During sorting, the key function is called only once per element.
299299
///
300-
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n + n log n)`
300+
/// This sort is stable (i.e., does not reorder equal elements) and `O(m * n + n * log(n))`
301301
/// worst-case, where the key function is `O(m)`.
302302
///
303303
/// For simple key functions (e.g., functions that are property accesses or
@@ -935,7 +935,7 @@ where
935935
/// 1. for every `i` in `1..runs.len()`: `runs[i - 1].len > runs[i].len`
936936
/// 2. for every `i` in `2..runs.len()`: `runs[i - 2].len > runs[i - 1].len + runs[i].len`
937937
///
938-
/// The invariants ensure that the total running time is `O(n log n)` worst-case.
938+
/// The invariants ensure that the total running time is `O(n * log(n))` worst-case.
939939
fn merge_sort<T, F>(v: &mut [T], mut is_less: F)
940940
where
941941
F: FnMut(&T, &T) -> bool,

src/libcore/slice/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ impl<T> [T] {
16061606
/// Sorts the slice, but may not preserve the order of equal elements.
16071607
///
16081608
/// This sort is unstable (i.e., may reorder equal elements), in-place
1609-
/// (i.e., does not allocate), and `O(n log n)` worst-case.
1609+
/// (i.e., does not allocate), and `O(n * log(n))` worst-case.
16101610
///
16111611
/// # Current implementation
16121612
///
@@ -1642,7 +1642,7 @@ impl<T> [T] {
16421642
/// elements.
16431643
///
16441644
/// This sort is unstable (i.e., may reorder equal elements), in-place
1645-
/// (i.e., does not allocate), and `O(n log n)` worst-case.
1645+
/// (i.e., does not allocate), and `O(n * log(n))` worst-case.
16461646
///
16471647
/// The comparator function must define a total ordering for the elements in the slice. If
16481648
/// the ordering is not total, the order of the elements is unspecified. An order is a
@@ -1697,7 +1697,7 @@ impl<T> [T] {
16971697
/// elements.
16981698
///
16991699
/// This sort is unstable (i.e., may reorder equal elements), in-place
1700-
/// (i.e., does not allocate), and `O(m n log n)` worst-case, where the key function is
1700+
/// (i.e., does not allocate), and `O(m * n * log(n))` worst-case, where the key function is
17011701
/// `O(m)`.
17021702
///
17031703
/// # Current implementation
@@ -1957,7 +1957,7 @@ impl<T> [T] {
19571957
// over all the elements, swapping as we go so that at the end
19581958
// the elements we wish to keep are in the front, and those we
19591959
// wish to reject are at the back. We can then split the slice.
1960-
// This operation is still O(n).
1960+
// This operation is still `O(n)`.
19611961
//
19621962
// Example: We start in this state, where `r` represents "next
19631963
// read" and `w` represents "next_write`.

src/libcore/slice/sort.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ where
143143
}
144144
}
145145

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

623623
// If too many bad pivot choices were made, simply fall back to heapsort in order to
624-
// guarantee `O(n log n)` worst-case.
624+
// guarantee `O(n * log(n))` worst-case.
625625
if limit == 0 {
626626
heapsort(v, is_less);
627627
return;
@@ -684,7 +684,7 @@ where
684684
}
685685
}
686686

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

src/libstd/collections/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@
110110
//!
111111
//! For Sets, all operations have the cost of the equivalent Map operation.
112112
//!
113-
//! | | get | insert | remove | predecessor | append |
114-
//! |--------------|-----------|----------|----------|-------------|--------|
115-
//! | [`HashMap`] | O(1)~ | O(1)~* | O(1)~ | N/A | N/A |
116-
//! | [`BTreeMap`] | O(log n) | O(log n) | O(log n) | O(log n) | O(n+m) |
113+
//! | | get | insert | remove | predecessor | append |
114+
//! |--------------|-----------|-----------|-----------|-------------|--------|
115+
//! | [`HashMap`] | O(1)~ | O(1)~* | O(1)~ | N/A | N/A |
116+
//! | [`BTreeMap`] | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n+m) |
117117
//!
118118
//! # Correct and Efficient Usage of Collections
119119
//!

src/libstd/ffi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
//! terminator, so the buffer length is really `len+1` characters.
4444
//! Rust strings don't have a nul terminator; their length is always
4545
//! stored and does not need to be calculated. While in Rust
46-
//! accessing a string's length is a O(1) operation (because the
47-
//! length is stored); in C it is an O(length) operation because the
46+
//! accessing a string's length is a `O(1)` operation (because the
47+
//! length is stored); in C it is an `O(length)` operation because the
4848
//! length needs to be computed by scanning the string for the nul
4949
//! terminator.
5050
//!

0 commit comments

Comments
 (0)