-
Notifications
You must be signed in to change notification settings - Fork 237
Added from_vec and into_sorted_vec for BinaryHeap
#576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,6 @@ | |
| //! Insertion and popping the largest element have *O*(log n) time complexity. | ||
| //! Checking the smallest/largest element is *O*(1). | ||
|
|
||
| // TODO not yet implemented | ||
| // Converting a vector to a binary heap can be done in-place, and has *O*(n) complexity. A binary | ||
| // heap can also be converted to a sorted vector in-place, allowing it to be used for an | ||
| // *O*(n log n) in-place heapsort. | ||
|
|
||
| use core::{ | ||
| cmp::Ordering, | ||
| fmt, | ||
|
|
@@ -192,6 +187,36 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> { | |
| pub fn into_vec(self) -> Vec<T, N, usize> { | ||
| self.data | ||
| } | ||
|
|
||
| /// Returns the underlying `Vec<T,N>` sorted in ascending order. | ||
| /// The time complexity is *O*(n log n). | ||
| /// | ||
| /// ``` | ||
| /// use heapless::binary_heap::{BinaryHeap, Max}; | ||
| /// | ||
| /// let mut heap: BinaryHeap<_, Max, 4> = BinaryHeap::new(); | ||
| /// heap.push(4).unwrap(); | ||
| /// heap.push(2).unwrap(); | ||
| /// heap.push(8).unwrap(); | ||
| /// heap.push(1).unwrap(); | ||
| /// assert_eq!(heap.into_sorted_vec(), [1, 2, 4, 8]); | ||
| /// ``` | ||
| pub fn into_sorted_vec(mut self) -> Vec<T, N, usize> | ||
| where | ||
| K: Kind, | ||
| T: Ord, | ||
| { | ||
| let mut i = self.data.len(); | ||
| while i > 0 { | ||
| i -= 1; | ||
| unsafe { | ||
| let p = self.data.as_mut_ptr(); | ||
| ptr::swap(p, p.add(i)); | ||
| } | ||
| self.sift_down_to_bottom(0, i); | ||
| } | ||
| self.data | ||
| } | ||
| } | ||
|
|
||
| impl<T, K, S: VecStorage<T>> BinaryHeapInner<T, K, S> { | ||
|
|
@@ -419,7 +444,7 @@ where | |
|
|
||
| if !self.is_empty() { | ||
| mem::swap(&mut item, self.data.as_mut_slice().get_unchecked_mut(0)); | ||
| self.sift_down_to_bottom(0); | ||
| self.sift_down_to_bottom(0, self.len()); | ||
| } | ||
| item | ||
| } | ||
|
|
@@ -471,8 +496,7 @@ where | |
| } | ||
|
|
||
| /* Private API */ | ||
| fn sift_down_to_bottom(&mut self, mut pos: usize) { | ||
| let end = self.len(); | ||
| fn sift_down_to_bottom(&mut self, mut pos: usize, end: usize) { | ||
| let start = pos; | ||
| unsafe { | ||
| let mut hole = Hole::new(self.data.as_mut_slice(), pos); | ||
|
|
@@ -607,7 +631,7 @@ where | |
| { | ||
| fn drop(&mut self) { | ||
| if self.sift { | ||
| self.heap.sift_down_to_bottom(0); | ||
| self.heap.sift_down_to_bottom(0, self.heap.len()); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -687,6 +711,19 @@ where | |
| } | ||
| } | ||
|
|
||
| impl<T: Ord, K: Kind, const N: usize> From<Vec<T, N, usize>> for BinaryHeap<T, K, N> { | ||
| fn from(vec: Vec<T, N, usize>) -> Self { | ||
| let mut heap = Self { | ||
| _kind: PhantomData, | ||
| data: vec, | ||
| }; | ||
| let len = heap.len(); | ||
| for i in (0..len / 2).rev() { | ||
| heap.sift_down_to_bottom(i, len); | ||
| } | ||
| heap | ||
| } | ||
| } | ||
| impl<T, K, S> fmt::Debug for BinaryHeapInner<T, K, S> | ||
| where | ||
| K: Kind, | ||
|
|
@@ -916,4 +953,31 @@ mod tests { | |
| ) -> &'c BinaryHeapView<&'b (), Max> { | ||
| x | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_vec() { | ||
| use crate::vec::Vec; | ||
|
|
||
| let src: Vec<_, 16, _> = Vec::from_array([4, 1, 12, 8, 7, 3, 0, 6, 9, 2, 5, 11, 10]); | ||
| let heap: BinaryHeap<u8, Min, 16> = BinaryHeap::from(src); | ||
| assert_eq!(heap.len(), 13); | ||
| assert_eq!(heap.capacity(), 16); | ||
| assert_eq!( | ||
| &heap.into_vec(), | ||
| &[0, 1, 3, 6, 2, 4, 12, 8, 9, 7, 5, 11, 10] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn into_sorted_vec() { | ||
| use crate::vec::Vec; | ||
| use core::array; | ||
|
|
||
| let src: Vec<_, 16, _> = Vec::from_array([4, 1, 12, 8, 7, 3, 0, 6, 9, 2, 5, 11, 10]); | ||
| let heap: BinaryHeap<u8, Min, 16> = BinaryHeap::from(src); | ||
| let dst = heap.into_sorted_vec(); | ||
| assert_eq!(dst.len(), 13); | ||
| assert_eq!(dst.capacity(), 16); | ||
| assert_eq!(&dst, &array::from_fn::<u8, 13, _>(|x| 12 - x as u8)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The docstring for Maybe fixing this should just be a doc change to say that the order is different for Min vs Max heaps? |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no
from_vecadded in this PR, rename toFrom<Vec>>?