Skip to content

Commit 597aeb8

Browse files
authored
Merge pull request sekineh#5 from sekineh/depend-external-compare-crate
v0.2.0
2 parents 3aa4535 + 833bd66 commit 597aeb8

File tree

6 files changed

+82
-74
lines changed

6 files changed

+82
-74
lines changed

Cargo.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "binary-heap-plus"
3-
version = "0.1.6"
3+
version = "0.2.0"
44
authors = ["SEKINE, Hideki <sekineh@me.com>"]
55
description = "Enhanced version of std::collections::BinaryHeap that supports max, min, and custom-order heaps."
66
license = "MIT"
@@ -9,12 +9,9 @@ readme = "README.md"
99
keywords = ["binary", "heap", "priority", "queue"]
1010
category = ["data-structures", "algorithms", ]
1111

12-
[features]
13-
serde1 = ["serde", "serde_derive"]
14-
1512
[dependencies]
16-
serde = { version = "1", optional = true }
17-
serde_derive = { version = "^1.0.38", optional = true }
13+
compare = "0.1.0"
14+
serde = { version = "1.0", optional = true, features = ["derive"] }
1815

1916
[dev-dependencies]
2017
serde_json = "1.0.39"

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ for the same purpose.
5454

5555
# Changes
5656

57+
## v0.2.0
58+
59+
* [COMPATIBILITY CHANGE] Use `Compare` trait from `compare` crate instead of our own definition.
60+
Most users should not be affected by this. TIP: External `Compare<T>` impls needs to be updated to use `Fn` instead of `FnMut`.
61+
* [COMPATIBILITY CHANGE] rename feature `serde1` to `serde` in order to comply with the guideline:
62+
https://rust-lang-nursery.github.io/api-guidelines/interoperability.html#c-serde
63+
* Refactor ctor impl.
64+
5765
## v0.1.6
5866

5967
* Add generic constructor `from_vec()` and `from_vec_cmp()`.

appveyor.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ test_script:
4545
# we don't run the "test phase" when doing deploys
4646
- if [%APPVEYOR_REPO_TAG%]==[false] (
4747
cargo build --target %TARGET% &&
48-
cargo build --target %TARGET% --features serde1 &&
48+
cargo build --target %TARGET% --features serde &&
4949
cargo build --target %TARGET% --release &&
50-
cargo build --target %TARGET% --release --features serde1 &&
50+
cargo build --target %TARGET% --release --features serde &&
5151
cargo test --target %TARGET% &&
52-
cargo test --target %TARGET% --features serde1 &&
52+
cargo test --target %TARGET% --features serde &&
5353
cargo test --target %TARGET% --release &&
54-
cargo test --target %TARGET% --release --features serde1
54+
cargo test --target %TARGET% --release --features serde
5555
)
5656

5757
before_deploy:

ci/script.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ set -ex
55
# DONE This is the "test phase", tweak it as you see fit
66
main() {
77
cross build --target $TARGET
8+
cross build --target $TARGET --features serde
89
cross build --target $TARGET --release
10+
cross build --target $TARGET --release --features serde
911

1012
if [ ! -z $DISABLE_TESTS ]; then
1113
return
1214
fi
1315

1416
cross test --target $TARGET
15-
cross test --target $TARGET --features serde1
17+
cross test --target $TARGET --features serde
1618
cross test --target $TARGET --release
17-
cross test --target $TARGET --release --features serde1
19+
cross test --target $TARGET --release --features serde
1820

1921
# cross run --target $TARGET
2022
# cross run --target $TARGET --release

src/binary_heap.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ use std::iter::FromIterator;
165165
use std::slice;
166166
// use std::iter::FusedIterator;
167167
// use std::vec::Drain;
168+
use compare::Compare;
168169
use core::fmt;
169170
use core::mem::{size_of, swap};
170171
use core::ptr;
172+
#[cfg(feature = "serde")]
173+
use serde::{Deserialize, Serialize};
171174
use std::ops::Deref;
172175
use std::ops::DerefMut;
173176
use std::vec;
@@ -229,7 +232,7 @@ use std::vec;
229232
/// assert!(heap.is_empty())
230233
/// ```
231234
// #[stable(feature = "rust1", since = "1.0.0")]
232-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
235+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
233236
pub struct BinaryHeap<T, C = MaxComparator>
234237
where
235238
C: Compare<T>,
@@ -238,61 +241,54 @@ where
238241
cmp: C,
239242
}
240243

241-
/// Simpler replacement for the `Ord` trait.
242-
/// The difference is that you can define multiple sort orders on a single type `T`.
243-
/// Unlike `Ord` trait, `Compare<T>` trait can be easily implemented by providing a single function.
244-
pub trait Compare<T>: Clone {
245-
fn compare(&mut self, a: &T, b: &T) -> Ordering;
246-
}
247-
248244
/// For `T` that implements `Ord`, you can use this struct to quickly
249245
/// set up a max heap.
250-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
251-
#[derive(Clone, Debug, Default)]
246+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
247+
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
252248
pub struct MaxComparator;
253249

254250
impl<T: Ord> Compare<T> for MaxComparator {
255-
fn compare(&mut self, a: &T, b: &T) -> Ordering {
251+
fn compare(&self, a: &T, b: &T) -> Ordering {
256252
a.cmp(&b)
257253
}
258254
}
259255

260256
/// For `T` that implements `Ord`, you can use this struct to quickly
261257
/// set up a min heap.
262-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
263-
#[derive(Clone, Debug, Default)]
258+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
259+
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
264260
pub struct MinComparator;
265261

266262
impl<T: Ord> Compare<T> for MinComparator {
267-
fn compare(&mut self, a: &T, b: &T) -> Ordering {
263+
fn compare(&self, a: &T, b: &T) -> Ordering {
268264
b.cmp(&a)
269265
}
270266
}
271267

272268
/// The comparator defined by closure
273-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
274-
#[derive(Clone, Debug)]
269+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
270+
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
275271
pub struct FnComparator<F>(pub F);
276272

277273
impl<T, F> Compare<T> for FnComparator<F>
278274
where
279-
F: Clone + FnMut(&T, &T) -> Ordering,
275+
F: Clone + Fn(&T, &T) -> Ordering,
280276
{
281-
fn compare(&mut self, a: &T, b: &T) -> Ordering {
277+
fn compare(&self, a: &T, b: &T) -> Ordering {
282278
self.0(a, b)
283279
}
284280
}
285281

286282
/// The comparator ordered by key
287-
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
288-
#[derive(Clone, Debug)]
283+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
284+
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
289285
pub struct KeyComparator<F: Clone>(pub F);
290286

291287
impl<K: Ord, T, F> Compare<T> for KeyComparator<F>
292288
where
293-
F: Clone + FnMut(&T) -> K,
289+
F: Clone + Fn(&T) -> K,
294290
{
295-
fn compare(&mut self, a: &T, b: &T) -> Ordering {
291+
fn compare(&self, a: &T, b: &T) -> Ordering {
296292
self.0(a).cmp(&self.0(b))
297293
}
298294
}
@@ -353,7 +349,7 @@ impl<'a, T, C: Compare<T>> PeekMut<'a, T, C> {
353349
}
354350

355351
// #[stable(feature = "rust1", since = "1.0.0")]
356-
impl<T: Clone, C: Compare<T>> Clone for BinaryHeap<T, C> {
352+
impl<T: Clone, C: Compare<T> + Clone> Clone for BinaryHeap<T, C> {
357353
fn clone(&self) -> Self {
358354
BinaryHeap {
359355
data: self.data.clone(),
@@ -383,22 +379,22 @@ impl<T: fmt::Debug, C: Compare<T>> fmt::Debug for BinaryHeap<T, C> {
383379
}
384380

385381
impl<T, C: Compare<T> + Default> BinaryHeap<T, C> {
382+
/// Generic constructor for `BinaryHeap` from `Vec`.
383+
///
384+
/// Because `BinaryHeap` stores the elements in its internal `Vec`,
385+
/// it's natural to construct it from `Vec`.
386386
pub fn from_vec(vec: Vec<T>) -> Self {
387-
let mut heap = BinaryHeap {
388-
data: vec,
389-
cmp: C::default(),
390-
};
391-
heap.rebuild();
392-
heap
387+
BinaryHeap::from_vec_cmp(vec, C::default())
393388
}
394389
}
395390

396391
impl<T, C: Compare<T>> BinaryHeap<T, C> {
392+
/// Generic constructor for `BinaryHeap` from `Vec` and comparator.
393+
///
394+
/// Because `BinaryHeap` stores the elements in its internal `Vec`,
395+
/// it's natural to construct it from `Vec`.
397396
pub fn from_vec_cmp(vec: Vec<T>, cmp: C) -> Self {
398-
let mut heap = BinaryHeap {
399-
data: vec,
400-
cmp,
401-
};
397+
let mut heap = BinaryHeap { data: vec, cmp };
402398
heap.rebuild();
403399
heap
404400
}
@@ -500,7 +496,7 @@ impl<T: Ord> BinaryHeap<T, MinComparator> {
500496

501497
impl<T, F> BinaryHeap<T, FnComparator<F>>
502498
where
503-
F: Clone + FnMut(&T, &T) -> Ordering,
499+
F: Clone + Fn(&T, &T) -> Ordering,
504500
{
505501
/// Creates an empty `BinaryHeap`.
506502
///
@@ -549,7 +545,7 @@ where
549545

550546
impl<T, F, K: Ord> BinaryHeap<T, KeyComparator<F>>
551547
where
552-
F: Clone + FnMut(&T) -> K,
548+
F: Clone + Fn(&T) -> K,
553549
{
554550
/// Creates an empty `BinaryHeap`.
555551
///

src/lib.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,58 @@
11
//! This crate provides `BinaryHeap` which is backward-compatible with `std::collections::BinaryHeap`.
22
//!
3-
//! # Constructers
4-
//!
5-
//! ## Dedicated methods to create different kind of heaps
6-
//!
7-
//! * `BinaryHeap::new()` creates a max heap.
8-
//! * `BinaryHeap::new_min()` creates a min heap.
9-
//! * `BinaryHeap::new_by()` creates a heap sorted by the given closure.
10-
//! * `BinaryHeap::new_by_key()` creates a heap sorted by the key generated by the given closure.
3+
//! Added features include:
4+
//! * Heaps other than max heap.
5+
//! * Optional `serde` feature.
116
//!
7+
//! # Constructers
8+
//!
129
//! ## Generic methods to create different kind of heaps from initial `vec` data.
13-
//!
10+
//!
1411
//! * `BinaryHeap::from_vec(vec)`
1512
//! * `BinaryHeap::from_vec_cmp(vec, cmp)`
13+
//!
1614
//! ```
1715
//! use binary_heap_plus::*;
18-
//!
19-
//! // max heap
16+
//!
17+
//! // max heap (default)
2018
//! let mut heap: BinaryHeap<i32> = BinaryHeap::from_vec(vec![1,5,3]);
2119
//! assert_eq!(heap.pop(), Some(5));
22-
//!
20+
//!
2321
//! // min heap
2422
//! let mut heap: BinaryHeap<i32, MinComparator> = BinaryHeap::from_vec(vec![1,5,3]);
2523
//! assert_eq!(heap.pop(), Some(1));
26-
//!
24+
//!
2725
//! // custom-sort heap
28-
//! let mut heap = BinaryHeap::from_vec_cmp(vec![1,5,3], FnComparator(|a: &i32, b: &i32| a.cmp(b)));
29-
//! assert_eq!(heap.pop(), Some(5));
30-
//!
26+
//! let mut heap = BinaryHeap::from_vec_cmp(vec![1,5,3], FnComparator(|a: &i32, b: &i32| b.cmp(a)));
27+
//! assert_eq!(heap.pop(), Some(1));
28+
//!
3129
//! // custom-key heap
3230
//! let mut heap = BinaryHeap::from_vec_cmp(vec![6,3,1], KeyComparator(|k: &i32| k % 4));
3331
//! assert_eq!(heap.pop(), Some(3));
34-
//!
32+
//!
3533
//! // TIP: How to reuse a comparator
3634
//! let mod4_comparator = KeyComparator(|k: &_| k % 4);
37-
//! let mut heap = BinaryHeap::from_vec_cmp(vec![6,3,1], mod4_comparator);
38-
//! assert_eq!(heap.pop(), Some(3));
35+
//! let mut heap1 = BinaryHeap::from_vec_cmp(vec![6,3,1], mod4_comparator);
36+
//! assert_eq!(heap1.pop(), Some(3));
37+
//! let mut heap2 = BinaryHeap::from_vec_cmp(vec![2,4,1], mod4_comparator);
38+
//! assert_eq!(heap2.pop(), Some(2));
3939
//! ```
40+
//!
41+
//! ## Dedicated methods to create different kind of heaps
42+
//!
43+
//! * `BinaryHeap::new()` creates a max heap.
44+
//! * `BinaryHeap::new_min()` creates a min heap.
45+
//! * `BinaryHeap::new_by()` creates a heap sorted by the given closure.
46+
//! * `BinaryHeap::new_by_key()` creates a heap sorted by the key generated by the given closure.
47+
//!
4048
4149
mod binary_heap;
4250
pub use binary_heap::*;
51+
extern crate compare;
4352
extern crate core;
44-
#[cfg(feature = "serde1")]
53+
#[cfg(feature = "serde")]
4554
extern crate serde;
46-
#[cfg(feature = "serde1")]
47-
#[macro_use]
48-
extern crate serde_derive;
49-
50-
#[cfg(all(feature = "serde1", test))]
55+
#[cfg(all(feature = "serde", test))]
5156
extern crate serde_json;
5257

5358
/// An intermediate trait for specialization of `Extend`.
@@ -361,9 +366,9 @@ mod from_liballoc {
361366

362367
}
363368

364-
#[cfg(feature = "serde1")]
369+
#[cfg(feature = "serde")]
365370
#[cfg(test)]
366-
mod tests_serde1 {
371+
mod tests_serde {
367372
use super::binary_heap::*;
368373
use serde_json;
369374

0 commit comments

Comments
 (0)