Initial commit

This commit is contained in:
hal8174 2023-12-06 21:25:03 +01:00
commit 7d47a10acf
18 changed files with 1545 additions and 0 deletions

170
src/belt_finding/mod.rs Normal file
View file

@ -0,0 +1,170 @@
use crate::{misc::Map, priority_queue::PriorityQueue};
#[derive(Clone, Copy)]
pub enum Direction {
Up,
Right,
Down,
Left,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Position {
pub x: usize,
pub y: usize,
}
#[derive(Default, Clone, Copy)]
pub struct Field {
pub blocked: bool,
}
#[derive(Debug, Clone)]
pub struct QueueObject {
pos: Position,
score: usize,
}
impl QueueObject {
fn new(pos: Position, score: usize) -> Self {
Self { pos, score }
}
}
impl Eq for QueueObject {}
impl Ord for QueueObject {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.score.cmp(&other.score)
}
}
impl PartialOrd for QueueObject {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.score.cmp(&other.score))
}
}
impl PartialEq for QueueObject {
fn eq(&self, other: &Self) -> bool {
self.score == other.score
}
}
struct FieldInternal<K> {
score: usize,
marked: bool,
key: Option<K>,
parent: Option<Direction>,
}
impl<K> Default for FieldInternal<K> {
fn default() -> Self {
Self {
score: usize::MAX,
marked: false,
key: None,
parent: None,
}
}
}
fn relax<P>(
internal_map: &mut Map<FieldInternal<P::Handle>>,
q: &mut P,
x: usize,
y: usize,
score: usize,
parent: Direction,
) where
P: PriorityQueue<QueueObject>,
{
let f = internal_map.get_mut(x, y);
if !f.marked {
if let Some(h) = &mut f.key {
if score < f.score {
f.score = score;
f.parent = Some(parent);
q.decrease_key(h, |o| o.score = score);
}
} else {
let key = q.insert(QueueObject::new(Position { x, y }, score));
internal_map.set(
x,
y,
FieldInternal {
score,
marked: false,
key: Some(key),
parent: Some(parent),
},
)
}
}
}
pub fn find_path<P>(map: Map<Field>, start: Position, end: Position) -> Option<Vec<Position>>
where
P: PriorityQueue<QueueObject> + std::fmt::Debug,
{
let mut q = P::new();
let mut internal_map: Map<FieldInternal<P::Handle>> = Map::new(map.width, map.height);
q.insert(QueueObject::new(start, 0));
internal_map.get_mut(start.x, start.y).score = 0;
while let Some(o) = q.pop_min() {
dbg!(&o);
if o.pos.x > 0 && !map.get(o.pos.x - 1, o.pos.y).blocked {
relax::<P>(
&mut internal_map,
&mut q,
o.pos.x - 1,
o.pos.y,
o.score + 1,
Direction::Right,
);
}
if o.pos.x < map.width - 1 && !map.get(o.pos.x + 1, o.pos.y).blocked {
relax::<P>(
&mut internal_map,
&mut q,
o.pos.x + 1,
o.pos.y,
o.score + 1,
Direction::Left,
);
}
if o.pos.y > 0 && !map.get(o.pos.x, o.pos.y - 1).blocked {
relax::<P>(
&mut internal_map,
&mut q,
o.pos.x,
o.pos.y - 1,
o.score + 1,
Direction::Down,
);
}
if o.pos.y < map.height - 1 && !map.get(o.pos.x, o.pos.y + 1).blocked {
relax::<P>(
&mut internal_map,
&mut q,
o.pos.x,
o.pos.y + 1,
o.score + 1,
Direction::Up,
);
}
// dbg!(&q);
if o.pos == end {
break;
}
internal_map.get_mut(o.pos.x, o.pos.y).marked = true;
}
None
}

36
src/blueprint/mod.rs Normal file
View file

