1#![forbid(missing_docs)]
31#![cfg_attr(all(feature = "nightly", test), feature(test))]
32
33#![cfg_attr(feature = "clippy", feature(plugin))]
34#![cfg_attr(feature = "clippy", plugin(clippy))]
35#![cfg_attr(feature = "clippy", deny(clippy))]
36
37#[cfg(feature = "serde_impl")]
39pub mod serde;
40#[cfg(feature = "heapsize_impl")]
42mod heapsize;
43
44use std::borrow::Borrow;
45use std::cmp::Ordering;
46use std::collections::hash_map::{self, HashMap};
47use std::fmt;
48use std::hash::{BuildHasher, Hash, Hasher};
49use std::iter;
50use std::marker;
51use std::mem;
52use std::ops::{Index, IndexMut};
53use std::ptr;
54
55struct KeyRef<K> { k: *const K }
56
57struct Node<K, V> {
58 next: *mut Node<K, V>,
59 prev: *mut Node<K, V>,
60 key: K,
61 value: V,
62}
63
64pub struct LinkedHashMap<K, V, S = hash_map::RandomState> {
66 map: HashMap<KeyRef<K>, *mut Node<K, V>, S>,
67 head: *mut Node<K, V>,
68 free: *mut Node<K, V>,
69}
70
71impl<K: Hash> Hash for KeyRef<K> {
72 fn hash<H: Hasher>(&self, state: &mut H) {
73 unsafe { (*self.k).hash(state) }
74 }
75}
76
77impl<K: PartialEq> PartialEq for KeyRef<K> {
78 fn eq(&self, other: &Self) -> bool {
79 unsafe{ (*self.k).eq(&*other.k) }
80 }
81}
82
83impl<K: Eq> Eq for KeyRef<K> {}
84
85#[derive(Hash, PartialEq, Eq)]
89struct Qey<Q: ?Sized>(Q);
90
91impl<Q: ?Sized> Qey<Q> {
92 fn from_ref(q: &Q) -> &Self { unsafe { mem::transmute(q) } }
93}
94
95impl<K, Q: ?Sized> Borrow<Qey<Q>> for KeyRef<K> where K: Borrow<Q> {
96 fn borrow(&self) -> &Qey<Q> {
97 Qey::from_ref(unsafe { (*self.k).borrow() })
98 }
99}
100
101impl<K, V> Node<K, V> {
102 fn new(k: K, v: V) -> Self {
103 Node {
104 key: k,
105 value: v,
106 next: ptr::null_mut(),
107 prev: ptr::null_mut(),
108 }
109 }
110}
111
112unsafe fn drop_empty_node<K, V>(the_box: *mut Node<K, V>) {
113 let Node { key, value, .. } = *Box::from_raw(the_box);
115 mem::forget(key);
116 mem::forget(value);
117}
118
119impl<K: Hash + Eq, V> LinkedHashMap<K, V> {
120 pub fn new() -> Self { Self::with_map(HashMap::new()) }
122
123 pub fn with_capacity(capacity: usize) -> Self {
125 Self::with_map(HashMap::with_capacity(capacity))
126 }
127}
128
129impl<K, V, S> LinkedHashMap<K, V, S> {
130 #[inline]
131 fn detach(&mut self, node: *mut Node<K, V>) {
132 unsafe {
133 (*(*node).prev).next = (*node).next;
134 (*(*node).next).prev = (*node).prev;
135 }
136 }
137
138 #[inline]
139 fn attach(&mut self, node: *mut Node<K, V>) {
140 unsafe {
141 (*node).next = (*self.head).next;
142 (*node).prev = self.head;
143 (*self.head).next = node;
144 (*(*node).next).prev = node;
145 }
146 }
147
148 unsafe fn drop_entries(&mut self) {
150 let mut cur = (*self.head).next;
151 while cur != self.head {
152 let next = (*cur).next;
153 Box::from_raw(cur);
154 cur = next;
155 }
156 }
157
158 fn clear_free_list(&mut self) {
159 unsafe {
160 let mut free = self.free;
161 while ! free.is_null() {
162 let next_free = (*free).next;
163 drop_empty_node(free);
164 free = next_free;
165 }
166 self.free = ptr::null_mut();
167 }
168 }
169
170 fn ensure_guard_node(&mut self) {
171 if self.head.is_null() {
172 unsafe {
174 let node_layout = std::alloc::Layout::new::<Node<K, V>>();
175 self.head = std::alloc::alloc(node_layout) as *mut Node<K, V>;
176 (*self.head).next = self.head;
177 (*self.head).prev = self.head;
178 }
179 }
180 }
181}
182
183impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
184 fn with_map(map: HashMap<KeyRef<K>, *mut Node<K, V>, S>) -> Self {
185 LinkedHashMap {
186 map: map,
187 head: ptr::null_mut(),
188 free: ptr::null_mut(),
189 }
190 }
191
192 pub fn with_hasher(hash_builder: S) -> Self {
194 Self::with_map(HashMap::with_hasher(hash_builder))
195 }
196
197 pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
199 Self::with_map(HashMap::with_capacity_and_hasher(capacity, hash_builder))
200 }
201
202 pub fn reserve(&mut self, additional: usize) { self.map.reserve(additional); }
209
210 pub fn shrink_to_fit(&mut self) {
214 self.map.shrink_to_fit();
215 self.clear_free_list();
216 }
217
218 pub fn entry(&mut self, k: K) -> Entry<K, V, S> {
238 let self_ptr: *mut Self = self;
239
240 if let Some(entry) = self.map.get_mut(&KeyRef{k: &k}) {
241 return Entry::Occupied(OccupiedEntry {
242 entry: *entry,
243 map: self_ptr,
244 marker: marker::PhantomData,
245 });
246 }
247
248 Entry::Vacant(VacantEntry {
249 key: k,
250 map: self,
251 })
252 }
253
254 pub fn entries(&mut self) -> Entries<K, V, S> {
277 let head = if ! self.head.is_null() {
278 unsafe { (*self.head).prev }
279 } else {
280 ptr::null_mut()
281 };
282 Entries {
283 map: self,
284 head: head,
285 remaining: self.len(),
286 marker: marker::PhantomData,
287 }
288 }
289
290 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
305 self.ensure_guard_node();
306
307 let (node, old_val) = match self.map.get(&KeyRef{k: &k}) {
308 Some(node) => {
309 let old_val = unsafe { ptr::replace(&mut (**node).value, v) };
310 (*node, Some(old_val))
311 }
312 None => {
313 let node = if self.free.is_null() {
314 Box::into_raw(Box::new(Node::new(k, v)))
315 } else {
316 unsafe {
318 let free = self.free;
319 self.free = (*free).next;
320 ptr::write(free, Node::new(k, v));
321 free
322 }
323 };
324 (node, None)
325 }
326 };
327 match old_val {
328 Some(_) => {
329 self.detach(node);
331 self.attach(node);
332 }
333 None => {
334 let keyref = unsafe { &(*node).key };
335 self.map.insert(KeyRef{k: keyref}, node);
336 self.attach(node);
337 }
338 }
339 old_val
340 }
341
342 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool where K: Borrow<Q>, Q: Eq + Hash {
344 self.map.contains_key(Qey::from_ref(k))
345 }
346
347 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> where K: Borrow<Q>, Q: Eq + Hash {
364 self.map.get(Qey::from_ref(k)).map(|e| unsafe { &(**e).value })
365 }
366
367 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Eq + Hash {
382 self.map.get(Qey::from_ref(k)).map(|e| unsafe { &mut (**e).value })
383 }
384
385 pub fn get_refresh<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Eq + Hash {
405 let (value, node_ptr_opt) = match self.map.get(Qey::from_ref(k)) {
406 None => (None, None),
407 Some(node) => {
408 (Some(unsafe { &mut (**node).value }), Some(*node))
409 }
410 };
411 if let Some(node_ptr) = node_ptr_opt {
412 self.detach(node_ptr);
413 self.attach(node_ptr);
414 }
415 value
416 }
417
418 pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where K: Borrow<Q>, Q: Eq + Hash {
434 let removed = self.map.remove(Qey::from_ref(k));
435 removed.map(|node| {
436 self.detach(node);
437 unsafe {
438 (*node).next = self.free;
440 self.free = node;
441 drop(ptr::read(&(*node).key));
443 ptr::read(&(*node).value)
444 }
445 })
446 }
447
448 pub fn capacity(&self) -> usize {
458 self.map.capacity()
459 }
460
461 #[inline]
477 pub fn pop_front(&mut self) -> Option<(K, V)> {
478 if self.is_empty() {
479 return None
480 }
481 let lru = unsafe { (*self.head).prev };
482 self.detach(lru);
483 self.map
484 .remove(&KeyRef{k: unsafe { &(*lru).key }})
485 .map(|e| {
486 let e = *unsafe { Box::from_raw(e) };
487 (e.key, e.value)
488 })
489 }
490
491 #[inline]
503 pub fn front(&self) -> Option<(&K, &V)> {
504 if self.is_empty() {
505 return None
506 }
507 let lru = unsafe { (*self.head).prev };
508 self.map
509 .get(&KeyRef{k: unsafe { &(*lru).key }})
510 .map(|e| unsafe { (&(**e).key, &(**e).value) })
511 }
512
513 #[inline]
527 pub fn pop_back(&mut self) -> Option<(K, V)> {
528 if self.is_empty() {
529 return None
530 }
531 let mru = unsafe { (*self.head).next };
532 self.detach(mru);
533 self.map
534 .remove(&KeyRef{k: unsafe { &(*mru).key }})
535 .map(|e| {
536 let e = *unsafe { Box::from_raw(e) };
537 (e.key, e.value)
538 })
539 }
540
541 #[inline]
553 pub fn back(&mut self) -> Option<(&K, &V)> {
554 if self.is_empty() {
555 return None
556 }
557 let mru = unsafe { (*self.head).next };
558 self.map
559 .get(&KeyRef{k: unsafe { &(*mru).key }})
560 .map(|e| unsafe { (&(**e).key, &(**e).value) })
561 }
562
563 pub fn len(&self) -> usize { self.map.len() }
565
566 pub fn is_empty(&self) -> bool { self.len() == 0 }
568
569 pub fn hasher(&self) -> &S {
571 self.map.hasher()
572 }
573
574 pub fn clear(&mut self) {
576 self.map.clear();
577 if ! self.head.is_null() {
579 unsafe {
580 self.drop_entries();
581 (*self.head).prev = self.head;
582 (*self.head).next = self.head;
583 }
584 }
585 }
586
587 pub fn iter(&self) -> Iter<K, V> {
606 let head = if self.head.is_null() {
607 ptr::null_mut()
608 } else {
609 unsafe { (*self.head).prev }
610 };
611 Iter {
612 head: head,
613 tail: self.head,
614 remaining: self.len(),
615 marker: marker::PhantomData,
616 }
617 }
618
619 pub fn iter_mut(&mut self) -> IterMut<K, V> {
640 let head = if self.head.is_null() {
641 ptr::null_mut()
642 } else {
643 unsafe { (*self.head).prev }
644 };
645 IterMut {
646 head: head,
647 tail: self.head,
648 remaining: self.len(),
649 marker: marker::PhantomData,
650 }
651 }
652
653 pub fn keys(&self) -> Keys<K, V> {
671 Keys { inner: self.iter() }
672 }
673
674 pub fn values(&self) -> Values<K, V> {
692 Values { inner: self.iter() }
693 }
694}
695
696impl<'a, K, V, S, Q: ?Sized> Index<&'a Q> for LinkedHashMap<K, V, S>
697 where K: Hash + Eq + Borrow<Q>, S: BuildHasher, Q: Eq + Hash
698{
699 type Output = V;
700
701 fn index(&self, index: &'a Q) -> &V {
702 self.get(index).expect("no entry found for key")
703 }
704}
705
706impl<'a, K, V, S, Q: ?Sized> IndexMut<&'a Q> for LinkedHashMap<K, V, S>
707 where K: Hash + Eq + Borrow<Q>, S: BuildHasher, Q: Eq + Hash
708{
709 fn index_mut(&mut self, index: &'a Q) -> &mut V {
710 self.get_mut(index).expect("no entry found for key")
711 }
712}
713
714impl<K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone> Clone for LinkedHashMap<K, V, S> {
715 fn clone(&self) -> Self {
716 let mut map = Self::with_hasher(self.map.hasher().clone());
717 map.extend(self.iter().map(|(k, v)| (k.clone(), v.clone())));
718 map
719 }
720}
721
722impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for LinkedHashMap<K, V, S> {
723 fn default() -> Self { Self::with_hasher(S::default()) }
724}
725
726impl<K: Hash + Eq, V, S: BuildHasher> Extend<(K, V)> for LinkedHashMap<K, V, S> {
727 fn extend<I: IntoIterator<Item=(K, V)>>(&mut self, iter: I) {
728 for (k, v) in iter {
729 self.insert(k, v);
730 }
731 }
732}
733
734impl<'a, K, V, S> Extend<(&'a K, &'a V)> for LinkedHashMap<K, V, S>
735 where K: 'a + Hash + Eq + Copy, V: 'a + Copy, S: BuildHasher,
736{
737 fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
738 for (&k, &v) in iter {
739 self.insert(k, v);
740 }
741 }
742}
743
744impl<K: Hash + Eq, V, S: BuildHasher + Default> iter::FromIterator<(K, V)> for LinkedHashMap<K, V, S> {
745 fn from_iter<I: IntoIterator<Item=(K, V)>>(iter: I) -> Self {
746 let iter = iter.into_iter();
747 let mut map = Self::with_capacity_and_hasher(iter.size_hint().0, S::default());
748 map.extend(iter);
749 map
750 }
751}
752
753impl<A: fmt::Debug + Hash + Eq, B: fmt::Debug, S: BuildHasher> fmt::Debug for LinkedHashMap<A, B, S> {
754 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
756 f.debug_map().entries(self).finish()
757 }
758}
759
760impl<K: Hash + Eq, V: PartialEq, S: BuildHasher> PartialEq for LinkedHashMap<K, V, S> {
761 fn eq(&self, other: &Self) -> bool {
762 self.len() == other.len() && self.iter().eq(other)
763 }
764}
765
766impl<K: Hash + Eq, V: Eq, S: BuildHasher> Eq for LinkedHashMap<K, V, S> {}
767
768impl<K: Hash + Eq + PartialOrd, V: PartialOrd, S: BuildHasher> PartialOrd for LinkedHashMap<K, V, S> {
769 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
770 self.iter().partial_cmp(other)
771 }
772
773 fn lt(&self, other: &Self) -> bool {
774 self.iter().lt(other)
775 }
776
777 fn le(&self, other: &Self) -> bool {
778 self.iter().le(other)
779 }
780
781 fn ge(&self, other: &Self) -> bool {
782 self.iter().ge(other)
783 }
784
785 fn gt(&self, other: &Self) -> bool {
786 self.iter().gt(other)
787 }
788}
789
790impl<K: Hash + Eq + Ord, V: Ord, S: BuildHasher> Ord for LinkedHashMap<K, V, S> {
791 fn cmp(&self, other: &Self) -> Ordering {
792 self.iter().cmp(other)
793 }
794}
795
796impl<K: Hash + Eq, V: Hash, S: BuildHasher> Hash for LinkedHashMap<K, V, S> {
797 fn hash<H: Hasher>(&self, h: &mut H) { for e in self.iter() { e.hash(h); } }
798}
799
800unsafe impl<K: Send, V: Send, S: Send> Send for LinkedHashMap<K, V, S> {}
801
802unsafe impl<K: Sync, V: Sync, S: Sync> Sync for LinkedHashMap<K, V, S> {}
803
804impl<K, V, S> Drop for LinkedHashMap<K, V, S> {
805 fn drop(&mut self) {
806 if !self.head.is_null() {
807 unsafe {
808 self.drop_entries();
809 drop_empty_node(self.head);
810 }
811 }
812 self.clear_free_list();
813 }
814}
815
816pub struct Iter<'a, K: 'a, V: 'a> {
819 head: *const Node<K, V>,
820 tail: *const Node<K, V>,
821 remaining: usize,
822 marker: marker::PhantomData<(&'a K, &'a V)>,
823}
824
825pub struct IterMut<'a, K: 'a, V: 'a> {
828 head: *mut Node<K, V>,
829 tail: *mut Node<K, V>,
830 remaining: usize,
831 marker: marker::PhantomData<(&'a K, &'a mut V)>,
832}
833
834pub struct IntoIter<K, V> {
836 head: *mut Node<K, V>,
837 tail: *mut Node<K, V>,
838 remaining: usize,
839 marker: marker::PhantomData<(K, V)>,
840}
841
842pub struct Entries<'a, K: 'a, V: 'a, S: 'a = hash_map::RandomState> {
845 map: *mut LinkedHashMap<K, V, S>,
846 head: *mut Node<K, V>,
847 remaining: usize,
848 marker: marker::PhantomData<(&'a K, &'a mut V, &'a S)>,
849}
850
851unsafe impl<'a, K, V> Send for Iter<'a, K, V> where K: Send, V: Send {}
852
853unsafe impl<'a, K, V> Send for IterMut<'a, K, V> where K: Send, V: Send {}
854
855unsafe impl<K, V> Send for IntoIter<K, V> where K: Send, V: Send {}
856
857unsafe impl<'a, K, V, S> Send for Entries<'a, K, V, S> where K: Send, V: Send, S: Send {}
858
859unsafe impl<'a, K, V> Sync for Iter<'a, K, V> where K: Sync, V: Sync {}
860
861unsafe impl<'a, K, V> Sync for IterMut<'a, K, V> where K: Sync, V: Sync {}
862
863unsafe impl<K, V> Sync for IntoIter<K, V> where K: Sync, V: Sync {}
864
865unsafe impl<'a, K, V, S> Sync for Entries<'a, K, V, S> where K: Sync, V: Sync, S: Sync {}
866
867impl<'a, K, V> Clone for Iter<'a, K, V> {
868 fn clone(&self) -> Self { Iter { ..*self } }
869}
870
871impl<K, V> Clone for IntoIter<K, V> where K: Clone, V: Clone {
872 fn clone(&self) -> Self {
873 if self.remaining == 0 {
874 return IntoIter { ..*self }
875 }
876
877 fn clone_node<K, V>(e: *mut Node<K, V>) -> *mut Node<K, V>
878 where K: Clone, V: Clone,
879 {
880 Box::into_raw(Box::new(Node::new(
881 unsafe { (*e).key.clone() }, unsafe { (*e).value.clone() }
882 )))
883 }
884
885 let mut cur = self.head;
886 let head = clone_node(cur);
887 let mut tail = head;
888 for _ in 1..self.remaining {
889 unsafe {
890 (*tail).prev = clone_node((*cur).prev);
891 (*(*tail).prev).next = tail;
892 tail = (*tail).prev;
893 cur = (*cur).prev;
894 }
895 }
896
897 IntoIter {
898 head: head,
899 tail: tail,
900 remaining: self.remaining,
901 marker: marker::PhantomData,
902 }
903 }
904}
905
906impl<'a, K, V> Iterator for Iter<'a, K, V> {
907 type Item = (&'a K, &'a V);
908
909 fn next(&mut self) -> Option<(&'a K, &'a V)> {
910 if self.head == self.tail {
911 None
912 } else {
913 self.remaining -= 1;
914 unsafe {
915 let r = Some((&(*self.head).key, &(*self.head).value));
916 self.head = (*self.head).prev;
917 r
918 }
919 }
920 }
921
922 fn size_hint(&self) -> (usize, Option<usize>) {
923 (self.remaining, Some(self.remaining))
924 }
925}
926
927impl<'a, K, V> Iterator for IterMut<'a, K, V> {
928 type Item = (&'a K, &'a mut V);
929
930 fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
931 if self.head == self.tail {
932 None
933 } else {
934 self.remaining -= 1;
935 unsafe {
936 let r = Some((&(*self.head).key, &mut (*self.head).value));
937 self.head = (*self.head).prev;
938 r
939 }
940 }
941 }
942
943 fn size_hint(&self) -> (usize, Option<usize>) {
944 (self.remaining, Some(self.remaining))
945 }
946}
947
948impl<K, V> Iterator for IntoIter<K, V> {
949 type Item = (K, V);
950
951 fn next(&mut self) -> Option<(K, V)> {
952 if self.remaining == 0 {
953 return None
954 }
955 self.remaining -= 1;
956 unsafe {
957 let prev = (*self.head).prev;
958 let e = *Box::from_raw(self.head);
959 self.head = prev;
960 Some((e.key, e.value))
961 }
962 }
963
964 fn size_hint(&self) -> (usize, Option<usize>) {
965 (self.remaining, Some(self.remaining))
966 }
967}
968
969impl<'a, K, V, S: BuildHasher> Iterator for Entries<'a, K, V, S> {
970 type Item = OccupiedEntry<'a, K, V, S>;
971
972 fn next(&mut self) -> Option<OccupiedEntry<'a, K, V, S>> {
973 if self.remaining == 0 {
974 None
975 } else {
976 self.remaining -= 1;
977 unsafe {
978 let r = Some(OccupiedEntry {
979 map: self.map,
980 entry: self.head,
981 marker: marker::PhantomData,
982 });
983
984 self.head = (*self.head).prev;
985 r
986 }
987 }
988 }
989
990 fn size_hint(&self) -> (usize, Option<usize>) {
991 (self.remaining, Some(self.remaining))
992 }
993}
994
995impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
996 fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
997 if self.head == self.tail {
998 None
999 } else {
1000 self.remaining -= 1;
1001 unsafe {
1002 self.tail = (*self.tail).next;
1003 Some((&(*self.tail).key, &(*self.tail).value))
1004 }
1005 }
1006 }
1007}
1008
1009impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
1010 fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> {
1011 if self.head == self.tail {
1012 None
1013 } else {
1014 self.remaining -= 1;
1015 unsafe {
1016 self.tail = (*self.tail).next;
1017 Some((&(*self.tail).key, &mut (*self.tail).value))
1018 }
1019 }
1020 }
1021}
1022
1023impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
1024 fn next_back(&mut self) -> Option<(K, V)> {
1025 if self.remaining == 0 {
1026 return None
1027 }
1028 self.remaining -= 1;
1029 unsafe {
1030 let next = (*self.tail).next;
1031 let e = *Box::from_raw(self.tail);
1032 self.tail = next;
1033 Some((e.key, e.value))
1034 }
1035 }
1036}
1037
1038impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
1039 fn len(&self) -> usize { self.remaining }
1040}
1041
1042impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
1043 fn len(&self) -> usize { self.remaining }
1044}
1045
1046impl<K, V> ExactSizeIterator for IntoIter<K, V> {
1047 fn len(&self) -> usize { self.remaining }
1048}
1049
1050impl<K, V> Drop for IntoIter<K, V> {
1051 fn drop(&mut self) {
1052 for _ in 0..self.remaining {
1053 unsafe {
1054 let next = (*self.tail).next;
1055 Box::from_raw(self.tail);
1056 self.tail = next;
1057 }
1058 }
1059 }
1060}
1061
1062pub struct Keys<'a, K: 'a, V: 'a> {
1064 inner: Iter<'a, K, V>,
1065}
1066
1067impl<'a, K, V> Clone for Keys<'a, K, V> {
1068 fn clone(&self) -> Self { Keys { inner: self.inner.clone() } }
1069}
1070
1071impl<'a, K, V> Iterator for Keys<'a, K, V> {
1072 type Item = &'a K;
1073
1074 #[inline] fn next(&mut self) -> Option<&'a K> { self.inner.next().map(|e| e.0) }
1075 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1076}
1077
1078impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
1079 #[inline] fn next_back(&mut self) -> Option<&'a K> { self.inner.next_back().map(|e| e.0) }
1080}
1081
1082impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
1083 fn len(&self) -> usize { self.inner.len() }
1084}
1085
1086pub struct Values<'a, K: 'a, V: 'a> {
1088 inner: Iter<'a, K, V>,
1089}
1090
1091impl<'a, K, V> Clone for Values<'a, K, V> {
1092 fn clone(&self) -> Self { Values { inner: self.inner.clone() } }
1093}
1094
1095impl<'a, K, V> Iterator for Values<'a, K, V> {
1096 type Item = &'a V;
1097
1098 #[inline] fn next(&mut self) -> Option<&'a V> { self.inner.next().map(|e| e.1) }
1099 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1100}
1101
1102impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
1103 #[inline] fn next_back(&mut self) -> Option<&'a V> { self.inner.next_back().map(|e| e.1) }
1104}
1105
1106impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
1107 fn len(&self) -> usize { self.inner.len() }
1108}
1109
1110impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a LinkedHashMap<K, V, S> {
1111 type Item = (&'a K, &'a V);
1112 type IntoIter = Iter<'a, K, V>;
1113 fn into_iter(self) -> Iter<'a, K, V> { self.iter() }
1114}
1115
1116impl<'a, K: Hash + Eq, V, S: BuildHasher> IntoIterator for &'a mut LinkedHashMap<K, V, S> {
1117 type Item = (&'a K, &'a mut V);
1118 type IntoIter = IterMut<'a, K, V>;
1119 fn into_iter(self) -> IterMut<'a, K, V> { self.iter_mut() }
1120}
1121
1122impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LinkedHashMap<K, V, S> {
1123 type Item = (K, V);
1124 type IntoIter = IntoIter<K, V>;
1125 fn into_iter(mut self) -> IntoIter<K, V> {
1126 let (head, tail) = if !self.head.is_null() {
1127 unsafe { ((*self.head).prev, (*self.head).next) }
1128 } else {
1129 (ptr::null_mut(), ptr::null_mut())
1130 };
1131 let len = self.len();
1132
1133 if !self.head.is_null() {
1134 unsafe { drop_empty_node(self.head) }
1135 }
1136 self.clear_free_list();
1137 unsafe { ptr::drop_in_place(&mut self.map); }
1139 mem::forget(self);
1140
1141 IntoIter {
1142 head: head,
1143 tail: tail,
1144 remaining: len,
1145 marker: marker::PhantomData,
1146 }
1147 }
1148}
1149
1150pub enum Entry<'a, K: 'a, V: 'a, S: 'a = hash_map::RandomState> {
1152 Occupied(OccupiedEntry<'a, K, V, S>),
1154 Vacant(VacantEntry<'a, K, V, S>),
1156}
1157
1158pub struct OccupiedEntry<'a, K: 'a, V: 'a, S: 'a = hash_map::RandomState> {
1160 entry: *mut Node<K, V>,
1161 map: *mut LinkedHashMap<K, V, S>,
1162 marker: marker::PhantomData<&'a K>,
1163}
1164
1165pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a = hash_map::RandomState> {
1167 key: K,
1168 map: &'a mut LinkedHashMap<K, V, S>,
1169}
1170
1171impl<'a, K: Hash + Eq, V, S: BuildHasher> Entry<'a, K, V, S> {
1172 pub fn key(&self) -> &K {
1184 match *self {
1185 Entry::Occupied(ref e) => e.key(),
1186 Entry::Vacant(ref e) => e.key(),
1187 }
1188 }
1189
1190 pub fn or_insert(self, default: V) -> &'a mut V {
1193 match self {
1194 Entry::Occupied(entry) => entry.into_mut(),
1195 Entry::Vacant(entry) => entry.insert(default),
1196 }
1197 }
1198
1199 pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1202 match self {
1203 Entry::Occupied(entry) => entry.into_mut(),
1204 Entry::Vacant(entry) => entry.insert(default()),
1205 }
1206 }
1207}
1208
1209impl<'a, K: Hash + Eq, V, S: BuildHasher> OccupiedEntry<'a, K, V, S> {
1210 pub fn key(&self) -> &K {
1223 unsafe { &(*self.entry).key }
1224 }
1225
1226 pub fn get(&self) -> &V {
1228 unsafe { &(*self.entry).value }
1229 }
1230
1231 pub fn get_mut(&mut self) -> &mut V {
1233 unsafe { &mut (*self.entry).value }
1234 }
1235
1236 pub fn into_mut(self) -> &'a mut V {
1239 unsafe { &mut (*self.entry).value }
1240 }
1241
1242 pub fn insert(&mut self, value: V) -> V {
1244 unsafe {
1245 (*self.map).ensure_guard_node();
1246
1247 let old_val = mem::replace(&mut (*self.entry).value, value);
1248 let node_ptr: *mut Node<K, V> = self.entry;
1249
1250 (*self.map).detach(node_ptr);
1252 (*self.map).attach(node_ptr);
1253
1254 old_val
1255 }
1256 }
1257
1258 pub fn remove(self) -> V {
1260 unsafe { (*self.map).remove(&(*self.entry).key) }.unwrap()
1261 }
1262}
1263
1264impl<'a, K: 'a + Hash + Eq, V: 'a, S: BuildHasher> VacantEntry<'a, K, V, S> {
1265 pub fn key(&self) -> &K {
1277 &self.key
1278 }
1279
1280 pub fn insert(self, value: V) -> &'a mut V {
1283 self.map.ensure_guard_node();
1284
1285 let node = if self.map.free.is_null() {
1286 Box::into_raw(Box::new(Node::new(self.key, value)))
1287 } else {
1288 unsafe {
1290 let free = self.map.free;
1291 self.map.free = (*free).next;
1292 ptr::write(free, Node::new(self.key, value));
1293 free
1294 }
1295 };
1296
1297 let keyref = unsafe { &(*node).key };
1298
1299 self.map.attach(node);
1300
1301 let ret = self.map.map.entry(KeyRef{k: keyref}).or_insert(node);
1302 unsafe { &mut (**ret).value }
1303 }
1304}
1305
1306#[cfg(all(feature = "nightly", test))]
1307mod bench {
1308 extern crate test;
1309
1310 use super::LinkedHashMap;
1311
1312 #[bench]
1313 fn not_recycled_cycling(b: &mut test::Bencher) {
1314 let mut hash_map = LinkedHashMap::with_capacity(1000);
1315 for i in 0usize..1000 {
1316 hash_map.insert(i, i);
1317 }
1318 b.iter(|| {
1319 for i in 0usize..1000 {
1320 hash_map.remove(&i);
1321 }
1322 hash_map.clear_free_list();
1323 for i in 0usize..1000 {
1324 hash_map.insert(i, i);
1325 }
1326 })
1327 }
1328
1329 #[bench]
1330 fn recycled_cycling(b: &mut test::Bencher) {
1331 let mut hash_map = LinkedHashMap::with_capacity(1000);
1332 for i in 0usize..1000 {
1333 hash_map.insert(i, i);
1334 }
1335 b.iter(|| {
1336 for i in 0usize..1000 {
1337 hash_map.remove(&i);
1338 }
1339 for i in 0usize..1000 {
1340 hash_map.insert(i, i);
1341 }
1342 })
1343 }
1344}