graph: Move boilerplate `impl`s to a new `impl_type` macro

This commit is contained in:
Mikolaj Wielgus 2023-10-21 06:04:41 +00:00
parent 403e3e4f98
commit 2966df6b2c
2 changed files with 34 additions and 75 deletions

View File

@ -34,47 +34,51 @@ pub enum Weight {
Bend(BendWeight), Bend(BendWeight),
} }
#[derive(Debug, Clone, Copy, PartialEq)] macro_rules! impl_type {
pub struct DotWeight { ($weight_struct_name:ident, $weight_variant_name:ident, $index_struct_name:ident) => {
pub net: i64, impl Retag for $weight_struct_name {
pub circle: Circle,
}
impl Retag for DotWeight {
fn retag(&self, index: NodeIndex<usize>) -> Index { fn retag(&self, index: NodeIndex<usize>) -> Index {
Index::Dot(DotIndex { Index::$weight_variant_name($index_struct_name {
node_index: index, node_index: index,
marker: PhantomData, marker: PhantomData,
}) })
} }
} }
impl GetNet for DotWeight { impl GetNet for $weight_struct_name {
fn net(&self) -> i64 { fn net(&self) -> i64 {
self.net self.net
} }
} }
pub type $index_struct_name = GenericIndex<$weight_struct_name>;
impl MakePrimitive for $index_struct_name {
fn primitive<'a>(
&self,
graph: &'a StableDiGraph<Weight, Label, usize>,
) -> Primitive<'a> {
Primitive::$weight_variant_name(GenericPrimitive::new(*self, graph))
}
}
};
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DotWeight {
pub net: i64,
pub circle: Circle,
}
impl_type!(DotWeight, Dot, DotIndex);
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct SegWeight { pub struct SegWeight {
pub net: i64, pub net: i64,
pub width: f64, pub width: f64,
} }
impl Retag for SegWeight { impl_type!(SegWeight, Seg, SegIndex);
fn retag(&self, index: NodeIndex<usize>) -> Index {
Index::Seg(SegIndex {
node_index: index,
marker: PhantomData,
})
}
}
impl GetNet for SegWeight {
fn net(&self) -> i64 {
self.net
}
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct BendWeight { pub struct BendWeight {
@ -82,20 +86,7 @@ pub struct BendWeight {
pub cw: bool, pub cw: bool,
} }
impl Retag for BendWeight { impl_type!(BendWeight, Bend, BendIndex);
fn retag(&self, index: NodeIndex<usize>) -> Index {
Index::Bend(BendIndex {
node_index: index,
marker: PhantomData,
})
}
}
impl GetNet for BendWeight {
fn net(&self) -> i64 {
self.net
}
}
#[derive(Debug, EnumAsInner, Clone, Copy, PartialEq)] #[derive(Debug, EnumAsInner, Clone, Copy, PartialEq)]
pub enum Label { pub enum Label {
@ -142,27 +133,3 @@ impl<W> GetNodeIndex for GenericIndex<W> {
self.node_index self.node_index
} }
} }
pub type DotIndex = GenericIndex<DotWeight>;
impl MakePrimitive for DotIndex {
fn primitive<'a>(&self, graph: &'a StableDiGraph<Weight, Label, usize>) -> Primitive<'a> {
Primitive::Dot(GenericPrimitive::new(*self, graph))
}
}
pub type SegIndex = GenericIndex<SegWeight>;
impl MakePrimitive for SegIndex {
fn primitive<'a>(&self, graph: &'a StableDiGraph<Weight, Label, usize>) -> Primitive<'a> {
Primitive::Seg(GenericPrimitive::new(*self, graph))
}
}
pub type BendIndex = GenericIndex<BendWeight>;
impl MakePrimitive for BendIndex {
fn primitive<'a>(&self, graph: &'a StableDiGraph<Weight, Label, usize>) -> Primitive<'a> {
Primitive::Bend(GenericPrimitive::new(*self, graph))
}
}

View File

@ -22,15 +22,7 @@ pub trait GetConnectable: GetNet + GetGraph {
let this = self.net(); let this = self.net();
let other = index.primitive(self.graph()).net(); let other = index.primitive(self.graph()).net();
if this == other { (this == other) || this == -1 || other == -1
true
} else if this == -1 || other == -1 {
true
} else if this == -2 || other == -2 {
false
} else {
this == other
}
} }
} }