Skip to content

Commit 13f9a2f

Browse files
authored
add .into_iter_sorted(). (sekineh#20)
* add .into_iter_sorted(). * Add README.md and CHANGELOG.md entry.
1 parent 2c4e2ca commit 13f9a2f

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010

1111
* Quickstart section in the doc
12+
* `.into_iter_sorted()` which returns the values in *heap* order. (#13)
13+
* Note: `.into_iter()` returns the values in *arbitrary* order. There is a good reason (ex. make `.extend()` efficient) for `std` to do that, it was surprising to some people.
1214

1315
### Changed
1416

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ It supports the following features and still maintains backward compatibility.
1010
- Heap ordered by closure
1111
- Heap ordered by key generated by closure
1212

13+
Notable added method is:
14+
- `.into_iter_sorted()` backported from `std`.
15+
1316
## MSRV (Minimum Supported Rust Version)
1417

1518
This crate requires Rust 1.31.1 or later.

src/binary_heap.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,26 @@ impl<T, C: Compare<T>> BinaryHeap<T, C> {
616616
}
617617
}
618618

619+
/// Returns an iterator which retrieves elements in heap order.
620+
/// This method consumes the original heap.
621+
///
622+
/// # Examples
623+
///
624+
/// Basic usage:
625+
///
626+
/// ```
627+
/// use binary_heap_plus::*;
628+
/// let heap = BinaryHeap::from(vec![1, 2, 3, 4, 5]);
629+
///
630+
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
631+
/// ```
632+
// #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
633+
pub fn into_iter_sorted(self) -> IntoIterSorted<T, C> {
634+
IntoIterSorted {
635+
inner: self,
636+
}
637+
}
638+
619639
/// Returns the greatest item in the binary heap, or `None` if it is empty.
620640
///
621641
/// # Examples
@@ -1285,6 +1305,28 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
12851305
// #[stable(feature = "fused", since = "1.26.0")]
12861306
// impl<T> FusedIterator for IntoIter<T> {}
12871307

1308+
// #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
1309+
#[derive(Clone, Debug)]
1310+
pub struct IntoIterSorted<T, C: Compare<T>> {
1311+
inner: BinaryHeap<T, C>,
1312+
}
1313+
1314+
// #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
1315+
impl<T, C: Compare<T>> Iterator for IntoIterSorted<T, C> {
1316+
type Item = T;
1317+
1318+
#[inline]
1319+
fn next(&mut self) -> Option<T> {
1320+
self.inner.pop()
1321+
}
1322+
1323+
#[inline]
1324+
fn size_hint(&self) -> (usize, Option<usize>) {
1325+
let exact = self.inner.len();
1326+
(exact, Some(exact))
1327+
}
1328+
}
1329+
12881330
/// A draining iterator over the elements of a `BinaryHeap`.
12891331
///
12901332
/// This `struct` is created by the [`drain`] method on [`BinaryHeap`]. See its

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ mod from_liballoc {
171171
assert_eq!(v, iterout);
172172
}
173173

174+
#[test]
175+
fn test_into_iter_sorted_collect() {
176+
let heap = BinaryHeap::from(vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]);
177+
let it = heap.into_iter_sorted();
178+
let sorted = it.collect::<Vec<_>>();
179+
assert_eq!(sorted, vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 2, 1, 1, 0]);
180+
}
181+
174182
#[test]
175183
fn test_peek_and_pop() {
176184
let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];

0 commit comments

Comments
 (0)