Refactor graph and misc
This commit is contained in:
parent
5f5fe0c149
commit
8f163269bd
22 changed files with 126 additions and 45 deletions
|
|
@ -15,6 +15,7 @@ harness = false
|
|||
|
||||
[dependencies]
|
||||
factorio-core = { path = "../factorio-core" }
|
||||
factorio-graph = { path = "../factorio-graph" }
|
||||
factorio-blueprint = { path = "../factorio-blueprint" }
|
||||
base64 = "0.22.1"
|
||||
bon = "3.0.2"
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use factorio_core::{pathfield::PathField, prelude::*, visualize::Visualize};
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::misc::Map;
|
||||
use factorio_core::misc::Map;
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct BruteforceField {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{belt_finding::brute_force::BruteforceBuilder, misc::Map};
|
||||
use factorio_blueprint::{belt::convert_to_blueprint, BlueprintEntity};
|
||||
use crate::belt_finding::brute_force::BruteforceBuilder;
|
||||
use factorio_blueprint::{BlueprintEntity, belt::convert_to_blueprint};
|
||||
use factorio_core::misc::Map;
|
||||
use factorio_core::{
|
||||
beltoptions::Beltspeed, pathfield::PathField, prelude::*, visualize::Visualize,
|
||||
};
|
||||
|
|
@ -7,7 +8,7 @@ use std::{
|
|||
ops::RangeInclusive,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tracing::{span, Level};
|
||||
use tracing::{Level, span};
|
||||
|
||||
use super::Problem;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ use crate::Map as _;
|
|||
use crate::SinglePathInput;
|
||||
use crate::SinglePathfinder;
|
||||
use crate::examples::HashMapMap;
|
||||
use crate::graph::wheighted_graph::WheightedGraph;
|
||||
use crate::graph::wheighted_graph::shortest_path::a_star;
|
||||
use crate::misc::Map;
|
||||
use crate::priority_queue::binary_heap::FastBinaryHeap;
|
||||
use factorio_core::misc::Map;
|
||||
use factorio_core::{prelude::*, visualize::Visualize};
|
||||
use factorio_graph::priority_queue::binary_heap::FastBinaryHeap;
|
||||
use factorio_graph::wheighted_graph::WheightedGraph;
|
||||
use factorio_graph::wheighted_graph::shortest_path::a_star;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::Level;
|
||||
use tracing::span;
|
||||
|
|
@ -222,7 +222,11 @@ struct MapInternal<'a> {
|
|||
impl WheightedGraph for MapInternal<'_> {
|
||||
type Node = (Position, Direction);
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
fn edge(
|
||||
&self,
|
||||
node: &(Position, Direction),
|
||||
num: usize,
|
||||
) -> Option<((Position, Direction), f64)> {
|
||||
let next = node.0.in_direction(&node.1, 1);
|
||||
next.in_range(
|
||||
&Position::new(0, 0),
|
||||
|
|
@ -276,10 +280,10 @@ impl Problem {
|
|||
};
|
||||
let p = {
|
||||
// dijkstra::<MapInternal, FastBinaryHeap<_>>(&m, self.start[i], self.end[i])
|
||||
a_star::<MapInternal, FastBinaryHeap<_>, _>(
|
||||
a_star::<MapInternal, FastBinaryHeap<_>, _, _>(
|
||||
&m,
|
||||
self.start[i],
|
||||
self.end[i],
|
||||
|&n| n == self.end[i],
|
||||
|&(p, _)| {
|
||||
1.5 * (PositionType::abs_diff(p.x, self.end[i].0.x)
|
||||
+ PositionType::abs_diff(p.y, self.end[i].0.y))
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
pub mod wheighted_graph;
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
use super::WheightedGraph;
|
||||
|
||||
pub struct WheightedAdjacencyList {
|
||||
pub(crate) nodes: Vec<usize>,
|
||||
pub(crate) edges: Vec<(usize, f64)>,
|
||||
}
|
||||
|
||||
impl WheightedGraph for WheightedAdjacencyList {
|
||||
type Node = usize;
|
||||
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
assert!(*node < self.nodes.len());
|
||||
if *node == self.nodes.len() - 1 {
|
||||
self.nodes.len() - self.nodes[0]
|
||||
} else {
|
||||
self.nodes[node + 1] - self.nodes[*node]
|
||||
}
|
||||
}
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
if num < self.num_edges(node) {
|
||||
Some(self.edges[self.nodes[*node] + num])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
pub mod adjacency_list;
|
||||
pub mod shortest_path;
|
||||
|
||||
pub trait WheightedGraph: Sized {
|
||||
type Node;
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
self.edge_iter(node).count()
|
||||
}
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)>;
|
||||
|
||||
fn edge_iter<'a, 'b>(&'a self, node: &'b Self::Node) -> WheightedGraphEdgeIter<'a, 'b, Self> {
|
||||
WheightedGraphEdgeIter::new(self, node)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WheightedGraphEdgeIter<'a, 'b, G>
|
||||
where
|
||||
G: WheightedGraph,
|
||||
{
|
||||
graph: &'a G,
|
||||
node: &'b G::Node,
|
||||
count: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'b, G> WheightedGraphEdgeIter<'a, 'b, G>
|
||||
where
|
||||
G: WheightedGraph,
|
||||
{
|
||||
pub fn new(graph: &'a G, node: &'b G::Node) -> Self {
|
||||
Self {
|
||||
graph,
|
||||
node,
|
||||
count: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<G> Iterator for WheightedGraphEdgeIter<'_, '_, G>
|
||||
where
|
||||
G: WheightedGraph,
|
||||
{
|
||||
type Item = (G::Node, f64);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.count += 1;
|
||||
self.graph.edge(self.node, self.count - 1)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,273 +0,0 @@
|
|||
use std::{collections::HashMap, fmt::Debug, hash::Hash, hash::Hasher};
|
||||
|
||||
use tracing::{field::Empty, trace_span};
|
||||
|
||||
use crate::priority_queue::PriorityQueue;
|
||||
|
||||
use super::WheightedGraph;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct QueueObject<N> {
|
||||
node: N,
|
||||
score: f64,
|
||||
}
|
||||
|
||||
impl<N> QueueObject<N> {
|
||||
fn new(node: N, score: f64) -> Self {
|
||||
Self { node, score }
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> PartialOrd for QueueObject<N> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
self.score.partial_cmp(&other.score)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> PartialEq for QueueObject<N> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.score == other.score
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> Hash for QueueObject<N>
|
||||
where
|
||||
N: Hash,
|
||||
{
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.node.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MapObject<N, H> {
|
||||
parent: N,
|
||||
score: f64,
|
||||
key: Option<H>,
|
||||
}
|
||||
|
||||
impl<N, H> MapObject<N, H> {
|
||||
fn new(parent: N, score: f64, key: H) -> Self {
|
||||
Self {
|
||||
parent,
|
||||
score,
|
||||
key: Some(key),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dijkstra<G, P>(graph: &G, start: G::Node, end: G::Node) -> Option<Vec<G::Node>>
|
||||
where
|
||||
P: PriorityQueue<QueueObject<G::Node>> + Debug,
|
||||
P::Handle: Debug,
|
||||
G::Node: Eq + Hash + Clone + Debug,
|
||||
G: WheightedGraph,
|
||||
{
|
||||
let span = trace_span!("graph", seen_nodes = Empty, visited_nodes = Empty).entered();
|
||||
if start == end {
|
||||
return Some(vec![start]);
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
let mut visited_nodes: usize = 1;
|
||||
|
||||
while let Some(o) = q.pop_min() {
|
||||
visited_nodes += 1;
|
||||
if let Some(m) = map.get_mut(&o.node) {
|
||||
m.key = None;
|
||||
}
|
||||
|
||||
if o.node == end {
|
||||
break;
|
||||
}
|
||||
|
||||
for (node, wheight) in graph.edge_iter(&o.node) {
|
||||
let score = o.score + wheight;
|
||||
if let Some(n) = map.get_mut(&node) {
|
||||
if let Some(h) = &n.key {
|
||||
if score < n.score {
|
||||
n.parent = o.node.clone();
|
||||
n.score = score;
|
||||
q.decrease_key(h, |i| i.score = score);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let h = q.insert(QueueObject::new(node.clone(), o.score + wheight));
|
||||
map.insert(node, MapObject::new(o.node.clone(), o.score + wheight, h));
|
||||
}
|
||||
}
|
||||
// dbg!(&q);
|
||||
}
|
||||
|
||||
span.record("seen_nodes", map.len());
|
||||
span.record("visited_nodes", visited_nodes);
|
||||
|
||||
map.get(&end)?;
|
||||
|
||||
let mut result = vec![end];
|
||||
|
||||
// dbg!(&map);
|
||||
|
||||
loop {
|
||||
let parent = map.get(result.last().expect("last")).expect("get");
|
||||
result.push(parent.parent.clone());
|
||||
if parent.parent == start {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result.reverse();
|
||||
|
||||
Some(result)
|
||||
}
|
||||
|
||||
struct GraphWrapper<'a, G, F> {
|
||||
g: &'a G,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<G: WheightedGraph, F: Fn(&G::Node) -> f64> WheightedGraph for GraphWrapper<'_, G, F> {
|
||||
type Node = G::Node;
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
self.g.edge(node, num).map(|(n, s)| {
|
||||
let s = s - (self.f)(node) + (self.f)(&n);
|
||||
(n, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn a_star<G, P, F>(graph: &G, start: G::Node, end: G::Node, dist: F) -> Option<Vec<G::Node>>
|
||||
where
|
||||
P: PriorityQueue<QueueObject<G::Node>> + Debug,
|
||||
P::Handle: Debug,
|
||||
G::Node: Eq + Hash + Clone + Debug,
|
||||
G: WheightedGraph,
|
||||
F: Fn(&G::Node) -> f64,
|
||||
{
|
||||
let g = GraphWrapper { g: graph, f: dist };
|
||||
|
||||
dijkstra::<GraphWrapper<G, F>, P>(&g, start, end)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use crate::{
|
||||
graph::wheighted_graph::{
|
||||
adjacency_list::WheightedAdjacencyList, shortest_path::dijkstra, WheightedGraph,
|
||||
},
|
||||
priority_queue::{binary_heap::BinaryHeap, fibonacci_heap::FibonacciHeap},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn trivial() {
|
||||
let a = WheightedAdjacencyList {
|
||||
nodes: vec![0, 1],
|
||||
edges: vec![(1, 1.0)],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, BinaryHeap<_>>(&a, 0, 1),
|
||||
Some(vec![0, 1])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple() {
|
||||
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),
|
||||
],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, BinaryHeap<_>>(&a, 0, 4),
|
||||
Some(vec![0, 1, 2, 5, 4])
|
||||
);
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, BinaryHeap<_>>(&a, 0, 6),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, FibonacciHeap<_>>(&a, 0, 4),
|
||||
Some(vec![0, 1, 2, 5, 4])
|
||||
);
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, FibonacciHeap<_>>(&a, 0, 6),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
struct Grid {
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
impl WheightedGraph for Grid {
|
||||
type Node = (usize, usize);
|
||||
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
let mut c = 0;
|
||||
|
||||
if node.0 > 0 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
if node.0 < self.width - 1 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
if node.1 > 0 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
if node.1 < self.height - 1 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
c
|
||||
}
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
dbg!(&node);
|
||||
let edges = [
|
||||
(node.0 > 0).then_some((node.0.saturating_sub(1), node.1)),
|
||||
(node.0 < self.width - 1).then_some((node.0 + 1, node.1)),
|
||||
(node.1 > 0).then_some((node.0, node.1.saturating_sub(1))),
|
||||
(node.1 < self.height - 1).then_some((node.0, node.1 + 1)),
|
||||
];
|
||||
|
||||
edges.iter().flatten().nth(num).map(|&p| (p, 1.0))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid() {
|
||||
let g = Grid {
|
||||
width: 600,
|
||||
height: 600,
|
||||
};
|
||||
|
||||
dbg!(dijkstra::<Grid, BinaryHeap<_>>(
|
||||
&g,
|
||||
(0, 0),
|
||||
(g.width - 1, g.height - 1)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,9 +2,6 @@ use factorio_core::{beltoptions::Beltspeed, pathfield::PathField, prelude::*};
|
|||
|
||||
pub mod belt_finding;
|
||||
pub mod examples;
|
||||
pub mod graph;
|
||||
pub mod misc;
|
||||
pub mod priority_queue;
|
||||
|
||||
pub struct PathInput<'c, M> {
|
||||
pub connections: &'c [Connection],
|
||||
|
|
|
|||
|
|
@ -1,52 +0,0 @@
|
|||
#[derive(Debug)]
|
||||
pub struct Arena<T> {
|
||||
data: Vec<Option<T>>,
|
||||
free: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ArenaKey(usize);
|
||||
|
||||
impl Default for ArenaKey {
|
||||
fn default() -> Self {
|
||||
ArenaKey(usize::MAX)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Arena<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
free: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, item: T) -> ArenaKey {
|
||||
if let Some(i) = self.free.pop() {
|
||||
self.data[i] = Some(item);
|
||||
ArenaKey(i)
|
||||
} else {
|
||||
self.data.push(Some(item));
|
||||
ArenaKey(self.data.len() - 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &ArenaKey) -> &T {
|
||||
self.data.get(key.0).unwrap().as_ref().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, key: &ArenaKey) -> &mut T {
|
||||
self.data.get_mut(key.0).unwrap().as_mut().unwrap()
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, key: ArenaKey) -> T {
|
||||
self.free.push(key.0);
|
||||
self.data[key.0].take().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Arena<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Map<T> {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T> Map<T>
|
||||
where
|
||||
T: Default,
|
||||
{
|
||||
pub fn new(width: usize, height: usize) -> Self {
|
||||
let mut data = Vec::new();
|
||||
for _ in 0..(width * height) {
|
||||
data.push(T::default());
|
||||
}
|
||||
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
fn index(&self, x: usize, y: usize) -> usize {
|
||||
x + y * self.width
|
||||
}
|
||||
|
||||
pub fn get(&self, x: usize, y: usize) -> &T {
|
||||
assert!(
|
||||
x < self.width,
|
||||
"assertion failed: x < self.width; x: {}, self.width: {}",
|
||||
x,
|
||||
self.width
|
||||
);
|
||||
assert!(
|
||||
y < self.height,
|
||||
"assertion failed: y < self.height; y: {}, self.height: {}",
|
||||
y,
|
||||
self.height
|
||||
);
|
||||
let i = self.index(x, y);
|
||||
self.data.get(i).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, x: usize, y: usize) -> &mut T {
|
||||
assert!(
|
||||
x < self.width,
|
||||
"assertion failed: x < self.width; x: {}, self.width: {}",
|
||||
x,
|
||||
self.width
|
||||
);
|
||||
assert!(
|
||||
y < self.height,
|
||||
"assertion failed: y < self.height; y: {}, self.height: {}",
|
||||
y,
|
||||
self.height
|
||||
);
|
||||
let i = self.index(x, y);
|
||||
self.data.get_mut(i).unwrap()
|
||||
}
|
||||
|
||||
pub fn set(&mut self, x: usize, y: usize, item: T) {
|
||||
*self.get_mut(x, y) = item;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
pub mod arena;
|
||||
pub mod map;
|
||||
|
||||
pub use arena::*;
|
||||
pub use map::*;
|
||||
|
|
@ -1,231 +0,0 @@
|
|||
use std::{collections::HashMap, fmt::Debug};
|
||||
|
||||
use super::PriorityQueue;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryHeap<Item> {
|
||||
nextfree: usize,
|
||||
data: Vec<BinaryHeapEntry<Item>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BinaryHeapEntry<Item> {
|
||||
id: usize,
|
||||
item: Item,
|
||||
}
|
||||
|
||||
impl<Item> BinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd,
|
||||
{
|
||||
fn downheap(&mut self, index: usize) {
|
||||
let left = 2 * index + 1;
|
||||
let right = 2 * index + 2;
|
||||
|
||||
if right < self.data.len() {
|
||||
let smaller = if self.data[left].item < self.data[right].item {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
};
|
||||
if self.data[index].item > self.data[smaller].item {
|
||||
self.data.swap(index, smaller);
|
||||
self.downheap(smaller);
|
||||
}
|
||||
} else if left < self.data.len() && self.data[index].item > self.data[left].item {
|
||||
self.data.swap(index, left);
|
||||
self.downheap(left);
|
||||
}
|
||||
}
|
||||
|
||||
fn upheap(&mut self, index: usize) {
|
||||
if index > 0 {
|
||||
let parent = (index - 1) / 2;
|
||||
if self.data[parent].item > self.data[index].item {
|
||||
self.data.swap(parent, index);
|
||||
self.upheap(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn search(&self, id: usize) -> Option<usize> {
|
||||
for (i, d) in self.data.iter().enumerate() {
|
||||
if d.id == id {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<Item> PriorityQueue<Item> for BinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd + Clone,
|
||||
{
|
||||
type Handle = usize;
|
||||
|
||||
fn insert(&mut self, item: Item) -> Self::Handle {
|
||||
self.data.push(BinaryHeapEntry {
|
||||
id: self.nextfree,
|
||||
item: item.clone(),
|
||||
});
|
||||
self.upheap(self.data.len() - 1);
|
||||
self.nextfree += 1;
|
||||
self.nextfree - 1
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<Item> {
|
||||
if self.data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let d = self.data.swap_remove(0);
|
||||
self.downheap(0);
|
||||
Some(d.item)
|
||||
}
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item)) {
|
||||
if let Some(index) = self.search(*handle) {
|
||||
f(&mut self.data[index].item);
|
||||
self.upheap(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
nextfree: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Trace<P> {
|
||||
inner: P,
|
||||
}
|
||||
|
||||
impl<P, I> PriorityQueue<I> for Trace<P>
|
||||
where
|
||||
I: PartialOrd + Clone + Debug,
|
||||
P: PriorityQueue<I>,
|
||||
{
|
||||
type Handle = P::Handle;
|
||||
|
||||
fn new() -> Self {
|
||||
println!("New priority queue.");
|
||||
Self { inner: P::new() }
|
||||
}
|
||||
|
||||
fn insert(&mut self, item: I) -> Self::Handle {
|
||||
println!("Insert: {item:?}");
|
||||
self.inner.insert(item)
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<I> {
|
||||
let min = self.inner.pop_min();
|
||||
println!("Pop min: {min:?}");
|
||||
min
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut I)) {
|
||||
self.inner.decrease_key(handle, |i| {
|
||||
let old_i = i.clone();
|
||||
f(i);
|
||||
println!("Decrease key: {old_i:?} -> {i:?}");
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FastBinaryHeap<Item> {
|
||||
nextfree: usize,
|
||||
data: Vec<BinaryHeapEntry<Item>>,
|
||||
map: HashMap<usize, usize>,
|
||||
}
|
||||
|
||||
impl<Item> FastBinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd,
|
||||
{
|
||||
fn downheap(&mut self, index: usize) {
|
||||
let left = 2 * index + 1;
|
||||
let right = 2 * index + 2;
|
||||
|
||||
if right < self.data.len() {
|
||||
let smaller = if self.data[left].item < self.data[right].item {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
};
|
||||
if self.data[index].item > self.data[smaller].item {
|
||||
self.data.swap(index, smaller);
|
||||
self.downheap(smaller);
|
||||
}
|
||||
} else if left < self.data.len() && self.data[index].item > self.data[left].item {
|
||||
self.data.swap(index, left);
|
||||
self.downheap(left);
|
||||
}
|
||||
self.map.insert(self.data[index].id, index);
|
||||
}
|
||||
|
||||
fn upheap(&mut self, index: usize) {
|
||||
if index > 0 {
|
||||
let parent = (index - 1) / 2;
|
||||
if self.data[parent].item > self.data[index].item {
|
||||
self.data.swap(parent, index);
|
||||
self.upheap(parent);
|
||||
}
|
||||
}
|
||||
self.map.insert(self.data[index].id, index);
|
||||
}
|
||||
|
||||
fn search(&self, id: usize) -> Option<usize> {
|
||||
self.map.get(&id).copied()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Item> PriorityQueue<Item> for FastBinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd + Clone,
|
||||
{
|
||||
type Handle = usize;
|
||||
|
||||
fn insert(&mut self, item: Item) -> Self::Handle {
|
||||
self.map.insert(self.nextfree, self.data.len());
|
||||
self.data.push(BinaryHeapEntry {
|
||||
id: self.nextfree,
|
||||
item: item.clone(),
|
||||
});
|
||||
self.upheap(self.data.len() - 1);
|
||||
self.nextfree += 1;
|
||||
self.nextfree - 1
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<Item> {
|
||||
if self.data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let d = self.data.swap_remove(0);
|
||||
self.map.remove(&d.id);
|
||||
if !self.data.is_empty() {
|
||||
self.downheap(0);
|
||||
}
|
||||
Some(d.item)
|
||||
}
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item)) {
|
||||
if let Some(index) = self.search(*handle) {
|
||||
f(&mut self.data[index].item);
|
||||
self.upheap(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
map: HashMap::new(),
|
||||
nextfree: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,278 +0,0 @@
|
|||
use std::fmt::Debug;
|
||||
|
||||
use super::PriorityQueue;
|
||||
|
||||
type Index = u32;
|
||||
|
||||
pub struct FibonacciHeap<I> {
|
||||
min: Option<Index>,
|
||||
data: Vec<Option<Node<I>>>,
|
||||
free: Vec<Index>,
|
||||
temp: [Option<Index>; 64],
|
||||
}
|
||||
|
||||
impl<I: std::fmt::Debug> std::fmt::Debug for FibonacciHeap<I> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
struct PrintData<'a, I>(&'a Vec<Option<Node<I>>>);
|
||||
impl<I: Debug> Debug for PrintData<'_, I> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_map()
|
||||
.entries(self.0.iter().enumerate().filter_map(|d| {
|
||||
if let (i, Some(c)) = d {
|
||||
Some((i, c))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
f.debug_struct("FibonacciHeap")
|
||||
.field("min", &self.min)
|
||||
.field("data", &PrintData(&self.data))
|
||||
.field("free", &self.free)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FibonacciHeapHandle(Index);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Node<I> {
|
||||
item: I,
|
||||
parent: Index,
|
||||
left: Index,
|
||||
right: Index,
|
||||
child: Index,
|
||||
mark: bool,
|
||||
rank: u16,
|
||||
}
|
||||
|
||||
impl<I> Node<I> {
|
||||
fn new(item: I) -> Self {
|
||||
Self {
|
||||
item,
|
||||
parent: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: 0,
|
||||
mark: false,
|
||||
rank: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> FibonacciHeap<I>
|
||||
where
|
||||
I: PartialOrd,
|
||||
{
|
||||
fn add_to_list(&mut self, list: Index, i: Index) {
|
||||
let right = self.get(list).right;
|
||||
let n = self.get_mut(i);
|
||||
n.right = right;
|
||||
n.left = list;
|
||||
self.get_mut(right).left = i;
|
||||
self.get_mut(list).right = i;
|
||||
}
|
||||
|
||||
fn remove_from_list(&mut self, i: Index) -> Option<Index> {
|
||||
let left = self.get(i).left;
|
||||
if left == i {
|
||||
return None;
|
||||
}
|
||||
|
||||
let right = self.get(i).right;
|
||||
|
||||
self.get_mut(left).right = right;
|
||||
self.get_mut(right).left = left;
|
||||
|
||||
Some(right)
|
||||
}
|
||||
|
||||
fn insert_tree(&mut self, i: Index) {
|
||||
if let Some(min) = self.min {
|
||||
self.add_to_list(min, i);
|
||||
|
||||
self.get_mut(i).parent = i;
|
||||
|
||||
if self.get(i).item < self.get(min).item {
|
||||
self.min = Some(i);
|
||||
}
|
||||
} else {
|
||||
let m = self.get_mut(i);
|
||||
m.parent = i;
|
||||
m.left = i;
|
||||
m.right = i;
|
||||
self.min = Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
fn union(&mut self, a: Index, b: Index) -> Index {
|
||||
let min;
|
||||
let max;
|
||||
|
||||
if self.get(a).item < self.get(b).item {
|
||||
min = a;
|
||||
max = b;
|
||||
} else {
|
||||
min = b;
|
||||
max = a;
|
||||
}
|
||||
|
||||
if self.get(min).rank == 0 {
|
||||
self.get_mut(min).child = max;
|
||||
self.get_mut(min).rank = 1;
|
||||
let m = self.get_mut(max);
|
||||
m.parent = min;
|
||||
m.left = max;
|
||||
m.right = max;
|
||||
} else {
|
||||
self.get_mut(max).parent = min;
|
||||
self.add_to_list(self.get(min).child, max);
|
||||
self.get_mut(min).rank += 1;
|
||||
}
|
||||
|
||||
min
|
||||
}
|
||||
fn insert_temp(&mut self, i: Index) {
|
||||
let rank = self.get(i).rank;
|
||||
if let Some(t) = self.temp[rank as usize].take() {
|
||||
let m = self.union(t, i);
|
||||
|
||||
self.insert_temp(m);
|
||||
} else {
|
||||
self.temp[rank as usize] = Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_data(&mut self, data: I) -> Index {
|
||||
if let Some(i) = self.free.pop() {
|
||||
self.data[i as usize] = Some(Node::new(data));
|
||||
i
|
||||
} else {
|
||||
let i = self.data.len() as Index;
|
||||
self.data.push(Some(Node::new(data)));
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&self, i: Index) -> &Node<I> {
|
||||
self.data.get(i as usize).unwrap().as_ref().unwrap()
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, i: Index) -> &mut Node<I> {
|
||||
self.data.get_mut(i as usize).unwrap().as_mut().unwrap()
|
||||
}
|
||||
|
||||
fn remove_data(&mut self, i: Index) -> I {
|
||||
let n = self.data[i as usize].take().unwrap();
|
||||
|
||||
self.free.push(i);
|
||||
|
||||
n.item
|
||||
}
|
||||
|
||||
fn cut(&mut self, i: Index) {
|
||||
if self.get(i).item < self.get(self.min.unwrap()).item {
|
||||
self.min = Some(i);
|
||||
}
|
||||
let parent = self.get(i).parent;
|
||||
if parent == i {
|
||||
return;
|
||||
}
|
||||
let c = self.remove_from_list(i);
|
||||
if self.get(parent).rank > 1 {
|
||||
self.get_mut(parent).child = c.unwrap();
|
||||
}
|
||||
self.get_mut(parent).rank -= 1;
|
||||
|
||||
self.add_to_list(self.min.unwrap(), i);
|
||||
self.get_mut(i).mark = false;
|
||||
self.get_mut(i).parent = i;
|
||||
|
||||
if self.get(parent).mark {
|
||||
self.cut(parent);
|
||||
} else {
|
||||
self.get_mut(parent).mark = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> PriorityQueue<I> for FibonacciHeap<I>
|
||||
where
|
||||
I: PartialOrd,
|
||||
{
|
||||
type Handle = FibonacciHeapHandle;
|
||||
|
||||
fn new() -> Self {
|
||||
FibonacciHeap {
|
||||
min: None,
|
||||
data: Vec::new(),
|
||||
free: Vec::new(),
|
||||
temp: [None; 64],
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, item: I) -> Self::Handle {
|
||||
let i = self.insert_data(item);
|
||||
|
||||
self.insert_tree(i);
|
||||
|
||||
FibonacciHeapHandle(i)
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<I> {
|
||||
if let Some(min) = self.min.take() {
|
||||
if self.get(min).rank != 0 {
|
||||
let mut c = self.get(min).child;
|
||||
loop {
|
||||
let t = c;
|
||||
c = self.get(c).right;
|
||||
self.add_to_list(min, t);
|
||||
self.get_mut(t).parent = t;
|
||||
self.get_mut(t).mark = false;
|
||||
if c == self.get(min).child {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(m) = self.remove_from_list(min) {
|
||||
let mut c = m;
|
||||
loop {
|
||||
let t = c;
|
||||
c = self.get(c).right;
|
||||
self.insert_temp(t);
|
||||
if c == m {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..self.temp.len() {
|
||||
if let Some(i) = self.temp[i].take() {
|
||||
if let Some(min) = self.min {
|
||||
self.add_to_list(min, i);
|
||||
if self.get(i).item < self.get(min).item {
|
||||
self.min = Some(i);
|
||||
}
|
||||
} else {
|
||||
self.get_mut(i).left = i;
|
||||
self.get_mut(i).right = i;
|
||||
self.min = Some(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(self.remove_data(min))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut I)) {
|
||||
let h = self.get_mut(handle.0);
|
||||
f(&mut h.item);
|
||||
self.cut(handle.0);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
pub mod binary_heap;
|
||||
pub mod fibonacci_heap;
|
||||
|
||||
pub trait PriorityQueue<Item>
|
||||
where
|
||||
Item: PartialOrd,
|
||||
{
|
||||
type Handle;
|
||||
fn new() -> Self;
|
||||
// fn with_capacity() -> Self {
|
||||
// Self::new()
|
||||
// }
|
||||
|
||||
fn insert(&mut self, item: Item) -> Self::Handle;
|
||||
fn pop_min(&mut self) -> Option<Item>;
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::binary_heap::{BinaryHeap, FastBinaryHeap};
|
||||
use super::fibonacci_heap::FibonacciHeap;
|
||||
use super::PriorityQueue;
|
||||
|
||||
macro_rules! test_generics {
|
||||
($($fun_mul:ident )+ ; $gen:ident $($gen_mul:ident)+) => {
|
||||
test_generics!(@internal_mod $($fun_mul )*; $gen);
|
||||
test_generics!($($fun_mul )*; $($gen_mul )*);
|
||||
};
|
||||
($($fun_mul:ident )+ ;$gen:ident) => {
|
||||
test_generics!(@internal_mod $($fun_mul )*; $gen);
|
||||
};
|
||||
(@internal_mod $($fun_mul:ident )+; $gen:ident) => {
|
||||
#[allow(non_snake_case)]
|
||||
mod $gen {
|
||||
test_generics!(@internal $($fun_mul )*; $gen);
|
||||
}
|
||||
};
|
||||
(@internal $fun:ident $($fun_mul:ident )+; $gen:ident) => {
|
||||
test_generics!(@internal $fun; $gen);
|
||||
test_generics!(@internal $($fun_mul )*; $gen);
|
||||
};
|
||||
(@internal $fun:ident; $gen:ident) => {
|
||||
|
||||
#[test]
|
||||
fn $fun() {
|
||||
super::super::$fun::<super::super::$gen<usize>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod test_macro {
|
||||
test_generics!(basic_generic decrease_key_generic; BinaryHeap FastBinaryHeap FibonacciHeap);
|
||||
}
|
||||
|
||||
fn basic_generic<T: PriorityQueue<usize>>() {
|
||||
let mut q = T::new();
|
||||
|
||||
q.insert(2);
|
||||
q.insert(3);
|
||||
q.insert(10);
|
||||
q.insert(12);
|
||||
q.insert(9);
|
||||
q.insert(6);
|
||||
q.insert(4);
|
||||
q.insert(5);
|
||||
q.insert(8);
|
||||
q.insert(7);
|
||||
q.insert(11);
|
||||
q.insert(1);
|
||||
|
||||
assert_eq!(q.pop_min(), Some(1));
|
||||
assert_eq!(q.pop_min(), Some(2));
|
||||
assert_eq!(q.pop_min(), Some(3));
|
||||
assert_eq!(q.pop_min(), Some(4));
|
||||
assert_eq!(q.pop_min(), Some(5));
|
||||
assert_eq!(q.pop_min(), Some(6));
|
||||
assert_eq!(q.pop_min(), Some(7));
|
||||
assert_eq!(q.pop_min(), Some(8));
|
||||
assert_eq!(q.pop_min(), Some(9));
|
||||
assert_eq!(q.pop_min(), Some(10));
|
||||
assert_eq!(q.pop_min(), Some(11));
|
||||
assert_eq!(q.pop_min(), Some(12));
|
||||
assert_eq!(q.pop_min(), None);
|
||||
}
|
||||
|
||||
fn decrease_key_generic<T: PriorityQueue<usize>>() {
|
||||
let mut q = T::new();
|
||||
|
||||
q.insert(4);
|
||||
let h = q.insert(5);
|
||||
|
||||
q.decrease_key(&h, |i| *i -= 3);
|
||||
|
||||
assert_eq!(q.pop_min(), Some(2));
|
||||
assert_eq!(q.pop_min(), Some(4));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue