Skip to content

Commit 350cfa0

Browse files
authored
Merge pull request sekineh#8 from sekineh/sekineh-patch-1
add instruction to create various heaps
2 parents 77f8c3a + 227ec71 commit 350cfa0

File tree

6 files changed

+77
-47
lines changed

6 files changed

+77
-47
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
### Added
10+
11+
* Quickstart section in the doc

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT"
77
repository = "https://github.com/sekineh/binary-heap-plus-rs"
88
readme = "README.md"
99
keywords = ["binary", "heap", "priority", "queue"]
10-
category = ["data-structures", "algorithms", ]
10+
categories = ["data-structures", "algorithms", ]
1111

1212
[dependencies]
1313
compare = "0.1.0"

README.md

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,9 @@ It supports the following features and still maintains backward compatibility.
1111
- Heap ordered by closure
1212
- Heap ordered by key generated by closure
1313

14-
You can change the line
15-
16-
```
17-
use std::collections::BinaryHeap;
18-
```
19-
20-
to like below.
21-
22-
```
23-
use binary_heap_plus::*;
24-
```
25-
26-
Your code will compile as before unless you use unstable APIs.
27-
2814
This crate requires Rust 1.26 or later.
2915

30-
# Added muthods
31-
32-
## `BinaryHeap::new_xxx()`
33-
34-
- (original) `::new()` // creates a max heap
35-
- `::new_min()` // creates a min heap
36-
- `::new_by(f)` // creates a heap ordered by the given closure `f`
37-
- `::new_by_key(g)` // creates a heap ordered by key generated by the given closure `g`
38-
39-
## `BinaryHeap::with_capacity_xxx()`
40-
41-
- (original) `::with_capacity(n)` // creates a max heap with capacity `n`
42-
- `::with_capacity_min(n)` // creates a min heap with capacity `n`
43-
- `::with_capacity_by(n, f)` // creates a heap with capacity `n`, ordered by the given closure `f`
44-
- `::with_capacity_by_key(n, g)` // creates a heap with capacity `n`, ordered by key generated by the given closure `g`
45-
46-
## `BinaryHeap::from_vec()`
16+
## Note on `BinaryHeap::from_vec()`
4717

4818
Currently, the `From<Vec<T>>` trait is implemented for max heap only.
4919
If you add generic impl for other heaps, the existing code breaks, requires

appveyor.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,14 @@ install:
4242

4343
# TODO This is the "test phase", tweak it as you see fit
4444
test_script:
45-
# we don't run the "test phase" when doing deploys
46-
- if [%APPVEYOR_REPO_TAG%]==[false] (
47-
cargo build --target %TARGET% &&
48-
cargo build --target %TARGET% --features serde &&
49-
cargo build --target %TARGET% --release &&
50-
cargo build --target %TARGET% --release --features serde &&
51-
cargo test --target %TARGET% &&
52-
cargo test --target %TARGET% --features serde &&
53-
cargo test --target %TARGET% --release &&
54-
cargo test --target %TARGET% --release --features serde
55-
)
45+
- cargo build --target %TARGET%
46+
- cargo test --target %TARGET%
47+
- cargo build --target %TARGET% --features serde
48+
- cargo test --target %TARGET% --features serde
49+
- cargo build --target %TARGET% --release
50+
- cargo test --target %TARGET% --release
51+
- cargo build --target %TARGET% --release --features serde
52+
- cargo test --target %TARGET% --release --features serde
5653

5754
before_deploy:
5855
# TODO Update this to build the artifacts that matter to you

ci/script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ main() {
1313
return
1414
fi
1515

16+
cross clean
1617
cross test --target $TARGET
1718
cross test --target $TARGET --features serde
19+
cross clean
1820
cross test --target $TARGET --release
1921
cross test --target $TARGET --release --features serde
2022

src/lib.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,66 @@
11
//! This crate provides `BinaryHeap` which is backward-compatible with `std::collections::BinaryHeap`.
2-
//!
2+
//!
33
//! Added features include:
44
//! * Heaps other than max heap.
55
//! * Optional `serde` feature.
6+
//!
7+
//! # Quick start
8+
//!
9+
//! ## Max/Min Heap
10+
//!
11+
//! For max heap, `BiaryHeap::from_vec()` is the most versatile way to create a heap.
12+
//!
13+
//! ```rust
14+
//! // extern crate binary_heap_plus;
15+
//! use binary_heap_plus::*;
16+
//!
17+
//! // max heap
18+
//! let mut h: BinaryHeap<i32> = BinaryHeap::from_vec(vec![]);
19+
//! // max heap with initial capacity
20+
//! let mut h: BinaryHeap<i32> = BinaryHeap::from_vec(Vec::with_capacity(16));
21+
//! // max heap from iterator
22+
//! let mut h: BinaryHeap<i32> = BinaryHeap::from_vec((0..42).collect());
23+
//! assert_eq!(h.pop(), Some(41));
24+
//! ```
625
//!
26+
//! Min heap is similar, but requires type annotation.
27+
//!
28+
//! ```rust
29+
//! // extern crate binary_heap_plus;
30+
//! use binary_heap_plus::*;
31+
//!
32+
//! // min heap
33+
//! let mut h: BinaryHeap<i32, MinComparator> = BinaryHeap::from_vec(vec![]);
34+
//! // min heap with initial capacity
35+
//! let mut h: BinaryHeap<i32, MinComparator> = BinaryHeap::from_vec(Vec::with_capacity(16));
36+
//! // min heap from iterator
37+
//! let mut h: BinaryHeap<i32, MinComparator> = BinaryHeap::from_vec((0..42).collect());
38+
//! assert_eq!(h.pop(), Some(0));
39+
//! ```
40+
//!
41+
//! ## Custom Heap
42+
//!
43+
//! For custom heap, `BinaryHeap::from_vec_cmp()` works in a similar way to max/min heap. The only difference is that you add the comparator closure with apropriate signature.
44+
//!
45+
//! ```rust
46+
//! // extern crate binary_heap_plus;
47+
//! use binary_heap_plus::*;
48+
//!
49+
//! // custom heap: ordered by second value (_.1) of the tuples; min first
50+
//! let mut h = BinaryHeap::from_vec_cmp(
51+
//! vec![(1, 5), (3, 2), (2, 3)],
52+
//! |a: &(i32, i32), b: &(i32, i32)| b.1.cmp(&a.1), // comparator closure here
53+
//! );
54+
//! assert_eq!(h.pop(), Some((3, 2)));
55+
//! ```
56+
//!
757
//! # Constructers
858
//!
959
//! ## Generic methods to create different kind of heaps from initial `vec` data.
1060
//!
1161
//! * `BinaryHeap::from_vec(vec)`
1262
//! * `BinaryHeap::from_vec_cmp(vec, cmp)`
13-
//!
63+
//!
1464
//! ```
1565
//! use binary_heap_plus::*;
1666
//!
@@ -23,7 +73,7 @@
2373
//! assert_eq!(heap.pop(), Some(1));
2474
//!
2575
//! // custom-sort heap
26-
//! let mut heap = BinaryHeap::from_vec_cmp(vec![1,5,3], FnComparator(|a: &i32, b: &i32| b.cmp(a)));
76+
//! let mut heap = BinaryHeap::from_vec_cmp(vec![1,5,3], |a: &i32, b: &i32| b.cmp(a));
2777
//! assert_eq!(heap.pop(), Some(1));
2878
//!
2979
//! // custom-key heap
@@ -47,7 +97,7 @@
4797
//!
4898
4999
mod binary_heap;
50-
pub use binary_heap::*;
100+
pub use crate::binary_heap::*;
51101
extern crate compare;
52102
extern crate core;
53103
#[cfg(feature = "serde")]

0 commit comments

Comments
 (0)