@ -0,0 +1,36 @@
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use std::io::Cursor;
use std::io::Read;
pub mod structs;
pub use structs::*;
pub fn decode(s: &str) -> String {
let raw = s.as_bytes();
assert!(raw[0] == b'0');
let u = STANDARD.decode(&raw[1..]).unwrap();
let mut o = String::new();
flate2::bufread::ZlibDecoder::new(Cursor::new(u))
.read_to_string(&mut o)
.unwrap();
o
}
pub fn encode(s: &str) -> String {
let mut u = Vec::new();
flate2::bufread::ZlibEncoder::new(Cursor::new(s), flate2::Compression::new(9))
.read_to_end(&mut u)
.unwrap();
let mut o = String::from("0");
STANDARD.encode_string(u, &mut o);
o
}

196
src/blueprint/structs.rs Normal file
View file

@ -0,0 +1,196 @@
use std::collections::HashMap;
use serde::Deserialize;
use serde::Serialize;
#[derive(Serialize, Deserialize, Debug)]
pub enum BlueprintString {
#[serde(rename = "blueprint_book")]
BlueprintBook(BlueprintBook),
#[serde(rename = "blueprint")]
Blueprint(Blueprint),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintBook {
item: String,
label: Option<String>,
label_color: Option<BlueprintColor>,
blueprints: Vec<BlueprintBookEntry>,
active_index: i32,
version: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintBookEntry {
#[serde(flatten)]
entry: BlueprintString,
index: u32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Blueprint {
item: String,
label: Option<String>,
label_color: Option<BlueprintColor>,
#[serde(default)]
entities: Vec<BlueprintEntity>,
#[serde(default)]
tiles: Vec<BlueprintTile>,
#[serde(default)]
icons: Vec<BlueprintIcon>,
#[serde(default)]
schedules: Vec<BlueprintSchedule>,
description: Option<String>,
snap_to_grid: Option<BlueprintPosition>,
absolute_snapping: Option<bool>,
position_relative_to_grid: Option<BlueprintPosition>,
version: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintEntity {
entity_number: u64,
name: String,
position: BlueprintPosition,
direciton: Option<u8>,
connections: Option<BlueprintConnection>,
neighbours: Option<Vec<u64>>,
control_behaviour: Option<()>,
items: Option<HashMap<String, u32>>,
recipe: Option<String>,
bar: Option<u64>,
inventory: Option<BlueprintInventory>,
infinity_settings: Option<()>,
#[serde(rename = "type")]
underground_type: Option<String>,
input_priority: Option<String>,
output_priority: Option<String>,
filter: Option<String>,
filters: Option<Vec<BlueprintItemFilter>>,
filter_mode: Option<String>,
override_stack_size: Option<u8>,
drop_position: Option<BlueprintPosition>,
pickup_position: Option<BlueprintPosition>,
request_filters: Option<Vec<BlueprintLogisticFilter>>,
request_from_buffers: Option<bool>,
parameters: Option<BlueprintSpeakerParameter>,
alert_parameters: Option<BlueprintSpeakerAlertParameter>,
auto_launch: Option<bool>,
variation: Option<u8>,
color: Option<BlueprintColor>,
station: Option<String>,
manuel_trains_limit: Option<u32>,
switch_state: Option<bool>,
tags: Option<serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintInventory {
filters: Vec<BlueprintItemFilter>,
bar: u16,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintConnection {
#[serde(rename = "1")]
first: Option<BlueprintConnectionPoint>,
#[serde(rename = "2")]
second: Option<BlueprintConnectionPoint>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintConnectionPoint {
#[serde(default)]
red: Vec<BlueprintConnectionData>,
#[serde(default)]
green: Vec<BlueprintConnectionData>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintConnectionData {
entity_id: u64,
circuit_id: Option<u64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintItemFilter {
name: String,
index: u32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintLogisticFilter {
name: String,
index: u32,
count: u32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintSpeakerParameter {
playback_volume: f64,
playback_globally: bool,
allow_polyphony: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintSpeakerAlertParameter {
show_alert: bool,
show_on_map: bool,
icon_signal_id: BlueprintSignalID,
alert_message: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintSignalID {
name: String,
#[serde(rename = "type")]
signal_type: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintPosition {
x: f64,
y: f64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintIcon {
index: u32,
signal: BlueprintSignalID,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintSchedule {
schedule: Vec<BlueprintScheduleRecord>,
locomotives: Vec<u64>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintScheduleRecord {
station: String,
wait_conditions: Vec<BlueprintWaitCondition>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintWaitCondition {
#[serde(rename = "type")]
condition_type: String,
compare_type: String,
ticks: Option<u64>,
condition: Option<()>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintTile {
name: String,
position: BlueprintPosition,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlueprintColor {
r: f32,
g: f32,
b: f32,
a: f32,
}

1
src/graph/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod wheighted_graph;

View file

@ -0,0 +1,27 @@
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
}
}
}

View file

@ -0,0 +1,48 @@
pub mod adjacency_list;
pub mod shortest_path;
use clap::builder::NonEmptyStringValueParser;
pub trait WheightedGraph: Sized {
type Node;
fn num_edges(&self, node: &Self::Node) -> usize;
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<'a, 'b, G> Iterator for WheightedGraphEdgeIter<'a, 'b, 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)
}
}

View file

@ -0,0 +1,230 @@
use std::{
collections::{BinaryHeap, HashMap},
fmt::Debug,
hash::Hash,
hash::Hasher,
};
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 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));
while let Some(o) = q.pop_min() {
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);
}
if map.get(&end).is_none() {
return None;
}
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)
}
#[cfg(test)]
mod test {
use crate::{
belt_finding::QueueObject,
graph::wheighted_graph::{
adjacency_list::WheightedAdjacencyList, shortest_path::dijkstra, WheightedGraph,
},
priority_queue::{BinaryHeap, PriorityQueue},
};
#[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
);
}
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)
));
}
}

5
src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod belt_finding;
pub mod blueprint;
pub mod graph;
pub mod misc;
pub mod priority_queue;

52
src/misc/arena.rs Normal file
View file

@ -0,0 +1,52 @@
#[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()
}
}

45
src/misc/map.rs Normal file
View file

@ -0,0 +1,45 @@
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);
assert!(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);
assert!(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;
}
}

