/rust/registry/src/index.crates.io-6f17d22bba15001f/indexmap-2.10.0/src/map.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! [`IndexMap`] is a hash table where the iteration order of the key-value |
2 | | //! pairs is independent of the hash values of the keys. |
3 | | |
4 | | mod core; |
5 | | mod iter; |
6 | | mod mutable; |
7 | | mod slice; |
8 | | |
9 | | #[cfg(feature = "serde")] |
10 | | #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] |
11 | | pub mod serde_seq; |
12 | | |
13 | | #[cfg(test)] |
14 | | mod tests; |
15 | | |
16 | | pub use self::core::raw_entry_v1::{self, RawEntryApiV1}; |
17 | | pub use self::core::{Entry, IndexedEntry, OccupiedEntry, VacantEntry}; |
18 | | pub use self::iter::{ |
19 | | Drain, ExtractIf, IntoIter, IntoKeys, IntoValues, Iter, IterMut, IterMut2, Keys, Splice, |
20 | | Values, ValuesMut, |
21 | | }; |
22 | | pub use self::mutable::MutableEntryKey; |
23 | | pub use self::mutable::MutableKeys; |
24 | | pub use self::slice::Slice; |
25 | | |
26 | | #[cfg(feature = "rayon")] |
27 | | pub use crate::rayon::map as rayon; |
28 | | |
29 | | use ::core::cmp::Ordering; |
30 | | use ::core::fmt; |
31 | | use ::core::hash::{BuildHasher, Hash, Hasher}; |
32 | | use ::core::mem; |
33 | | use ::core::ops::{Index, IndexMut, RangeBounds}; |
34 | | use alloc::boxed::Box; |
35 | | use alloc::vec::Vec; |
36 | | |
37 | | #[cfg(feature = "std")] |
38 | | use std::collections::hash_map::RandomState; |
39 | | |
40 | | pub(crate) use self::core::{ExtractCore, IndexMapCore}; |
41 | | use crate::util::{third, try_simplify_range}; |
42 | | use crate::{Bucket, Equivalent, GetDisjointMutError, HashValue, TryReserveError}; |
43 | | |
44 | | /// A hash table where the iteration order of the key-value pairs is independent |
45 | | /// of the hash values of the keys. |
46 | | /// |
47 | | /// The interface is closely compatible with the standard |
48 | | /// [`HashMap`][std::collections::HashMap], |
49 | | /// but also has additional features. |
50 | | /// |
51 | | /// # Order |
52 | | /// |
53 | | /// The key-value pairs have a consistent order that is determined by |
54 | | /// the sequence of insertion and removal calls on the map. The order does |
55 | | /// not depend on the keys or the hash function at all. |
56 | | /// |
57 | | /// All iterators traverse the map in *the order*. |
58 | | /// |
59 | | /// The insertion order is preserved, with **notable exceptions** like the |
60 | | /// [`.remove()`][Self::remove] or [`.swap_remove()`][Self::swap_remove] methods. |
61 | | /// Methods such as [`.sort_by()`][Self::sort_by] of |
62 | | /// course result in a new order, depending on the sorting order. |
63 | | /// |
64 | | /// # Indices |
65 | | /// |
66 | | /// The key-value pairs are indexed in a compact range without holes in the |
67 | | /// range `0..self.len()`. For example, the method `.get_full` looks up the |
68 | | /// index for a key, and the method `.get_index` looks up the key-value pair by |
69 | | /// index. |
70 | | /// |
71 | | /// # Examples |
72 | | /// |
73 | | /// ``` |
74 | | /// use indexmap::IndexMap; |
75 | | /// |
76 | | /// // count the frequency of each letter in a sentence. |
77 | | /// let mut letters = IndexMap::new(); |
78 | | /// for ch in "a short treatise on fungi".chars() { |
79 | | /// *letters.entry(ch).or_insert(0) += 1; |
80 | | /// } |
81 | | /// |
82 | | /// assert_eq!(letters[&'s'], 2); |
83 | | /// assert_eq!(letters[&'t'], 3); |
84 | | /// assert_eq!(letters[&'u'], 1); |
85 | | /// assert_eq!(letters.get(&'y'), None); |
86 | | /// ``` |
87 | | #[cfg(feature = "std")] |
88 | | pub struct IndexMap<K, V, S = RandomState> { |
89 | | pub(crate) core: IndexMapCore<K, V>, |
90 | | hash_builder: S, |
91 | | } |
92 | | #[cfg(not(feature = "std"))] |
93 | | pub struct IndexMap<K, V, S> { |
94 | | pub(crate) core: IndexMapCore<K, V>, |
95 | | hash_builder: S, |
96 | | } |
97 | | |
98 | | impl<K, V, S> Clone for IndexMap<K, V, S> |
99 | | where |
100 | | K: Clone, |
101 | | V: Clone, |
102 | | S: Clone, |
103 | | { |
104 | 14.9k | fn clone(&self) -> Self { |
105 | 14.9k | IndexMap { |
106 | 14.9k | core: self.core.clone(), |
107 | 14.9k | hash_builder: self.hash_builder.clone(), |
108 | 14.9k | } |
109 | 14.9k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState> as core::clone::Clone>::clone Line | Count | Source | 104 | 14.9k | fn clone(&self) -> Self { | 105 | 14.9k | IndexMap { | 106 | 14.9k | core: self.core.clone(), | 107 | 14.9k | hash_builder: self.hash_builder.clone(), | 108 | 14.9k | } | 109 | 14.9k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::clone::Clone>::clone |
110 | | |
111 | 0 | fn clone_from(&mut self, other: &Self) { |
112 | 0 | self.core.clone_from(&other.core); |
113 | 0 | self.hash_builder.clone_from(&other.hash_builder); |
114 | 0 | } |
115 | | } |
116 | | |
117 | | impl<K, V, S> fmt::Debug for IndexMap<K, V, S> |
118 | | where |
119 | | K: fmt::Debug, |
120 | | V: fmt::Debug, |
121 | | { |
122 | | #[cfg(not(feature = "test_debug"))] |
123 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
124 | 0 | f.debug_map().entries(self.iter()).finish() |
125 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState> as core::fmt::Debug>::fmt Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::fmt::Debug>::fmt Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::fmt::Debug>::fmt |
126 | | |
127 | | #[cfg(feature = "test_debug")] |
128 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
129 | | // Let the inner `IndexMapCore` print all of its details |
130 | | f.debug_struct("IndexMap") |
131 | | .field("core", &self.core) |
132 | | .finish() |
133 | | } |
134 | | } |
135 | | |
136 | | #[cfg(feature = "std")] |
137 | | #[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
138 | | impl<K, V> IndexMap<K, V> { |
139 | | /// Create a new map. (Does not allocate.) |
140 | | #[inline] |
141 | 0 | pub fn new() -> Self { |
142 | 0 | Self::with_capacity(0) |
143 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::new Unexecuted instantiation: <indexmap::map::IndexMap<_, _>>::new |
144 | | |
145 | | /// Create a new map with capacity for `n` key-value pairs. (Does not |
146 | | /// allocate if `n` is zero.) |
147 | | /// |
148 | | /// Computes in **O(n)** time. |
149 | | #[inline] |
150 | 0 | pub fn with_capacity(n: usize) -> Self { |
151 | 0 | Self::with_capacity_and_hasher(n, <_>::default()) |
152 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::with_capacity Unexecuted instantiation: <indexmap::map::IndexMap<_, _>>::with_capacity |
153 | | } |
154 | | |
155 | | impl<K, V, S> IndexMap<K, V, S> { |
156 | | /// Create a new map with capacity for `n` key-value pairs. (Does not |
157 | | /// allocate if `n` is zero.) |
158 | | /// |
159 | | /// Computes in **O(n)** time. |
160 | | #[inline] |
161 | 701k | pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self { |
162 | 701k | if n == 0 { |
163 | 701k | Self::with_hasher(hash_builder) |
164 | | } else { |
165 | 0 | IndexMap { |
166 | 0 | core: IndexMapCore::with_capacity(n), |
167 | 0 | hash_builder, |
168 | 0 | } |
169 | | } |
170 | 701k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::with_capacity_and_hasher Line | Count | Source | 161 | 701k | pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self { | 162 | 701k | if n == 0 { | 163 | 701k | Self::with_hasher(hash_builder) | 164 | | } else { | 165 | 0 | IndexMap { | 166 | 0 | core: IndexMapCore::with_capacity(n), | 167 | 0 | hash_builder, | 168 | 0 | } | 169 | | } | 170 | 701k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::with_capacity_and_hasher Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::with_capacity_and_hasher |
171 | | |
172 | | /// Create a new map with `hash_builder`. |
173 | | /// |
174 | | /// This function is `const`, so it |
175 | | /// can be called in `static` contexts. |
176 | 701k | pub const fn with_hasher(hash_builder: S) -> Self { |
177 | 701k | IndexMap { |
178 | 701k | core: IndexMapCore::new(), |
179 | 701k | hash_builder, |
180 | 701k | } |
181 | 701k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::with_hasher Line | Count | Source | 176 | 701k | pub const fn with_hasher(hash_builder: S) -> Self { | 177 | 701k | IndexMap { | 178 | 701k | core: IndexMapCore::new(), | 179 | 701k | hash_builder, | 180 | 701k | } | 181 | 701k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::with_hasher Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::with_hasher |
182 | | |
183 | | #[inline] |
184 | 14.9k | pub(crate) fn into_entries(self) -> Vec<Bucket<K, V>> { |
185 | 14.9k | self.core.into_entries() |
186 | 14.9k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::into_entries Line | Count | Source | 184 | 14.9k | pub(crate) fn into_entries(self) -> Vec<Bucket<K, V>> { | 185 | 14.9k | self.core.into_entries() | 186 | 14.9k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::into_entries Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::into_entries |
187 | | |
188 | | #[inline] |
189 | 326k | pub(crate) fn as_entries(&self) -> &[Bucket<K, V>] { |
190 | 326k | self.core.as_entries() |
191 | 326k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::as_entries Line | Count | Source | 189 | 326k | pub(crate) fn as_entries(&self) -> &[Bucket<K, V>] { | 190 | 326k | self.core.as_entries() | 191 | 326k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::as_entries Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::as_entries |
192 | | |
193 | | #[inline] |
194 | 0 | pub(crate) fn as_entries_mut(&mut self) -> &mut [Bucket<K, V>] { |
195 | 0 | self.core.as_entries_mut() |
196 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::as_entries_mut Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::as_entries_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::as_entries_mut |
197 | | |
198 | 0 | pub(crate) fn with_entries<F>(&mut self, f: F) |
199 | 0 | where |
200 | 0 | F: FnOnce(&mut [Bucket<K, V>]), |
201 | 0 | { |
202 | 0 | self.core.with_entries(f); |
203 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::with_entries::<<indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::sort_unstable_keys::{closure#0}> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::with_entries::<_> |
204 | | |
205 | | /// Return the number of elements the map can hold without reallocating. |
206 | | /// |
207 | | /// This number is a lower bound; the map might be able to hold more, |
208 | | /// but is guaranteed to be able to hold at least this many. |
209 | | /// |
210 | | /// Computes in **O(1)** time. |
211 | 0 | pub fn capacity(&self) -> usize { |
212 | 0 | self.core.capacity() |
213 | 0 | } |
214 | | |
215 | | /// Return a reference to the map's `BuildHasher`. |
216 | 0 | pub fn hasher(&self) -> &S { |
217 | 0 | &self.hash_builder |
218 | 0 | } |
219 | | |
220 | | /// Return the number of key-value pairs in the map. |
221 | | /// |
222 | | /// Computes in **O(1)** time. |
223 | | #[inline] |
224 | 36.1k | pub fn len(&self) -> usize { |
225 | 36.1k | self.core.len() |
226 | 36.1k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::len Line | Count | Source | 224 | 36.1k | pub fn len(&self) -> usize { | 225 | 36.1k | self.core.len() | 226 | 36.1k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::len Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::len |
227 | | |
228 | | /// Returns true if the map contains no elements. |
229 | | /// |
230 | | /// Computes in **O(1)** time. |
231 | | #[inline] |
232 | 0 | pub fn is_empty(&self) -> bool { |
233 | 0 | self.len() == 0 |
234 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::is_empty Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::is_empty Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::is_empty |
235 | | |
236 | | /// Return an iterator over the key-value pairs of the map, in their order |
237 | 26.8k | pub fn iter(&self) -> Iter<'_, K, V> { |
238 | 26.8k | Iter::new(self.as_entries()) |
239 | 26.8k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::iter Line | Count | Source | 237 | 26.8k | pub fn iter(&self) -> Iter<'_, K, V> { | 238 | 26.8k | Iter::new(self.as_entries()) | 239 | 26.8k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::iter Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::iter |
240 | | |
241 | | /// Return an iterator over the key-value pairs of the map, in their order |
242 | 0 | pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { |
243 | 0 | IterMut::new(self.as_entries_mut()) |
244 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::iter_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::iter_mut |
245 | | |
246 | | /// Return an iterator over the keys of the map, in their order |
247 | 0 | pub fn keys(&self) -> Keys<'_, K, V> { |
248 | 0 | Keys::new(self.as_entries()) |
249 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::keys Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::keys |
250 | | |
251 | | /// Return an owning iterator over the keys of the map, in their order |
252 | 0 | pub fn into_keys(self) -> IntoKeys<K, V> { |
253 | 0 | IntoKeys::new(self.into_entries()) |
254 | 0 | } |
255 | | |
256 | | /// Return an iterator over the values of the map, in their order |
257 | 0 | pub fn values(&self) -> Values<'_, K, V> { |
258 | 0 | Values::new(self.as_entries()) |
259 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::values Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::values |
260 | | |
261 | | /// Return an iterator over mutable references to the values of the map, |
262 | | /// in their order |
263 | 0 | pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { |
264 | 0 | ValuesMut::new(self.as_entries_mut()) |
265 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::values_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::values_mut |
266 | | |
267 | | /// Return an owning iterator over the values of the map, in their order |
268 | 0 | pub fn into_values(self) -> IntoValues<K, V> { |
269 | 0 | IntoValues::new(self.into_entries()) |
270 | 0 | } |
271 | | |
272 | | /// Remove all key-value pairs in the map, while preserving its capacity. |
273 | | /// |
274 | | /// Computes in **O(n)** time. |
275 | 0 | pub fn clear(&mut self) { |
276 | 0 | self.core.clear(); |
277 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::clear Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::clear |
278 | | |
279 | | /// Shortens the map, keeping the first `len` elements and dropping the rest. |
280 | | /// |
281 | | /// If `len` is greater than the map's current length, this has no effect. |
282 | 0 | pub fn truncate(&mut self, len: usize) { |
283 | 0 | self.core.truncate(len); |
284 | 0 | } |
285 | | |
286 | | /// Clears the `IndexMap` in the given index range, returning those |
287 | | /// key-value pairs as a drain iterator. |
288 | | /// |
289 | | /// The range may be any type that implements [`RangeBounds<usize>`], |
290 | | /// including all of the `std::ops::Range*` types, or even a tuple pair of |
291 | | /// `Bound` start and end values. To drain the map entirely, use `RangeFull` |
292 | | /// like `map.drain(..)`. |
293 | | /// |
294 | | /// This shifts down all entries following the drained range to fill the |
295 | | /// gap, and keeps the allocated memory for reuse. |
296 | | /// |
297 | | /// ***Panics*** if the starting point is greater than the end point or if |
298 | | /// the end point is greater than the length of the map. |
299 | | #[track_caller] |
300 | 0 | pub fn drain<R>(&mut self, range: R) -> Drain<'_, K, V> |
301 | 0 | where |
302 | 0 | R: RangeBounds<usize>, |
303 | 0 | { |
304 | 0 | Drain::new(self.core.drain(range)) |
305 | 0 | } |
306 | | |
307 | | /// Creates an iterator which uses a closure to determine if an element should be removed, |
308 | | /// for all elements in the given range. |
309 | | /// |
310 | | /// If the closure returns true, the element is removed from the map and yielded. |
311 | | /// If the closure returns false, or panics, the element remains in the map and will not be |
312 | | /// yielded. |
313 | | /// |
314 | | /// Note that `extract_if` lets you mutate every value in the filter closure, regardless of |
315 | | /// whether you choose to keep or remove it. |
316 | | /// |
317 | | /// The range may be any type that implements [`RangeBounds<usize>`], |
318 | | /// including all of the `std::ops::Range*` types, or even a tuple pair of |
319 | | /// `Bound` start and end values. To check the entire map, use `RangeFull` |
320 | | /// like `map.extract_if(.., predicate)`. |
321 | | /// |
322 | | /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating |
323 | | /// or the iteration short-circuits, then the remaining elements will be retained. |
324 | | /// Use [`retain`] with a negated predicate if you do not need the returned iterator. |
325 | | /// |
326 | | /// [`retain`]: IndexMap::retain |
327 | | /// |
328 | | /// ***Panics*** if the starting point is greater than the end point or if |
329 | | /// the end point is greater than the length of the map. |
330 | | /// |
331 | | /// # Examples |
332 | | /// |
333 | | /// Splitting a map into even and odd keys, reusing the original map: |
334 | | /// |
335 | | /// ``` |
336 | | /// use indexmap::IndexMap; |
337 | | /// |
338 | | /// let mut map: IndexMap<i32, i32> = (0..8).map(|x| (x, x)).collect(); |
339 | | /// let extracted: IndexMap<i32, i32> = map.extract_if(.., |k, _v| k % 2 == 0).collect(); |
340 | | /// |
341 | | /// let evens = extracted.keys().copied().collect::<Vec<_>>(); |
342 | | /// let odds = map.keys().copied().collect::<Vec<_>>(); |
343 | | /// |
344 | | /// assert_eq!(evens, vec![0, 2, 4, 6]); |
345 | | /// assert_eq!(odds, vec![1, 3, 5, 7]); |
346 | | /// ``` |
347 | | #[track_caller] |
348 | 0 | pub fn extract_if<F, R>(&mut self, range: R, pred: F) -> ExtractIf<'_, K, V, F> |
349 | 0 | where |
350 | 0 | F: FnMut(&K, &mut V) -> bool, |
351 | 0 | R: RangeBounds<usize>, |
352 | 0 | { |
353 | 0 | ExtractIf::new(&mut self.core, range, pred) |
354 | 0 | } |
355 | | |
356 | | /// Splits the collection into two at the given index. |
357 | | /// |
358 | | /// Returns a newly allocated map containing the elements in the range |
359 | | /// `[at, len)`. After the call, the original map will be left containing |
360 | | /// the elements `[0, at)` with its previous capacity unchanged. |
361 | | /// |
362 | | /// ***Panics*** if `at > len`. |
363 | | #[track_caller] |
364 | 0 | pub fn split_off(&mut self, at: usize) -> Self |
365 | 0 | where |
366 | 0 | S: Clone, |
367 | 0 | { |
368 | 0 | Self { |
369 | 0 | core: self.core.split_off(at), |
370 | 0 | hash_builder: self.hash_builder.clone(), |
371 | 0 | } |
372 | 0 | } |
373 | | |
374 | | /// Reserve capacity for `additional` more key-value pairs. |
375 | | /// |
376 | | /// Computes in **O(n)** time. |
377 | 0 | pub fn reserve(&mut self, additional: usize) { |
378 | 0 | self.core.reserve(additional); |
379 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::reserve Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::reserve |
380 | | |
381 | | /// Reserve capacity for `additional` more key-value pairs, without over-allocating. |
382 | | /// |
383 | | /// Unlike `reserve`, this does not deliberately over-allocate the entry capacity to avoid |
384 | | /// frequent re-allocations. However, the underlying data structures may still have internal |
385 | | /// capacity requirements, and the allocator itself may give more space than requested, so this |
386 | | /// cannot be relied upon to be precisely minimal. |
387 | | /// |
388 | | /// Computes in **O(n)** time. |
389 | 0 | pub fn reserve_exact(&mut self, additional: usize) { |
390 | 0 | self.core.reserve_exact(additional); |
391 | 0 | } |
392 | | |
393 | | /// Try to reserve capacity for `additional` more key-value pairs. |
394 | | /// |
395 | | /// Computes in **O(n)** time. |
396 | 0 | pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { |
397 | 0 | self.core.try_reserve(additional) |
398 | 0 | } |
399 | | |
400 | | /// Try to reserve capacity for `additional` more key-value pairs, without over-allocating. |
401 | | /// |
402 | | /// Unlike `try_reserve`, this does not deliberately over-allocate the entry capacity to avoid |
403 | | /// frequent re-allocations. However, the underlying data structures may still have internal |
404 | | /// capacity requirements, and the allocator itself may give more space than requested, so this |
405 | | /// cannot be relied upon to be precisely minimal. |
406 | | /// |
407 | | /// Computes in **O(n)** time. |
408 | 0 | pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { |
409 | 0 | self.core.try_reserve_exact(additional) |
410 | 0 | } |
411 | | |
412 | | /// Shrink the capacity of the map as much as possible. |
413 | | /// |
414 | | /// Computes in **O(n)** time. |
415 | 0 | pub fn shrink_to_fit(&mut self) { |
416 | 0 | self.core.shrink_to(0); |
417 | 0 | } |
418 | | |
419 | | /// Shrink the capacity of the map with a lower limit. |
420 | | /// |
421 | | /// Computes in **O(n)** time. |
422 | 0 | pub fn shrink_to(&mut self, min_capacity: usize) { |
423 | 0 | self.core.shrink_to(min_capacity); |
424 | 0 | } |
425 | | } |
426 | | |
427 | | impl<K, V, S> IndexMap<K, V, S> |
428 | | where |
429 | | K: Hash + Eq, |
430 | | S: BuildHasher, |
431 | | { |
432 | | /// Insert a key-value pair in the map. |
433 | | /// |
434 | | /// If an equivalent key already exists in the map: the key remains and |
435 | | /// retains in its place in the order, its corresponding value is updated |
436 | | /// with `value`, and the older value is returned inside `Some(_)`. |
437 | | /// |
438 | | /// If no equivalent key existed in the map: the new key-value pair is |
439 | | /// inserted, last in order, and `None` is returned. |
440 | | /// |
441 | | /// Computes in **O(1)** time (amortized average). |
442 | | /// |
443 | | /// See also [`entry`][Self::entry] if you want to insert *or* modify, |
444 | | /// or [`insert_full`][Self::insert_full] if you need to get the index of |
445 | | /// the corresponding key-value pair. |
446 | 2.60M | pub fn insert(&mut self, key: K, value: V) -> Option<V> { |
447 | 2.60M | self.insert_full(key, value).1 |
448 | 2.60M | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::insert Line | Count | Source | 446 | 2.60M | pub fn insert(&mut self, key: K, value: V) -> Option<V> { | 447 | 2.60M | self.insert_full(key, value).1 | 448 | 2.60M | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::insert Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::insert |
449 | | |
450 | | /// Insert a key-value pair in the map, and get their index. |
451 | | /// |
452 | | /// If an equivalent key already exists in the map: the key remains and |
453 | | /// retains in its place in the order, its corresponding value is updated |
454 | | /// with `value`, and the older value is returned inside `(index, Some(_))`. |
455 | | /// |
456 | | /// If no equivalent key existed in the map: the new key-value pair is |
457 | | /// inserted, last in order, and `(index, None)` is returned. |
458 | | /// |
459 | | /// Computes in **O(1)** time (amortized average). |
460 | | /// |
461 | | /// See also [`entry`][Self::entry] if you want to insert *or* modify. |
462 | 2.60M | pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) { |
463 | 2.60M | let hash = self.hash(&key); |
464 | 2.60M | self.core.insert_full(hash, key, value) |
465 | 2.60M | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::insert_full Line | Count | Source | 462 | 2.60M | pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) { | 463 | 2.60M | let hash = self.hash(&key); | 464 | 2.60M | self.core.insert_full(hash, key, value) | 465 | 2.60M | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::insert_full Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::insert_full |
466 | | |
467 | | /// Insert a key-value pair in the map at its ordered position among sorted keys. |
468 | | /// |
469 | | /// This is equivalent to finding the position with |
470 | | /// [`binary_search_keys`][Self::binary_search_keys], then either updating |
471 | | /// it or calling [`insert_before`][Self::insert_before] for a new key. |
472 | | /// |
473 | | /// If the sorted key is found in the map, its corresponding value is |
474 | | /// updated with `value`, and the older value is returned inside |
475 | | /// `(index, Some(_))`. Otherwise, the new key-value pair is inserted at |
476 | | /// the sorted position, and `(index, None)` is returned. |
477 | | /// |
478 | | /// If the existing keys are **not** already sorted, then the insertion |
479 | | /// index is unspecified (like [`slice::binary_search`]), but the key-value |
480 | | /// pair is moved to or inserted at that position regardless. |
481 | | /// |
482 | | /// Computes in **O(n)** time (average). Instead of repeating calls to |
483 | | /// `insert_sorted`, it may be faster to call batched [`insert`][Self::insert] |
484 | | /// or [`extend`][Self::extend] and only call [`sort_keys`][Self::sort_keys] |
485 | | /// or [`sort_unstable_keys`][Self::sort_unstable_keys] once. |
486 | 0 | pub fn insert_sorted(&mut self, key: K, value: V) -> (usize, Option<V>) |
487 | 0 | where |
488 | 0 | K: Ord, |
489 | 0 | { |
490 | 0 | match self.binary_search_keys(&key) { |
491 | 0 | Ok(i) => (i, Some(mem::replace(&mut self[i], value))), |
492 | 0 | Err(i) => self.insert_before(i, key, value), |
493 | | } |
494 | 0 | } |
495 | | |
496 | | /// Insert a key-value pair in the map before the entry at the given index, or at the end. |
497 | | /// |
498 | | /// If an equivalent key already exists in the map: the key remains and |
499 | | /// is moved to the new position in the map, its corresponding value is updated |
500 | | /// with `value`, and the older value is returned inside `Some(_)`. The returned index |
501 | | /// will either be the given index or one less, depending on how the entry moved. |
502 | | /// (See [`shift_insert`](Self::shift_insert) for different behavior here.) |
503 | | /// |
504 | | /// If no equivalent key existed in the map: the new key-value pair is |
505 | | /// inserted exactly at the given index, and `None` is returned. |
506 | | /// |
507 | | /// ***Panics*** if `index` is out of bounds. |
508 | | /// Valid indices are `0..=map.len()` (inclusive). |
509 | | /// |
510 | | /// Computes in **O(n)** time (average). |
511 | | /// |
512 | | /// See also [`entry`][Self::entry] if you want to insert *or* modify, |
513 | | /// perhaps only using the index for new entries with [`VacantEntry::shift_insert`]. |
514 | | /// |
515 | | /// # Examples |
516 | | /// |
517 | | /// ``` |
518 | | /// use indexmap::IndexMap; |
519 | | /// let mut map: IndexMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect(); |
520 | | /// |
521 | | /// // The new key '*' goes exactly at the given index. |
522 | | /// assert_eq!(map.get_index_of(&'*'), None); |
523 | | /// assert_eq!(map.insert_before(10, '*', ()), (10, None)); |
524 | | /// assert_eq!(map.get_index_of(&'*'), Some(10)); |
525 | | /// |
526 | | /// // Moving the key 'a' up will shift others down, so this moves *before* 10 to index 9. |
527 | | /// assert_eq!(map.insert_before(10, 'a', ()), (9, Some(()))); |
528 | | /// assert_eq!(map.get_index_of(&'a'), Some(9)); |
529 | | /// assert_eq!(map.get_index_of(&'*'), Some(10)); |
530 | | /// |
531 | | /// // Moving the key 'z' down will shift others up, so this moves to exactly 10. |
532 | | /// assert_eq!(map.insert_before(10, 'z', ()), (10, Some(()))); |
533 | | /// assert_eq!(map.get_index_of(&'z'), Some(10)); |
534 | | /// assert_eq!(map.get_index_of(&'*'), Some(11)); |
535 | | /// |
536 | | /// // Moving or inserting before the endpoint is also valid. |
537 | | /// assert_eq!(map.len(), 27); |
538 | | /// assert_eq!(map.insert_before(map.len(), '*', ()), (26, Some(()))); |
539 | | /// assert_eq!(map.get_index_of(&'*'), Some(26)); |
540 | | /// assert_eq!(map.insert_before(map.len(), '+', ()), (27, None)); |
541 | | /// assert_eq!(map.get_index_of(&'+'), Some(27)); |
542 | | /// assert_eq!(map.len(), 28); |
543 | | /// ``` |
544 | | #[track_caller] |
545 | 0 | pub fn insert_before(&mut self, mut index: usize, key: K, value: V) -> (usize, Option<V>) { |
546 | 0 | let len = self.len(); |
547 | 0 |
|
548 | 0 | assert!( |
549 | 0 | index <= len, |
550 | 0 | "index out of bounds: the len is {len} but the index is {index}. Expected index <= len" |
551 | | ); |
552 | | |
553 | 0 | match self.entry(key) { |
554 | 0 | Entry::Occupied(mut entry) => { |
555 | 0 | if index > entry.index() { |
556 | 0 | // Some entries will shift down when this one moves up, |
557 | 0 | // so "insert before index" becomes "move to index - 1", |
558 | 0 | // keeping the entry at the original index unmoved. |
559 | 0 | index -= 1; |
560 | 0 | } |
561 | 0 | let old = mem::replace(entry.get_mut(), value); |
562 | 0 | entry.move_index(index); |
563 | 0 | (index, Some(old)) |
564 | | } |
565 | 0 | Entry::Vacant(entry) => { |
566 | 0 | entry.shift_insert(index, value); |
567 | 0 | (index, None) |
568 | | } |
569 | | } |
570 | 0 | } |
571 | | |
572 | | /// Insert a key-value pair in the map at the given index. |
573 | | /// |
574 | | /// If an equivalent key already exists in the map: the key remains and |
575 | | /// is moved to the given index in the map, its corresponding value is updated |
576 | | /// with `value`, and the older value is returned inside `Some(_)`. |
577 | | /// Note that existing entries **cannot** be moved to `index == map.len()`! |
578 | | /// (See [`insert_before`](Self::insert_before) for different behavior here.) |
579 | | /// |
580 | | /// If no equivalent key existed in the map: the new key-value pair is |
581 | | /// inserted at the given index, and `None` is returned. |
582 | | /// |
583 | | /// ***Panics*** if `index` is out of bounds. |
584 | | /// Valid indices are `0..map.len()` (exclusive) when moving an existing entry, or |
585 | | /// `0..=map.len()` (inclusive) when inserting a new key. |
586 | | /// |
587 | | /// Computes in **O(n)** time (average). |
588 | | /// |
589 | | /// See also [`entry`][Self::entry] if you want to insert *or* modify, |
590 | | /// perhaps only using the index for new entries with [`VacantEntry::shift_insert`]. |
591 | | /// |
592 | | /// # Examples |
593 | | /// |
594 | | /// ``` |
595 | | /// use indexmap::IndexMap; |
596 | | /// let mut map: IndexMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect(); |
597 | | /// |
598 | | /// // The new key '*' goes exactly at the given index. |
599 | | /// assert_eq!(map.get_index_of(&'*'), None); |
600 | | /// assert_eq!(map.shift_insert(10, '*', ()), None); |
601 | | /// assert_eq!(map.get_index_of(&'*'), Some(10)); |
602 | | /// |
603 | | /// // Moving the key 'a' up to 10 will shift others down, including the '*' that was at 10. |
604 | | /// assert_eq!(map.shift_insert(10, 'a', ()), Some(())); |
605 | | /// assert_eq!(map.get_index_of(&'a'), Some(10)); |
606 | | /// assert_eq!(map.get_index_of(&'*'), Some(9)); |
607 | | /// |
608 | | /// // Moving the key 'z' down to 9 will shift others up, including the '*' that was at 9. |
609 | | /// assert_eq!(map.shift_insert(9, 'z', ()), Some(())); |
610 | | /// assert_eq!(map.get_index_of(&'z'), Some(9)); |
611 | | /// assert_eq!(map.get_index_of(&'*'), Some(10)); |
612 | | /// |
613 | | /// // Existing keys can move to len-1 at most, but new keys can insert at the endpoint. |
614 | | /// assert_eq!(map.len(), 27); |
615 | | /// assert_eq!(map.shift_insert(map.len() - 1, '*', ()), Some(())); |
616 | | /// assert_eq!(map.get_index_of(&'*'), Some(26)); |
617 | | /// assert_eq!(map.shift_insert(map.len(), '+', ()), None); |
618 | | /// assert_eq!(map.get_index_of(&'+'), Some(27)); |
619 | | /// assert_eq!(map.len(), 28); |
620 | | /// ``` |
621 | | /// |
622 | | /// ```should_panic |
623 | | /// use indexmap::IndexMap; |
624 | | /// let mut map: IndexMap<char, ()> = ('a'..='z').map(|c| (c, ())).collect(); |
625 | | /// |
626 | | /// // This is an invalid index for moving an existing key! |
627 | | /// map.shift_insert(map.len(), 'a', ()); |
628 | | /// ``` |
629 | | #[track_caller] |
630 | 0 | pub fn shift_insert(&mut self, index: usize, key: K, value: V) -> Option<V> { |
631 | 0 | let len = self.len(); |
632 | 0 | match self.entry(key) { |
633 | 0 | Entry::Occupied(mut entry) => { |
634 | 0 | assert!( |
635 | 0 | index < len, |
636 | 0 | "index out of bounds: the len is {len} but the index is {index}" |
637 | | ); |
638 | | |
639 | 0 | let old = mem::replace(entry.get_mut(), value); |
640 | 0 | entry.move_index(index); |
641 | 0 | Some(old) |
642 | | } |
643 | 0 | Entry::Vacant(entry) => { |
644 | 0 | assert!( |
645 | 0 | index <= len, |
646 | 0 | "index out of bounds: the len is {len} but the index is {index}. Expected index <= len" |
647 | | ); |
648 | | |
649 | 0 | entry.shift_insert(index, value); |
650 | 0 | None |
651 | | } |
652 | | } |
653 | 0 | } |
654 | | |
655 | | /// Get the given key’s corresponding entry in the map for insertion and/or |
656 | | /// in-place manipulation. |
657 | | /// |
658 | | /// Computes in **O(1)** time (amortized average). |
659 | 0 | pub fn entry(&mut self, key: K) -> Entry<'_, K, V> { |
660 | 0 | let hash = self.hash(&key); |
661 | 0 | self.core.entry(hash, key) |
662 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::entry Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::entry |
663 | | |
664 | | /// Creates a splicing iterator that replaces the specified range in the map |
665 | | /// with the given `replace_with` key-value iterator and yields the removed |
666 | | /// items. `replace_with` does not need to be the same length as `range`. |
667 | | /// |
668 | | /// The `range` is removed even if the iterator is not consumed until the |
669 | | /// end. It is unspecified how many elements are removed from the map if the |
670 | | /// `Splice` value is leaked. |
671 | | /// |
672 | | /// The input iterator `replace_with` is only consumed when the `Splice` |
673 | | /// value is dropped. If a key from the iterator matches an existing entry |
674 | | /// in the map (outside of `range`), then the value will be updated in that |
675 | | /// position. Otherwise, the new key-value pair will be inserted in the |
676 | | /// replaced `range`. |
677 | | /// |
678 | | /// ***Panics*** if the starting point is greater than the end point or if |
679 | | /// the end point is greater than the length of the map. |
680 | | /// |
681 | | /// # Examples |
682 | | /// |
683 | | /// ``` |
684 | | /// use indexmap::IndexMap; |
685 | | /// |
686 | | /// let mut map = IndexMap::from([(0, '_'), (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]); |
687 | | /// let new = [(5, 'E'), (4, 'D'), (3, 'C'), (2, 'B'), (1, 'A')]; |
688 | | /// let removed: Vec<_> = map.splice(2..4, new).collect(); |
689 | | /// |
690 | | /// // 1 and 4 got new values, while 5, 3, and 2 were newly inserted. |
691 | | /// assert!(map.into_iter().eq([(0, '_'), (1, 'A'), (5, 'E'), (3, 'C'), (2, 'B'), (4, 'D')])); |
692 | | /// assert_eq!(removed, &[(2, 'b'), (3, 'c')]); |
693 | | /// ``` |
694 | | #[track_caller] |
695 | 0 | pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, K, V, S> |
696 | 0 | where |
697 | 0 | R: RangeBounds<usize>, |
698 | 0 | I: IntoIterator<Item = (K, V)>, |
699 | 0 | { |
700 | 0 | Splice::new(self, range, replace_with.into_iter()) |
701 | 0 | } |
702 | | |
703 | | /// Moves all key-value pairs from `other` into `self`, leaving `other` empty. |
704 | | /// |
705 | | /// This is equivalent to calling [`insert`][Self::insert] for each |
706 | | /// key-value pair from `other` in order, which means that for keys that |
707 | | /// already exist in `self`, their value is updated in the current position. |
708 | | /// |
709 | | /// # Examples |
710 | | /// |
711 | | /// ``` |
712 | | /// use indexmap::IndexMap; |
713 | | /// |
714 | | /// // Note: Key (3) is present in both maps. |
715 | | /// let mut a = IndexMap::from([(3, "c"), (2, "b"), (1, "a")]); |
716 | | /// let mut b = IndexMap::from([(3, "d"), (4, "e"), (5, "f")]); |
717 | | /// let old_capacity = b.capacity(); |
718 | | /// |
719 | | /// a.append(&mut b); |
720 | | /// |
721 | | /// assert_eq!(a.len(), 5); |
722 | | /// assert_eq!(b.len(), 0); |
723 | | /// assert_eq!(b.capacity(), old_capacity); |
724 | | /// |
725 | | /// assert!(a.keys().eq(&[3, 2, 1, 4, 5])); |
726 | | /// assert_eq!(a[&3], "d"); // "c" was overwritten. |
727 | | /// ``` |
728 | 0 | pub fn append<S2>(&mut self, other: &mut IndexMap<K, V, S2>) { |
729 | 0 | self.extend(other.drain(..)); |
730 | 0 | } |
731 | | } |
732 | | |
733 | | impl<K, V, S> IndexMap<K, V, S> |
734 | | where |
735 | | S: BuildHasher, |
736 | | { |
737 | 2.75M | pub(crate) fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue { |
738 | 2.75M | let mut h = self.hash_builder.build_hasher(); |
739 | 2.75M | key.hash(&mut h); |
740 | 2.75M | HashValue(h.finish() as usize) |
741 | 2.75M | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::hash::<alloc::string::String> Line | Count | Source | 737 | 2.60M | pub(crate) fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue { | 738 | 2.60M | let mut h = self.hash_builder.build_hasher(); | 739 | 2.60M | key.hash(&mut h); | 740 | 2.60M | HashValue(h.finish() as usize) | 741 | 2.60M | } |
<indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::hash::<str> Line | Count | Source | 737 | 146k | pub(crate) fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue { | 738 | 146k | let mut h = self.hash_builder.build_hasher(); | 739 | 146k | key.hash(&mut h); | 740 | 146k | HashValue(h.finish() as usize) | 741 | 146k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::hash::<alloc::string::String> Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::hash::<str> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::hash::<_> |
742 | | |
743 | | /// Return `true` if an equivalent to `key` exists in the map. |
744 | | /// |
745 | | /// Computes in **O(1)** time (average). |
746 | 0 | pub fn contains_key<Q>(&self, key: &Q) -> bool |
747 | 0 | where |
748 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
749 | 0 | { |
750 | 0 | self.get_index_of(key).is_some() |
751 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::contains_key::<str> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::contains_key::<_> |
752 | | |
753 | | /// Return a reference to the value stored for `key`, if it is present, |
754 | | /// else `None`. |
755 | | /// |
756 | | /// Computes in **O(1)** time (average). |
757 | 149k | pub fn get<Q>(&self, key: &Q) -> Option<&V> |
758 | 149k | where |
759 | 149k | Q: ?Sized + Hash + Equivalent<K>, |
760 | 149k | { |
761 | 149k | if let Some(i) = self.get_index_of(key) { |
762 | 149k | let entry = &self.as_entries()[i]; |
763 | 149k | Some(&entry.value) |
764 | | } else { |
765 | 0 | None |
766 | | } |
767 | 149k | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::get::<alloc::string::String> <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::get::<str> Line | Count | Source | 757 | 149k | pub fn get<Q>(&self, key: &Q) -> Option<&V> | 758 | 149k | where | 759 | 149k | Q: ?Sized + Hash + Equivalent<K>, | 760 | 149k | { | 761 | 149k | if let Some(i) = self.get_index_of(key) { | 762 | 149k | let entry = &self.as_entries()[i]; | 763 | 149k | Some(&entry.value) | 764 | | } else { | 765 | 0 | None | 766 | | } | 767 | 149k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::get::<alloc::string::String> Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::get::<str> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::get::<_> |
768 | | |
769 | | /// Return references to the key-value pair stored for `key`, |
770 | | /// if it is present, else `None`. |
771 | | /// |
772 | | /// Computes in **O(1)** time (average). |
773 | 0 | pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> |
774 | 0 | where |
775 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
776 | 0 | { |
777 | 0 | if let Some(i) = self.get_index_of(key) { |
778 | 0 | let entry = &self.as_entries()[i]; |
779 | 0 | Some((&entry.key, &entry.value)) |
780 | | } else { |
781 | 0 | None |
782 | | } |
783 | 0 | } |
784 | | |
785 | | /// Return item index, key and value |
786 | 0 | pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)> |
787 | 0 | where |
788 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
789 | 0 | { |
790 | 0 | if let Some(i) = self.get_index_of(key) { |
791 | 0 | let entry = &self.as_entries()[i]; |
792 | 0 | Some((i, &entry.key, &entry.value)) |
793 | | } else { |
794 | 0 | None |
795 | | } |
796 | 0 | } |
797 | | |
798 | | /// Return item index, if it exists in the map |
799 | | /// |
800 | | /// Computes in **O(1)** time (average). |
801 | 149k | pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize> |
802 | 149k | where |
803 | 149k | Q: ?Sized + Hash + Equivalent<K>, |
804 | 149k | { |
805 | 149k | match self.as_entries() { |
806 | 149k | [] => None, |
807 | 3.65k | [x] => key.equivalent(&x.key).then_some(0), |
808 | | _ => { |
809 | 146k | let hash = self.hash(key); |
810 | 146k | self.core.get_index_of(hash, key) |
811 | | } |
812 | | } |
813 | 149k | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::get_index_of::<alloc::string::String> <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState>>::get_index_of::<str> Line | Count | Source | 801 | 149k | pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize> | 802 | 149k | where | 803 | 149k | Q: ?Sized + Hash + Equivalent<K>, | 804 | 149k | { | 805 | 149k | match self.as_entries() { | 806 | 149k | [] => None, | 807 | 3.65k | [x] => key.equivalent(&x.key).then_some(0), | 808 | | _ => { | 809 | 146k | let hash = self.hash(key); | 810 | 146k | self.core.get_index_of(hash, key) | 811 | | } | 812 | | } | 813 | 149k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::get_index_of::<alloc::string::String> Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::get_index_of::<str> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::get_index_of::<_> |
814 | | |
815 | 0 | pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> |
816 | 0 | where |
817 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
818 | 0 | { |
819 | 0 | if let Some(i) = self.get_index_of(key) { |
820 | 0 | let entry = &mut self.as_entries_mut()[i]; |
821 | 0 | Some(&mut entry.value) |
822 | | } else { |
823 | 0 | None |
824 | | } |
825 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::get_mut::<alloc::string::String> Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::get_mut::<str> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::get_mut::<_> |
826 | | |
827 | 0 | pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)> |
828 | 0 | where |
829 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
830 | 0 | { |
831 | 0 | if let Some(i) = self.get_index_of(key) { |
832 | 0 | let entry = &mut self.as_entries_mut()[i]; |
833 | 0 | Some((i, &entry.key, &mut entry.value)) |
834 | | } else { |
835 | 0 | None |
836 | | } |
837 | 0 | } |
838 | | |
839 | | /// Return the values for `N` keys. If any key is duplicated, this function will panic. |
840 | | /// |
841 | | /// # Examples |
842 | | /// |
843 | | /// ``` |
844 | | /// let mut map = indexmap::IndexMap::from([(1, 'a'), (3, 'b'), (2, 'c')]); |
845 | | /// assert_eq!(map.get_disjoint_mut([&2, &1]), [Some(&mut 'c'), Some(&mut 'a')]); |
846 | | /// ``` |
847 | 0 | pub fn get_disjoint_mut<Q, const N: usize>(&mut self, keys: [&Q; N]) -> [Option<&mut V>; N] |
848 | 0 | where |
849 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
850 | 0 | { |
851 | 0 | let indices = keys.map(|key| self.get_index_of(key)); |
852 | 0 | match self.as_mut_slice().get_disjoint_opt_mut(indices) { |
853 | | Err(GetDisjointMutError::IndexOutOfBounds) => { |
854 | 0 | unreachable!( |
855 | 0 | "Internal error: indices should never be OOB as we got them from get_index_of" |
856 | 0 | ); |
857 | | } |
858 | | Err(GetDisjointMutError::OverlappingIndices) => { |
859 | 0 | panic!("duplicate keys found"); |
860 | | } |
861 | 0 | Ok(key_values) => key_values.map(|kv_opt| kv_opt.map(|kv| kv.1)), |
862 | 0 | } |
863 | 0 | } |
864 | | |
865 | | /// Remove the key-value pair equivalent to `key` and return |
866 | | /// its value. |
867 | | /// |
868 | | /// **NOTE:** This is equivalent to [`.swap_remove(key)`][Self::swap_remove], replacing this |
869 | | /// entry's position with the last element, and it is deprecated in favor of calling that |
870 | | /// explicitly. If you need to preserve the relative order of the keys in the map, use |
871 | | /// [`.shift_remove(key)`][Self::shift_remove] instead. |
872 | | #[deprecated(note = "`remove` disrupts the map order -- \ |
873 | | use `swap_remove` or `shift_remove` for explicit behavior.")] |
874 | 0 | pub fn remove<Q>(&mut self, key: &Q) -> Option<V> |
875 | 0 | where |
876 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
877 | 0 | { |
878 | 0 | self.swap_remove(key) |
879 | 0 | } |
880 | | |
881 | | /// Remove and return the key-value pair equivalent to `key`. |
882 | | /// |
883 | | /// **NOTE:** This is equivalent to [`.swap_remove_entry(key)`][Self::swap_remove_entry], |
884 | | /// replacing this entry's position with the last element, and it is deprecated in favor of |
885 | | /// calling that explicitly. If you need to preserve the relative order of the keys in the map, |
886 | | /// use [`.shift_remove_entry(key)`][Self::shift_remove_entry] instead. |
887 | | #[deprecated(note = "`remove_entry` disrupts the map order -- \ |
888 | | use `swap_remove_entry` or `shift_remove_entry` for explicit behavior.")] |
889 | 0 | pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> |
890 | 0 | where |
891 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
892 | 0 | { |
893 | 0 | self.swap_remove_entry(key) |
894 | 0 | } |
895 | | |
896 | | /// Remove the key-value pair equivalent to `key` and return |
897 | | /// its value. |
898 | | /// |
899 | | /// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
900 | | /// last element of the map and popping it off. **This perturbs |
901 | | /// the position of what used to be the last element!** |
902 | | /// |
903 | | /// Return `None` if `key` is not in map. |
904 | | /// |
905 | | /// Computes in **O(1)** time (average). |
906 | 0 | pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V> |
907 | 0 | where |
908 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
909 | 0 | { |
910 | 0 | self.swap_remove_full(key).map(third) |
911 | 0 | } |
912 | | |
913 | | /// Remove and return the key-value pair equivalent to `key`. |
914 | | /// |
915 | | /// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
916 | | /// last element of the map and popping it off. **This perturbs |
917 | | /// the position of what used to be the last element!** |
918 | | /// |
919 | | /// Return `None` if `key` is not in map. |
920 | | /// |
921 | | /// Computes in **O(1)** time (average). |
922 | 0 | pub fn swap_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> |
923 | 0 | where |
924 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
925 | 0 | { |
926 | 0 | match self.swap_remove_full(key) { |
927 | 0 | Some((_, key, value)) => Some((key, value)), |
928 | 0 | None => None, |
929 | | } |
930 | 0 | } |
931 | | |
932 | | /// Remove the key-value pair equivalent to `key` and return it and |
933 | | /// the index it had. |
934 | | /// |
935 | | /// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
936 | | /// last element of the map and popping it off. **This perturbs |
937 | | /// the position of what used to be the last element!** |
938 | | /// |
939 | | /// Return `None` if `key` is not in map. |
940 | | /// |
941 | | /// Computes in **O(1)** time (average). |
942 | 0 | pub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> |
943 | 0 | where |
944 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
945 | 0 | { |
946 | 0 | match self.as_entries() { |
947 | 0 | [x] if key.equivalent(&x.key) => { |
948 | 0 | let (k, v) = self.core.pop()?; |
949 | 0 | Some((0, k, v)) |
950 | | } |
951 | 0 | [_] | [] => None, |
952 | | _ => { |
953 | 0 | let hash = self.hash(key); |
954 | 0 | self.core.swap_remove_full(hash, key) |
955 | | } |
956 | | } |
957 | 0 | } |
958 | | |
959 | | /// Remove the key-value pair equivalent to `key` and return |
960 | | /// its value. |
961 | | /// |
962 | | /// Like [`Vec::remove`], the pair is removed by shifting all of the |
963 | | /// elements that follow it, preserving their relative order. |
964 | | /// **This perturbs the index of all of those elements!** |
965 | | /// |
966 | | /// Return `None` if `key` is not in map. |
967 | | /// |
968 | | /// Computes in **O(n)** time (average). |
969 | 0 | pub fn shift_remove<Q>(&mut self, key: &Q) -> Option<V> |
970 | 0 | where |
971 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
972 | 0 | { |
973 | 0 | self.shift_remove_full(key).map(third) |
974 | 0 | } |
975 | | |
976 | | /// Remove and return the key-value pair equivalent to `key`. |
977 | | /// |
978 | | /// Like [`Vec::remove`], the pair is removed by shifting all of the |
979 | | /// elements that follow it, preserving their relative order. |
980 | | /// **This perturbs the index of all of those elements!** |
981 | | /// |
982 | | /// Return `None` if `key` is not in map. |
983 | | /// |
984 | | /// Computes in **O(n)** time (average). |
985 | 0 | pub fn shift_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> |
986 | 0 | where |
987 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
988 | 0 | { |
989 | 0 | match self.shift_remove_full(key) { |
990 | 0 | Some((_, key, value)) => Some((key, value)), |
991 | 0 | None => None, |
992 | | } |
993 | 0 | } |
994 | | |
995 | | /// Remove the key-value pair equivalent to `key` and return it and |
996 | | /// the index it had. |
997 | | /// |
998 | | /// Like [`Vec::remove`], the pair is removed by shifting all of the |
999 | | /// elements that follow it, preserving their relative order. |
1000 | | /// **This perturbs the index of all of those elements!** |
1001 | | /// |
1002 | | /// Return `None` if `key` is not in map. |
1003 | | /// |
1004 | | /// Computes in **O(n)** time (average). |
1005 | 0 | pub fn shift_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> |
1006 | 0 | where |
1007 | 0 | Q: ?Sized + Hash + Equivalent<K>, |
1008 | 0 | { |
1009 | 0 | match self.as_entries() { |
1010 | 0 | [x] if key.equivalent(&x.key) => { |
1011 | 0 | let (k, v) = self.core.pop()?; |
1012 | 0 | Some((0, k, v)) |
1013 | | } |
1014 | 0 | [_] | [] => None, |
1015 | | _ => { |
1016 | 0 | let hash = self.hash(key); |
1017 | 0 | self.core.shift_remove_full(hash, key) |
1018 | | } |
1019 | | } |
1020 | 0 | } |
1021 | | } |
1022 | | |
1023 | | impl<K, V, S> IndexMap<K, V, S> { |
1024 | | /// Remove the last key-value pair |
1025 | | /// |
1026 | | /// This preserves the order of the remaining elements. |
1027 | | /// |
1028 | | /// Computes in **O(1)** time (average). |
1029 | | #[doc(alias = "pop_last")] // like `BTreeMap` |
1030 | 0 | pub fn pop(&mut self) -> Option<(K, V)> { |
1031 | 0 | self.core.pop() |
1032 | 0 | } |
1033 | | |
1034 | | /// Scan through each key-value pair in the map and keep those where the |
1035 | | /// closure `keep` returns `true`. |
1036 | | /// |
1037 | | /// The elements are visited in order, and remaining elements keep their |
1038 | | /// order. |
1039 | | /// |
1040 | | /// Computes in **O(n)** time (average). |
1041 | 0 | pub fn retain<F>(&mut self, mut keep: F) |
1042 | 0 | where |
1043 | 0 | F: FnMut(&K, &mut V) -> bool, |
1044 | 0 | { |
1045 | 0 | self.core.retain_in_order(move |k, v| keep(k, v)); |
1046 | 0 | } |
1047 | | |
1048 | | /// Sort the map’s key-value pairs by the default ordering of the keys. |
1049 | | /// |
1050 | | /// This is a stable sort -- but equivalent keys should not normally coexist in |
1051 | | /// a map at all, so [`sort_unstable_keys`][Self::sort_unstable_keys] is preferred |
1052 | | /// because it is generally faster and doesn't allocate auxiliary memory. |
1053 | | /// |
1054 | | /// See [`sort_by`](Self::sort_by) for details. |
1055 | 0 | pub fn sort_keys(&mut self) |
1056 | 0 | where |
1057 | 0 | K: Ord, |
1058 | 0 | { |
1059 | 0 | self.with_entries(move |entries| { |
1060 | 0 | entries.sort_by(move |a, b| K::cmp(&a.key, &b.key)); |
1061 | 0 | }); |
1062 | 0 | } |
1063 | | |
1064 | | /// Sort the map’s key-value pairs in place using the comparison |
1065 | | /// function `cmp`. |
1066 | | /// |
1067 | | /// The comparison function receives two key and value pairs to compare (you |
1068 | | /// can sort by keys or values or their combination as needed). |
1069 | | /// |
1070 | | /// Computes in **O(n log n + c)** time and **O(n)** space where *n* is |
1071 | | /// the length of the map and *c* the capacity. The sort is stable. |
1072 | 0 | pub fn sort_by<F>(&mut self, mut cmp: F) |
1073 | 0 | where |
1074 | 0 | F: FnMut(&K, &V, &K, &V) -> Ordering, |
1075 | 0 | { |
1076 | 0 | self.with_entries(move |entries| { |
1077 | 0 | entries.sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value)); |
1078 | 0 | }); |
1079 | 0 | } |
1080 | | |
1081 | | /// Sort the key-value pairs of the map and return a by-value iterator of |
1082 | | /// the key-value pairs with the result. |
1083 | | /// |
1084 | | /// The sort is stable. |
1085 | 0 | pub fn sorted_by<F>(self, mut cmp: F) -> IntoIter<K, V> |
1086 | 0 | where |
1087 | 0 | F: FnMut(&K, &V, &K, &V) -> Ordering, |
1088 | 0 | { |
1089 | 0 | let mut entries = self.into_entries(); |
1090 | 0 | entries.sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value)); |
1091 | 0 | IntoIter::new(entries) |
1092 | 0 | } |
1093 | | |
1094 | | /// Sort the map's key-value pairs by the default ordering of the keys, but |
1095 | | /// may not preserve the order of equal elements. |
1096 | | /// |
1097 | | /// See [`sort_unstable_by`](Self::sort_unstable_by) for details. |
1098 | 0 | pub fn sort_unstable_keys(&mut self) |
1099 | 0 | where |
1100 | 0 | K: Ord, |
1101 | 0 | { |
1102 | 0 | self.with_entries(move |entries| { |
1103 | 0 | entries.sort_unstable_by(move |a, b| K::cmp(&a.key, &b.key)); Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::sort_unstable_keys::{closure#0}::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::sort_unstable_keys::{closure#0}::{closure#0} |
1104 | 0 | }); Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::sort_unstable_keys::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::sort_unstable_keys::{closure#0} |
1105 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value>>::sort_unstable_keys Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _>>::sort_unstable_keys |
1106 | | |
1107 | | /// Sort the map's key-value pairs in place using the comparison function `cmp`, but |
1108 | | /// may not preserve the order of equal elements. |
1109 | | /// |
1110 | | /// The comparison function receives two key and value pairs to compare (you |
1111 | | /// can sort by keys or values or their combination as needed). |
1112 | | /// |
1113 | | /// Computes in **O(n log n + c)** time where *n* is |
1114 | | /// the length of the map and *c* is the capacity. The sort is unstable. |
1115 | 0 | pub fn sort_unstable_by<F>(&mut self, mut cmp: F) |
1116 | 0 | where |
1117 | 0 | F: FnMut(&K, &V, &K, &V) -> Ordering, |
1118 | 0 | { |
1119 | 0 | self.with_entries(move |entries| { |
1120 | 0 | entries.sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value)); |
1121 | 0 | }); |
1122 | 0 | } |
1123 | | |
1124 | | /// Sort the key-value pairs of the map and return a by-value iterator of |
1125 | | /// the key-value pairs with the result. |
1126 | | /// |
1127 | | /// The sort is unstable. |
1128 | | #[inline] |
1129 | 0 | pub fn sorted_unstable_by<F>(self, mut cmp: F) -> IntoIter<K, V> |
1130 | 0 | where |
1131 | 0 | F: FnMut(&K, &V, &K, &V) -> Ordering, |
1132 | 0 | { |
1133 | 0 | let mut entries = self.into_entries(); |
1134 | 0 | entries.sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value)); |
1135 | 0 | IntoIter::new(entries) |
1136 | 0 | } |
1137 | | |
1138 | | /// Sort the map’s key-value pairs in place using a sort-key extraction function. |
1139 | | /// |
1140 | | /// During sorting, the function is called at most once per entry, by using temporary storage |
1141 | | /// to remember the results of its evaluation. The order of calls to the function is |
1142 | | /// unspecified and may change between versions of `indexmap` or the standard library. |
1143 | | /// |
1144 | | /// Computes in **O(m n + n log n + c)** time () and **O(n)** space, where the function is |
1145 | | /// **O(m)**, *n* is the length of the map, and *c* the capacity. The sort is stable. |
1146 | 0 | pub fn sort_by_cached_key<T, F>(&mut self, mut sort_key: F) |
1147 | 0 | where |
1148 | 0 | T: Ord, |
1149 | 0 | F: FnMut(&K, &V) -> T, |
1150 | 0 | { |
1151 | 0 | self.with_entries(move |entries| { |
1152 | 0 | entries.sort_by_cached_key(move |a| sort_key(&a.key, &a.value)); |
1153 | 0 | }); |
1154 | 0 | } |
1155 | | |
1156 | | /// Search over a sorted map for a key. |
1157 | | /// |
1158 | | /// Returns the position where that key is present, or the position where it can be inserted to |
1159 | | /// maintain the sort. See [`slice::binary_search`] for more details. |
1160 | | /// |
1161 | | /// Computes in **O(log(n))** time, which is notably less scalable than looking the key up |
1162 | | /// using [`get_index_of`][IndexMap::get_index_of], but this can also position missing keys. |
1163 | 0 | pub fn binary_search_keys(&self, x: &K) -> Result<usize, usize> |
1164 | 0 | where |
1165 | 0 | K: Ord, |
1166 | 0 | { |
1167 | 0 | self.as_slice().binary_search_keys(x) |
1168 | 0 | } |
1169 | | |
1170 | | /// Search over a sorted map with a comparator function. |
1171 | | /// |
1172 | | /// Returns the position where that value is present, or the position where it can be inserted |
1173 | | /// to maintain the sort. See [`slice::binary_search_by`] for more details. |
1174 | | /// |
1175 | | /// Computes in **O(log(n))** time. |
1176 | | #[inline] |
1177 | 0 | pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> |
1178 | 0 | where |
1179 | 0 | F: FnMut(&'a K, &'a V) -> Ordering, |
1180 | 0 | { |
1181 | 0 | self.as_slice().binary_search_by(f) |
1182 | 0 | } |
1183 | | |
1184 | | /// Search over a sorted map with an extraction function. |
1185 | | /// |
1186 | | /// Returns the position where that value is present, or the position where it can be inserted |
1187 | | /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. |
1188 | | /// |
1189 | | /// Computes in **O(log(n))** time. |
1190 | | #[inline] |
1191 | 0 | pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> |
1192 | 0 | where |
1193 | 0 | F: FnMut(&'a K, &'a V) -> B, |
1194 | 0 | B: Ord, |
1195 | 0 | { |
1196 | 0 | self.as_slice().binary_search_by_key(b, f) |
1197 | 0 | } |
1198 | | |
1199 | | /// Returns the index of the partition point of a sorted map according to the given predicate |
1200 | | /// (the index of the first element of the second partition). |
1201 | | /// |
1202 | | /// See [`slice::partition_point`] for more details. |
1203 | | /// |
1204 | | /// Computes in **O(log(n))** time. |
1205 | | #[must_use] |
1206 | 0 | pub fn partition_point<P>(&self, pred: P) -> usize |
1207 | 0 | where |
1208 | 0 | P: FnMut(&K, &V) -> bool, |
1209 | 0 | { |
1210 | 0 | self.as_slice().partition_point(pred) |
1211 | 0 | } |
1212 | | |
1213 | | /// Reverses the order of the map’s key-value pairs in place. |
1214 | | /// |
1215 | | /// Computes in **O(n)** time and **O(1)** space. |
1216 | 0 | pub fn reverse(&mut self) { |
1217 | 0 | self.core.reverse() |
1218 | 0 | } |
1219 | | |
1220 | | /// Returns a slice of all the key-value pairs in the map. |
1221 | | /// |
1222 | | /// Computes in **O(1)** time. |
1223 | 0 | pub fn as_slice(&self) -> &Slice<K, V> { |
1224 | 0 | Slice::from_slice(self.as_entries()) |
1225 | 0 | } |
1226 | | |
1227 | | /// Returns a mutable slice of all the key-value pairs in the map. |
1228 | | /// |
1229 | | /// Computes in **O(1)** time. |
1230 | 0 | pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> { |
1231 | 0 | Slice::from_mut_slice(self.as_entries_mut()) |
1232 | 0 | } |
1233 | | |
1234 | | /// Converts into a boxed slice of all the key-value pairs in the map. |
1235 | | /// |
1236 | | /// Note that this will drop the inner hash table and any excess capacity. |
1237 | 0 | pub fn into_boxed_slice(self) -> Box<Slice<K, V>> { |
1238 | 0 | Slice::from_boxed(self.into_entries().into_boxed_slice()) |
1239 | 0 | } |
1240 | | |
1241 | | /// Get a key-value pair by index |
1242 | | /// |
1243 | | /// Valid indices are `0 <= index < self.len()`. |
1244 | | /// |
1245 | | /// Computes in **O(1)** time. |
1246 | 0 | pub fn get_index(&self, index: usize) -> Option<(&K, &V)> { |
1247 | 0 | self.as_entries().get(index).map(Bucket::refs) |
1248 | 0 | } |
1249 | | |
1250 | | /// Get a key-value pair by index |
1251 | | /// |
1252 | | /// Valid indices are `0 <= index < self.len()`. |
1253 | | /// |
1254 | | /// Computes in **O(1)** time. |
1255 | 0 | pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { |
1256 | 0 | self.as_entries_mut().get_mut(index).map(Bucket::ref_mut) |
1257 | 0 | } |
1258 | | |
1259 | | /// Get an entry in the map by index for in-place manipulation. |
1260 | | /// |
1261 | | /// Valid indices are `0 <= index < self.len()`. |
1262 | | /// |
1263 | | /// Computes in **O(1)** time. |
1264 | 0 | pub fn get_index_entry(&mut self, index: usize) -> Option<IndexedEntry<'_, K, V>> { |
1265 | 0 | if index >= self.len() { |
1266 | 0 | return None; |
1267 | 0 | } |
1268 | 0 | Some(IndexedEntry::new(&mut self.core, index)) |
1269 | 0 | } |
1270 | | |
1271 | | /// Get an array of `N` key-value pairs by `N` indices |
1272 | | /// |
1273 | | /// Valid indices are *0 <= index < self.len()* and each index needs to be unique. |
1274 | | /// |
1275 | | /// # Examples |
1276 | | /// |
1277 | | /// ``` |
1278 | | /// let mut map = indexmap::IndexMap::from([(1, 'a'), (3, 'b'), (2, 'c')]); |
1279 | | /// assert_eq!(map.get_disjoint_indices_mut([2, 0]), Ok([(&2, &mut 'c'), (&1, &mut 'a')])); |
1280 | | /// ``` |
1281 | 0 | pub fn get_disjoint_indices_mut<const N: usize>( |
1282 | 0 | &mut self, |
1283 | 0 | indices: [usize; N], |
1284 | 0 | ) -> Result<[(&K, &mut V); N], GetDisjointMutError> { |
1285 | 0 | self.as_mut_slice().get_disjoint_mut(indices) |
1286 | 0 | } |
1287 | | |
1288 | | /// Returns a slice of key-value pairs in the given range of indices. |
1289 | | /// |
1290 | | /// Valid indices are `0 <= index < self.len()`. |
1291 | | /// |
1292 | | /// Computes in **O(1)** time. |
1293 | 0 | pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> { |
1294 | 0 | let entries = self.as_entries(); |
1295 | 0 | let range = try_simplify_range(range, entries.len())?; |
1296 | 0 | entries.get(range).map(Slice::from_slice) |
1297 | 0 | } |
1298 | | |
1299 | | /// Returns a mutable slice of key-value pairs in the given range of indices. |
1300 | | /// |
1301 | | /// Valid indices are `0 <= index < self.len()`. |
1302 | | /// |
1303 | | /// Computes in **O(1)** time. |
1304 | 0 | pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> { |
1305 | 0 | let entries = self.as_entries_mut(); |
1306 | 0 | let range = try_simplify_range(range, entries.len())?; |
1307 | 0 | entries.get_mut(range).map(Slice::from_mut_slice) |
1308 | 0 | } |
1309 | | |
1310 | | /// Get the first key-value pair |
1311 | | /// |
1312 | | /// Computes in **O(1)** time. |
1313 | | #[doc(alias = "first_key_value")] // like `BTreeMap` |
1314 | 0 | pub fn first(&self) -> Option<(&K, &V)> { |
1315 | 0 | self.as_entries().first().map(Bucket::refs) |
1316 | 0 | } |
1317 | | |
1318 | | /// Get the first key-value pair, with mutable access to the value |
1319 | | /// |
1320 | | /// Computes in **O(1)** time. |
1321 | 0 | pub fn first_mut(&mut self) -> Option<(&K, &mut V)> { |
1322 | 0 | self.as_entries_mut().first_mut().map(Bucket::ref_mut) |
1323 | 0 | } |
1324 | | |
1325 | | /// Get the first entry in the map for in-place manipulation. |
1326 | | /// |
1327 | | /// Computes in **O(1)** time. |
1328 | 0 | pub fn first_entry(&mut self) -> Option<IndexedEntry<'_, K, V>> { |
1329 | 0 | self.get_index_entry(0) |
1330 | 0 | } |
1331 | | |
1332 | | /// Get the last key-value pair |
1333 | | /// |
1334 | | /// Computes in **O(1)** time. |
1335 | | #[doc(alias = "last_key_value")] // like `BTreeMap` |
1336 | 0 | pub fn last(&self) -> Option<(&K, &V)> { |
1337 | 0 | self.as_entries().last().map(Bucket::refs) |
1338 | 0 | } |
1339 | | |
1340 | | /// Get the last key-value pair, with mutable access to the value |
1341 | | /// |
1342 | | /// Computes in **O(1)** time. |
1343 | 0 | pub fn last_mut(&mut self) -> Option<(&K, &mut V)> { |
1344 | 0 | self.as_entries_mut().last_mut().map(Bucket::ref_mut) |
1345 | 0 | } |
1346 | | |
1347 | | /// Get the last entry in the map for in-place manipulation. |
1348 | | /// |
1349 | | /// Computes in **O(1)** time. |
1350 | 0 | pub fn last_entry(&mut self) -> Option<IndexedEntry<'_, K, V>> { |
1351 | 0 | self.get_index_entry(self.len().checked_sub(1)?) |
1352 | 0 | } |
1353 | | |
1354 | | /// Remove the key-value pair by index |
1355 | | /// |
1356 | | /// Valid indices are `0 <= index < self.len()`. |
1357 | | /// |
1358 | | /// Like [`Vec::swap_remove`], the pair is removed by swapping it with the |
1359 | | /// last element of the map and popping it off. **This perturbs |
1360 | | /// the position of what used to be the last element!** |
1361 | | /// |
1362 | | /// Computes in **O(1)** time (average). |
1363 | 0 | pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> { |
1364 | 0 | self.core.swap_remove_index(index) |
1365 | 0 | } |
1366 | | |
1367 | | /// Remove the key-value pair by index |
1368 | | /// |
1369 | | /// Valid indices are `0 <= index < self.len()`. |
1370 | | /// |
1371 | | /// Like [`Vec::remove`], the pair is removed by shifting all of the |
1372 | | /// elements that follow it, preserving their relative order. |
1373 | | /// **This perturbs the index of all of those elements!** |
1374 | | /// |
1375 | | /// Computes in **O(n)** time (average). |
1376 | 0 | pub fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> { |
1377 | 0 | self.core.shift_remove_index(index) |
1378 | 0 | } |
1379 | | |
1380 | | /// Moves the position of a key-value pair from one index to another |
1381 | | /// by shifting all other pairs in-between. |
1382 | | /// |
1383 | | /// * If `from < to`, the other pairs will shift down while the targeted pair moves up. |
1384 | | /// * If `from > to`, the other pairs will shift up while the targeted pair moves down. |
1385 | | /// |
1386 | | /// ***Panics*** if `from` or `to` are out of bounds. |
1387 | | /// |
1388 | | /// Computes in **O(n)** time (average). |
1389 | | #[track_caller] |
1390 | 0 | pub fn move_index(&mut self, from: usize, to: usize) { |
1391 | 0 | self.core.move_index(from, to) |
1392 | 0 | } |
1393 | | |
1394 | | /// Swaps the position of two key-value pairs in the map. |
1395 | | /// |
1396 | | /// ***Panics*** if `a` or `b` are out of bounds. |
1397 | | /// |
1398 | | /// Computes in **O(1)** time (average). |
1399 | | #[track_caller] |
1400 | 0 | pub fn swap_indices(&mut self, a: usize, b: usize) { |
1401 | 0 | self.core.swap_indices(a, b) |
1402 | 0 | } |
1403 | | } |
1404 | | |
1405 | | /// Access [`IndexMap`] values corresponding to a key. |
1406 | | /// |
1407 | | /// # Examples |
1408 | | /// |
1409 | | /// ``` |
1410 | | /// use indexmap::IndexMap; |
1411 | | /// |
1412 | | /// let mut map = IndexMap::new(); |
1413 | | /// for word in "Lorem ipsum dolor sit amet".split_whitespace() { |
1414 | | /// map.insert(word.to_lowercase(), word.to_uppercase()); |
1415 | | /// } |
1416 | | /// assert_eq!(map["lorem"], "LOREM"); |
1417 | | /// assert_eq!(map["ipsum"], "IPSUM"); |
1418 | | /// ``` |
1419 | | /// |
1420 | | /// ```should_panic |
1421 | | /// use indexmap::IndexMap; |
1422 | | /// |
1423 | | /// let mut map = IndexMap::new(); |
1424 | | /// map.insert("foo", 1); |
1425 | | /// println!("{:?}", map["bar"]); // panics! |
1426 | | /// ``` |
1427 | | impl<K, V, Q: ?Sized, S> Index<&Q> for IndexMap<K, V, S> |
1428 | | where |
1429 | | Q: Hash + Equivalent<K>, |
1430 | | S: BuildHasher, |
1431 | | { |
1432 | | type Output = V; |
1433 | | |
1434 | | /// Returns a reference to the value corresponding to the supplied `key`. |
1435 | | /// |
1436 | | /// ***Panics*** if `key` is not present in the map. |
1437 | 0 | fn index(&self, key: &Q) -> &V { |
1438 | 0 | self.get(key).expect("no entry found for key") |
1439 | 0 | } |
1440 | | } |
1441 | | |
1442 | | /// Access [`IndexMap`] values corresponding to a key. |
1443 | | /// |
1444 | | /// Mutable indexing allows changing / updating values of key-value |
1445 | | /// pairs that are already present. |
1446 | | /// |
1447 | | /// You can **not** insert new pairs with index syntax, use `.insert()`. |
1448 | | /// |
1449 | | /// # Examples |
1450 | | /// |
1451 | | /// ``` |
1452 | | /// use indexmap::IndexMap; |
1453 | | /// |
1454 | | /// let mut map = IndexMap::new(); |
1455 | | /// for word in "Lorem ipsum dolor sit amet".split_whitespace() { |
1456 | | /// map.insert(word.to_lowercase(), word.to_string()); |
1457 | | /// } |
1458 | | /// let lorem = &mut map["lorem"]; |
1459 | | /// assert_eq!(lorem, "Lorem"); |
1460 | | /// lorem.retain(char::is_lowercase); |
1461 | | /// assert_eq!(map["lorem"], "orem"); |
1462 | | /// ``` |
1463 | | /// |
1464 | | /// ```should_panic |
1465 | | /// use indexmap::IndexMap; |
1466 | | /// |
1467 | | /// let mut map = IndexMap::new(); |
1468 | | /// map.insert("foo", 1); |
1469 | | /// map["bar"] = 1; // panics! |
1470 | | /// ``` |
1471 | | impl<K, V, Q: ?Sized, S> IndexMut<&Q> for IndexMap<K, V, S> |
1472 | | where |
1473 | | Q: Hash + Equivalent<K>, |
1474 | | S: BuildHasher, |
1475 | | { |
1476 | | /// Returns a mutable reference to the value corresponding to the supplied `key`. |
1477 | | /// |
1478 | | /// ***Panics*** if `key` is not present in the map. |
1479 | 0 | fn index_mut(&mut self, key: &Q) -> &mut V { |
1480 | 0 | self.get_mut(key).expect("no entry found for key") |
1481 | 0 | } |
1482 | | } |
1483 | | |
1484 | | /// Access [`IndexMap`] values at indexed positions. |
1485 | | /// |
1486 | | /// See [`Index<usize> for Keys`][keys] to access a map's keys instead. |
1487 | | /// |
1488 | | /// [keys]: Keys#impl-Index<usize>-for-Keys<'a,+K,+V> |
1489 | | /// |
1490 | | /// # Examples |
1491 | | /// |
1492 | | /// ``` |
1493 | | /// use indexmap::IndexMap; |
1494 | | /// |
1495 | | /// let mut map = IndexMap::new(); |
1496 | | /// for word in "Lorem ipsum dolor sit amet".split_whitespace() { |
1497 | | /// map.insert(word.to_lowercase(), word.to_uppercase()); |
1498 | | /// } |
1499 | | /// assert_eq!(map[0], "LOREM"); |
1500 | | /// assert_eq!(map[1], "IPSUM"); |
1501 | | /// map.reverse(); |
1502 | | /// assert_eq!(map[0], "AMET"); |
1503 | | /// assert_eq!(map[1], "SIT"); |
1504 | | /// map.sort_keys(); |
1505 | | /// assert_eq!(map[0], "AMET"); |
1506 | | /// assert_eq!(map[1], "DOLOR"); |
1507 | | /// ``` |
1508 | | /// |
1509 | | /// ```should_panic |
1510 | | /// use indexmap::IndexMap; |
1511 | | /// |
1512 | | /// let mut map = IndexMap::new(); |
1513 | | /// map.insert("foo", 1); |
1514 | | /// println!("{:?}", map[10]); // panics! |
1515 | | /// ``` |
1516 | | impl<K, V, S> Index<usize> for IndexMap<K, V, S> { |
1517 | | type Output = V; |
1518 | | |
1519 | | /// Returns a reference to the value at the supplied `index`. |
1520 | | /// |
1521 | | /// ***Panics*** if `index` is out of bounds. |
1522 | 0 | fn index(&self, index: usize) -> &V { |
1523 | 0 | if let Some((_, value)) = self.get_index(index) { |
1524 | 0 | value |
1525 | | } else { |
1526 | 0 | panic!( |
1527 | 0 | "index out of bounds: the len is {len} but the index is {index}", |
1528 | 0 | len = self.len() |
1529 | 0 | ); |
1530 | | } |
1531 | 0 | } |
1532 | | } |
1533 | | |
1534 | | /// Access [`IndexMap`] values at indexed positions. |
1535 | | /// |
1536 | | /// Mutable indexing allows changing / updating indexed values |
1537 | | /// that are already present. |
1538 | | /// |
1539 | | /// You can **not** insert new values with index syntax -- use [`.insert()`][IndexMap::insert]. |
1540 | | /// |
1541 | | /// # Examples |
1542 | | /// |
1543 | | /// ``` |
1544 | | /// use indexmap::IndexMap; |
1545 | | /// |
1546 | | /// let mut map = IndexMap::new(); |
1547 | | /// for word in "Lorem ipsum dolor sit amet".split_whitespace() { |
1548 | | /// map.insert(word.to_lowercase(), word.to_string()); |
1549 | | /// } |
1550 | | /// let lorem = &mut map[0]; |
1551 | | /// assert_eq!(lorem, "Lorem"); |
1552 | | /// lorem.retain(char::is_lowercase); |
1553 | | /// assert_eq!(map["lorem"], "orem"); |
1554 | | /// ``` |
1555 | | /// |
1556 | | /// ```should_panic |
1557 | | /// use indexmap::IndexMap; |
1558 | | /// |
1559 | | /// let mut map = IndexMap::new(); |
1560 | | /// map.insert("foo", 1); |
1561 | | /// map[10] = 1; // panics! |
1562 | | /// ``` |
1563 | | impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> { |
1564 | | /// Returns a mutable reference to the value at the supplied `index`. |
1565 | | /// |
1566 | | /// ***Panics*** if `index` is out of bounds. |
1567 | 0 | fn index_mut(&mut self, index: usize) -> &mut V { |
1568 | 0 | let len: usize = self.len(); |
1569 | | |
1570 | 0 | if let Some((_, value)) = self.get_index_mut(index) { |
1571 | 0 | value |
1572 | | } else { |
1573 | 0 | panic!("index out of bounds: the len is {len} but the index is {index}"); |
1574 | | } |
1575 | 0 | } |
1576 | | } |
1577 | | |
1578 | | impl<K, V, S> FromIterator<(K, V)> for IndexMap<K, V, S> |
1579 | | where |
1580 | | K: Hash + Eq, |
1581 | | S: BuildHasher + Default, |
1582 | | { |
1583 | | /// Create an `IndexMap` from the sequence of key-value pairs in the |
1584 | | /// iterable. |
1585 | | /// |
1586 | | /// `from_iter` uses the same logic as `extend`. See |
1587 | | /// [`extend`][IndexMap::extend] for more details. |
1588 | 0 | fn from_iter<I: IntoIterator<Item = (K, V)>>(iterable: I) -> Self { |
1589 | 0 | let iter = iterable.into_iter(); |
1590 | 0 | let (low, _) = iter.size_hint(); |
1591 | 0 | let mut map = Self::with_capacity_and_hasher(low, <_>::default()); |
1592 | 0 | map.extend(iter); |
1593 | 0 | map |
1594 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::iter::traits::collect::FromIterator<(alloc::string::String, serde_json::value::Value)>>::from_iter::<core::iter::adapters::map::Map<bson::document::IntoIter, <bson::bson::Bson>::into_relaxed_extjson::{closure#0}>> Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::iter::traits::collect::FromIterator<(alloc::string::String, serde_json::value::Value)>>::from_iter::<core::iter::adapters::map::Map<bson::document::IntoIter, <bson::bson::Bson>::into_canonical_extjson::{closure#0}>> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::iter::traits::collect::FromIterator<(_, _)>>::from_iter::<_> |
1595 | | } |
1596 | | |
1597 | | #[cfg(feature = "std")] |
1598 | | #[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
1599 | | impl<K, V, const N: usize> From<[(K, V); N]> for IndexMap<K, V, RandomState> |
1600 | | where |
1601 | | K: Hash + Eq, |
1602 | | { |
1603 | | /// # Examples |
1604 | | /// |
1605 | | /// ``` |
1606 | | /// use indexmap::IndexMap; |
1607 | | /// |
1608 | | /// let map1 = IndexMap::from([(1, 2), (3, 4)]); |
1609 | | /// let map2: IndexMap<_, _> = [(1, 2), (3, 4)].into(); |
1610 | | /// assert_eq!(map1, map2); |
1611 | | /// ``` |
1612 | 0 | fn from(arr: [(K, V); N]) -> Self { |
1613 | 0 | Self::from_iter(arr) |
1614 | 0 | } |
1615 | | } |
1616 | | |
1617 | | impl<K, V, S> Extend<(K, V)> for IndexMap<K, V, S> |
1618 | | where |
1619 | | K: Hash + Eq, |
1620 | | S: BuildHasher, |
1621 | | { |
1622 | | /// Extend the map with all key-value pairs in the iterable. |
1623 | | /// |
1624 | | /// This is equivalent to calling [`insert`][IndexMap::insert] for each of |
1625 | | /// them in order, which means that for keys that already existed |
1626 | | /// in the map, their value is updated but it keeps the existing order. |
1627 | | /// |
1628 | | /// New keys are inserted in the order they appear in the sequence. If |
1629 | | /// equivalents of a key occur more than once, the last corresponding value |
1630 | | /// prevails. |
1631 | 0 | fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iterable: I) { |
1632 | 0 | // (Note: this is a copy of `std`/`hashbrown`'s reservation logic.) |
1633 | 0 | // Keys may be already present or show multiple times in the iterator. |
1634 | 0 | // Reserve the entire hint lower bound if the map is empty. |
1635 | 0 | // Otherwise reserve half the hint (rounded up), so the map |
1636 | 0 | // will only resize twice in the worst case. |
1637 | 0 | let iter = iterable.into_iter(); |
1638 | 0 | let reserve = if self.is_empty() { |
1639 | 0 | iter.size_hint().0 |
1640 | | } else { |
1641 | 0 | (iter.size_hint().0 + 1) / 2 |
1642 | | }; |
1643 | 0 | self.reserve(reserve); |
1644 | 0 | iter.for_each(move |(k, v)| { |
1645 | 0 | self.insert(k, v); |
1646 | 0 | }); Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::iter::traits::collect::Extend<(alloc::string::String, serde_json::value::Value)>>::extend::<core::iter::adapters::map::Map<bson::document::IntoIter, <bson::bson::Bson>::into_relaxed_extjson::{closure#0}>>::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::iter::traits::collect::Extend<(alloc::string::String, serde_json::value::Value)>>::extend::<core::iter::adapters::map::Map<bson::document::IntoIter, <bson::bson::Bson>::into_canonical_extjson::{closure#0}>>::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::iter::traits::collect::Extend<(_, _)>>::extend::<_>::{closure#0} |
1647 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::iter::traits::collect::Extend<(alloc::string::String, serde_json::value::Value)>>::extend::<core::iter::adapters::map::Map<bson::document::IntoIter, <bson::bson::Bson>::into_relaxed_extjson::{closure#0}>> Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, serde_json::value::Value> as core::iter::traits::collect::Extend<(alloc::string::String, serde_json::value::Value)>>::extend::<core::iter::adapters::map::Map<bson::document::IntoIter, <bson::bson::Bson>::into_canonical_extjson::{closure#0}>> Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::iter::traits::collect::Extend<(_, _)>>::extend::<_> |
1648 | | } |
1649 | | |
1650 | | impl<'a, K, V, S> Extend<(&'a K, &'a V)> for IndexMap<K, V, S> |
1651 | | where |
1652 | | K: Hash + Eq + Copy, |
1653 | | V: Copy, |
1654 | | S: BuildHasher, |
1655 | | { |
1656 | | /// Extend the map with all key-value pairs in the iterable. |
1657 | | /// |
1658 | | /// See the first extend method for more details. |
1659 | 0 | fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iterable: I) { |
1660 | 0 | self.extend(iterable.into_iter().map(|(&key, &value)| (key, value))); |
1661 | 0 | } |
1662 | | } |
1663 | | |
1664 | | impl<K, V, S> Default for IndexMap<K, V, S> |
1665 | | where |
1666 | | S: Default, |
1667 | | { |
1668 | | /// Return an empty [`IndexMap`] |
1669 | 701k | fn default() -> Self { |
1670 | 701k | Self::with_capacity_and_hasher(0, S::default()) |
1671 | 701k | } <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState> as core::default::Default>::default Line | Count | Source | 1669 | 701k | fn default() -> Self { | 1670 | 701k | Self::with_capacity_and_hasher(0, S::default()) | 1671 | 701k | } |
Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::default::Default>::default |
1672 | | } |
1673 | | |
1674 | | impl<K, V1, S1, V2, S2> PartialEq<IndexMap<K, V2, S2>> for IndexMap<K, V1, S1> |
1675 | | where |
1676 | | K: Hash + Eq, |
1677 | | V1: PartialEq<V2>, |
1678 | | S1: BuildHasher, |
1679 | | S2: BuildHasher, |
1680 | | { |
1681 | 0 | fn eq(&self, other: &IndexMap<K, V2, S2>) -> bool { |
1682 | 0 | if self.len() != other.len() { |
1683 | 0 | return false; |
1684 | 0 | } |
1685 | 0 |
|
1686 | 0 | self.iter() |
1687 | 0 | .all(|(key, value)| other.get(key).map_or(false, |v| *value == *v)) Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState> as core::cmp::PartialEq>::eq::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::cmp::PartialEq<indexmap::map::IndexMap<_, _, _>>>::eq::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState> as core::cmp::PartialEq>::eq::{closure#0}::{closure#0} Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::cmp::PartialEq<indexmap::map::IndexMap<_, _, _>>>::eq::{closure#0}::{closure#0} |
1688 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<alloc::string::String, bson::bson::Bson, ahash::random_state::RandomState> as core::cmp::PartialEq>::eq Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::cmp::PartialEq<indexmap::map::IndexMap<_, _, _>>>::eq |
1689 | | } |
1690 | | |
1691 | | impl<K, V, S> Eq for IndexMap<K, V, S> |
1692 | | where |
1693 | | K: Eq + Hash, |
1694 | | V: Eq, |
1695 | | S: BuildHasher, |
1696 | | { |
1697 | | } |