use fixedbitset::FixedBitSet;
use std::collections::{
HashSet,
VecDeque,
};
use std::hash::Hash;
use super::{
graphmap,
graph,
EdgeType,
EdgeDirection,
Graph,
GraphMap,
Incoming,
Outgoing,
};
use graph::{
IndexType,
};
#[cfg(feature = "stable_graph")]
use graph::stable::StableGraph;
pub trait Graphlike {
type NodeId: Clone;
}
pub trait NeighborIter<'a> : Graphlike {
type Iter: Iterator<Item=Self::NodeId>;
fn neighbors(&'a self, n: Self::NodeId) -> Self::Iter;
}
impl<'a, N, E: 'a, Ty, Ix> NeighborIter<'a> for Graph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
{
type Iter = graph::Neighbors<'a, E, Ix>;
fn neighbors(&'a self, n: graph::NodeIndex<Ix>) -> graph::Neighbors<'a, E, Ix>
{
Graph::neighbors(self, n)
}
}
#[cfg(feature = "stable_graph")]
impl<'a, N, E: 'a, Ty, Ix> NeighborIter<'a> for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
{
type Iter = graph::stable::Neighbors<'a, E, Ix>;
fn neighbors(&'a self, n: graph::NodeIndex<Ix>)
-> graph::stable::Neighbors<'a, E, Ix>
{
StableGraph::neighbors(self, n)
}
}
impl<'a, N: 'a, E> NeighborIter<'a> for GraphMap<N, E>
where N: Copy + Ord + Hash
{
type Iter = graphmap::Neighbors<'a, N>;
fn neighbors(&'a self, n: N) -> graphmap::Neighbors<'a, N>
{
GraphMap::neighbors(self, n)
}
}
pub struct AsUndirected<G>(pub G);
pub struct Reversed<G>(pub G);
impl<'a, 'b, N, E: 'a, Ty, Ix> NeighborIter<'a> for AsUndirected<&'b Graph<N, E, Ty, Ix>> where
Ty: EdgeType,
Ix: IndexType,
{
type Iter = graph::Neighbors<'a, E, Ix>;
fn neighbors(&'a self, n: graph::NodeIndex<Ix>) -> graph::Neighbors<'a, E, Ix>
{
Graph::neighbors_undirected(self.0, n)
}
}
impl<'a, 'b, N, E: 'a, Ty, Ix> NeighborIter<'a> for Reversed<&'b Graph<N, E, Ty, Ix>> where
Ty: EdgeType,
Ix: IndexType,
{
type Iter = graph::Neighbors<'a, E, Ix>;
fn neighbors(&'a self, n: graph::NodeIndex<Ix>) -> graph::Neighbors<'a, E, Ix>
{
Graph::neighbors_directed(self.0, n, EdgeDirection::Incoming)
}
}
pub trait NeighborsDirected<'a> : Graphlike {
type NeighborsDirected: Iterator<Item=Self::NodeId>;
fn neighbors_directed(&'a self, n: Self::NodeId,
d: EdgeDirection) -> Self::NeighborsDirected;
}
impl<'a, N, E: 'a, Ty, Ix> NeighborsDirected<'a> for Graph<N, E, Ty, Ix>
where Ty: EdgeType,
Ix: IndexType,
{
type NeighborsDirected = graph::Neighbors<'a, E, Ix>;
fn neighbors_directed(&'a self, n: graph::NodeIndex<Ix>,
d: EdgeDirection) -> graph::Neighbors<'a, E, Ix>
{
Graph::neighbors_directed(self, n, d)
}
}
#[cfg(feature = "stable_graph")]
impl<'a, N, E: 'a, Ty, Ix> NeighborsDirected<'a> for StableGraph<N, E, Ty, Ix>
where Ty: EdgeType,
Ix: IndexType,
{
type NeighborsDirected = graph::stable::Neighbors<'a, E, Ix>;
fn neighbors_directed(&'a self, n: graph::NodeIndex<Ix>, d: EdgeDirection)
-> graph::stable::Neighbors<'a, E, Ix>
{
StableGraph::neighbors_directed(self, n, d)
}
}
impl<'a, 'b, G> NeighborsDirected<'a> for Reversed<&'b G>
where G: NeighborsDirected<'a>,
{
type NeighborsDirected = <G as NeighborsDirected<'a>>::NeighborsDirected;
fn neighbors_directed(&'a self, n: G::NodeId,
d: EdgeDirection) -> Self::NeighborsDirected
{
self.0.neighbors_directed(n, d.opposite())
}
}
pub trait Externals<'a> : Graphlike {
type Externals: Iterator<Item=Self::NodeId>;
fn externals(&'a self, d: EdgeDirection) -> Self::Externals;
}
impl<'a, N: 'a, E, Ty, Ix> Externals<'a> for Graph<N, E, Ty, Ix>
where Ty: EdgeType,
Ix: IndexType,
{
type Externals = graph::Externals<'a, N, Ty, Ix>;
fn externals(&'a self, d: EdgeDirection) -> graph::Externals<'a, N, Ty, Ix> {
Graph::externals(self, d)
}
}
impl<'a, 'b, G> Externals<'a> for Reversed<&'b G>
where G: Externals<'a>,
{
type Externals = <G as Externals<'a>>::Externals;
fn externals(&'a self, d: EdgeDirection) -> Self::Externals {
self.0.externals(d.opposite())
}
}
pub trait VisitMap<N> {
fn visit(&mut self, N) -> bool;
fn is_visited(&self, &N) -> bool;
}
impl<Ix> VisitMap<graph::NodeIndex<Ix>> for FixedBitSet where
Ix: IndexType,
{
fn visit(&mut self, x: graph::NodeIndex<Ix>) -> bool {
let present = self.contains(x.index());
self.insert(x.index());
!present
}
fn is_visited(&self, x: &graph::NodeIndex<Ix>) -> bool {
self.contains(x.index())
}
}
impl<Ix> VisitMap<graph::EdgeIndex<Ix>> for FixedBitSet where
Ix: IndexType,
{
fn visit(&mut self, x: graph::EdgeIndex<Ix>) -> bool {
let present = self.contains(x.index());
self.insert(x.index());
!present
}
fn is_visited(&self, x: &graph::EdgeIndex<Ix>) -> bool {
self.contains(x.index())
}
}
impl<N: Eq + Hash> VisitMap<N> for HashSet<N> {
fn visit(&mut self, x: N) -> bool {
self.insert(x)
}
fn is_visited(&self, x: &N) -> bool {
self.contains(x)
}
}
pub trait Visitable : Graphlike {
type Map: VisitMap<Self::NodeId>;
fn visit_map(&self) -> Self::Map;
}
pub trait Revisitable : Visitable {
fn reset_map(&self, &mut Self::Map);
}
impl<N, E, Ty, Ix> Graphlike for Graph<N, E, Ty, Ix> where
Ix: IndexType,
{
type NodeId = graph::NodeIndex<Ix>;
}
impl<N, E, Ty, Ix> Visitable for Graph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
{
type Map = FixedBitSet;
fn visit_map(&self) -> FixedBitSet { FixedBitSet::with_capacity(self.node_count()) }
}
impl<N, E, Ty, Ix> Revisitable for Graph<N, E, Ty, Ix>
where Ty: EdgeType,
Ix: IndexType,
{
fn reset_map(&self, map: &mut Self::Map) {
map.clear();
map.grow(self.node_count());
}
}
#[cfg(feature = "stable_graph")]
impl<N, E, Ty, Ix> Graphlike for StableGraph<N, E, Ty, Ix> where
Ix: IndexType,
{
type NodeId = graph::NodeIndex<Ix>;
}
#[cfg(feature = "stable_graph")]
impl<N, E, Ty, Ix> Visitable for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
{
type Map = FixedBitSet;
fn visit_map(&self) -> FixedBitSet { FixedBitSet::with_capacity(self.node_count()) }
}
#[cfg(feature = "stable_graph")]
impl<N, E, Ty, Ix> Revisitable for StableGraph<N, E, Ty, Ix>
where Ty: EdgeType,
Ix: IndexType,
{
fn reset_map(&self, map: &mut Self::Map) {
map.clear();
map.grow(self.node_count());
}
}
impl<'a, G> Revisitable for Reversed<&'a G>
where G: Revisitable
{
fn reset_map(&self, map: &mut Self::Map) {
self.0.reset_map(map);
}
}
impl<N: Clone, E> Graphlike for GraphMap<N, E>
{
type NodeId = N;
}
impl<N, E> Visitable for GraphMap<N, E>
where N: Copy + Ord + Hash
{
type Map = HashSet<N>;
fn visit_map(&self) -> HashSet<N> { HashSet::with_capacity(self.node_count()) }
}
impl<N, E> Revisitable for GraphMap<N, E>
where N: Copy + Ord + Hash
{
fn reset_map(&self, map: &mut Self::Map) {
map.clear();
}
}
impl<'a, G: Graphlike> Graphlike for AsUndirected<&'a G>
{
type NodeId = G::NodeId;
}
impl<'a, G: Graphlike> Graphlike for Reversed<&'a G>
{
type NodeId = G::NodeId;
}
impl<'a, G: Visitable> Visitable for AsUndirected<&'a G>
{
type Map = G::Map;
fn visit_map(&self) -> G::Map {
self.0.visit_map()
}
}
impl<'a, G: Visitable> Visitable for Reversed<&'a G>
{
type Map = G::Map;
fn visit_map(&self) -> G::Map {
self.0.visit_map()
}
}
pub trait GetAdjacencyMatrix : Graphlike {
type AdjMatrix;
fn adjacency_matrix(&self) -> Self::AdjMatrix;
fn is_adjacent(&self, matrix: &Self::AdjMatrix, a: Self::NodeId, b: Self::NodeId) -> bool;
}
impl<N, E> GetAdjacencyMatrix for GraphMap<N, E>
where N: Copy + Ord + Hash
{
type AdjMatrix = ();
#[inline]
fn adjacency_matrix(&self) { }
#[inline]
fn is_adjacent(&self, _: &(), a: N, b: N) -> bool {
self.contains_edge(a, b)
}
}
#[derive(Clone, Debug)]
pub struct Dfs<N, VM> {
pub stack: Vec<N>,
pub discovered: VM,
}
impl<N, VM> Dfs<N, VM>
where N: Clone,
VM: VisitMap<N>,
{
pub fn new<G>(graph: &G, start: N) -> Self
where G: Visitable<NodeId=N, Map=VM>
{
let mut dfs = Dfs::empty(graph);
dfs.move_to(start);
dfs
}
pub fn empty<G>(graph: &G) -> Self
where G: Visitable<NodeId=N, Map=VM>
{
Dfs {
stack: Vec::new(),
discovered: graph.visit_map(),
}
}
pub fn move_to(&mut self, start: N)
{
self.discovered.visit(start.clone());
self.stack.clear();
self.stack.push(start);
}
pub fn next<'a, G>(&mut self, graph: &'a G) -> Option<N> where
G: Graphlike<NodeId=N>,
G: NeighborIter<'a>,
{
while let Some(node) = self.stack.pop() {
for succ in graph.neighbors(node.clone()) {
if self.discovered.visit(succ.clone()) {
self.stack.push(succ);
}
}
return Some(node);
}
None
}
}
pub struct DfsIter<'a, G: 'a + Visitable>
{
graph: &'a G,
dfs: Dfs<G::NodeId, G::Map>,
}
impl<'a, G: Visitable> DfsIter<'a, G>
{
pub fn new(graph: &'a G, start: G::NodeId) -> Self
{
let mut dfs = Dfs::empty(graph);
dfs.move_to(start);
DfsIter {
graph: graph,
dfs: dfs,
}
}
pub fn move_to(&mut self, start: G::NodeId)
{
self.dfs.move_to(start)
}
}
impl<'a, G: 'a + Visitable> Iterator for DfsIter<'a, G> where
G: NeighborIter<'a>,
{
type Item = G::NodeId;
#[inline]
fn next(&mut self) -> Option<G::NodeId>
{
self.dfs.next(self.graph)
}
fn size_hint(&self) -> (usize, Option<usize>)
{
(self.dfs.stack.len(), None)
}
}
impl<'a, G: Visitable> Clone for DfsIter<'a, G> where Dfs<G::NodeId, G::Map>: Clone
{
fn clone(&self) -> Self {
DfsIter {
graph: self.graph,
dfs: self.dfs.clone(),
}
}
}
#[derive(Clone)]
pub struct Bfs<N, VM> {
pub stack: VecDeque<N>,
pub discovered: VM,
}
impl<N, VM> Bfs<N, VM>
where N: Clone,
VM: VisitMap<N>,
{
pub fn new<G>(graph: &G, start: N) -> Self
where G: Visitable<NodeId=N, Map=VM>
{
let mut discovered = graph.visit_map();
discovered.visit(start.clone());
let mut stack = VecDeque::new();
stack.push_front(start.clone());
Bfs {
stack: stack,
discovered: discovered,
}
}
pub fn next<'a, G>(&mut self, graph: &'a G) -> Option<N> where
G: Graphlike<NodeId=N>,
G: NeighborIter<'a>,
{
while let Some(node) = self.stack.pop_front() {
for succ in graph.neighbors(node.clone()) {
if self.discovered.visit(succ.clone()) {
self.stack.push_back(succ);
}
}
return Some(node);
}
None
}
}
pub struct BfsIter<'a, G: 'a + Visitable> {
graph: &'a G,
bfs: Bfs<G::NodeId, G::Map>,
}
impl<'a, G: Visitable> BfsIter<'a, G> where
G::NodeId: Clone,
{
pub fn new(graph: &'a G, start: G::NodeId) -> Self
{
let mut discovered = graph.visit_map();
discovered.visit(start.clone());
let mut stack = VecDeque::new();
stack.push_front(start.clone());
let bfs = Bfs {
stack: stack,
discovered: discovered,
};
BfsIter {
graph: graph,
bfs: bfs,
}
}
}
impl<'a, G: 'a + Visitable> Iterator for BfsIter<'a, G> where
G::NodeId: Clone,
G: NeighborIter<'a>,
{
type Item = G::NodeId;
fn next(&mut self) -> Option<G::NodeId>
{
self.bfs.next(self.graph)
}
fn size_hint(&self) -> (usize, Option<usize>)
{
(self.bfs.stack.len(), None)
}
}
impl<'a, G: Visitable> Clone for BfsIter<'a, G> where Bfs<G::NodeId, G::Map>: Clone
{
fn clone(&self) -> Self {
BfsIter {
graph: self.graph,
bfs: self.bfs.clone(),
}
}
}
#[derive(Clone)]
pub struct Topo<N, VM> {
tovisit: Vec<N>,
ordered: VM,
}
impl<N, VM> Topo<N, VM>
where N: Clone,
VM: VisitMap<N>,
{
pub fn new<'a, G>(graph: &'a G) -> Self
where G: Externals<'a> + Visitable<NodeId=N, Map=VM>,
{
let mut topo = Self::empty(graph);
topo.tovisit.extend(graph.externals(Incoming));
topo
}
fn empty<G>(graph: &G) -> Self
where G: Visitable<NodeId=N, Map=VM>
{
Topo {
ordered: graph.visit_map(),
tovisit: Vec::new(),
}
}
pub fn reset<'a, G>(&mut self, graph: &'a G)
where G: Externals<'a> + Revisitable<NodeId=N, Map=VM>,
{
graph.reset_map(&mut self.ordered);
self.tovisit.clear();
self.tovisit.extend(graph.externals(Incoming));
}
pub fn next<'a, G>(&mut self, g: &'a G) -> Option<N>
where G: NeighborsDirected<'a> + Visitable<NodeId=N, Map=VM>,
{
while let Some(nix) = self.tovisit.pop() {
if self.ordered.is_visited(&nix) {
continue;
}
self.ordered.visit(nix.clone());
for neigh in g.neighbors_directed(nix.clone(), Outgoing) {
if g.neighbors_directed(neigh.clone(), Incoming).all(|b| self.ordered.is_visited(&b)) {
self.tovisit.push(neigh);
}
}
return Some(nix);
}
None
}
}
#[derive(Clone)]
pub struct SubTopo<N, VM> {
tovisit: Vec<N>,
notvisited: VecDeque<N>,
ordered: VM,
}
impl<N, VM> SubTopo<N, VM>
where N: Clone,
VM: VisitMap<N>,
{
pub fn from_node<'a, G>(graph: &'a G, node: N) -> Self
where G: Externals<'a> + Visitable<NodeId=N, Map=VM>,
{
let mut topo = Self::empty(graph);
topo.tovisit.push(node);
topo
}
fn empty<G>(graph: &G) -> Self
where G: Visitable<NodeId=N, Map=VM>
{
SubTopo {
ordered: graph.visit_map(),
tovisit: Vec::new(),
notvisited: VecDeque::new(),
}
}
pub fn reset_with_node<'a, G>(&mut self, graph: &'a G, node: N)
where G: Revisitable<NodeId=N, Map=VM>,
{
graph.reset_map(&mut self.ordered);
self.tovisit.clear();
self.tovisit.push(node);
}
pub fn next<'a, G>(&mut self, g: &'a G) -> Option<N>
where G: NeighborsDirected<'a> + Visitable<NodeId=N, Map=VM>,
{
loop {
while let Some(nix) = self.tovisit.pop() {
if self.ordered.is_visited(&nix) {
continue;
}
self.ordered.visit(nix.clone());
for neigh in g.neighbors_directed(nix.clone(), Outgoing) {
if g.neighbors_directed(neigh.clone(), Incoming).all(|b| self.ordered.is_visited(&b)) {
self.tovisit.push(neigh);
} else {
self.notvisited.push_back(neigh);
}
}
return Some(nix);
}
if let Some(nix) = self.notvisited.pop_front() {
self.tovisit.push(nix);
} else {
return None;
}
}
}
}