Skip to content

Commit 7cf3d29

Browse files
lengyijungarro95
authored andcommitted
deduplicate better_to_rebuild
1 parent 6f2ebd0 commit 7cf3d29

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

src/double_priority_queue/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +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::*;
38-
use crate::store::{left, level, log2_fast, parent, right};
39+
use crate::store::{left, level, parent, right};
3940
use crate::store::{Index, Position, Store};
4041
use crate::TryReserveError;
4142
use iterators::*;
@@ -1219,21 +1220,6 @@ where
12191220
}
12201221
}
12211222

1222-
// `rebuild` takes O(len1 + len2) operations
1223-
// and about 2 * (len1 + len2) comparisons in the worst case
1224-
// while `extend` takes O(len2 * log_2(len1)) operations
1225-
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
1226-
// assuming len1 >= len2.
1227-
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
1228-
// log(1) == 0, so the inequation always falsy
1229-
// log(0) is inapplicable and produces panic
1230-
if len1 <= 1 {
1231-
return false;
1232-
}
1233-
1234-
2 * (len1 + len2) < len2 * log2_fast(len1)
1235-
}
1236-
12371223
#[cfg(feature = "serde")]
12381224
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
12391225
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: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +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::*;
39-
use crate::store::{left, log2_fast, parent, right};
40+
use crate::store::{left, parent, right};
4041
use crate::store::{Index, Position, Store};
4142
use crate::TryReserveError;
4243
use iterators::*;
@@ -978,21 +979,6 @@ where
978979
}
979980
}
980981

981-
// `rebuild` takes O(len1 + len2) operations
982-
// and about 2 * (len1 + len2) comparisons in the worst case
983-
// while `extend` takes O(len2 * log_2(len1)) operations
984-
// and about 1 * len2 * log_2(len1) comparisons in the worst case,
985-
// assuming len1 >= len2.
986-
fn better_to_rebuild(len1: usize, len2: usize) -> bool {
987-
// log(1) == 0, so the inequation always falsy
988-
// log(0) is inapplicable and produces panic
989-
if len1 <= 1 {
990-
return false;
991-
}
992-
993-
2 * (len1 + len2) < len2 * log2_fast(len1)
994-
}
995-
996982
#[cfg(feature = "serde")]
997983
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
998984
mod serde {

0 commit comments

Comments
 (0)