bounded_graph 0.3.0

A thin newtype wrapper for `petgraph` to assist in the creation of graphs with restrictions on their edges
Documentation
use fixedbitset::FixedBitSet;
use petgraph::{
    data::{DataMap, DataMapMut},
    graph::{EdgeIndex, IndexType, NodeIndex},
    graph6::ToGraph6,
    visit::{
        Data, EdgeCount, EdgeIndexable, GraphBase, GraphProp, IntoNeighbors, IntoNeighborsDirected,
        IntoNodeIdentifiers, NodeCompactIndexable, NodeCount, NodeIndexable, Visitable,
    },
    EdgeType, Undirected,
};

use crate::{BoundedGraph, BoundedNode};

// GraphBase - defines NodeId and EdgeId associated types
impl<N, E, Ix, G> GraphBase for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>,
{
    type NodeId = NodeIndex<Ix>;
    type EdgeId = EdgeIndex<Ix>;
}

// Data - defines NodeWeight and EdgeWeight
impl<N, E, Ix, G> Data for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>
        + Data<NodeWeight = N, EdgeWeight = E>,
{
    type NodeWeight = N;
    type EdgeWeight = E;
}

// GraphProp - defines EdgeType and is_directed
impl<N, E, Ty, Ix, G> GraphProp for BoundedGraph<N, E, G>
where
    Ty: EdgeType,
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + GraphProp<EdgeType = Ty>,
{
    fn is_directed(&self) -> bool {
        Ty::is_directed()
    }

    type EdgeType = Ty;
}

// DataMap - read-only access to node/edge weights
impl<N, E, Ix, G> DataMap for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>
        + Data<NodeWeight = N, EdgeWeight = E>
        + DataMap,
{
    fn node_weight(&self, node: NodeIndex<Ix>) -> Option<&N> {
        self.graph.node_weight(node)
    }

    fn edge_weight(&self, edge: EdgeIndex<Ix>) -> Option<&E> {
        self.graph.edge_weight(edge)
    }
}

// DataMapMut - mutable access (only for nodes with immutable bounds)
impl<N, E, Ix, G> DataMapMut for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    N: BoundedNode<Ix> + crate::ImmutableEdgeBounds,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>
        + Data<NodeWeight = N, EdgeWeight = E>
        + DataMapMut,
{
    fn node_weight_mut(&mut self, node: NodeIndex<Ix>) -> Option<&mut N> {
        self.graph.node_weight_mut(node)
    }

    fn edge_weight_mut(&mut self, edge: EdgeIndex<Ix>) -> Option<&mut E> {
        self.graph.edge_weight_mut(edge)
    }
}

// EdgeCount
impl<N, E, Ix, G> EdgeCount for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + EdgeCount,
{
    fn edge_count(&self) -> usize {
        self.graph.edge_count()
    }
}

// EdgeIndexable
impl<N, E, Ix, G> EdgeIndexable for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + EdgeIndexable,
{
    fn edge_bound(&self) -> usize {
        self.graph.edge_bound()
    }

    fn to_index(&self, a: Self::EdgeId) -> usize {
        self.graph.to_index(a)
    }

    fn from_index(&self, i: usize) -> Self::EdgeId {
        self.graph.from_index(i)
    }
}

// NodeIndexable
impl<N, E, Ix, G> NodeIndexable for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + NodeIndexable,
{
    fn node_bound(&self) -> usize {
        self.graph.node_bound()
    }

    fn to_index(&self, a: Self::NodeId) -> usize {
        self.graph.to_index(a)
    }

    fn from_index(&self, i: usize) -> Self::NodeId {
        self.graph.from_index(i)
    }
}

// IntoNeighbors
impl<'a, N, E, Ix, G> IntoNeighbors for &'a BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>,
    &'a G: IntoNeighbors<NodeId = NodeIndex<Ix>>,
{
    type Neighbors = <&'a G as IntoNeighbors>::Neighbors;

    fn neighbors(self, a: Self::NodeId) -> Self::Neighbors {
        self.graph.neighbors(a)
    }
}

// IntoNeighborsDirected
impl<'a, N, E, Ix, G> IntoNeighborsDirected for &'a BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>,
    &'a G: IntoNeighborsDirected<NodeId = NodeIndex<Ix>>,
{
    type NeighborsDirected = <&'a G as IntoNeighborsDirected>::NeighborsDirected;

    fn neighbors_directed(
        self,
        a: Self::NodeId,
        dir: petgraph::Direction,
    ) -> Self::NeighborsDirected {
        self.graph.neighbors_directed(a, dir)
    }
}

// IntoNodeIdentifiers
impl<'a, N, E, Ix, G> IntoNodeIdentifiers for &'a BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>,
    &'a G: IntoNodeIdentifiers<NodeId = NodeIndex<Ix>>,
{
    type NodeIdentifiers = <&'a G as IntoNodeIdentifiers>::NodeIdentifiers;

    fn node_identifiers(self) -> Self::NodeIdentifiers {
        self.graph.node_identifiers()
    }
}

// NodeCount
impl<N, E, Ix, G> NodeCount for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + NodeCount,
{
    fn node_count(&self) -> usize {
        self.graph.node_count()
    }
}

// ToGraph6 (for undirected graphs)
impl<N, E, Ix, G> ToGraph6 for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>>
        + GraphProp<EdgeType = Undirected>
        + ToGraph6,
{
    fn graph6_string(&self) -> String {
        self.graph.graph6_string()
    }
}

// Visitable
impl<N, E, Ix, G> Visitable for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + Visitable<Map = FixedBitSet>,
{
    type Map = FixedBitSet;

    fn visit_map(&self) -> Self::Map {
        self.graph.visit_map()
    }

    fn reset_map(&self, map: &mut Self::Map) {
        self.graph.reset_map(map)
    }
}

// NodeCompactIndexable
impl<N, E, Ix, G> NodeCompactIndexable for BoundedGraph<N, E, G>
where
    Ix: IndexType,
    G: GraphBase<NodeId = NodeIndex<Ix>, EdgeId = EdgeIndex<Ix>> + NodeCompactIndexable,
{
}