Skip to content

Commit fa1cabc

Browse files
committed
Replace from_vec with From<Vec<T,N>> impl
1 parent 03fc8f4 commit fa1cabc

1 file changed

Lines changed: 15 additions & 33 deletions

File tree

src/binary_heap.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -180,37 +180,6 @@ impl<T, K, const N: usize> BinaryHeap<T, K, N> {
180180
data: Vec::new(),
181181
}
182182
}
183-
184-
/// Creates a new `BinaryHeap` from a `Vec<T,N>`.
185-
/// The heap uses the vector as its backing storage, no additional space is required.
186-
/// The elements in the vector are rearranged to create the heap.
187-
/// The time complexity is *O*(n).
188-
///
189-
/// ```
190-
/// use heapless::binary_heap::{BinaryHeap, Max};
191-
/// use heapless::vec::Vec;
192-
///
193-
/// let vec = Vec::from_array([4, 1, 8, 7, 3, 0, 6, 9, 2, 5]);
194-
/// let heap: BinaryHeap<_, Max, 12> = BinaryHeap::from_vec(vec);
195-
/// assert_eq!(heap.len(), 10);
196-
/// assert_eq!(heap.capacity(), 12);
197-
/// assert_eq!(heap.peek(), Some(&9));
198-
/// ```
199-
pub fn from_vec(vec: Vec<T, N, usize>) -> Self
200-
where
201-
T: Ord,
202-
K: Kind,
203-
{
204-
let mut heap = Self {
205-
_kind: PhantomData,
206-
data: vec,
207-
};
208-
let len = heap.len();
209-
for i in (0..len / 2).rev() {
210-
heap.sift_down_to_bottom(i, len);
211-
}
212-
heap
213-
}
214183
}
215184

216185
impl<T, K, const N: usize> BinaryHeap<T, K, N> {
@@ -742,6 +711,19 @@ where
742711
}
743712
}
744713

714+
impl<T: Ord, K: Kind, const N: usize> From<Vec<T, N, usize>> for BinaryHeap<T, K, N> {
715+
fn from(vec: Vec<T, N, usize>) -> Self {
716+
let mut heap = Self {
717+
_kind: PhantomData,
718+
data: vec,
719+
};
720+
let len = heap.len();
721+
for i in (0..len / 2).rev() {
722+
heap.sift_down_to_bottom(i, len);
723+
}
724+
heap
725+
}
726+
}
745727
impl<T, K, S> fmt::Debug for BinaryHeapInner<T, K, S>
746728
where
747729
K: Kind,
@@ -977,7 +959,7 @@ mod tests {
977959
use crate::vec::Vec;
978960

979961
let src: Vec<_, 16, _> = Vec::from_array([4, 1, 12, 8, 7, 3, 0, 6, 9, 2, 5, 11, 10]);
980-
let heap: BinaryHeap<u8, Min, 16> = BinaryHeap::from_vec(src);
962+
let heap: BinaryHeap<u8, Min, 16> = BinaryHeap::from(src);
981963
assert_eq!(heap.len(), 13);
982964
assert_eq!(heap.capacity(), 16);
983965
assert_eq!(
@@ -992,7 +974,7 @@ mod tests {
992974
use core::array;
993975

994976
let src: Vec<_, 16, _> = Vec::from_array([4, 1, 12, 8, 7, 3, 0, 6, 9, 2, 5, 11, 10]);
995-
let heap: BinaryHeap<u8, Min, 16> = BinaryHeap::from_vec(src);
977+
let heap: BinaryHeap<u8, Min, 16> = BinaryHeap::from(src);
996978
let dst = heap.into_sorted_vec();
997979
assert_eq!(dst.len(), 13);
998980
assert_eq!(dst.capacity(), 16);

0 commit comments

Comments
 (0)