Use i64 instead of f64 for WheightedGraph

This commit is contained in:
hal8174 2025-03-26 17:22:37 +01:00
parent 068297e2c2
commit 6f74f1345e
6 changed files with 52 additions and 52 deletions

View file

@ -2,7 +2,7 @@ use super::WheightedGraph;
pub struct WheightedAdjacencyList {
pub(crate) nodes: Vec<usize>,
pub(crate) edges: Vec<(usize, f64)>,
pub(crate) edges: Vec<(usize, i64)>,
}
impl WheightedGraph for WheightedAdjacencyList {
@ -17,7 +17,7 @@ impl WheightedGraph for WheightedAdjacencyList {
}
}
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
if num < self.num_edges(node) {
Some(self.edges[self.nodes[*node] + num])
} else {

View file

@ -8,7 +8,7 @@ pub trait WheightedGraph: Sized {
self.edge_iter(node).count()
}
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)>;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)>;
fn edge_iter<'a, 'b>(&'a self, node: &'b Self::Node) -> WheightedGraphEdgeIter<'a, 'b, Self> {
WheightedGraphEdgeIter::new(self, node)
@ -41,7 +41,7 @@ impl<G> Iterator for WheightedGraphEdgeIter<'_, '_, G>
where
G: WheightedGraph,
{
type Item = (G::Node, f64);
type Item = (G::Node, i64);
fn next(&mut self) -> Option<Self::Item> {
self.count += 1;

View file

@ -9,11 +9,11 @@ use super::WheightedGraph;
#[derive(Debug, Clone, Copy)]
pub struct QueueObject<N> {
node: N,
score: f64,
score: i64,
}
impl<N> QueueObject<N> {
fn new(node: N, score: f64) -> Self {
fn new(node: N, score: i64) -> Self {
Self { node, score }
}
}
@ -42,12 +42,12 @@ where
#[derive(Debug)]
struct MapObject<N, H> {
parent: N,
score: f64,
score: i64,
key: Option<H>,
}
impl<N, H> MapObject<N, H> {
fn new(parent: N, score: f64, key: H) -> Self {
fn new(parent: N, score: i64, key: H) -> Self {
Self {
parent,
score,
@ -72,7 +72,7 @@ where
let mut map: HashMap<G::Node, MapObject<G::Node, P::Handle>> = HashMap::new();
let mut q = P::new();
q.insert(QueueObject::new(start.clone(), 0.0));
q.insert(QueueObject::new(start.clone(), 0));
let mut visited_nodes: usize = 1;
@ -137,10 +137,10 @@ struct GraphWrapper<'a, G, F> {
f: F,
}
impl<G: WheightedGraph, F: Fn(&G::Node) -> f64> WheightedGraph for GraphWrapper<'_, G, F> {
impl<G: WheightedGraph, F: Fn(&G::Node) -> i64> WheightedGraph for GraphWrapper<'_, G, F> {
type Node = G::Node;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
self.g.edge(node, num).map(|(n, s)| {
let s = s - (self.f)(node) + (self.f)(&n);
(n, s)
@ -155,7 +155,7 @@ where
G::Node: Eq + Hash + Clone + Debug,
G: WheightedGraph,
E: Fn(&'_ G::Node) -> bool,
F: Fn(&G::Node) -> f64,
F: Fn(&G::Node) -> i64,
{
let g = GraphWrapper { g: graph, f: dist };
@ -176,7 +176,7 @@ mod test {
fn trivial() {
let a = WheightedAdjacencyList {
nodes: vec![0, 1],
edges: vec![(1, 1.0)],
edges: vec![(1, 1)],
};
assert_eq!(
@ -190,17 +190,17 @@ mod test {
let a = WheightedAdjacencyList {
nodes: vec![0, 2, 3, 5, 5, 7, 10],
edges: vec![
(1, 2.0),
(4, 10.0),
(2, 3.0),
(3, 2.0),
(5, 1.0),
(0, 4.0),
(2, 5.0),
(2, 9.0),
(3, 8.0),
(4, 0.0),
(5, 7.0),
(1, 2),
(4, 10),
(2, 3),
(3, 2),
(5, 1),
(0, 4),
(2, 5),
(2, 9),
(3, 8),
(4, 0),
(5, 7),
],
};
@ -252,7 +252,7 @@ mod test {
c
}
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
dbg!(&node);
let edges = [
(node.0 > 0).then_some((node.0.saturating_sub(1), node.1)),
@ -261,7 +261,7 @@ mod test {
(node.1 < self.height - 1).then_some((node.0, node.1 + 1)),
];
edges.iter().flatten().nth(num).map(|&p| (p, 1.0))
edges.iter().flatten().nth(num).map(|&p| (p, 1))
}
}

View file

@ -20,10 +20,10 @@ where
{
type Node = Option<G::Node>;
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, i64)> {
match node {
Some(n) => self.graph.edge(n, num).map(|(n, v)| (Some(n), v)),
None => self.start_nodes.get(num).cloned().map(|n| (Some(n), 0.0)),
None => self.start_nodes.get(num).cloned().map(|n| (Some(n), 0)),
}
}
}