Skip to content

Commit 7754b9d

Browse files
authored
Add unsafe constructor BinaryHeap::from_vec_cmp_raw() (sekineh#22)
1 parent 94023c8 commit 7754b9d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
11+
* generic constructor `from_vec_cmp_raw()`.
12+
913
## [0.3.0] - 2020-07-08
1014

1115
### Added

src/binary_heap.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,21 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
394394
/// Because `BinaryHeap` stores the elements in its internal `Vec`,
395395
/// it's natural to construct it from `Vec`.
396396
pub fn from_vec_cmp(vec: Vec<T>, cmp: C) -> Self {
397+
unsafe { BinaryHeap::from_vec_cmp_raw(vec, cmp, true) }
398+
}
399+
400+
/// Generic constructor for `BinaryHeap` from `Vec` and comparator.
401+
///
402+
/// Because `BinaryHeap` stores the elements in its internal `Vec`,
403+
/// it's natural to construct it from `Vec`.
404+
///
405+
/// # Safety
406+
/// User is responsible for providing valid `rebuild` value.
407+
pub unsafe fn from_vec_cmp_raw(vec: Vec<T>, cmp: C, rebuild: bool) -> Self {
397408
let mut heap = BinaryHeap { data: vec, cmp };
398-
heap.rebuild();
409+
if rebuild && !heap.data.is_empty() {
410+
heap.rebuild();
411+
}
399412
heap
400413
}
401414
}

0 commit comments

Comments
 (0)