5
src/misc/mod.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod arena;
pub mod map;
pub use arena::*;
pub use map::*;

View file

@ -0,0 +1,186 @@
use crate::misc::{Arena, ArenaKey};
use super::PriorityQueue;
#[derive(Debug)]
struct FibonacciHeapObject<T> {
left: ArenaKey,
right: ArenaKey,
parent: Option<ArenaKey>,
child: Option<ArenaKey>,
rank: usize,
marked: bool,
data: T,
}
#[derive(Debug)]
pub struct FibonacciHeap<T> {
arena: Arena<FibonacciHeapObject<T>>,
min: Option<ArenaKey>,
update_rank: Vec<Option<ArenaKey>>,
}
pub struct FibonacciHeapHandle(ArenaKey);
impl<T> PriorityQueue<T> for FibonacciHeap<T>
where
T: std::cmp::Ord,
{
type Handle = FibonacciHeapHandle;
fn new() -> Self {
todo!()
}
fn insert(&mut self, item: T) -> Self::Handle {
let h = self.arena.insert(FibonacciHeapObject {
left: ArenaKey::default(),
right: ArenaKey::default(),
parent: None,
child: None,
rank: 0,
marked: false,
data: item,
});
self.insert_tree(h);
FibonacciHeapHandle(h)
}
fn pop_min(&mut self) -> Option<T> {
if let Some(k) = self.min.take() {
let o = self.arena.remove(k);
if let Some(child) = o.child {
let mut p = child;
loop {
self.arena.get_mut(&p).parent = None;
self.arena.get_mut(&p).marked = false;
let t = self.arena.get(&p).right;
self.insert_tree(p);
p = t;
if p == child {
break;
}
}
}
if o.left != o.right {
self.arena.get_mut(&o.left).right = o.right;
self.arena.get_mut(&o.right).left = o.left;
self.update_min(o.left);
}
Some(o.data)
} else {
None
}
}
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut T)) {
todo!()
}
}
impl<T> FibonacciHeap<T>
where
T: std::cmp::Ord,
{
fn insert_tree(&mut self, h: ArenaKey) {
if let Some(h_min) = self.min {
let h_last = self.arena.get(&h_min).left;
self.arena.get_mut(&h_min).left = h;
self.arena.get_mut(&h).left = h_last;
self.arena.get_mut(&h).right = h_min;
self.arena.get_mut(&h_last).right = h;
if self.arena.get(&h).data < self.arena.get(&h_min).data {
self.min = Some(h)
}
} else {
self.arena.get_mut(&h).left = h;
self.arena.get_mut(&h).right = h;
self.min = Some(h);
}
}
fn update_min(&mut self, mut h: ArenaKey) {
let h_start = h;
loop {
let next_h = self.arena.get(&h).right;
self.add_to_update_rank(h);
h = next_h;
if h == h_start {
break;
}
}
let mut prev = None;
let mut start = None;
for h in &mut self.update_rank {
if let Some(h) = h.take() {
if let Some(p) = prev {
self.arena.get_mut(&h).left = p;
self.arena.get_mut(&p).right = h;
} else {
prev = Some(h);
start = Some(h);
}
if let Some(m) = self.min {
if self.arena.get(&h).data < self.arena.get(&m).data {
self.min = Some(h);
}
} else {
self.min = Some(h);
}
}
}
}
fn add_to_update_rank(&mut self, h: ArenaKey) {
let rank = self.arena.get(&h).rank;
while self.update_rank.len() + 1 < rank {
self.update_rank.push(None)
}
if let Some(o) = self.update_rank.get_mut(rank).unwrap().take() {
if let Some(child) = self.arena.get(&h).child {
let last = self.arena.get(&child).left;
self.arena.get_mut(&child).left = h;
self.arena.get_mut(&last).right = h;
self.arena.get_mut(&o).parent = Some(h);
self.arena.get_mut(&o).marked = false;
self.arena.get_mut(&o).left = last;
self.arena.get_mut(&o).right = child;
} else {
self.arena.get_mut(&h).child = Some(o);
self.arena.get_mut(&o).parent = Some(h);
self.arena.get_mut(&o).marked = false;
self.arena.get_mut(&o).left = o;
self.arena.get_mut(&o).right = o;
}
self.arena.get_mut(&h).rank += 1;
self.add_to_update_rank(h);
} else {
*self.update_rank.get_mut(rank).unwrap() = Some(h);
}
}
}

