Assign redirects a cost of 0
This commit is contained in:
parent
d1a80a6ae6
commit
32b72c10c8
1 changed files with 10 additions and 6 deletions
|
|
@ -20,13 +20,15 @@ fn find_index_of_title(pages: &[Page<PageInfo>], title: &str) -> u32 {
|
||||||
struct DijkstraPageInfo {
|
struct DijkstraPageInfo {
|
||||||
cost: u32,
|
cost: u32,
|
||||||
prev_page_idx: u32,
|
prev_page_idx: u32,
|
||||||
|
redirect: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DijkstraPageInfo {
|
impl DijkstraPageInfo {
|
||||||
fn default() -> Self {
|
fn from_page_info(info: PageInfo) -> Self {
|
||||||
Self {
|
Self {
|
||||||
cost: u32::MAX,
|
cost: u32::MAX,
|
||||||
prev_page_idx: u32::MAX,
|
prev_page_idx: u32::MAX,
|
||||||
|
redirect: info.redirect,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -39,7 +41,7 @@ impl DijkstraLinkInfo {
|
||||||
const BASE_COST: u32 = 1000;
|
const BASE_COST: u32 = 1000;
|
||||||
|
|
||||||
fn from_link_info(info: LinkInfo) -> Self {
|
fn from_link_info(info: LinkInfo) -> Self {
|
||||||
DijkstraLinkInfo {
|
Self {
|
||||||
cost: Self::BASE_COST + info.start,
|
cost: Self::BASE_COST + info.start,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +83,7 @@ fn dijkstra(
|
||||||
) -> Option<Vec<u32>> {
|
) -> Option<Vec<u32>> {
|
||||||
println!("> Prepare state");
|
println!("> Prepare state");
|
||||||
let mut data = data
|
let mut data = data
|
||||||
.change_page_data(&|_| DijkstraPageInfo::default())
|
.change_page_data(&DijkstraPageInfo::from_page_info)
|
||||||
.change_link_data(&DijkstraLinkInfo::from_link_info);
|
.change_link_data(&DijkstraLinkInfo::from_link_info);
|
||||||
let mut queue = BinaryHeap::new();
|
let mut queue = BinaryHeap::new();
|
||||||
data.page_mut(from_idx).data.cost = 0;
|
data.page_mut(from_idx).data.cost = 0;
|
||||||
|
|
@ -94,16 +96,18 @@ fn dijkstra(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if cost > data.page(page_idx).data.cost {
|
let page = data.page(page_idx);
|
||||||
|
if cost > page.data.cost {
|
||||||
// This queue entry is outdated
|
// This queue entry is outdated
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let redirect = page.data.redirect;
|
||||||
for link_idx in data.link_range(page_idx) {
|
for link_idx in data.link_range(page_idx) {
|
||||||
let link = data.link(link_idx);
|
let link = data.link(link_idx);
|
||||||
|
|
||||||
let next = Entry {
|
let next = Entry {
|
||||||
cost: cost + link.data.cost,
|
cost: cost + if redirect { 0 } else { link.data.cost },
|
||||||
page_idx: link.to,
|
page_idx: link.to,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue