1#![doc(html_root_url = "https://docs.rs/min-max-heap/1.3.0")]
2#![warn(missing_docs)]
29
30#[cfg(feature = "serde")]
31use serde::{Serialize, Deserialize};
32
33use std::iter::FromIterator;
34use std::{fmt, mem, slice, vec};
35use std::ops::{Deref, DerefMut};
36
37mod hole;
38mod index;
39
40use self::hole::*;
41
42#[derive(Clone, Debug)]
46#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
47pub struct MinMaxHeap<T>(Vec<T>);
48
49impl<T> Default for MinMaxHeap<T> {
50 fn default() -> Self {
51 MinMaxHeap::new()
52 }
53}
54
55impl<T> MinMaxHeap<T> {
56 pub fn new() -> Self {
60 MinMaxHeap(Vec::new())
61 }
62
63 pub fn with_capacity(len: usize) -> Self {
68 MinMaxHeap(Vec::with_capacity(len))
69 }
70
71 pub fn len(&self) -> usize {
75 self.0.len()
76 }
77
78 pub fn is_empty(&self) -> bool {
82 self.0.is_empty()
83 }
84}
85
86impl<T: Ord> MinMaxHeap<T> {
87 pub fn push(&mut self, element: T) {
92 let pos = self.len();
93 self.0.push(element);
94 self.bubble_up(pos);
95 }
96
97 pub fn peek_min(&self) -> Option<&T> {
101 if self.is_empty() {
102 None
103 } else {
104 Some(&self.0[0])
105 }
106 }
107
108 pub fn peek_min_mut(&mut self) -> Option<PeekMinMut<T>> {
116 if self.is_empty() {
117 None
118 } else {
119 Some(PeekMinMut {
120 heap: self,
121 removed: false,
122 })
123 }
124 }
125
126 pub fn peek_max(&self) -> Option<&T> {
130 self.find_max().map(|i| &self.0[i])
131 }
132
133 pub fn peek_max_mut(&mut self) -> Option<PeekMaxMut<T>> {
141 self.find_max().map(move |i| PeekMaxMut {
142 heap: self,
143 max_index: i,
144 removed: false,
145 })
146 }
147
148 fn find_max_len(&self, len: usize) -> Option<usize> {
149 match len {
150 0 => None,
151 1 => Some(0),
152 2 => Some(1),
153 _ => if self.0[1] > self.0[2] { Some(1) } else { Some(2) }
154 }
155 }
156
157 fn find_max(&self) -> Option<usize> {
158 self.find_max_len(self.len())
159 }
160
161 pub fn pop_min(&mut self) -> Option<T> {
165 self.0.pop().map(|mut item| {
166 if !self.is_empty() {
167 mem::swap(&mut item, &mut self.0[0]);
168 self.trickle_down_min(0);
169 }
170
171 item
172 })
173 }
174
175 pub fn pop_max(&mut self) -> Option<T> {
179 self.find_max().map(|max| {
180 let mut item = self.0.pop().unwrap();
181
182 if max < self.len() {
183 mem::swap(&mut item, &mut self.0[max]);
184 self.trickle_down_max(max);
185 }
186
187 item
188 })
189 }
190
191 pub fn push_pop_min(&mut self, mut element: T) -> T {
198 if self.is_empty() { return element; }
199
200 if element < self.0[0] { return element; }
201
202 mem::swap(&mut element, &mut self.0[0]);
203 self.trickle_down_min(0);
204 element
205 }
206
207 pub fn push_pop_max(&mut self, mut element: T) -> T {
215 if let Some(i) = self.find_max() {
216 if element > self.0[i] { return element }
217
218 mem::swap(&mut element, &mut self.0[i]);
219
220 if self.0[i] < self.0[0] {
221 self.0.swap(0, i);
222 }
223
224 self.trickle_down_max(i);
225 element
226 } else { element }
227 }
228
229 pub fn replace_min(&mut self, mut element: T) -> Option<T> {
234 if self.is_empty() {
235 self.push(element);
236 return None;
237 }
238
239 mem::swap(&mut element, &mut self.0[0]);
240 self.trickle_down_min(0);
241 Some(element)
242 }
243
244 pub fn replace_max(&mut self, mut element: T) -> Option<T> {
249 if let Some(i) = self.find_max() {
250 mem::swap(&mut element, &mut self.0[i]);
251
252 if self.0[i] < self.0[0] {
253 self.0.swap(0, i);
254 }
255
256 self.trickle_down_max(i);
257 Some(element)
258 } else {
259 self.push(element);
260 None
261 }
262 }
263
264 pub fn into_vec_asc(mut self) -> Vec<T> {
269 let mut end = self.len();
270 while let Some(max) = self.find_max_len(end) {
271 end -= 1;
272 self.0.swap(max, end);
273 self.trickle_down_len(max, end);
274 }
275 self.into_vec()
276 }
277
278 pub fn into_vec_desc(mut self) -> Vec<T> {
283 let mut end = self.len();
284 while end > 1 {
285 end -= 1;
286 self.0.swap(0, end);
287 self.trickle_down_min_len(0, end);
288 }
289 self.into_vec()
290 }
291
292 #[inline]
293 fn trickle_down_min(&mut self, pos: usize) {
294 Hole::new(&mut self.0, pos).trickle_down_min();
295 }
296
297 #[inline]
298 fn trickle_down_max(&mut self, pos: usize) {
299 Hole::new(&mut self.0, pos).trickle_down_max();
300 }
301
302 #[inline]
303 fn trickle_down(&mut self, pos: usize) {
304 Hole::new(&mut self.0, pos).trickle_down();
305 }
306
307 #[inline]
308 fn trickle_down_min_len(&mut self, pos: usize, len: usize) {
309 Hole::new(&mut self.0, pos).trickle_down_min_len(len);
310 }
311
312 #[inline]
313 fn trickle_down_len(&mut self, pos: usize, len: usize) {
314 Hole::new(&mut self.0, pos).trickle_down_len(len);
315 }
316
317 #[inline]
318 fn bubble_up(&mut self, pos: usize) {
319 Hole::new(&mut self.0, pos).bubble_up();
320 }
321
322 fn rebuild(&mut self) {
323 let mut n = self.len() / 2;
324 while n > 0 {
325 n -= 1;
326 self.trickle_down(n);
327 }
328 }
329}
330
331impl<T> MinMaxHeap<T> {
332 pub fn clear(&mut self) {
336 self.0.clear();
337 }
338
339 pub fn capacity(&self) -> usize {
343 self.0.capacity()
344 }
345
346 pub fn reserve_exact(&mut self, additional: usize) {
355 self.0.reserve_exact(additional)
356 }
357
358 pub fn reserve(&mut self, additional: usize) {
367 self.0.reserve(additional)
368 }
369
370 pub fn shrink_to_fit(&mut self) {
374 self.0.shrink_to_fit()
375 }
376
377 pub fn into_vec(self) -> Vec<T> {
382 self.0
383 }
384
385 pub fn iter(&self) -> Iter<T> {
390 Iter(self.0.iter())
391 }
392
393 pub fn drain(&mut self) -> Drain<T> {
398 Drain(self.0.drain(..))
399 }
400
401 pub fn drain_asc(&mut self) -> DrainAsc<T> {
406 DrainAsc(self)
407 }
408
409 pub fn drain_desc(&mut self) -> DrainDesc<T> {
414 DrainDesc(self)
415 }
416}
417
418pub struct Iter<'a, T: 'a>(slice::Iter<'a, T>);
428
429impl<'a, T> Iterator for Iter<'a, T> {
430 type Item = &'a T;
431 fn next(&mut self) -> Option<Self::Item> { self.0.next() }
432
433 fn size_hint(&self) -> (usize, Option<usize>) {
434 self.0.size_hint()
435 }
436}
437
438impl<'a, T> ExactSizeIterator for Iter<'a, T> { }
439
440impl<'a, T> IntoIterator for &'a MinMaxHeap<T> {
441 type Item = &'a T;
442 type IntoIter = Iter<'a, T>;
443 fn into_iter(self) -> Self::IntoIter { self.iter() }
444}
445
446pub struct IntoIter<T>(vec::IntoIter<T>);
449
450impl<T> Iterator for IntoIter<T> {
451 type Item = T;
452 fn next(&mut self) -> Option<Self::Item> { self.0.next() }
453
454 fn size_hint(&self) -> (usize, Option<usize>) {
455 self.0.size_hint()
456 }
457}
458
459impl<T> ExactSizeIterator for IntoIter<T> { }
460
461impl<'a, T> IntoIterator for MinMaxHeap<T> {
462 type Item = T;
463 type IntoIter = IntoIter<T>;
464 fn into_iter(self) -> Self::IntoIter {
465 IntoIter(self.0.into_iter())
466 }
467}
468
469pub struct Drain<'a, T: 'a>(vec::Drain<'a, T>);
475
476impl<'a, T> Iterator for Drain<'a, T> {
477 type Item = T;
478 fn next(&mut self) -> Option<Self::Item> { self.0.next() }
479
480 fn size_hint(&self) -> (usize, Option<usize>) {
481 self.0.size_hint()
482 }
483}
484
485impl<'a, T> ExactSizeIterator for Drain<'a, T> { }
486
487impl<T: Ord> FromIterator<T> for MinMaxHeap<T> {
488 fn from_iter<I>(iter: I) -> Self
489 where I: IntoIterator<Item = T> {
490 let mut result = MinMaxHeap::new();
491 result.extend(iter);
492 result
493 }
494}
495
496#[derive(Debug)]
506pub struct DrainAsc<'a, T: 'a>(&'a mut MinMaxHeap<T>);
507
508#[derive(Debug)]
518pub struct DrainDesc<'a, T: 'a>(&'a mut MinMaxHeap<T>);
519
520impl<'a, T> Drop for DrainAsc<'a, T> {
521 fn drop(&mut self) {
522 let _ = (self.0).0.drain(..);
523 }
524}
525
526impl<'a, T> Drop for DrainDesc<'a, T> {
527 fn drop(&mut self) {
528 let _ = (self.0).0.drain(..);
529 }
530}
531
532impl<'a, T: Ord> Iterator for DrainAsc<'a, T> {
533 type Item = T;
534
535 fn next(&mut self) -> Option<T> {
536 self.0.pop_min()
537 }
538
539 fn size_hint(&self) -> (usize, Option<usize>) {
540 (self.len(), Some(self.len()))
541 }
542}
543
544impl<'a, T: Ord> Iterator for DrainDesc<'a, T> {
545 type Item = T;
546
547 fn next(&mut self) -> Option<T> {
548 self.0.pop_max()
549 }
550
551 fn size_hint(&self) -> (usize, Option<usize>) {
552 (self.len(), Some(self.len()))
553 }
554}
555
556impl<'a, T: Ord> DoubleEndedIterator for DrainAsc<'a, T> {
557 fn next_back(&mut self) -> Option<T> {
558 self.0.pop_max()
559 }
560}
561
562impl<'a, T: Ord> DoubleEndedIterator for DrainDesc<'a, T> {
563 fn next_back(&mut self) -> Option<T> {
564 self.0.pop_min()
565 }
566}
567
568impl<'a, T: Ord> ExactSizeIterator for DrainAsc<'a, T> {
569 fn len(&self) -> usize {
570 self.0.len()
571 }
572}
573
574impl<'a, T: Ord> ExactSizeIterator for DrainDesc<'a, T> {
575 fn len(&self) -> usize {
576 self.0.len()
577 }
578}
579
580impl<T: Ord> From<Vec<T>> for MinMaxHeap<T> {
585 fn from(vec: Vec<T>) -> Self {
586 let mut heap = MinMaxHeap(vec);
587 heap.rebuild();
588 heap
589 }
590}
591
592impl<T: Ord> Extend<T> for MinMaxHeap<T> {
597 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
598 for elem in iter {
599 self.push(elem)
600 }
601 }
602}
603
604impl<'a, T: Ord + Clone + 'a> Extend<&'a T> for MinMaxHeap<T> {
605 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
606 for elem in iter {
607 self.push(elem.clone())
608 }
609 }
610}
611
612pub struct PeekMinMut<'a, T: 'a + Ord> {
621 heap: &'a mut MinMaxHeap<T>,
622 removed: bool,
623}
624
625impl<T: Ord + fmt::Debug> fmt::Debug for PeekMinMut<'_, T> {
626 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
627 f.debug_tuple("PeekMinMut")
628 .field(&self.heap.0[0])
629 .finish()
630 }
631}
632
633impl<'a, T: Ord> Drop for PeekMinMut<'a, T> {
634 fn drop(&mut self) {
635 if !self.removed {
636 self.heap.trickle_down_min(0);
637 }
638 }
639}
640
641impl<'a, T: Ord> Deref for PeekMinMut<'a, T> {
642 type Target = T;
643 fn deref(&self) -> &T {
644 debug_assert!(!self.heap.is_empty());
645 unsafe { self.heap.0.get_unchecked(0) }
647 }
648}
649
650impl<'a, T: Ord> DerefMut for PeekMinMut<'a, T> {
651 fn deref_mut(&mut self) -> &mut T {
652 debug_assert!(!self.heap.is_empty());
653 unsafe { self.heap.0.get_unchecked_mut(0) }
655 }
656}
657
658impl<'a, T: Ord> PeekMinMut<'a, T> {
659 pub fn pop(mut self) -> T {
661 let value = self.heap.pop_min().unwrap();
662 self.removed = true;
663 value
664 }
665}
666
667pub struct PeekMaxMut<'a, T: 'a + Ord> {
676 heap: &'a mut MinMaxHeap<T>,
677 max_index: usize,
678 removed: bool,
679}
680
681impl<T: Ord + fmt::Debug> fmt::Debug for PeekMaxMut<'_, T> {
682 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
683 f.debug_tuple("PeekMaxMut")
684 .field(&self.heap.0[self.max_index])
685 .finish()
686 }
687}
688
689impl<'a, T: Ord> Drop for PeekMaxMut<'a, T> {
690 fn drop(&mut self) {
691 if !self.removed {
692 let mut hole = Hole::new(&mut self.heap.0, self.max_index);
693
694 if hole.element() < hole.get_parent() {
695 hole.swap_with_parent();
696 }
697
698 hole.trickle_down_max();
699 }
700 }
701}
702
703impl<'a, T: Ord> Deref for PeekMaxMut<'a, T> {
704 type Target = T;
705 fn deref(&self) -> &T {
706 debug_assert!(self.max_index < self.heap.len());
707 unsafe { self.heap.0.get_unchecked(self.max_index) }
709 }
710}
711
712impl<'a, T: Ord> DerefMut for PeekMaxMut<'a, T> {
713 fn deref_mut(&mut self) -> &mut T {
714 debug_assert!(self.max_index < self.heap.len());
715 unsafe { self.heap.0.get_unchecked_mut(self.max_index) }
717 }
718}
719
720impl<'a, T: Ord> PeekMaxMut<'a, T> {
721 pub fn pop(mut self) -> T {
723 let value = self.heap.pop_max().unwrap();
724 self.removed = true;
725 value
726 }
727}
728
729#[cfg(test)]
730mod tests {
731 extern crate rand;
732
733 use super::*;
734 use self::rand::seq::SliceRandom;
735
736 #[test]
737 fn example() {
738 let mut h = MinMaxHeap::new();
739 assert!(h.is_empty());
740
741 h.push(5);
742 assert!(!h.is_empty());
743 assert_eq!(Some(&5), h.peek_min());
744 assert_eq!(Some(&5), h.peek_max());
745
746 h.push(7);
747 assert_eq!(Some(&5), h.peek_min());
748 assert_eq!(Some(&7), h.peek_max());
749
750 h.push(6);
751 assert_eq!(Some(&5), h.peek_min());
752 assert_eq!(Some(&7), h.peek_max());
753
754 assert_eq!(Some(5), h.pop_min());
755 assert_eq!(Some(7), h.pop_max());
756 assert_eq!(Some(6), h.pop_max());
757 assert_eq!(None, h.pop_min());
758 }
759
760 #[test]
761 fn drain_asc() {
762 let mut h = MinMaxHeap::from(vec![3, 2, 4, 1]);
763 let mut i = h.drain_asc();
764 assert_eq!( i.next(), Some(1) );
765 assert_eq!( i.next(), Some(2) );
766 assert_eq!( i.next(), Some(3) );
767 assert_eq!( i.next(), Some(4) );
768 assert_eq!( i.next(), None );
769 }
770
771 #[test]
773 fn random_vectors() {
774 for i in 0 .. 300 {
775 check_heap(&random_heap(i));
776 }
777 }
778
779 #[test]
780 fn from_vector() {
781 for i in 0 .. 300 {
782 check_heap(&MinMaxHeap::from(random_vec(i)))
783 }
784 }
785
786 fn check_heap(heap: &MinMaxHeap<usize>) {
787 let asc = iota_asc(heap.len());
788 let desc = iota_desc(heap.len());
789
790 assert_eq!(asc, into_vec_asc(heap.clone()));
791 assert_eq!(desc, into_vec_desc(heap.clone()));
792 assert_eq!(asc, heap.clone().into_vec_asc());
793 assert_eq!(desc, heap.clone().into_vec_desc());
794 }
795
796 fn random_vec(len: usize) -> Vec<usize> {
797 let mut result = (0 .. len).collect::<Vec<_>>();
798 result.shuffle(&mut rand::thread_rng());
799 result
800 }
801
802 fn random_heap(len: usize) -> MinMaxHeap<usize> {
803 MinMaxHeap::from_iter(random_vec(len))
804 }
805
806 fn into_vec_asc(mut heap: MinMaxHeap<usize>) -> Vec<usize> {
807 let mut result = Vec::with_capacity(heap.len());
808 while let Some(elem) = heap.pop_min() {
809 result.push(elem)
810 }
811 result
812 }
813
814 fn into_vec_desc(mut heap: MinMaxHeap<usize>) -> Vec<usize> {
815 let mut result = Vec::with_capacity(heap.len());
816 while let Some(elem) = heap.pop_max() {
817 result.push(elem)
818 }
819 result
820 }
821
822 fn iota_asc(len: usize) -> Vec<usize> {
823 (0 .. len).collect()
824 }
825
826 fn iota_desc(len: usize) -> Vec<usize> {
827 let mut result = (0 .. len).collect::<Vec<_>>();
828 result.reverse();
829 result
830 }
831
832 #[test]
833 fn replace_max() {
834 let mut h = MinMaxHeap::from(vec![1, 2]);
835 assert_eq!(Some(2), h.replace_max(3));
836 assert_eq!(Some(&1), h.peek_min());
837 assert_eq!(Some(&3), h.peek_max());
838
839 assert_eq!(Some(3), h.replace_max(0));
840 assert_eq!(Some(&0), h.peek_min());
841 assert_eq!(Some(&1), h.peek_max());
842 }
843
844 #[test]
845 fn peek_min_mut() {
846 let mut h = MinMaxHeap::from(vec![2, 3, 4]);
847 *h.peek_min_mut().unwrap() = 1;
848 assert_eq!(Some(&1), h.peek_min());
849 assert_eq!(Some(&4), h.peek_max());
850
851 *h.peek_min_mut().unwrap() = 8;
852 assert_eq!(Some(&3), h.peek_min());
853 assert_eq!(Some(&8), h.peek_max());
854
855 assert_eq!(3, h.peek_min_mut().unwrap().pop());
856 assert_eq!(Some(&4), h.peek_min());
857 assert_eq!(Some(&8), h.peek_max());
858 }
859
860 #[test]
861 fn peek_max_mut() {
862 let mut h = MinMaxHeap::from(vec![1, 2]);
863 *h.peek_max_mut().unwrap() = 3;
864 assert_eq!(Some(&1), h.peek_min());
865 assert_eq!(Some(&3), h.peek_max());
866
867 *h.peek_max_mut().unwrap() = 0;
868 assert_eq!(Some(&0), h.peek_min());
869 assert_eq!(Some(&1), h.peek_max());
870
871 assert_eq!(1, h.peek_max_mut().unwrap().pop());
872 assert_eq!(Some(&0), h.peek_min());
873 assert_eq!(Some(&0), h.peek_max());
874 }
875
876 #[test]
877 fn push_pop_max() {
878 let mut h = MinMaxHeap::from(vec![1, 2]);
879 assert_eq!(3, h.push_pop_max(3));
880 assert_eq!(2, h.push_pop_max(0));
881 assert_eq!(Some(&0), h.peek_min());
882 assert_eq!(Some(&1), h.peek_max());
883 }
884}