176
src/priority_queue/mod.rs Normal file
View file

@ -0,0 +1,176 @@
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));
}
#[derive(Debug)]
pub struct BinaryHeap<Item> {
data: Vec<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] < self.data[right] {
left
} else {
right
};
if self.data[index] > self.data[smaller] {
self.data.swap(index, smaller);
self.downheap(smaller);
}
} else if left < self.data.len() && self.data[index] > self.data[left] {
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] > self.data[index] {
self.data.swap(parent, index);
self.upheap(parent);
}
}
}
fn search(&self, item: &Item) -> Option<usize> {
for (i, d) in self.data.iter().enumerate() {
if d == item {
return Some(i);
}
}
None
}
}
impl<Item> PriorityQueue<Item> for BinaryHeap<Item>
where
Item: PartialOrd + Clone,
{
type Handle = Item;
fn insert(&mut self, item: Item) -> Self::Handle {
self.data.push(item.clone());
self.upheap(self.data.len() - 1);
item
}
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)
}
}
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]);
self.upheap(index);
}
}
fn new() -> Self {
Self { data: Vec::new() }
}
}
#[cfg(test)]
mod tests {
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::$fun::<super::super::$gen<usize>>();
}
}
}
test_generics!(basic_generic decrease_key_generic; BinaryHeap);
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));
}
}