Skip to content

Commit 6f68f81

Browse files
committed
Applied requested changes
- typos and wording - merged Sequence and Map performance cost tables
1 parent 1c5ee9b commit 6f68f81

File tree

1 file changed

+21
-24
lines changed
  • library/std/src/collections

1 file changed

+21
-24
lines changed

library/std/src/collections/mod.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@
7979
//! see each type's documentation, and note that the names of actual methods may
8080
//! differ from the tables below on certain collections.
8181
//!
82-
//! Throughout the documentation, we will adhere to the following conventions for
83-
//! operation notation:
82+
//! Throughout the documentation, we will adhere to the following conventions
83+
//! for operation notation:
8484
//!
8585
//! * The collection's size is denoted by `n`.
86-
//! * If a second collection is involved, it's size is denoted by `m`.
86+
//! * If a second collection is involved, its size is denoted by `m`.
8787
//! * Item indices are denoted by `i`.
8888
//! * Operations which have an *amortized* cost are suffixed with a `*`.
8989
//! * Operations with an *expected* cost are suffixed with a `~`.
@@ -94,38 +94,35 @@
9494
//! *Amortized* costs are calculated to account for the time cost of such resize
9595
//! operations *over a sufficiently large series of operations*. An individual
9696
//! operation may be slower or faster due to the sporadic nature of collection
97-
//! resizing, however the average cost per operation will eventually equal the
98-
//! amortized cost.
97+
//! resizing, however the average cost per operation will approach the amortized
98+
//! cost.
9999
//!
100100
//! Rust's collections never automatically shrink, so removal operations aren't
101101
//! amortized.
102102
//!
103-
//! [`HashMap`] uses *expected* costs to denote that it is theoretically possible,
104-
//! though very unlikely, for [`HashMap`] to experience worse performance than
105-
//! the expected cost. This is due to the probablilistic nature of hashing -
106-
//! i.e. it is possible to generate a duplicate hash given some input key that
107-
//! requires extra computation to correct, but exceptionally unlikely.
103+
//! [`HashMap`] uses *expected* costs. It is theoretically possible, though very
104+
//! unlikely, for [`HashMap`] to experience significantly worse performance than
105+
//! the expected cost. This is due to the probabilistic nature of hashing - i.e.
106+
//! it is possible to generate a duplicate hash given some input key that will
107+
//! requires extra computation to correct.
108108
//!
109-
//! ## Cost of Sequence Operations
109+
//! ## Cost of Collection Operations
110110
//!
111-
//! | | get(i) | insert(i) | remove(i) | append(Vec(m)) | split_off(i) |
112-
//! |----------------|------------------------|-------------------------|------------------------|-------------------|------------------------|
113-
//! | [`Vec`] | *O*(1) | *O*(*n*-*i*)* | *O*(*n*-*i*) | *O*(*m*)* | *O*(*n*-*i*) |
114-
//! | [`VecDeque`] | *O*(1) | *O*(min(*i*, *n*-*i*))* | *O*(min(*i*, *n*-*i*)) | *O*(*m*)* | *O*(min(*i*, *n*-*i*)) |
115-
//! | [`LinkedList`] | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(1) | *O*(min(*i*, *n*-*i*)) |
116111
//!
117-
//! Note that where ties occur, [`Vec`] is generally going to be faster than [`VecDeque`], and
118-
//! [`VecDeque`] is generally going to be faster than [`LinkedList`].
112+
//! | | get(i) | insert(i) | remove(i) | append(Vec(m)) | split_off(i) | range | append |
113+
//! |----------------|------------------------|-------------------------|------------------------|-------------------|------------------------|-----------------|--------------|
114+
//! | [`Vec`] | *O*(1) | *O*(*n*-*i*)* | *O*(*n*-*i*) | *O*(*m*)* | *O*(*n*-*i*) | N/A | N/A |
115+
//! | [`VecDeque`] | *O*(1) | *O*(min(*i*, *n*-*i*))* | *O*(min(*i*, *n*-*i*)) | *O*(*m*)* | *O*(min(*i*, *n*-*i*)) | N/A | N/A |
116+
//! | [`LinkedList`] | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(1) | *O*(min(*i*, *n*-*i*)) | N/A | N/A |
117+
//! | [`HashMap`] | *O*(1)~ | *O*(1)~* | *O*(1)~ | N/A | N/A | N/A | N/A |
118+
//! | [`BTreeMap`] | *O*(log(*n*)) | *O*(log(*n*)) | *O*(log(*n*)) | N/A | N/A | *O*(log(*n*)) | *O*(*n*+*m*) |
119119
//!
120-
//! ## Cost of Map Operations
120+
//! Note that where ties occur, [`Vec`] is generally going to be faster than
121+
//! [`VecDeque`], and [`VecDeque`] is generally going to be faster than
122+
//! [`LinkedList`].
121123
//!
122124
//! For Sets, all operations have the cost of the equivalent Map operation.
123125
//!
124-
//! | | get | insert | remove | range | append |
125-
//! |--------------|---------------|---------------|---------------|---------------|--------------|
126-
//! | [`HashMap`] | *O*(1)~ | *O*(1)~* | *O*(1)~ | N/A | N/A |
127-
//! | [`BTreeMap`] | *O*(log(*n*)) | *O*(log(*n*)) | *O*(log(*n*)) | *O*(log(*n*)) | *O*(*n*+*m*) |
128-
//!
129126
//! # Correct and Efficient Usage of Collections
130127
//!
131128
//! Of course, knowing which collection is the right one for the job doesn't

0 commit comments

Comments
 (0)