1use std::borrow::Borrow;
25use std::collections;
26use std::collections::hash_map::RandomState;
27use std::fmt::{Debug, Error, Formatter};
28use std::hash::{BuildHasher, Hash};
29use std::iter::{FromIterator, FusedIterator, Sum};
30use std::mem;
31use std::ops::{Add, Index, IndexMut};
32
33use archery::{SharedPointer, SharedPointerKind};
34
35use crate::nodes::hamt::{
36 hash_key, Drain as NodeDrain, HashBits, HashValue, Iter as NodeIter, IterMut as NodeIterMut,
37 Node,
38};
39use crate::shared_ptr::DefaultSharedPtr;
40
41#[macro_export]
60macro_rules! hashmap {
61 () => { $crate::hashmap::HashMap::new() };
62
63 ( $( $key:expr => $value:expr ),* ) => {{
64 let mut map = $crate::hashmap::HashMap::new();
65 $({
66 map.insert($key, $value);
67 })*;
68 map
69 }};
70
71 ( $( $key:expr => $value:expr ,)* ) => {{
72 let mut map = $crate::hashmap::HashMap::new();
73 $({
74 map.insert($key, $value);
75 })*;
76 map
77 }};
78}
79
80pub type HashMap<K, V> = GenericHashMap<K, V, RandomState, DefaultSharedPtr>;
86
87pub struct GenericHashMap<K, V, S, P: SharedPointerKind> {
107 size: usize,
108 root: Option<SharedPointer<Node<(K, V), P>, P>>,
109 hasher: S,
110}
111
112impl<K, V> HashValue for (K, V)
113where
114 K: Eq,
115{
116 type Key = K;
117
118 fn extract_key(&self) -> &Self::Key {
119 &self.0
120 }
121
122 fn ptr_eq(&self, _other: &Self) -> bool {
123 false
124 }
125}
126
127impl<K, V, P> GenericHashMap<K, V, RandomState, P>
128where
129 K: Hash + Eq + Clone,
130 V: Clone,
131 P: SharedPointerKind,
132{
133 #[inline]
147 #[must_use]
148 pub fn unit(k: K, v: V) -> GenericHashMap<K, V, RandomState, P> {
149 GenericHashMap::new().update(k, v)
150 }
151}
152
153impl<K, V, S, P: SharedPointerKind> GenericHashMap<K, V, S, P> {
154 #[inline]
156 #[must_use]
157 pub fn new() -> Self
158 where
159 S: Default,
160 {
161 Self::default()
162 }
163
164 #[inline]
181 #[must_use]
182 pub fn is_empty(&self) -> bool {
183 self.len() == 0
184 }
185
186 #[inline]
202 #[must_use]
203 pub fn len(&self) -> usize {
204 self.size
205 }
206
207 pub fn ptr_eq(&self, other: &Self) -> bool {
217 match (&self.root, &other.root) {
218 (Some(a), Some(b)) => SharedPointer::ptr_eq(a, b),
219 (None, None) => true,
220 _ => false,
221 }
222 }
223
224 #[inline]
226 #[must_use]
227 pub fn with_hasher(hasher: S) -> Self {
228 GenericHashMap {
229 size: 0,
230 hasher,
231 root: None,
232 }
233 }
234
235 #[must_use]
239 pub fn hasher(&self) -> &S {
240 &self.hasher
241 }
242
243 #[inline]
246 #[must_use]
247 pub fn new_from<K1, V1>(&self) -> GenericHashMap<K1, V1, S, P>
248 where
249 K1: Hash + Eq + Clone,
250 V1: Clone,
251 S: Clone,
252 {
253 GenericHashMap {
254 size: 0,
255 root: None,
256 hasher: self.hasher.clone(),
257 }
258 }
259
260 #[inline]
268 #[must_use]
269 pub fn iter(&self) -> Iter<'_, K, V, P> {
270 Iter {
271 it: NodeIter::new(self.root.as_deref(), self.size),
272 }
273 }
274
275 #[inline]
283 #[must_use]
284 pub fn keys(&self) -> Keys<'_, K, V, P> {
285 Keys {
286 it: NodeIter::new(self.root.as_deref(), self.size),
287 }
288 }
289
290 #[inline]
298 #[must_use]
299 pub fn values(&self) -> Values<'_, K, V, P> {
300 Values {
301 it: NodeIter::new(self.root.as_deref(), self.size),
302 }
303 }
304
305 pub fn clear(&mut self) {
322 self.root = None;
323 self.size = 0;
324 }
325
326 #[cfg(test)]
329 pub fn print_structure_summary(&self) {
330 use crate::nodes::hamt::Entry as NodeEntry;
331 use std::collections::VecDeque;
332
333 println!("HashMap Structure Summary:");
334
335 #[derive(Default, Debug)]
336 struct LevelStats {
337 node_count: usize,
338 value_count: usize,
339 collision_count: usize,
340 collision_entry_sum: usize,
341 child_node_count: usize,
342 small_node_count: usize,
343 small_node_entry_sum: usize,
344 total_entries: usize,
345 }
346
347 if self.root.is_none() {
348 println!(" Empty HashMap (no root node)");
349 println!(" Total entries: 0");
350 return;
351 }
352
353 let mut level_stats: Vec<LevelStats> = Vec::new();
354 let mut queue: VecDeque<(usize, SharedPointer<Node<(K, V), P>, P>)> = VecDeque::new();
355
356 if let Some(ref root) = self.root {
358 queue.push_back((0, root.clone()));
359 }
360
361 while let Some((level, node)) = queue.pop_front() {
363 while level_stats.len() <= level {
365 level_stats.push(LevelStats::default());
366 }
367
368 let stats = &mut level_stats[level];
369 stats.node_count += 1;
370
371 node.analyze_structure(|entry| {
373 stats.total_entries += 1;
374 match entry {
375 NodeEntry::Value(_, _) => stats.value_count += 1,
376 NodeEntry::Collision(_coll) => {
377 stats.collision_count += 1;
378 }
380 NodeEntry::Node(child_node) => {
381 stats.child_node_count += 1;
382 queue.push_back((level + 1, child_node.clone()));
383 }
384 NodeEntry::SmallNode(small_node) => {
385 stats.small_node_count += 1;
386 stats.small_node_entry_sum += small_node.len();
387 }
388 }
389 })
390 }
391
392 println!(
394 " Hash level size (bits): {}",
395 crate::config::HASH_LEVEL_SIZE
396 );
397 println!(
398 " Branching factor: {}",
399 2_usize.pow(crate::config::HASH_LEVEL_SIZE as u32)
400 );
401 println!(" Total entries: {}", self.size);
402 println!(" Tree depth: {} levels", level_stats.len());
403 println!();
404
405 for (level, stats) in level_stats.iter().enumerate() {
406 println!(" Level {}:", level);
407 println!(" Nodes: {}", stats.node_count);
408
409 if stats.total_entries > 0 {
410 let avg_entries = stats.total_entries as f64 / stats.node_count as f64;
411 println!(" Average entries per node: {:.2}", avg_entries);
412
413 println!(" Entry types:");
414 println!(
415 " Values: {} ({:.1}%)",
416 stats.value_count,
417 (stats.value_count as f64 / stats.total_entries as f64) * 100.0
418 );
419 println!(
420 " Collisions: {} (avg len: {:.1}) ({:.1}%)",
421 stats.collision_count,
422 if stats.collision_count > 0 {
423 stats.collision_entry_sum as f64 / stats.collision_count as f64
424 } else {
425 0.0
426 },
427 (stats.collision_count as f64 / stats.total_entries as f64) * 100.0
428 );
429 println!(
430 " Child nodes: {} ({:.1}%)",
431 stats.child_node_count,
432 (stats.child_node_count as f64 / stats.total_entries as f64) * 100.0
433 );
434 println!(
435 " Small nodes: {} ({:.1}%)",
436 stats.small_node_count,
437 (stats.small_node_count as f64 / stats.total_entries as f64) * 100.0
438 );
439 if stats.small_node_count > 0 {
440 println!(
441 " → Avg entries per small node: {:.1}",
442 stats.small_node_entry_sum as f64 / stats.small_node_count as f64
443 );
444 }
445 }
446 println!();
447 }
448 }
449}
450
451impl<K, V, S, P> GenericHashMap<K, V, S, P>
452where
453 K: Hash + Eq,
454 S: BuildHasher + Clone,
455 P: SharedPointerKind,
456{
457 fn test_eq<S2: BuildHasher + Clone, P2: SharedPointerKind>(
458 &self,
459 other: &GenericHashMap<K, V, S2, P2>,
460 ) -> bool
461 where
462 V: PartialEq,
463 {
464 if self.len() != other.len() {
465 return false;
466 }
467 let mut seen = collections::HashSet::new();
468 for (key, value) in self.iter() {
469 if Some(value) != other.get(key) {
470 return false;
471 }
472 seen.insert(key);
473 }
474 for key in other.keys() {
475 if !seen.contains(&key) {
476 return false;
477 }
478 }
479 true
480 }
481
482 #[must_use]
498 pub fn get<BK>(&self, key: &BK) -> Option<&V>
499 where
500 BK: Hash + Eq + ?Sized,
501 K: Borrow<BK>,
502 {
503 if let Some(root) = &self.root {
504 root.get(hash_key(&self.hasher, key), 0, key)
505 .map(|(_, v)| v)
506 } else {
507 None
508 }
509 }
510
511 #[must_use]
527 pub fn get_key_value<BK>(&self, key: &BK) -> Option<(&K, &V)>
528 where
529 BK: Hash + Eq + ?Sized,
530 K: Borrow<BK>,
531 {
532 if let Some(root) = &self.root {
533 root.get(hash_key(&self.hasher, key), 0, key)
534 .map(|(k, v)| (k, v))
535 } else {
536 None
537 }
538 }
539
540 #[inline]
558 #[must_use]
559 pub fn contains_key<BK>(&self, k: &BK) -> bool
560 where
561 BK: Hash + Eq + ?Sized,
562 K: Borrow<BK>,
563 {
564 self.get(k).is_some()
565 }
566
567 #[must_use]
575 pub fn is_submap_by<B, RM, F, P2: SharedPointerKind>(&self, other: RM, mut cmp: F) -> bool
576 where
577 F: FnMut(&V, &B) -> bool,
578 RM: Borrow<GenericHashMap<K, B, S, P2>>,
579 {
580 self.iter()
581 .all(|(k, v)| other.borrow().get(k).map(|ov| cmp(v, ov)).unwrap_or(false))
582 }
583
584 #[must_use]
593 pub fn is_proper_submap_by<B, RM, F, P2: SharedPointerKind>(&self, other: RM, cmp: F) -> bool
594 where
595 F: FnMut(&V, &B) -> bool,
596 RM: Borrow<GenericHashMap<K, B, S, P2>>,
597 {
598 self.len() != other.borrow().len() && self.is_submap_by(other, cmp)
599 }
600
601 #[inline]
617 #[must_use]
618 pub fn is_submap<RM>(&self, other: RM) -> bool
619 where
620 V: PartialEq,
621 RM: Borrow<Self>,
622 {
623 self.is_submap_by(other.borrow(), PartialEq::eq)
624 }
625
626 #[inline]
647 #[must_use]
648 pub fn is_proper_submap<RM>(&self, other: RM) -> bool
649 where
650 V: PartialEq,
651 RM: Borrow<Self>,
652 {
653 self.is_proper_submap_by(other.borrow(), PartialEq::eq)
654 }
655}
656
657impl<K, V, S, P> GenericHashMap<K, V, S, P>
658where
659 K: Hash + Eq + Clone,
660 V: Clone,
661 S: BuildHasher + Clone,
662 P: SharedPointerKind,
663{
664 #[inline]
672 #[must_use]
673 pub fn iter_mut(&mut self) -> IterMut<'_, K, V, P> {
674 let root = self.root.as_mut().map(|r| SharedPointer::make_mut(r));
675 IterMut {
676 it: NodeIterMut::new(root, self.size),
677 }
678 }
679
680 #[must_use]
700 pub fn get_mut<BK>(&mut self, key: &BK) -> Option<&mut V>
701 where
702 BK: Hash + Eq + ?Sized,
703 K: Borrow<BK>,
704 {
705 self.get_key_value_mut(key).map(|(_, v)| v)
706 }
707
708 #[must_use]
724 pub fn get_key_value_mut<BK>(&mut self, key: &BK) -> Option<(&K, &mut V)>
725 where
726 BK: Hash + Eq + ?Sized,
727 K: Borrow<BK>,
728 {
729 let Some(root) = self.root.as_mut() else {
730 return None;
731 };
732 match SharedPointer::make_mut(root).get_mut(hash_key(&self.hasher, key), 0, key) {
733 None => None,
734 Some((key, value)) => Some((key, value)),
735 }
736 }
737
738 #[inline]
759 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
760 let hash = hash_key(&self.hasher, &k);
761 let root = SharedPointer::make_mut(self.root.get_or_insert_with(SharedPointer::default));
762 let result = root.insert(hash, 0, (k, v));
763 if result.is_none() {
764 self.size += 1;
765 }
766 result.map(|(_, v)| v)
767 }
768
769 pub fn remove<BK>(&mut self, k: &BK) -> Option<V>
790 where
791 BK: Hash + Eq + ?Sized,
792 K: Borrow<BK>,
793 {
794 self.remove_with_key(k).map(|(_, v)| v)
795 }
796
797 pub fn remove_with_key<BK>(&mut self, k: &BK) -> Option<(K, V)>
814 where
815 BK: Hash + Eq + ?Sized,
816 K: Borrow<BK>,
817 {
818 let Some(root) = &mut self.root else {
819 return None;
820 };
821 let result = SharedPointer::make_mut(root).remove(hash_key(&self.hasher, k), 0, k);
822 if result.is_some() {
823 self.size -= 1;
824 }
825 result
826 }
827
828 #[must_use]
834 pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S, P> {
835 let hash = hash_key(&self.hasher, &key);
836 if self
837 .root
838 .as_ref()
839 .and_then(|r| r.get(hash, 0, &key))
840 .is_some()
841 {
842 Entry::Occupied(OccupiedEntry {
843 map: self,
844 hash,
845 key,
846 })
847 } else {
848 Entry::Vacant(VacantEntry {
849 map: self,
850 hash,
851 key,
852 })
853 }
854 }
855
856 #[inline]
875 #[must_use]
876 pub fn update(&self, k: K, v: V) -> Self {
877 let mut out = self.clone();
878 out.insert(k, v);
879 out
880 }
881
882 #[must_use]
891 pub fn update_with<F>(&self, k: K, v: V, f: F) -> Self
892 where
893 F: FnOnce(V, V) -> V,
894 {
895 match self.extract_with_key(&k) {
896 None => self.update(k, v),
897 Some((_, v2, m)) => m.update(k, f(v2, v)),
898 }
899 }
900
901 #[must_use]
910 pub fn update_with_key<F>(&self, k: K, v: V, f: F) -> Self
911 where
912 F: FnOnce(&K, V, V) -> V,
913 {
914 match self.extract_with_key(&k) {
915 None => self.update(k, v),
916 Some((_, v2, m)) => {
917 let out_v = f(&k, v2, v);
918 m.update(k, out_v)
919 }
920 }
921 }
922
923 #[must_use]
933 pub fn update_lookup_with_key<F>(&self, k: K, v: V, f: F) -> (Option<V>, Self)
934 where
935 F: FnOnce(&K, &V, V) -> V,
936 {
937 match self.extract_with_key(&k) {
938 None => (None, self.update(k, v)),
939 Some((_, v2, m)) => {
940 let out_v = f(&k, &v2, v);
941 (Some(v2), m.update(k, out_v))
942 }
943 }
944 }
945
946 #[must_use]
959 pub fn alter<F>(&self, f: F, k: K) -> Self
960 where
961 F: FnOnce(Option<V>) -> Option<V>,
962 {
963 let pop = self.extract_with_key(&k);
964 match (f(pop.as_ref().map(|(_, v, _)| v.clone())), pop) {
965 (None, None) => self.clone(),
966 (Some(v), None) => self.update(k, v),
967 (None, Some((_, _, m))) => m,
968 (Some(v), Some((_, _, m))) => m.update(k, v),
969 }
970 }
971
972 #[must_use]
979 pub fn without<BK>(&self, k: &BK) -> Self
980 where
981 BK: Hash + Eq + ?Sized,
982 K: Borrow<BK>,
983 {
984 match self.extract_with_key(k) {
985 None => self.clone(),
986 Some((_, _, map)) => map,
987 }
988 }
989
990 pub fn retain<F>(&mut self, mut f: F)
1010 where
1011 F: FnMut(&K, &V) -> bool,
1012 {
1013 let Some(root) = &mut self.root else {
1014 return;
1015 };
1016 let old_root = root.clone();
1017 let root = SharedPointer::make_mut(root);
1018 for ((key, value), hash) in NodeIter::new(Some(&old_root), self.size) {
1019 if !f(key, value) && root.remove(hash, 0, key).is_some() {
1020 self.size -= 1;
1021 }
1022 }
1023 }
1024
1025 #[must_use]
1030 pub fn extract<BK>(&self, k: &BK) -> Option<(V, Self)>
1031 where
1032 BK: Hash + Eq + ?Sized,
1033 K: Borrow<BK>,
1034 {
1035 self.extract_with_key(k).map(|(_, v, m)| (v, m))
1036 }
1037
1038 #[must_use]
1043 pub fn extract_with_key<BK>(&self, k: &BK) -> Option<(K, V, Self)>
1044 where
1045 BK: Hash + Eq + ?Sized,
1046 K: Borrow<BK>,
1047 {
1048 let mut out = self.clone();
1049 out.remove_with_key(k).map(|(k, v)| (k, v, out))
1050 }
1051
1052 #[must_use]
1068 pub fn union(self, other: Self) -> Self {
1069 let (mut to_mutate, to_consume, use_to_consume) = if self.len() >= other.len() {
1070 (self, other, false)
1071 } else {
1072 (other, self, true)
1073 };
1074 for (k, v) in to_consume {
1075 match to_mutate.entry(k) {
1076 Entry::Occupied(mut e) if use_to_consume => {
1077 e.insert(v);
1078 }
1079 Entry::Vacant(e) => {
1080 e.insert(v);
1081 }
1082 _ => {}
1083 }
1084 }
1085 to_mutate
1086 }
1087
1088 #[inline]
1098 #[must_use]
1099 pub fn union_with<F>(self, other: Self, mut f: F) -> Self
1100 where
1101 F: FnMut(V, V) -> V,
1102 {
1103 self.union_with_key(other, |_, v1, v2| f(v1, v2))
1104 }
1105
1106 #[must_use]
1131 pub fn union_with_key<F>(self, other: Self, mut f: F) -> Self
1132 where
1133 F: FnMut(&K, V, V) -> V,
1134 {
1135 if self.len() >= other.len() {
1136 self.union_with_key_inner(other, f)
1137 } else {
1138 other.union_with_key_inner(self, |key, other_value, self_value| {
1139 f(key, self_value, other_value)
1140 })
1141 }
1142 }
1143
1144 fn union_with_key_inner<F>(mut self, other: Self, mut f: F) -> Self
1145 where
1146 F: FnMut(&K, V, V) -> V,
1147 {
1148 for (key, right_value) in other {
1149 match self.remove(&key) {
1150 None => {
1151 self.insert(key, right_value);
1152 }
1153 Some(left_value) => {
1154 let final_value = f(&key, left_value, right_value);
1155 self.insert(key, final_value);
1156 }
1157 }
1158 }
1159 self
1160 }
1161
1162 #[must_use]
1178 pub fn unions<I>(i: I) -> Self
1179 where
1180 S: Default,
1181 I: IntoIterator<Item = Self>,
1182 {
1183 i.into_iter().fold(Self::default(), Self::union)
1184 }
1185
1186 #[must_use]
1197 pub fn unions_with<I, F>(i: I, f: F) -> Self
1198 where
1199 S: Default,
1200 I: IntoIterator<Item = Self>,
1201 F: Fn(V, V) -> V,
1202 {
1203 i.into_iter()
1204 .fold(Self::default(), |a, b| a.union_with(b, &f))
1205 }
1206
1207 #[must_use]
1219 pub fn unions_with_key<I, F>(i: I, f: F) -> Self
1220 where
1221 S: Default,
1222 I: IntoIterator<Item = Self>,
1223 F: Fn(&K, V, V) -> V,
1224 {
1225 i.into_iter()
1226 .fold(Self::default(), |a, b| a.union_with_key(b, &f))
1227 }
1228
1229 #[deprecated(
1250 since = "2.0.1",
1251 note = "to avoid conflicting behaviors between std and imbl, the `difference` alias for `symmetric_difference` will be removed."
1252 )]
1253 #[inline]
1254 #[must_use]
1255 pub fn difference(self, other: Self) -> Self {
1256 self.symmetric_difference(other)
1257 }
1258
1259 #[inline]
1275 #[must_use]
1276 pub fn symmetric_difference(self, other: Self) -> Self {
1277 self.symmetric_difference_with_key(other, |_, _, _| None)
1278 }
1279
1280 #[deprecated(
1290 since = "2.0.1",
1291 note = "to avoid conflicting behaviors between std and imbl, the `difference_with` alias for `symmetric_difference_with` will be removed."
1292 )]
1293 #[inline]
1294 #[must_use]
1295 pub fn difference_with<F>(self, other: Self, f: F) -> Self
1296 where
1297 F: FnMut(V, V) -> Option<V>,
1298 {
1299 self.symmetric_difference_with(other, f)
1300 }
1301
1302 #[inline]
1307 #[must_use]
1308 pub fn symmetric_difference_with<F>(self, other: Self, mut f: F) -> Self
1309 where
1310 F: FnMut(V, V) -> Option<V>,
1311 {
1312 self.symmetric_difference_with_key(other, |_, a, b| f(a, b))
1313 }
1314
1315 #[deprecated(
1341 since = "2.0.1",
1342 note = "to avoid conflicting behaviors between std and imbl, the `difference_with_key` alias for `symmetric_difference_with_key` will be removed."
1343 )]
1344 #[must_use]
1345 pub fn difference_with_key<F>(self, other: Self, f: F) -> Self
1346 where
1347 F: FnMut(&K, V, V) -> Option<V>,
1348 {
1349 self.symmetric_difference_with_key(other, f)
1350 }
1351
1352 #[must_use]
1372 pub fn symmetric_difference_with_key<F>(mut self, other: Self, mut f: F) -> Self
1373 where
1374 F: FnMut(&K, V, V) -> Option<V>,
1375 {
1376 let mut out = self.new_from();
1377 for (key, right_value) in other {
1378 match self.remove(&key) {
1379 None => {
1380 out.insert(key, right_value);
1381 }
1382 Some(left_value) => {
1383 if let Some(final_value) = f(&key, left_value, right_value) {
1384 out.insert(key, final_value);
1385 }
1386 }
1387 }
1388 }
1389 out.union(self)
1390 }
1391
1392 #[inline]
1408 #[must_use]
1409 pub fn relative_complement(mut self, other: Self) -> Self {
1410 for (key, _) in other {
1411 let _ = self.remove(&key);
1412 }
1413 self
1414 }
1415
1416 #[inline]
1432 #[must_use]
1433 pub fn intersection(self, other: Self) -> Self {
1434 self.intersection_with_key(other, |_, v, _| v)
1435 }
1436
1437 #[inline]
1443 #[must_use]
1444 pub fn intersection_with<B, C, F>(
1445 self,
1446 other: GenericHashMap<K, B, S, P>,
1447 mut f: F,
1448 ) -> GenericHashMap<K, C, S, P>
1449 where
1450 B: Clone,
1451 C: Clone,
1452 F: FnMut(V, B) -> C,
1453 {
1454 self.intersection_with_key(other, |_, v1, v2| f(v1, v2))
1455 }
1456
1457 #[must_use]
1477 pub fn intersection_with_key<B, C, F>(
1478 mut self,
1479 other: GenericHashMap<K, B, S, P>,
1480 mut f: F,
1481 ) -> GenericHashMap<K, C, S, P>
1482 where
1483 B: Clone,
1484 C: Clone,
1485 F: FnMut(&K, V, B) -> C,
1486 {
1487 let mut out = self.new_from();
1488 for (key, right_value) in other {
1489 match self.remove(&key) {
1490 None => (),
1491 Some(left_value) => {
1492 let result = f(&key, left_value, right_value);
1493 out.insert(key, result);
1494 }
1495 }
1496 }
1497 out
1498 }
1499}
1500
1501pub enum Entry<'a, K, V, S, P>
1515where
1516 K: Hash + Eq + Clone,
1517 V: Clone,
1518 S: BuildHasher + Clone,
1519 P: SharedPointerKind,
1520{
1521 Occupied(OccupiedEntry<'a, K, V, S, P>),
1523 Vacant(VacantEntry<'a, K, V, S, P>),
1525}
1526
1527impl<'a, K, V, S, P> Entry<'a, K, V, S, P>
1528where
1529 K: 'a + Hash + Eq + Clone,
1530 V: 'a + Clone,
1531 S: 'a + BuildHasher + Clone,
1532 P: SharedPointerKind,
1533{
1534 pub fn or_insert(self, default: V) -> &'a mut V {
1537 self.or_insert_with(|| default)
1538 }
1539
1540 pub fn or_insert_with<F>(self, default: F) -> &'a mut V
1544 where
1545 F: FnOnce() -> V,
1546 {
1547 match self {
1548 Entry::Occupied(entry) => entry.into_mut(),
1549 Entry::Vacant(entry) => entry.insert(default()),
1550 }
1551 }
1552
1553 pub fn or_default(self) -> &'a mut V
1556 where
1557 V: Default,
1558 {
1559 #[allow(clippy::unwrap_or_default)]
1560 self.or_insert_with(Default::default)
1561 }
1562
1563 #[must_use]
1565 pub fn key(&self) -> &K {
1566 match self {
1567 Entry::Occupied(entry) => entry.key(),
1568 Entry::Vacant(entry) => entry.key(),
1569 }
1570 }
1571
1572 #[must_use]
1575 pub fn and_modify<F>(mut self, f: F) -> Self
1576 where
1577 F: FnOnce(&mut V),
1578 {
1579 match &mut self {
1580 Entry::Occupied(ref mut entry) => f(entry.get_mut()),
1581 Entry::Vacant(_) => (),
1582 }
1583 self
1584 }
1585}
1586
1587pub struct OccupiedEntry<'a, K, V, S, P>
1589where
1590 K: Hash + Eq + Clone,
1591 V: Clone,
1592 S: BuildHasher + Clone,
1593 P: SharedPointerKind,
1594{
1595 map: &'a mut GenericHashMap<K, V, S, P>,
1596 hash: HashBits,
1597 key: K,
1598}
1599
1600impl<'a, K, V, S, P> OccupiedEntry<'a, K, V, S, P>
1601where
1602 K: 'a + Hash + Eq + Clone,
1603 V: 'a + Clone,
1604 S: 'a + BuildHasher + Clone,
1605 P: SharedPointerKind,
1606{
1607 #[must_use]
1609 pub fn key(&self) -> &K {
1610 &self.key
1611 }
1612
1613 pub fn remove_entry(self) -> (K, V) {
1615 let root = SharedPointer::make_mut(self.map.root.as_mut().unwrap());
1617 let result = root.remove(self.hash, 0, &self.key);
1618 self.map.size -= 1;
1619 result.unwrap()
1620 }
1621
1622 #[must_use]
1624 pub fn get(&self) -> &V {
1625 &self
1627 .map
1628 .root
1629 .as_ref()
1630 .unwrap()
1631 .get(self.hash, 0, &self.key)
1632 .unwrap()
1633 .1
1634 }
1635
1636 #[must_use]
1638 pub fn get_mut(&mut self) -> &mut V {
1639 let root = SharedPointer::make_mut(self.map.root.as_mut().unwrap());
1641 &mut root.get_mut(self.hash, 0, &self.key).unwrap().1
1642 }
1643
1644 #[must_use]
1646 pub fn into_mut(self) -> &'a mut V {
1647 let root = SharedPointer::make_mut(self.map.root.as_mut().unwrap());
1649 &mut root.get_mut(self.hash, 0, &self.key).unwrap().1
1650 }
1651
1652 pub fn insert(&mut self, value: V) -> V {
1654 mem::replace(self.get_mut(), value)
1655 }
1656
1657 pub fn remove(self) -> V {
1659 self.remove_entry().1
1660 }
1661}
1662
1663pub struct VacantEntry<'a, K, V, S, P>
1665where
1666 K: Hash + Eq + Clone,
1667 V: Clone,
1668 S: BuildHasher + Clone,
1669 P: SharedPointerKind,
1670{
1671 map: &'a mut GenericHashMap<K, V, S, P>,
1672 hash: HashBits,
1673 key: K,
1674}
1675
1676impl<'a, K, V, S, P> VacantEntry<'a, K, V, S, P>
1677where
1678 K: 'a + Hash + Eq + Clone,
1679 V: 'a + Clone,
1680 S: 'a + BuildHasher + Clone,
1681 P: SharedPointerKind,
1682{
1683 #[must_use]
1685 pub fn key(&self) -> &K {
1686 &self.key
1687 }
1688
1689 #[must_use]
1691 pub fn into_key(self) -> K {
1692 self.key
1693 }
1694
1695 pub fn insert(self, value: V) -> &'a mut V {
1697 let root =
1698 SharedPointer::make_mut(self.map.root.get_or_insert_with(SharedPointer::default));
1699 if root
1700 .insert(self.hash, 0, (self.key.clone(), value))
1701 .is_none()
1702 {
1703 self.map.size += 1;
1704 }
1705 &mut root.get_mut(self.hash, 0, &self.key).unwrap().1
1708 }
1709}
1710
1711impl<K, V, S, P> Clone for GenericHashMap<K, V, S, P>
1714where
1715 K: Clone,
1716 V: Clone,
1717 S: Clone,
1718 P: SharedPointerKind,
1719{
1720 #[inline]
1724 fn clone(&self) -> Self {
1725 GenericHashMap {
1726 root: self.root.clone(),
1727 size: self.size,
1728 hasher: self.hasher.clone(),
1729 }
1730 }
1731}
1732
1733impl<K, V, S1, S2, P1, P2> PartialEq<GenericHashMap<K, V, S2, P2>> for GenericHashMap<K, V, S1, P1>
1734where
1735 K: Hash + Eq,
1736 V: PartialEq,
1737 S1: BuildHasher + Clone,
1738 S2: BuildHasher + Clone,
1739 P1: SharedPointerKind,
1740 P2: SharedPointerKind,
1741{
1742 fn eq(&self, other: &GenericHashMap<K, V, S2, P2>) -> bool {
1743 self.test_eq(other)
1744 }
1745}
1746
1747impl<K, V, S, P> Eq for GenericHashMap<K, V, S, P>
1748where
1749 K: Hash + Eq,
1750 V: Eq,
1751 S: BuildHasher + Clone,
1752 P: SharedPointerKind,
1753{
1754}
1755
1756impl<K, V, S, P> Default for GenericHashMap<K, V, S, P>
1757where
1758 S: Default,
1759 P: SharedPointerKind,
1760{
1761 #[inline]
1762 fn default() -> Self {
1763 GenericHashMap {
1764 size: 0,
1765 root: None,
1766 hasher: Default::default(),
1767 }
1768 }
1769}
1770
1771impl<K, V, S, P> Add for GenericHashMap<K, V, S, P>
1772where
1773 K: Hash + Eq + Clone,
1774 V: Clone,
1775 S: BuildHasher + Clone,
1776 P: SharedPointerKind,
1777{
1778 type Output = GenericHashMap<K, V, S, P>;
1779
1780 fn add(self, other: Self) -> Self::Output {
1781 self.union(other)
1782 }
1783}
1784
1785impl<'a, K, V, S, P> Add for &'a GenericHashMap<K, V, S, P>
1786where
1787 K: Hash + Eq + Clone,
1788 V: Clone,
1789 S: BuildHasher + Clone,
1790 P: SharedPointerKind,
1791{
1792 type Output = GenericHashMap<K, V, S, P>;
1793
1794 fn add(self, other: Self) -> Self::Output {
1795 self.clone().union(other.clone())
1796 }
1797}
1798
1799impl<K, V, S, P> Sum for GenericHashMap<K, V, S, P>
1800where
1801 K: Hash + Eq + Clone,
1802 V: Clone,
1803 S: BuildHasher + Default + Clone,
1804 P: SharedPointerKind,
1805{
1806 fn sum<I>(it: I) -> Self
1807 where
1808 I: Iterator<Item = Self>,
1809 {
1810 it.fold(Self::default(), |a, b| a + b)
1811 }
1812}
1813
1814impl<K, V, S, RK, RV, P> Extend<(RK, RV)> for GenericHashMap<K, V, S, P>
1815where
1816 K: Hash + Eq + Clone + From<RK>,
1817 V: Clone + From<RV>,
1818 S: BuildHasher + Clone,
1819 P: SharedPointerKind,
1820{
1821 fn extend<I>(&mut self, iter: I)
1822 where
1823 I: IntoIterator<Item = (RK, RV)>,
1824 {
1825 for (key, value) in iter {
1826 self.insert(From::from(key), From::from(value));
1827 }
1828 }
1829}
1830
1831impl<'a, BK, K, V, S, P> Index<&'a BK> for GenericHashMap<K, V, S, P>
1832where
1833 BK: Hash + Eq + ?Sized,
1834 K: Hash + Eq + Borrow<BK>,
1835 S: BuildHasher + Clone,
1836 P: SharedPointerKind,
1837{
1838 type Output = V;
1839
1840 fn index(&self, key: &BK) -> &Self::Output {
1841 match self.get(key) {
1842 None => panic!("HashMap::index: invalid key"),
1843 Some(value) => value,
1844 }
1845 }
1846}
1847
1848impl<'a, BK, K, V, S, P> IndexMut<&'a BK> for GenericHashMap<K, V, S, P>
1849where
1850 BK: Hash + Eq + ?Sized,
1851 K: Hash + Eq + Clone + Borrow<BK>,
1852 V: Clone,
1853 S: BuildHasher + Clone,
1854 P: SharedPointerKind,
1855{
1856 fn index_mut(&mut self, key: &BK) -> &mut Self::Output {
1857 match self.get_mut(key) {
1858 None => panic!("HashMap::index_mut: invalid key"),
1859 Some(value) => value,
1860 }
1861 }
1862}
1863
1864impl<K, V, S, P> Debug for GenericHashMap<K, V, S, P>
1865where
1866 K: Debug,
1867 V: Debug,
1868 P: SharedPointerKind,
1869{
1870 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
1871 let mut d = f.debug_map();
1872 for (k, v) in self {
1873 d.entry(k, v);
1874 }
1875 d.finish()
1876 }
1877}
1878
1879pub struct Iter<'a, K, V, P: SharedPointerKind> {
1883 it: NodeIter<'a, (K, V), P>,
1884}
1885
1886impl<'a, K, V, P: SharedPointerKind> Clone for Iter<'a, K, V, P> {
1888 fn clone(&self) -> Self {
1889 Iter {
1890 it: self.it.clone(),
1891 }
1892 }
1893}
1894
1895impl<'a, K, V, P: SharedPointerKind> Iterator for Iter<'a, K, V, P> {
1896 type Item = (&'a K, &'a V);
1897
1898 fn next(&mut self) -> Option<Self::Item> {
1899 self.it.next().map(|((k, v), _)| (k, v))
1900 }
1901
1902 fn size_hint(&self) -> (usize, Option<usize>) {
1903 self.it.size_hint()
1904 }
1905}
1906
1907impl<'a, K, V, P: SharedPointerKind> ExactSizeIterator for Iter<'a, K, V, P> {}
1908
1909impl<'a, K, V, P: SharedPointerKind> FusedIterator for Iter<'a, K, V, P> {}
1910
1911pub struct IterMut<'a, K, V, P>
1913where
1914 K: Clone,
1915 V: Clone,
1916 P: SharedPointerKind,
1917{
1918 it: NodeIterMut<'a, (K, V), P>,
1919}
1920
1921impl<'a, K, V, P> Iterator for IterMut<'a, K, V, P>
1922where
1923 K: Clone,
1924 V: Clone,
1925 P: SharedPointerKind,
1926{
1927 type Item = (&'a K, &'a mut V);
1928
1929 fn next(&mut self) -> Option<Self::Item> {
1930 self.it.next().map(|((k, v), _)| (&*k, v))
1931 }
1932
1933 fn size_hint(&self) -> (usize, Option<usize>) {
1934 self.it.size_hint()
1935 }
1936}
1937
1938impl<'a, K, V, P> ExactSizeIterator for IterMut<'a, K, V, P>
1939where
1940 K: Clone,
1941 V: Clone,
1942 P: SharedPointerKind,
1943{
1944}
1945
1946impl<'a, K, V, P> FusedIterator for IterMut<'a, K, V, P>
1947where
1948 K: Clone,
1949 V: Clone,
1950 P: SharedPointerKind,
1951{
1952}
1953
1954pub struct ConsumingIter<A: HashValue, P: SharedPointerKind> {
1956 it: NodeDrain<A, P>,
1957}
1958
1959impl<A, P: SharedPointerKind> Iterator for ConsumingIter<A, P>
1960where
1961 A: HashValue + Clone,
1962{
1963 type Item = A;
1964
1965 fn next(&mut self) -> Option<Self::Item> {
1966 self.it.next().map(|(p, _)| p)
1967 }
1968
1969 fn size_hint(&self) -> (usize, Option<usize>) {
1970 self.it.size_hint()
1971 }
1972}
1973
1974impl<A, P> ExactSizeIterator for ConsumingIter<A, P>
1975where
1976 A: HashValue + Clone,
1977 P: SharedPointerKind,
1978{
1979}
1980
1981impl<A, P> FusedIterator for ConsumingIter<A, P>
1982where
1983 A: HashValue + Clone,
1984 P: SharedPointerKind,
1985{
1986}
1987
1988pub struct Keys<'a, K, V, P: SharedPointerKind> {
1990 it: NodeIter<'a, (K, V), P>,
1991}
1992
1993impl<'a, K, V, P: SharedPointerKind> Iterator for Keys<'a, K, V, P> {
1994 type Item = &'a K;
1995
1996 fn next(&mut self) -> Option<Self::Item> {
1997 self.it.next().map(|((k, _), _)| k)
1998 }
1999
2000 fn size_hint(&self) -> (usize, Option<usize>) {
2001 self.it.size_hint()
2002 }
2003}
2004
2005impl<'a, K, V, P: SharedPointerKind> ExactSizeIterator for Keys<'a, K, V, P> {}
2006
2007impl<'a, K, V, P: SharedPointerKind> FusedIterator for Keys<'a, K, V, P> {}
2008
2009pub struct Values<'a, K, V, P: SharedPointerKind> {
2011 it: NodeIter<'a, (K, V), P>,
2012}
2013
2014impl<'a, K, V, P: SharedPointerKind> Iterator for Values<'a, K, V, P> {
2015 type Item = &'a V;
2016
2017 fn next(&mut self) -> Option<Self::Item> {
2018 self.it.next().map(|((_, v), _)| v)
2019 }
2020
2021 fn size_hint(&self) -> (usize, Option<usize>) {
2022 self.it.size_hint()
2023 }
2024}
2025
2026impl<'a, K, V, P: SharedPointerKind> ExactSizeIterator for Values<'a, K, V, P> {}
2027
2028impl<'a, K, V, P: SharedPointerKind> FusedIterator for Values<'a, K, V, P> {}
2029
2030impl<'a, K, V, S, P: SharedPointerKind> IntoIterator for &'a GenericHashMap<K, V, S, P> {
2031 type Item = (&'a K, &'a V);
2032 type IntoIter = Iter<'a, K, V, P>;
2033
2034 #[inline]
2035 fn into_iter(self) -> Self::IntoIter {
2036 self.iter()
2037 }
2038}
2039
2040impl<K, V, S, P> IntoIterator for GenericHashMap<K, V, S, P>
2041where
2042 K: Hash + Eq + Clone,
2043 V: Clone,
2044 S: BuildHasher + Clone,
2045 P: SharedPointerKind,
2046{
2047 type Item = (K, V);
2048 type IntoIter = ConsumingIter<(K, V), P>;
2049
2050 #[inline]
2051 fn into_iter(self) -> Self::IntoIter {
2052 ConsumingIter {
2053 it: NodeDrain::new(self.root, self.size),
2054 }
2055 }
2056}
2057
2058impl<K, V, S, P> FromIterator<(K, V)> for GenericHashMap<K, V, S, P>
2061where
2062 K: Hash + Eq + Clone,
2063 V: Clone,
2064 S: BuildHasher + Default + Clone,
2065 P: SharedPointerKind,
2066{
2067 fn from_iter<T>(i: T) -> Self
2068 where
2069 T: IntoIterator<Item = (K, V)>,
2070 {
2071 let mut map = Self::default();
2072 for (k, v) in i {
2073 map.insert(k, v);
2074 }
2075 map
2076 }
2077}
2078
2079impl<K, V, S, P: SharedPointerKind> AsRef<GenericHashMap<K, V, S, P>>
2080 for GenericHashMap<K, V, S, P>
2081{
2082 #[inline]
2083 fn as_ref(&self) -> &Self {
2084 self
2085 }
2086}
2087
2088impl<'m, 'k, 'v, K, V, OK, OV, SA, SB, P1, P2> From<&'m GenericHashMap<&'k K, &'v V, SA, P1>>
2089 for GenericHashMap<OK, OV, SB, P2>
2090where
2091 K: Hash + Eq + ToOwned<Owned = OK> + ?Sized,
2092 V: ToOwned<Owned = OV> + ?Sized,
2093 OK: Hash + Eq + Clone + Borrow<K>,
2094 OV: Borrow<V> + Clone,
2095 SA: BuildHasher + Clone,
2096 SB: BuildHasher + Default + Clone,
2097 P1: SharedPointerKind,
2098 P2: SharedPointerKind,
2099{
2100 fn from(m: &GenericHashMap<&K, &V, SA, P1>) -> Self {
2101 m.iter()
2102 .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
2103 .collect()
2104 }
2105}
2106
2107impl<'a, K, V, S, P> From<&'a [(K, V)]> for GenericHashMap<K, V, S, P>
2108where
2109 K: Hash + Eq + Clone,
2110 V: Clone,
2111 S: BuildHasher + Default + Clone,
2112 P: SharedPointerKind,
2113{
2114 fn from(m: &'a [(K, V)]) -> Self {
2115 m.iter().cloned().collect()
2116 }
2117}
2118
2119impl<K, V, S, P> From<Vec<(K, V)>> for GenericHashMap<K, V, S, P>
2120where
2121 K: Hash + Eq + Clone,
2122 V: Clone,
2123 S: BuildHasher + Default + Clone,
2124 P: SharedPointerKind,
2125{
2126 fn from(m: Vec<(K, V)>) -> Self {
2127 m.into_iter().collect()
2128 }
2129}
2130
2131impl<'a, K, V, S, P> From<&'a Vec<(K, V)>> for GenericHashMap<K, V, S, P>
2132where
2133 K: Hash + Eq + Clone,
2134 V: Clone,
2135 S: BuildHasher + Default + Clone,
2136 P: SharedPointerKind,
2137{
2138 fn from(m: &'a Vec<(K, V)>) -> Self {
2139 m.iter().cloned().collect()
2140 }
2141}
2142
2143impl<K, V, S1, S2, P> From<collections::HashMap<K, V, S2>> for GenericHashMap<K, V, S1, P>
2144where
2145 K: Hash + Eq + Clone,
2146 V: Clone,
2147 S1: BuildHasher + Default + Clone,
2148 S2: BuildHasher,
2149 P: SharedPointerKind,
2150{
2151 fn from(m: collections::HashMap<K, V, S2>) -> Self {
2152 m.into_iter().collect()
2153 }
2154}
2155
2156impl<'a, K, V, S1, S2, P> From<&'a collections::HashMap<K, V, S2>> for GenericHashMap<K, V, S1, P>
2157where
2158 K: Hash + Eq + Clone,
2159 V: Clone,
2160 S1: BuildHasher + Default + Clone,
2161 S2: BuildHasher,
2162 P: SharedPointerKind,
2163{
2164 fn from(m: &'a collections::HashMap<K, V, S2>) -> Self {
2165 m.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
2166 }
2167}
2168
2169impl<K, V, S, P> From<collections::BTreeMap<K, V>> for GenericHashMap<K, V, S, P>
2170where
2171 K: Hash + Eq + Clone,
2172 V: Clone,
2173 S: BuildHasher + Default + Clone,
2174 P: SharedPointerKind,
2175{
2176 fn from(m: collections::BTreeMap<K, V>) -> Self {
2177 m.into_iter().collect()
2178 }
2179}
2180
2181impl<'a, K, V, S, P> From<&'a collections::BTreeMap<K, V>> for GenericHashMap<K, V, S, P>
2182where
2183 K: Hash + Eq + Clone,
2184 V: Clone,
2185 S: BuildHasher + Default + Clone,
2186 P: SharedPointerKind,
2187{
2188 fn from(m: &'a collections::BTreeMap<K, V>) -> Self {
2189 m.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
2190 }
2191}
2192
2193#[cfg(any(test, feature = "proptest"))]
2213#[doc(hidden)]
2214pub mod proptest {
2215 #[deprecated(
2216 since = "14.3.0",
2217 note = "proptest strategies have moved to imbl::proptest"
2218 )]
2219 pub use crate::proptest::hash_map;
2220}
2221
2222#[cfg(test)]
2225mod test {
2226 use super::*;
2227 use crate::test::LolHasher;
2228 #[rustfmt::skip]
2229 use ::proptest::{collection, num::{i16, usize}, proptest};
2230 use static_assertions::{assert_impl_all, assert_not_impl_any};
2231 use std::hash::BuildHasherDefault;
2232
2233 assert_impl_all!(HashMap<i32, i32>: Send, Sync);
2234 assert_not_impl_any!(HashMap<i32, *const i32>: Send, Sync);
2235 assert_not_impl_any!(HashMap<*const i32, i32>: Send, Sync);
2236 assert_covariant!(HashMap<T, i32> in T);
2237 assert_covariant!(HashMap<i32, T> in T);
2238
2239 #[test]
2240 fn safe_mutation() {
2241 let v1: HashMap<usize, usize> = GenericHashMap::from_iter((0..131_072).map(|i| (i, i)));
2242 let mut v2 = v1.clone();
2243 v2.insert(131_000, 23);
2244 assert_eq!(Some(&23), v2.get(&131_000));
2245 assert_eq!(Some(&131_000), v1.get(&131_000));
2246 }
2247
2248 #[test]
2249 fn index_operator() {
2250 let mut map: HashMap<usize, usize> = hashmap![1 => 2, 3 => 4, 5 => 6];
2251 assert_eq!(4, map[&3]);
2252 map[&3] = 8;
2253 let target_map: HashMap<usize, usize> = hashmap![1 => 2, 3 => 8, 5 => 6];
2254 assert_eq!(target_map, map);
2255 }
2256
2257 #[test]
2258 fn proper_formatting() {
2259 let map: HashMap<usize, usize> = hashmap![1 => 2];
2260 assert_eq!("{1: 2}", format!("{:?}", map));
2261
2262 assert_eq!("{}", format!("{:?}", HashMap::<(), ()>::new()));
2263 }
2264
2265 #[test]
2266 fn remove_failing() {
2267 let pairs = [(1469, 0), (-67, 0)];
2268 let mut m: collections::HashMap<i16, i16, _> =
2269 collections::HashMap::with_hasher(BuildHasherDefault::<LolHasher>::default());
2270 for (k, v) in &pairs {
2271 m.insert(*k, *v);
2272 }
2273 let mut map: GenericHashMap<i16, i16, _, DefaultSharedPtr> =
2274 GenericHashMap::with_hasher(BuildHasherDefault::<LolHasher>::default());
2275 for (k, v) in &m {
2276 map = map.update(*k, *v);
2277 }
2278 for k in m.keys() {
2279 let l = map.len();
2280 assert_eq!(m.get(k).cloned(), map.get(k).cloned());
2281 map = map.without(k);
2282 assert_eq!(None, map.get(k));
2283 assert_eq!(l - 1, map.len());
2284 }
2285 }
2286
2287 #[test]
2288 fn match_string_keys_with_string_slices() {
2289 let tmp_map: HashMap<&str, &i32> = hashmap! { "foo" => &1, "bar" => &2, "baz" => &3 };
2290 let mut map: HashMap<String, i32> = From::from(&tmp_map);
2291 assert_eq!(Some(&1), map.get("foo"));
2292 map = map.without("foo");
2293 assert_eq!(Some(3), map.remove("baz"));
2294 map["bar"] = 8;
2295 assert_eq!(8, map["bar"]);
2296 }
2297
2298 #[test]
2299 fn macro_allows_trailing_comma() {
2300 let map1: HashMap<&str, i32> = hashmap! {"x" => 1, "y" => 2};
2301 let map2: HashMap<&str, i32> = hashmap! {
2302 "x" => 1,
2303 "y" => 2,
2304 };
2305 assert_eq!(map1, map2);
2306 }
2307
2308 #[test]
2309 fn remove_top_level_collisions() {
2310 let pairs = vec![9, 2569, 27145];
2311 let mut map: GenericHashMap<i16, i16, BuildHasherDefault<LolHasher>, DefaultSharedPtr> =
2312 Default::default();
2313 for k in pairs.clone() {
2314 map.insert(k, k);
2315 }
2316 assert_eq!(pairs.len(), map.len());
2317 let keys: Vec<_> = map.keys().cloned().collect();
2318 for k in keys {
2319 let l = map.len();
2320 assert_eq!(Some(&k), map.get(&k));
2321 map.remove(&k);
2322 assert_eq!(None, map.get(&k));
2323 assert_eq!(l - 1, map.len());
2324 }
2325 }
2326
2327 #[test]
2328 fn entry_api() {
2329 let mut map: HashMap<&str, i32> = hashmap! {"bar" => 5};
2330 map.entry("foo").and_modify(|v| *v += 5).or_insert(1);
2331 assert_eq!(1, map[&"foo"]);
2332 map.entry("foo").and_modify(|v| *v += 5).or_insert(1);
2333 assert_eq!(6, map[&"foo"]);
2334 map.entry("bar").and_modify(|v| *v += 5).or_insert(1);
2335 assert_eq!(10, map[&"bar"]);
2336 assert_eq!(
2337 10,
2338 match map.entry("bar") {
2339 Entry::Occupied(entry) => entry.remove(),
2340 _ => panic!(),
2341 }
2342 );
2343 assert!(!map.contains_key(&"bar"));
2344 }
2345
2346 #[test]
2347 fn refpool_crash() {
2348 let _map = HashMap::<u128, usize>::new();
2349 }
2350
2351 #[test]
2352 fn large_map() {
2353 let mut map = HashMap::<_, _>::new();
2354 let size = 32769;
2355 for i in 0..size {
2356 map.insert(i, i);
2357 }
2358 assert_eq!(size, map.len());
2359 for i in 0..size {
2360 assert_eq!(Some(&i), map.get(&i));
2361 }
2362 }
2363
2364 struct PanicOnClone;
2365
2366 impl Clone for PanicOnClone {
2367 fn clone(&self) -> Self {
2368 panic!("PanicOnClone::clone called")
2369 }
2370 }
2371
2372 #[test]
2373 fn into_iter_no_clone() {
2374 let mut map = HashMap::new();
2375 for i in 0..10_000 {
2376 map.insert(i, PanicOnClone);
2377 }
2378 let _ = map.into_iter().collect::<Vec<_>>();
2379 }
2380
2381 #[test]
2382 fn iter_mut_no_clone() {
2383 let mut map = HashMap::new();
2384 for i in 0..10_000 {
2385 map.insert(i, PanicOnClone);
2386 }
2387 let _ = map.iter_mut().collect::<Vec<_>>();
2388 }
2389
2390 #[test]
2391 fn iter_no_clone() {
2392 let mut map = HashMap::new();
2393 for i in 0..10_000 {
2394 map.insert(i, PanicOnClone);
2395 }
2396 let _ = map.iter().collect::<Vec<_>>();
2397 }
2398
2399 proptest! {
2400 #[test]
2401 fn update_and_length(ref m in collection::hash_map(i16::ANY, i16::ANY, 0..1000)) {
2402 let mut map: GenericHashMap<i16, i16, BuildHasherDefault<LolHasher>, DefaultSharedPtr> = Default::default();
2403 for (index, (k, v)) in m.iter().enumerate() {
2404 map = map.update(*k, *v);
2405 assert_eq!(Some(v), map.get(k));
2406 assert_eq!(index + 1, map.len());
2407 }
2408 }
2409
2410 #[test]
2411 fn from_iterator(ref m in collection::hash_map(i16::ANY, i16::ANY, 0..1000)) {
2412 let map: HashMap<i16, i16> =
2413 FromIterator::from_iter(m.iter().map(|(k, v)| (*k, *v)));
2414 assert_eq!(m.len(), map.len());
2415 }
2416
2417 #[test]
2418 fn iterate_over(ref m in collection::hash_map(i16::ANY, i16::ANY, 0..1000)) {
2419 let map: HashMap<i16, i16> = FromIterator::from_iter(m.iter().map(|(k, v)| (*k, *v)));
2420 assert_eq!(m.len(), map.iter().count());
2421 }
2422
2423 #[test]
2424 fn equality(ref m in collection::hash_map(i16::ANY, i16::ANY, 0..1000)) {
2425 let map1: HashMap<i16, i16> = FromIterator::from_iter(m.iter().map(|(k, v)| (*k, *v)));
2426 let map2: HashMap<i16, i16> = FromIterator::from_iter(m.iter().map(|(k, v)| (*k, *v)));
2427 assert_eq!(map1, map2);
2428 }
2429
2430 #[test]
2431 fn lookup(ref m in collection::hash_map(i16::ANY, i16::ANY, 0..1000)) {
2432 let map: HashMap<i16, i16> = FromIterator::from_iter(m.iter().map(|(k, v)| (*k, *v)));
2433 for (k, v) in m {
2434 assert_eq!(Some(*v), map.get(k).cloned(), "{k} not found in map {map:?}");
2435 }
2436 }
2437
2438 #[test]
2439 fn without(ref pairs in collection::vec((i16::ANY, i16::ANY), 0..100)) {
2440 let mut m: collections::HashMap<i16, i16, _> =
2441 collections::HashMap::with_hasher(BuildHasherDefault::<LolHasher>::default());
2442 for (k, v) in pairs {
2443 m.insert(*k, *v);
2444 }
2445 let mut map: GenericHashMap<i16, i16, _, DefaultSharedPtr> = GenericHashMap::with_hasher(BuildHasherDefault::<LolHasher>::default());
2446 for (k, v) in &m {
2447 map = map.update(*k, *v);
2448 }
2449 for k in m.keys() {
2450 let l = map.len();
2451 assert_eq!(m.get(k).cloned(), map.get(k).cloned());
2452 map = map.without(k);
2453 assert_eq!(None, map.get(k));
2454 assert_eq!(l - 1, map.len());
2455 }
2456 }
2457
2458 #[test]
2459 fn insert(ref m in collection::hash_map(i16::ANY, i16::ANY, 0..1000)) {
2460 let mut mut_map: GenericHashMap<i16, i16, BuildHasherDefault<LolHasher>, DefaultSharedPtr> = Default::default();
2461 let mut map: GenericHashMap<i16, i16, BuildHasherDefault<LolHasher>, DefaultSharedPtr> = Default::default();
2462 for (count, (k, v)) in m.iter().enumerate() {
2463 map = map.update(*k, *v);
2464 mut_map.insert(*k, *v);
2465 assert_eq!(count + 1, map.len());
2466 assert_eq!(count + 1, mut_map.len());
2467 }
2468 for (k, v) in m {
2469 assert_eq!(Some(v), map.get(&k));
2470 assert_eq!(Some(v), mut_map.get(&k));
2471 }
2472 assert_eq!(map, mut_map);
2473 }
2474
2475 #[test]
2476 fn remove(ref pairs in collection::vec((i16::ANY, i16::ANY), 0..100)) {
2477 let mut m: collections::HashMap<i16, i16, _> =
2478 collections::HashMap::with_hasher(BuildHasherDefault::<LolHasher>::default());
2479 for (k, v) in pairs {
2480 m.insert(*k, *v);
2481 }
2482 let mut map: GenericHashMap<i16, i16, _, DefaultSharedPtr> = GenericHashMap::with_hasher(BuildHasherDefault::<LolHasher>::default());
2483 for (k, v) in &m {
2484 map.insert(*k, *v);
2485 }
2486 for k in m.keys() {
2487 let l = map.len();
2488 assert_eq!(m.get(k).cloned(), map.get(k).cloned());
2489 map.remove(k);
2490 assert_eq!(None, map.get(k));
2491 assert_eq!(l - 1, map.len());
2492 }
2493 }
2494
2495 #[test]
2496 fn delete_and_reinsert(
2497 ref input in collection::hash_map(i16::ANY, i16::ANY, 1..1000),
2498 index_rand in usize::ANY
2499 ) {
2500 let index = *input.keys().nth(index_rand % input.len()).unwrap();
2501 let map1: HashMap<_, _> = HashMap::from_iter(input.clone());
2502 let (val, map2) = map1.extract(&index).unwrap();
2503 let map3 = map2.update(index, val);
2504 for key in map2.keys() {
2505 assert!(*key != index);
2506 }
2507 assert_eq!(map1.len(), map2.len() + 1);
2508 assert_eq!(map1, map3);
2509 }
2510
2511 #[test]
2512 fn proptest_works(ref m in proptest::hash_map(0..9999, ".*", 10..100)) {
2513 assert!(m.len() < 100);
2514 assert!(m.len() >= 10);
2515 }
2516
2517 #[test]
2518 fn exact_size_iterator(ref m in proptest::hash_map(i16::ANY, i16::ANY, 0..100)) {
2519 let mut should_be = m.len();
2520 let mut it = m.iter();
2521 loop {
2522 assert_eq!(should_be, it.len());
2523 match it.next() {
2524 None => break,
2525 Some(_) => should_be -= 1,
2526 }
2527 }
2528 assert_eq!(0, it.len());
2529 }
2530
2531 #[test]
2532 fn union(ref m1 in collection::hash_map(i16::ANY, i16::ANY, 0..100),
2533 ref m2 in collection::hash_map(i16::ANY, i16::ANY, 0..100)) {
2534 let map1: HashMap<i16, i16> = FromIterator::from_iter(m1.iter().map(|(k, v)| (*k, *v)));
2535 let map2: HashMap<i16, i16> = FromIterator::from_iter(m2.iter().map(|(k, v)| (*k, *v)));
2536 let union_map = map1.union(map2);
2537
2538 for k in m1.keys() {
2539 assert!(union_map.contains_key(k));
2540 }
2541
2542 for k in m2.keys() {
2543 assert!(union_map.contains_key(k));
2544 }
2545
2546 for (k, v) in union_map.iter() {
2547 assert_eq!(v, m1.get(k).or_else(|| m2.get(k)).unwrap());
2548 }
2549 }
2550 }
2551
2552 #[test]
2553 fn test_structure_summary() {
2554 let sizes = vec![10, 100, 1_000, 10_000, 100_000];
2556
2557 for size in sizes {
2558 println!("\n=== Testing with {} entries ===", size);
2559
2560 let mut map = HashMap::new();
2561
2562 for i in 0..size {
2564 map.insert(i, i * 2);
2566 }
2567
2568 map.print_structure_summary();
2570 }
2571 }
2572}