|
25 | 25 | //! |
26 | 26 | //! Rust's collections can be grouped into four major categories: |
27 | 27 | //! |
28 | | -//! * Sequences: `Vec`, `VecDeque`, `LinkedList`, `BitVec` |
29 | | -//! * Maps: `HashMap`, `BTreeMap`, `VecMap` |
30 | | -//! * Sets: `HashSet`, `BTreeSet`, `BitSet` |
| 28 | +//! * Sequences: `Vec`, `VecDeque`, `LinkedList` |
| 29 | +//! * Maps: `HashMap`, `BTreeMap` |
| 30 | +//! * Sets: `HashSet`, `BTreeSet` |
31 | 31 | //! * Misc: `BinaryHeap` |
32 | 32 | //! |
33 | 33 | //! # When Should You Use Which Collection? |
|
70 | 70 | //! * You want to be able to get all of the entries in order on-demand. |
71 | 71 | //! * You want a sorted map. |
72 | 72 | //! |
73 | | -//! ### Use a `VecMap` when: |
74 | | -//! * You want a `HashMap` but with known to be small `usize` keys. |
75 | | -//! * You want a `BTreeMap`, but with known to be small `usize` keys. |
76 | | -//! |
77 | 73 | //! ### Use the `Set` variant of any of these `Map`s when: |
78 | 74 | //! * You just want to remember which keys you've seen. |
79 | 75 | //! * There is no meaningful value to associate with your keys. |
80 | 76 | //! * You just want a set. |
81 | 77 | //! |
82 | | -//! ### Use a `BitVec` when: |
83 | | -//! * You want to store an unbounded number of booleans in a small space. |
84 | | -//! * You want a bit vector. |
85 | | -//! |
86 | | -//! ### Use a `BitSet` when: |
87 | | -//! * You want a `BitVec`, but want `Set` properties |
88 | | -//! |
89 | 78 | //! ### Use a `BinaryHeap` when: |
90 | 79 | //! |
91 | 80 | //! * You want to store a bunch of elements, but only ever want to process the |
|
123 | 112 | //! | Vec | O(1) | O(n-i)* | O(n-i) | O(m)* | O(n-i) | |
124 | 113 | //! | VecDeque | O(1) | O(min(i, n-i))* | O(min(i, n-i)) | O(m)* | O(min(i, n-i)) | |
125 | 114 | //! | LinkedList | O(min(i, n-i)) | O(min(i, n-i)) | O(min(i, n-i)) | O(1) | O(min(i, n-i)) | |
126 | | -//! | BitVec | O(1) | O(n-i)* | O(n-i) | O(m)* | O(n-i) | |
127 | 115 | //! |
128 | 116 | //! Note that where ties occur, Vec is generally going to be faster than VecDeque, and VecDeque |
129 | | -//! is generally going to be faster than LinkedList. BitVec is not a general purpose collection, and |
130 | | -//! therefore cannot reasonably be compared. |
| 117 | +//! is generally going to be faster than LinkedList. |
131 | 118 | //! |
132 | 119 | //! ## Maps |
133 | 120 | //! |
134 | | -//! For Sets, all operations have the cost of the equivalent Map operation. For |
135 | | -//! BitSet, |
136 | | -//! refer to VecMap. |
| 121 | +//! For Sets, all operations have the cost of the equivalent Map operation. |
137 | 122 | //! |
138 | 123 | //! | | get | insert | remove | predecessor | |
139 | 124 | //! |----------|-----------|----------|----------|-------------| |
140 | 125 | //! | HashMap | O(1)~ | O(1)~* | O(1)~ | N/A | |
141 | 126 | //! | BTreeMap | O(log n) | O(log n) | O(log n) | O(log n) | |
142 | | -//! | VecMap | O(1) | O(1)? | O(1) | O(n) | |
143 | | -//! |
144 | | -//! Note that VecMap is *incredibly* inefficient in terms of space. The O(1) |
145 | | -//! insertion time assumes space for the element is already allocated. |
146 | | -//! Otherwise, a large key may require a massive reallocation, with no direct |
147 | | -//! relation to the number of elements in the collection. VecMap should only be |
148 | | -//! seriously considered for small keys. |
149 | 127 | //! |
150 | | -//! Note also that BTreeMap's precise performance depends on the value of B. |
| 128 | +//! Note that BTreeMap's precise performance depends on the value of B. |
151 | 129 | //! |
152 | 130 | //! # Correct and Efficient Usage of Collections |
153 | 131 | //! |
|
0 commit comments