#![deprecated(since = "0.17.0", note = "use rs-graph-derive crate instead")]
use crate::builder::{Buildable, Builder};
use crate::traits::{Directed, GraphSize, GraphType, IndexGraph, Undirected};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
pub trait AttributedGraph<'a> {
type Graph: GraphType<'a>;
type Attributes: Attributes<
Node = <Self::Graph as GraphType<'a>>::Node,
Edge = <Self::Graph as GraphType<'a>>::Edge,
>;
fn split(&'a mut self) -> (&Self::Graph, Self::Attributes);
fn attr(&'a self) -> &'a <Self::Attributes as Attributes>::GraphAttr;
fn attr_mut(&'a mut self) -> &'a mut <Self::Attributes as Attributes>::GraphAttr;
fn node(&'a self, u: <Self::Graph as GraphType<'a>>::Node) -> &'a <Self::Attributes as Attributes>::NodeAttr;
fn node_mut(
&'a mut self,
u: <Self::Graph as GraphType<'a>>::Node,
) -> &'a mut <Self::Attributes as Attributes>::NodeAttr;
fn edge(&'a self, e: <Self::Graph as GraphType<'a>>::Edge) -> &'a <Self::Attributes as Attributes>::EdgeAttr;
fn edge_mut(
&'a mut self,
e: <Self::Graph as GraphType<'a>>::Edge,
) -> &'a mut <Self::Attributes as Attributes>::EdgeAttr;
}
pub trait Attributes {
type Node: Copy + Eq;
type Edge: Copy + Eq;
type GraphAttr;
type NodeAttr;
type EdgeAttr;
fn attr(&self) -> &Self::GraphAttr;
fn attr_mut(&mut self) -> &mut Self::GraphAttr;
fn node(&self, u: Self::Node) -> &Self::NodeAttr;
fn node_mut(&mut self, u: Self::Node) -> &mut Self::NodeAttr;
fn edge(&self, e: Self::Edge) -> &Self::EdgeAttr;
fn edge_mut(&mut self, e: Self::Edge) -> &mut Self::EdgeAttr;
}
pub struct Attributed<G, Gx = (), Nx = (), Ex = ()> {
graph: G,
attrs: AttributedAttrs<Gx, Nx, Ex>,
}
#[derive(Default)]
pub struct AttributedAttrs<Gx, Nx, Ex> {
attr: Gx,
nattrs: Vec<Nx>,
eattrs: Vec<Ex>,
}
impl<Gx, Nx, Ex> AttributedAttrs<Gx, Nx, Ex>
where
Gx: Default,
Nx: Default,
Ex: Default,
{
fn with_size(n: usize, m: usize) -> Self {
AttributedAttrs {
attr: Gx::default(),
nattrs: (0..n).map(|_| Default::default()).collect(),
eattrs: (0..m).map(|_| Default::default()).collect(),
}
}
}
pub struct GraphAttrs<'a, G, Gx, Nx, Ex>
where
G: 'a + GraphType<'a>,
Gx: 'a,
Nx: 'a,
Ex: 'a,
{
graph: &'a G,
attrs: &'a mut AttributedAttrs<Gx, Nx, Ex>,
}
impl<'a, G, Gx, Nx, Ex> Attributes for GraphAttrs<'a, G, Gx, Nx, Ex>
where
G: IndexGraph<'a>,
{
type Node = G::Node;
type Edge = G::Edge;
type GraphAttr = Gx;
type NodeAttr = Nx;
type EdgeAttr = Ex;
fn attr(&self) -> &Self::GraphAttr {
&self.attrs.attr
}
fn attr_mut(&mut self) -> &mut Self::GraphAttr {
&mut self.attrs.attr
}
fn node(&self, u: Self::Node) -> &Self::NodeAttr {
&self.attrs.nattrs[self.graph.node_id(u)]
}
fn node_mut(&mut self, u: Self::Node) -> &mut Self::NodeAttr {
&mut self.attrs.nattrs[self.graph.node_id(u)]
}
fn edge(&self, e: Self::Edge) -> &Self::EdgeAttr {
&self.attrs.eattrs[self.graph.edge_id(e)]
}
fn edge_mut(&mut self, e: Self::Edge) -> &mut Self::EdgeAttr {
&mut self.attrs.eattrs[self.graph.edge_id(e)]
}
}
impl<'a, G, Gx, Nx, Ex> Deref for GraphAttrs<'a, G, Gx, Nx, Ex>
where
G: IndexGraph<'a>,
{
type Target = Gx;
fn deref(&self) -> &Gx {
&self.attrs.attr
}
}
impl<'a, G, Gx, Nx, Ex> DerefMut for GraphAttrs<'a, G, Gx, Nx, Ex>
where
G: IndexGraph<'a>,
{
fn deref_mut(&mut self) -> &mut Gx {
&mut self.attrs.attr
}
}
pub struct AttributedBuilder<B, Gx, Nx, Ex>(B, PhantomData<(Gx, Nx, Ex)>);
impl<'b, B, G, Gx, Nx, Ex> Builder for AttributedBuilder<B, Gx, Nx, Ex>
where
B: Builder<Graph = G>,
G: GraphSize<'b>,
Gx: Default,
Nx: Default,
Ex: Default,
{
type Graph = Attributed<G, Gx, Nx, Ex>;
type Node = B::Node;
type Edge = B::Edge;
fn with_capacities(nnodes: usize, nedges: usize) -> Self {
AttributedBuilder(B::with_capacities(nnodes, nedges), PhantomData)
}
fn reserve(&mut self, nnodes: usize, nedges: usize) {
self.0.reserve(nnodes, nedges)
}
fn num_nodes(&self) -> usize {
self.0.num_nodes()
}
fn num_edges(&self) -> usize {
self.0.num_edges()
}
fn add_node(&mut self) -> Self::Node {
self.0.add_node()
}
fn add_nodes(&mut self, n: usize) -> Vec<Self::Node> {
self.0.add_nodes(n)
}
fn add_edge(&mut self, u: Self::Node, v: Self::Node) -> Self::Edge {
self.0.add_edge(u, v)
}
fn node2id(&self, u: Self::Node) -> usize {
self.0.node2id(u)
}
fn edge2id(&self, e: Self::Edge) -> usize {
self.0.edge2id(e)
}
fn to_graph(self) -> Attributed<G, Gx, Nx, Ex> {
let graph = self.0.to_graph();
let attrs = AttributedAttrs::with_size(graph.num_nodes(), graph.num_edges());
Attributed { graph, attrs }
}
}
impl<'a, G, Gx, Nx, Ex> GraphType<'a> for Attributed<G, Gx, Nx, Ex>
where
G: GraphType<'a>,
Gx: Default,
Nx: Default,
Ex: Default,
{
type Node = G::Node;
type Edge = G::Edge;
}
impl<'a, G, Gx, Nx, Ex> GraphSize<'a> for Attributed<G, Gx, Nx, Ex>
where
G: GraphSize<'a>,
Gx: Default,
Nx: Default,
Ex: Default,
{
type NodeIter = G::NodeIter;
type EdgeIter = G::EdgeIter;
fn num_nodes(&self) -> usize {
self.graph.num_nodes()
}
fn num_edges(&self) -> usize {
self.graph.num_edges()
}
fn nodes(&'a self) -> Self::NodeIter {
self.graph.nodes()
}
fn edges(&'a self) -> Self::EdgeIter {
self.graph.edges()
}
}
impl<'a, G, Gx, Nx, Ex> Undirected<'a> for Attributed<G, Gx, Nx, Ex>
where
G: Undirected<'a>,
Gx: Default,
Nx: Default,
Ex: Default,
{
type NeighIter = G::NeighIter;
fn enodes(&'a self, e: Self::Edge) -> (Self::Node, Self::Node) {
self.graph.enodes(e)
}
fn neighs(&'a self, u: Self::Node) -> Self::NeighIter {
self.graph.neighs(u)
}
}
impl<'a, G, Gx, Nx, Ex> Directed<'a> for Attributed<G, Gx, Nx, Ex>
where
G: Directed<'a>,
Gx: Default,
Nx: Default,
Ex: Default,
{
type OutEdgeIter = G::OutEdgeIter;
type InEdgeIter = G::InEdgeIter;
type IncidentIter = G::IncidentIter;
type DirectedEdge = G::DirectedEdge;
fn src(&'a self, e: Self::Edge) -> Self::Node {
self.graph.src(e)
}
fn snk(&'a self, e: Self::Edge) -> Self::Node {
self.graph.snk(e)
}
fn outedges(&'a self, u: Self::Node) -> Self::OutEdgeIter {
self.graph.outedges(u)
}
fn inedges(&'a self, u: Self::Node) -> Self::InEdgeIter {
self.graph.inedges(u)
}
fn incident_edges(&'a self, u: Self::Node) -> Self::IncidentIter {
self.graph.incident_edges(u)
}
}
impl<'a, G, Gx, Nx, Ex> IndexGraph<'a> for Attributed<G, Gx, Nx, Ex>
where
G: IndexGraph<'a>,
Gx: Default,
Nx: Default,
Ex: Default,
{
fn node_id(&self, u: Self::Node) -> usize {
self.graph.node_id(u)
}
fn id2node(&'a self, id: usize) -> Self::Node {
self.graph.id2node(id)
}
fn edge_id(&self, e: Self::Edge) -> usize {
self.graph.edge_id(e)
}
fn id2edge(&'a self, id: usize) -> Self::Edge {
self.graph.id2edge(id)
}
}
impl<'a, G, Gx, Nx, Ex> AttributedGraph<'a> for Attributed<G, Gx, Nx, Ex>
where
G: 'a + IndexGraph<'a>,
Gx: 'a + Default,
Nx: 'a + Default,
Ex: 'a + Default,
{
type Graph = G;
type Attributes = GraphAttrs<'a, G, Gx, Nx, Ex>;
fn split(&'a mut self) -> (&G, Self::Attributes) {
(
&self.graph,
GraphAttrs {
graph: &self.graph,
attrs: &mut self.attrs,
},
)
}
fn attr(&self) -> &Gx {
&self.attrs.attr
}
fn attr_mut(&mut self) -> &mut Gx {
&mut self.attrs.attr
}
fn node(&self, u: G::Node) -> &Nx {
&self.attrs.nattrs[self.graph.node_id(u)]
}
fn node_mut(&mut self, u: G::Node) -> &mut Nx {
&mut self.attrs.nattrs[self.graph.node_id(u)]
}
fn edge(&self, e: G::Edge) -> &Ex {
&self.attrs.eattrs[self.graph.edge_id(e)]
}
fn edge_mut(&mut self, e: G::Edge) -> &mut Ex {
&mut self.attrs.eattrs[self.graph.edge_id(e)]
}
}
impl<'a, G, Gx, Nx, Ex> Buildable for Attributed<G, Gx, Nx, Ex>
where
G: GraphSize<'a> + Buildable,
Gx: 'a + Default,
Nx: 'a + Default,
Ex: 'a + Default,
{
type Builder = AttributedBuilder<G::Builder, Gx, Nx, Ex>;
}