Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions generic_a_star/src/closed_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ pub trait AStarClosedList<Node: AStarNode>: Reset {
self.get(identifier).is_some()
}

/// Returns an iterator over the nodes in the closed list.
fn iter<'this: 'node, 'node>(
&'this self,
) -> impl use<'this, 'node, Self, Node> + Iterator<Item = &'node Node>
where
Node: 'node;

fn can_skip_node(&self, node: &Node, is_label_setting: bool) -> bool {
if let Some(previous_visit) = self.get(node.identifier()) {
if is_label_setting {
Expand Down Expand Up @@ -99,6 +106,15 @@ impl<Node: AStarNode> AStarClosedList<Node>
fn get(&self, identifier: &<Node as AStarNode>::Identifier) -> Option<&Node> {
Self::get(self, identifier)
}

fn iter<'this: 'node, 'node>(
&'this self,
) -> impl use<'this, 'node, Node> + Iterator<Item = &'node Node>
where
Node: 'node,
{
Self::values(self)
}
}

impl<Identifier, Node> Reset for FxHashMapSeed<Identifier, Node> {
Expand Down
19 changes: 16 additions & 3 deletions generic_a_star/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::{
str::FromStr,
};

use num_traits::{Bounded, CheckedAdd, CheckedSub, SaturatingSub, Zero};
use num_traits::{Bounded, CheckedAdd, CheckedSub, SaturatingAdd, SaturatingSub, Zero};

/// The cost of an A* node.
pub trait AStarCost:
From<u8>
+ Add<Output = Self>
+ Sub<Output = Self>
+ SaturatingAdd
+ SaturatingSub
+ CheckedAdd
+ CheckedSub
Expand Down Expand Up @@ -93,15 +94,21 @@ macro_rules! primitive_cost {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self(self.0.checked_add(rhs.0).unwrap())
Self(self.0.checked_add(rhs.0).unwrap_or_else(|| panic!("Overflow when adding costs {self} + {rhs}")))
}
}

impl std::ops::Sub for $name {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
Self(self.0.checked_sub(rhs.0).unwrap())
Self(self.0.checked_sub(rhs.0).unwrap_or_else(|| panic!("Overflow when subtracting costs {self} - {rhs}")))
}
}

impl num_traits::SaturatingAdd for $name {
fn saturating_add(&self, rhs: &Self) -> Self {
Self(self.0.saturating_add(rhs.0))
}
}

Expand Down Expand Up @@ -309,6 +316,12 @@ impl<A: CheckedSub, B: CheckedSub> CheckedSub for OrderedPairCost<A, B> {
}
}

impl<A: SaturatingAdd, B: SaturatingAdd> SaturatingAdd for OrderedPairCost<A, B> {
fn saturating_add(&self, rhs: &Self) -> Self {
Self(self.0.saturating_add(&rhs.0), self.1.saturating_add(&rhs.1))
}
}

impl<A: SaturatingSub, B: SaturatingSub> SaturatingSub for OrderedPairCost<A, B> {
fn saturating_sub(&self, rhs: &Self) -> Self {
Self(self.0.saturating_sub(&rhs.0), self.1.saturating_sub(&rhs.1))
Expand Down
10 changes: 9 additions & 1 deletion generic_a_star/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ impl<
self.closed_list.get(node_identifier)
}

pub fn iter_closed_nodes(&self) -> impl Iterator<Item = &Context::Node> {
self.closed_list.iter()
}

pub fn performance_counters(&self) -> &AStarPerformanceCounters {
&self.performance_counters
}
Expand Down Expand Up @@ -418,7 +422,11 @@ impl<

let previous_visit = self.closed_list.insert(node.identifier().clone(), node);
self.performance_counters.closed_nodes += 1;
debug_assert!(previous_visit.is_none() || !self.context.is_label_setting());
debug_assert!(
previous_visit.is_none() || !self.context.is_label_setting(),
"Node was visited previously: {}",
previous_visit.unwrap()
);
}

let Some(target_identifier) = target_identifier else {
Expand Down
12 changes: 11 additions & 1 deletion lib_ts_chainalign/src/alignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Representation of an alignment.

use std::fmt::Display;
use std::{fmt::Display, iter};

use crate::alignment::ts_kind::TsKind;

Expand Down Expand Up @@ -32,6 +32,16 @@ pub struct Alignment {
pub alignment: Vec<(usize, AlignmentType)>,
}

impl Alignment {
pub fn iter_unpacked(&self) -> impl Iterator<Item = AlignmentType> {
self.alignment
.iter()
.flat_map(|(multiplicity, alignment_type)| {
iter::repeat_n(*alignment_type, *multiplicity)
})
}
}

impl Display for GapType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
Loading
Loading