Skip to content

Commit 1fb35ce

Browse files
garro95lengyijun
andauthored
Chore (#72)
* Reuse a variable Performance * deduplicate Index fns * deduplicate better_to_rebuild * rm empty block * rm useless Eq of Store --------- Co-authored-by: lyj <[email protected]>
1 parent 9944734 commit 1fb35ce

File tree

4 files changed

+49
-95
lines changed

4 files changed

+49
-95
lines changed

src/double_priority_queue/mod.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ pub mod iterators;
3434
#[cfg(not(feature = "std"))]
3535
use std::vec::Vec;
3636

37+
use crate::better_to_rebuild;
3738
use crate::core_iterators::*;
39+
use crate::store::{left, level, parent, right};
3840
use crate::store::{Index, Position, Store};
3941
use crate::TryReserveError;
4042
use iterators::*;
@@ -1218,48 +1220,6 @@ where
12181220
}
12191221
}
12201222

1221-
/// Compute the index of the left child of an item from its index
1222-
#[inline(always)]
1223-
const fn left(i: Position) -> Position {
1224-
Position((i.0 * 2) + 1)
1225-
}
1226-
/// Compute the index of the right child of an item from its index
1227-
#[inline(always)]
1228-
const fn right(i: Position) -> Position {
1229-
Position((i.0 * 2) + 2)
1230-
}
1231-
/// Compute the index of the parent element in the heap from its index
1232-
#[inline(always)]
1233-
const fn parent(i: Position) -> Position {
1234-
Position((i.0 - 1) / 2)
1235-
}
1236-
1237-
// Compute the level of a node from its index
1238-
#[inline(always)]
1239-
const fn level(i: Position) -> usize {
1240-
log2_fast(i.0 + 1)
1241-
}
1242-
1243-
#[inline(always)]
1244-
const fn log2_fast(x: usize) -> usize {
1245-
(usize::BITS - x.leading_zeros() - 1) as usize
1246-
}
1247-
1248-
// `rebuild` takes O(len1 + len2) operations
1249-
// and about 2 * (len1 + len2) comparisons in the worst case
1250-
// while `extend` takes O(len2 * log_2(len1)) operations
1251-
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
1252-
// assuming len1 >= len2.
1253-
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
1254-
// log(1) == 0, so the inequation always falsy
1255-
// log(0) is inapplicable and produces panic
1256-
if len1 <= 1 {
1257-
return false;
1258-
}
1259-
1260-
2 * (len1 + len2) < len2 * log2_fast(len1)
1261-
}
1262-
12631223
#[cfg(feature = "serde")]
12641224
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
12651225
mod serde {

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ mod store;
119119

120120
pub use crate::double_priority_queue::DoublePriorityQueue;
121121
pub use crate::priority_queue::PriorityQueue;
122+
use crate::store::log2_fast;
122123

123124
use indexmap::TryReserveError as IndexMapTryReserveError;
124125
use std::collections::TryReserveError as StdTryReserveError;
@@ -161,3 +162,18 @@ impl From<IndexMapTryReserveError> for TryReserveError {
161162
}
162163
}
163164
}
165+
166+
// `rebuild` takes O(len1 + len2) operations
167+
// and about 2 * (len1 + len2) comparisons in the worst case
168+
// while `extend` takes O(len2 * log_2(len1)) operations
169+
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
170+
// assuming len1 >= len2.
171+
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
172+
// log(1) == 0, so the inequation always falsy
173+
// log(0) is inapplicable and produces panic
174+
if len1 <= 1 {
175+
return false;
176+
}
177+
178+
2 * (len1 + len2) < len2 * log2_fast(len1)
179+
}

src/priority_queue/mod.rs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub mod iterators;
3535
#[cfg(not(feature = "std"))]
3636
use std::vec::Vec;
3737

38+
use crate::better_to_rebuild;
3839
use crate::core_iterators::*;
40+
use crate::store::{left, parent, right};
3941
use crate::store::{Index, Position, Store};
4042
use crate::TryReserveError;
4143
use iterators::*;
@@ -730,13 +732,6 @@ where
730732
}
731733
}
732734

733-
impl<I, P, H> PriorityQueue<I, P, H>
734-
where
735-
P: Ord,
736-
I: Hash + Eq,
737-
{
738-
}
739-
740735
impl<I, P, H> PriorityQueue<I, P, H>
741736
where
742737
P: Ord,
@@ -772,7 +767,7 @@ where
772767
self.store.swap(i, largest);
773768

774769
i = largest;
775-
let mut largestp = unsafe { self.store.get_priority_from_position(i) };
770+
largestp = unsafe { self.store.get_priority_from_position(i) };
776771
l = left(i);
777772
if l.0 < self.len() {
778773
let childp = unsafe { self.store.get_priority_from_position(l) };
@@ -977,42 +972,6 @@ where
977972
}
978973
}
979974

980-
/// Compute the index of the left child of an item from its index
981-
#[inline(always)]
982-
const fn left(i: Position) -> Position {
983-
Position((i.0 * 2) + 1)
984-
}
985-
/// Compute the index of the right child of an item from its index
986-
#[inline(always)]
987-
const fn right(i: Position) -> Position {
988-
Position((i.0 * 2) + 2)
989-
}
990-
/// Compute the index of the parent element in the heap from its index
991-
#[inline(always)]
992-
const fn parent(i: Position) -> Position {
993-
Position((i.0 - 1) / 2)
994-
}
995-
996-
#[inline(always)]
997-
const fn log2_fast(x: usize) -> usize {
998-
(usize::BITS - x.leading_zeros() - 1) as usize
999-
}
1000-
1001-
// `rebuild` takes O(len1 + len2) operations
1002-
// and about 2 * (len1 + len2) comparisons in the worst case
1003-
// while `extend` takes O(len2 * log_2(len1)) operations
1004-
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
1005-
// assuming len1 >= len2.
1006-
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
1007-
// log(1) == 0, so the inequation always falsy
1008-
// log(0) is inapplicable and produces panic
1009-
if len1 <= 1 {
1010-
return false;
1011-
}
1012-
1013-
2 * (len1 + len2) < len2 * log2_fast(len1)
1014-
}
1015-
1016975
#[cfg(feature = "serde")]
1017976
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
1018977
mod serde {

src/store.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,38 @@ use indexmap::map::{IndexMap, MutableKeys};
4646
/// The Index of the element in the Map
4747
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
4848
pub(crate) struct Index(pub usize);
49+
4950
/// The Position of the element in the Heap
5051
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
5152
pub(crate) struct Position(pub usize);
5253

54+
/// Compute the index of the left child of an item from its index
55+
#[inline(always)]
56+
pub(crate) const fn left(i: Position) -> Position {
57+
Position((i.0 * 2) + 1)
58+
}
59+
/// Compute the index of the right child of an item from its index
60+
#[inline(always)]
61+
pub(crate) const fn right(i: Position) -> Position {
62+
Position((i.0 * 2) + 2)
63+
}
64+
/// Compute the index of the parent element in the heap from its index
65+
#[inline(always)]
66+
pub(crate) const fn parent(i: Position) -> Position {
67+
Position((i.0 - 1) / 2)
68+
}
69+
70+
// Compute the level of a node from its index
71+
#[inline(always)]
72+
pub(crate) const fn level(i: Position) -> usize {
73+
log2_fast(i.0 + 1)
74+
}
75+
76+
#[inline(always)]
77+
pub(crate) const fn log2_fast(x: usize) -> usize {
78+
(usize::BITS - x.leading_zeros() - 1) as usize
79+
}
80+
5381
/// Internal storage of PriorityQueue and DoublePriorityQueue
5482
#[derive(Clone)]
5583
#[cfg(feature = "std")]
@@ -71,15 +99,6 @@ pub(crate) struct Store<I, P, H> {
7199
pub size: usize, // The size of the heap
72100
}
73101

74-
// do not [derive(Eq)] to loosen up trait requirements for other types and impls
75-
impl<I, P, H> Eq for Store<I, P, H>
76-
where
77-
I: Hash + Eq,
78-
P: Ord,
79-
H: BuildHasher,
80-
{
81-
}
82-
83102
impl<I, P, H> Default for Store<I, P, H>
84103
where
85104
I: Hash + Eq,

0 commit comments

Comments
 (0)