@@ -17,7 +17,10 @@ use core::{
17
17
ptr, slice,
18
18
} ;
19
19
20
- use crate :: vec:: Vec ;
20
+ use crate :: {
21
+ storage:: { OwnedStorage , Storage , ViewStorage } ,
22
+ vec:: { Vec , VecInner } ,
23
+ } ;
21
24
22
25
/// Min-heap
23
26
pub enum Min { }
@@ -51,6 +54,15 @@ mod private {
51
54
impl private:: Sealed for Max { }
52
55
impl private:: Sealed for Min { }
53
56
57
+ /// Base struct for [`BinaryHeap`] and [`BinaryHeapView`], generic over the [`Storage`].
58
+ ///
59
+ /// In most cases you should use [`BinaryHeap`] or [`BinaryHeapView`] directly. Only use this
60
+ /// struct if you want to write code that's generic over both.
61
+ pub struct BinaryHeapInner < T , K , S : Storage > {
62
+ pub ( crate ) _kind : PhantomData < K > ,
63
+ pub ( crate ) data : VecInner < T , S > ,
64
+ }
65
+
54
66
/// A priority queue implemented with a binary heap.
55
67
///
56
68
/// This can be either a min-heap or a max-heap.
@@ -97,10 +109,56 @@ impl private::Sealed for Min {}
97
109
/// // The heap should now be empty.
98
110
/// assert!(heap.is_empty())
99
111
/// ```
100
- pub struct BinaryHeap < T , K , const N : usize > {
101
- pub ( crate ) _kind : PhantomData < K > ,
102
- pub ( crate ) data : Vec < T , N > ,
103
- }
112
+ pub type BinaryHeap < T , K , const N : usize > = BinaryHeapInner < T , K , OwnedStorage < N > > ;
113
+
114
+ /// A priority queue implemented with a binary heap.
115
+ ///
116
+ /// This can be either a min-heap or a max-heap.
117
+ ///
118
+ /// It is a logic error for an item to be modified in such a way that the item's ordering relative
119
+ /// to any other item, as determined by the `Ord` trait, changes while it is in the heap. This is
120
+ /// normally only possible through `Cell`, `RefCell`, global state, I/O, or unsafe code.
121
+ ///
122
+ /// ```
123
+ /// use heapless::binary_heap::{BinaryHeap, BinaryHeapView, Max};
124
+ ///
125
+ /// let mut heap_buffer: BinaryHeap<_, Max, 8> = BinaryHeap::new();
126
+ /// let heap: &mut BinaryHeapView<_, Max> = &mut heap_buffer;
127
+ ///
128
+ /// // We can use peek to look at the next item in the heap. In this case,
129
+ /// // there's no items in there yet so we get None.
130
+ /// assert_eq!(heap.peek(), None);
131
+ ///
132
+ /// // Let's add some scores...
133
+ /// heap.push(1).unwrap();
134
+ /// heap.push(5).unwrap();
135
+ /// heap.push(2).unwrap();
136
+ ///
137
+ /// // Now peek shows the most important item in the heap.
138
+ /// assert_eq!(heap.peek(), Some(&5));
139
+ ///
140
+ /// // We can check the length of a heap.
141
+ /// assert_eq!(heap.len(), 3);
142
+ ///
143
+ /// // We can iterate over the items in the heap, although they are returned in
144
+ /// // a random order.
145
+ /// for x in &*heap {
146
+ /// println!("{}", x);
147
+ /// }
148
+ ///
149
+ /// // If we instead pop these scores, they should come back in order.
150
+ /// assert_eq!(heap.pop(), Some(5));
151
+ /// assert_eq!(heap.pop(), Some(2));
152
+ /// assert_eq!(heap.pop(), Some(1));
153
+ /// assert_eq!(heap.pop(), None);
154
+ ///
155
+ /// // We can clear the heap of any remaining items.
156
+ /// heap.clear();
157
+ ///
158
+ /// // The heap should now be empty.
159
+ /// assert!(heap.is_empty())
160
+ /// ```
161
+ pub type BinaryHeapView < T , K > = BinaryHeapInner < T , K , ViewStorage > ;
104
162
105
163
impl < T , K , const N : usize > BinaryHeap < T , K , N > {
106
164
/* Constructors */
@@ -124,7 +182,23 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
124
182
}
125
183
}
126
184
127
- impl < T , K , const N : usize > BinaryHeap < T , K , N >
185
+ impl < T , K , const N : usize > BinaryHeap < T , K , N > {
186
+ /// Returns the underlying `Vec<T,N>`. Order is arbitrary and time is *O*(1).
187
+ pub fn into_vec ( self ) -> Vec < T , N > {
188
+ self . data
189
+ }
190
+
191
+ /// Get a reference to the `BinaryHeap`, erasing the `N` const-generic.
192
+ pub fn as_view ( & self ) -> & BinaryHeapView < T , K > {
193
+ self
194
+ }
195
+ /// Get a mutable reference to the `BinaryHeap`, erasing the `N` const-generic.
196
+ pub fn as_mut_view ( & mut self ) -> & mut BinaryHeapView < T , K > {
197
+ self
198
+ }
199
+ }
200
+
201
+ impl < T , K , S : Storage > BinaryHeapInner < T , K , S >
128
202
where
129
203
T : Ord ,
130
204
K : Kind ,
@@ -260,11 +334,11 @@ where
260
334
///
261
335
/// assert_eq!(heap.peek(), Some(&2));
262
336
/// ```
263
- pub fn peek_mut ( & mut self ) -> Option < PeekMut < ' _ , T , K , N > > {
337
+ pub fn peek_mut ( & mut self ) -> Option < PeekMutInner < ' _ , T , K , S > > {
264
338
if self . is_empty ( ) {
265
339
None
266
340
} else {
267
- Some ( PeekMut {
341
+ Some ( PeekMutInner {
268
342
heap : self ,
269
343
sift : true ,
270
344
} )
@@ -336,11 +410,6 @@ where
336
410
self . sift_up ( 0 , old_len) ;
337
411
}
338
412
339
- /// Returns the underlying `Vec<T,N>`. Order is arbitrary and time is *O*(1).
340
- pub fn into_vec ( self ) -> Vec < T , N > {
341
- self . data
342
- }
343
-
344
413
/* Private API */
345
414
fn sift_down_to_bottom ( & mut self , mut pos : usize ) {
346
415
let end = self . len ( ) ;
@@ -444,21 +513,37 @@ impl<'a, T> Hole<'a, T> {
444
513
/// Structure wrapping a mutable reference to the greatest item on a
445
514
/// `BinaryHeap`.
446
515
///
447
- /// This `struct` is created by [`BinaryHeap ::peek_mut`].
516
+ /// This `struct` is created by [`BinaryHeapInner ::peek_mut`].
448
517
/// See its documentation for more.
449
- pub struct PeekMut < ' a , T , K , const N : usize >
518
+ pub struct PeekMutInner < ' a , T , K , S >
450
519
where
451
520
T : Ord ,
452
521
K : Kind ,
522
+ S : Storage ,
453
523
{
454
- heap : & ' a mut BinaryHeap < T , K , N > ,
524
+ heap : & ' a mut BinaryHeapInner < T , K , S > ,
455
525
sift : bool ,
456
526
}
457
527
458
- impl < T , K , const N : usize > Drop for PeekMut < ' _ , T , K , N >
528
+ /// Structure wrapping a mutable reference to the greatest item on a
529
+ /// `BinaryHeap`.
530
+ ///
531
+ /// This `struct` is created by [`BinaryHeap::peek_mut`].
532
+ /// See its documentation for more.
533
+ pub type PeekMut < ' a , T , K , const N : usize > = PeekMutInner < ' a , T , K , OwnedStorage < N > > ;
534
+
535
+ /// Structure wrapping a mutable reference to the greatest item on a
536
+ /// `BinaryHeap`.
537
+ ///
538
+ /// This `struct` is created by [`BinaryHeapView::peek_mut`].
539
+ /// See its documentation for more.
540
+ pub type PeekMutView < ' a , T , K > = PeekMutInner < ' a , T , K , ViewStorage > ;
541
+
542
+ impl < T , K , S > Drop for PeekMutInner < ' _ , T , K , S >
459
543
where
460
544
T : Ord ,
461
545
K : Kind ,
546
+ S : Storage ,
462
547
{
463
548
fn drop ( & mut self ) {
464
549
if self . sift {
@@ -467,10 +552,11 @@ where
467
552
}
468
553
}
469
554
470
- impl < T , K , const N : usize > Deref for PeekMut < ' _ , T , K , N >
555
+ impl < T , K , S > Deref for PeekMutInner < ' _ , T , K , S >
471
556
where
472
557
T : Ord ,
473
558
K : Kind ,
559
+ S : Storage ,
474
560
{
475
561
type Target = T ;
476
562
fn deref ( & self ) -> & T {
@@ -480,10 +566,11 @@ where
480
566
}
481
567
}
482
568
483
- impl < T , K , const N : usize > DerefMut for PeekMut < ' _ , T , K , N >
569
+ impl < T , K , S > DerefMut for PeekMutInner < ' _ , T , K , S >
484
570
where
485
571
T : Ord ,
486
572
K : Kind ,
573
+ S : Storage ,
487
574
{
488
575
fn deref_mut ( & mut self ) -> & mut T {
489
576
debug_assert ! ( !self . heap. is_empty( ) ) ;
@@ -492,13 +579,14 @@ where
492
579
}
493
580
}
494
581
495
- impl < ' a , T , K , const N : usize > PeekMut < ' a , T , K , N >
582
+ impl < ' a , T , K , S > PeekMutInner < ' a , T , K , S >
496
583
where
497
584
T : Ord ,
498
585
K : Kind ,
586
+ S : Storage ,
499
587
{
500
588
/// Removes the peeked value from the heap and returns it.
501
- pub fn pop ( mut this : PeekMut < ' a , T , K , N > ) -> T {
589
+ pub fn pop ( mut this : PeekMutInner < ' a , T , K , S > ) -> T {
502
590
let value = this. heap . pop ( ) . unwrap ( ) ;
503
591
this. sift = false ;
504
592
value
@@ -539,20 +627,22 @@ where
539
627
}
540
628
}
541
629
542
- impl < T , K , const N : usize > fmt:: Debug for BinaryHeap < T , K , N >
630
+ impl < T , K , S > fmt:: Debug for BinaryHeapInner < T , K , S >
543
631
where
544
632
K : Kind ,
545
633
T : Ord + fmt:: Debug ,
634
+ S : Storage ,
546
635
{
547
636
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
548
637
f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
549
638
}
550
639
}
551
640
552
- impl < ' a , T , K , const N : usize > IntoIterator for & ' a BinaryHeap < T , K , N >
641
+ impl < ' a , T , K , S > IntoIterator for & ' a BinaryHeapInner < T , K , S >
553
642
where
554
643
K : Kind ,
555
644
T : Ord ,
645
+ S : Storage ,
556
646
{
557
647
type Item = & ' a T ;
558
648
type IntoIter = slice:: Iter < ' a , T > ;
0 commit comments