Remove petgraph
This commit is contained in:
parent
34df6c9f14
commit
778cb6748d
6 changed files with 0 additions and 166 deletions
|
|
@ -3,6 +3,5 @@ pub mod list_links;
|
|||
pub mod list_pages;
|
||||
pub mod longest_shortest_path;
|
||||
pub mod path;
|
||||
pub mod path_petgraph;
|
||||
pub mod philosophy_game;
|
||||
pub mod reexport;
|
||||
|
|
|
|||
|
|
@ -1,77 +0,0 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufReader},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use petgraph::{
|
||||
algo,
|
||||
graph::NodeIndex,
|
||||
visit::{EdgeRef, IntoNodeReferences},
|
||||
Graph,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
data::{
|
||||
info::{LinkInfo, PageInfo},
|
||||
store,
|
||||
},
|
||||
util::{self, normalize_link},
|
||||
};
|
||||
|
||||
pub fn find_index_of_title(graph: &Graph<PageInfo, LinkInfo>, title: &str) -> NodeIndex {
|
||||
let title = util::normalize_link(title);
|
||||
graph
|
||||
.node_references()
|
||||
.find(|(_, nw)| normalize_link(&nw.title) == title)
|
||||
.map(|(ni, _)| ni)
|
||||
.expect("invalid title")
|
||||
}
|
||||
|
||||
pub fn resolve_redirects(graph: &Graph<PageInfo, LinkInfo>, mut page: NodeIndex) -> NodeIndex {
|
||||
loop {
|
||||
if graph.node_weight(page).unwrap().redirect {
|
||||
if let Some(link) = graph.edges(page).next() {
|
||||
page = link.target();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return page;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn path(datafile: &Path, from: &str, to: &str) -> io::Result<()> {
|
||||
println!(">> Import");
|
||||
let mut databuf = BufReader::new(File::open(datafile)?);
|
||||
let graph = store::read_petgraph(&mut databuf)?;
|
||||
|
||||
println!(">> Locate from and to");
|
||||
let start = resolve_redirects(&graph, find_index_of_title(&graph, from));
|
||||
let goal = resolve_redirects(&graph, find_index_of_title(&graph, to));
|
||||
println!("From: {:?}", graph.node_weight(start).unwrap().title);
|
||||
println!("To: {:?}", graph.node_weight(goal).unwrap().title);
|
||||
|
||||
println!(">> Find path");
|
||||
let Some((cost, path)) = algo::astar(
|
||||
&graph,
|
||||
start,
|
||||
|n| n == goal,
|
||||
|e| !graph.node_weight(e.source()).unwrap().redirect as u32,
|
||||
|_| 0,
|
||||
) else {
|
||||
println!("No path found");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
println!("Path found (cost {cost}, length {}):", path.len());
|
||||
for page in path {
|
||||
let page = graph.node_weight(page).unwrap();
|
||||
if page.redirect {
|
||||
println!(" v {:?}", page.title);
|
||||
} else {
|
||||
println!(" - {:?}", page.title);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
use std::io::{self, Read, Write};
|
||||
|
||||
use petgraph::{graph::NodeIndex, Directed, Graph};
|
||||
|
||||
use super::{
|
||||
adjacency_list::{AdjacencyList, Link, Page},
|
||||
info::{LinkInfo, PageInfo},
|
||||
|
|
@ -134,34 +132,3 @@ pub fn read_adjacency_list<R: Read>(from: &mut R) -> io::Result<AdjacencyList<Pa
|
|||
|
||||
Ok(AdjacencyList { pages, links })
|
||||
}
|
||||
|
||||
pub fn read_petgraph<R: Read>(from: &mut R) -> io::Result<Graph<PageInfo, LinkInfo>> {
|
||||
let n_pages = read_u32(from)?;
|
||||
let n_links = read_u32(from)?;
|
||||
|
||||
let mut graph = Graph::<_, _, Directed, _>::with_capacity(n_pages as usize, n_links as usize);
|
||||
let mut page_starts = Vec::with_capacity(n_pages as usize);
|
||||
|
||||
for _ in 0..n_pages {
|
||||
let page = read_page(from)?;
|
||||
page_starts.push(page.start);
|
||||
graph.add_node(page.data);
|
||||
}
|
||||
|
||||
let mut ni = 0;
|
||||
for ei in 0..n_links {
|
||||
while ei >= page_starts.get(ni).copied().unwrap_or(u32::MAX) {
|
||||
ni += 1;
|
||||
}
|
||||
ni -= 1;
|
||||
|
||||
let link = read_link(from)?;
|
||||
graph.add_edge(
|
||||
NodeIndex::new(ni),
|
||||
NodeIndex::new(link.to as usize),
|
||||
link.data,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(graph)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,14 +35,6 @@ enum Command {
|
|||
#[arg(short, long)]
|
||||
flip: bool,
|
||||
},
|
||||
/// Find a path from one article to another.
|
||||
PathPetgraph {
|
||||
from: String,
|
||||
to: String,
|
||||
/// Flip start and end article.
|
||||
#[arg(short, long)]
|
||||
flip: bool,
|
||||
},
|
||||
/// Find the longest shortest path starting at an article.
|
||||
LongestShortestPath { from: String },
|
||||
/// Analyze articles using "Philosophy Game" rules.
|
||||
|
|
@ -82,13 +74,6 @@ fn main() -> io::Result<()> {
|
|||
commands::path::path(&args.datafile, &from, &to)
|
||||
}
|
||||
}
|
||||
Command::PathPetgraph { from, to, flip } => {
|
||||
if flip {
|
||||
commands::path_petgraph::path(&args.datafile, &to, &from)
|
||||
} else {
|
||||
commands::path_petgraph::path(&args.datafile, &from, &to)
|
||||
}
|
||||
}
|
||||
Command::LongestShortestPath { from } => {
|
||||
commands::longest_shortest_path::run(&args.datafile, &from)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue