Skip to content

Commit 6f2ebd0

Browse files
lengyijungarro95
authored andcommitted
deduplicate Index fns
1 parent 0622e7f commit 6f2ebd0

File tree

3 files changed

+30
-48
lines changed

3 files changed

+30
-48
lines changed

src/double_priority_queue/mod.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod iterators;
3535
use std::vec::Vec;
3636

3737
use crate::core_iterators::*;
38+
use crate::store::{left, level, log2_fast, parent, right};
3839
use crate::store::{Index, Position, Store};
3940
use crate::TryReserveError;
4041
use iterators::*;
@@ -1218,33 +1219,6 @@ where
12181219
}
12191220
}
12201221

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-
12481222
// `rebuild` takes O(len1 + len2) operations
12491223
// and about 2 * (len1 + len2) comparisons in the worst case
12501224
// while `extend` takes O(len2 * log_2(len1)) operations

src/priority_queue/mod.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub mod iterators;
3636
use std::vec::Vec;
3737

3838
use crate::core_iterators::*;
39+
use crate::store::{left, log2_fast, parent, right};
3940
use crate::store::{Index, Position, Store};
4041
use crate::TryReserveError;
4142
use iterators::*;
@@ -977,27 +978,6 @@ where
977978
}
978979
}
979980

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-
1001981
// `rebuild` takes O(len1 + len2) operations
1002982
// and about 2 * (len1 + len2) comparisons in the worst case
1003983
// while `extend` takes O(len2 * log_2(len1)) operations

src/store.rs

Lines changed: 28 additions & 0 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")]

0 commit comments

Comments
 (0)