1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2020-2021 Frank Fischer <frank-fischer@shadow-soft.de>
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see  <http://www.gnu.org/licenses/>
 */

///! Reference graph traits.
use super::{Directed, GraphIter, GraphSize, IndexGraph, Undirected};

/// A reference to a basic graph.
///
/// This trait contains methods with a unrestricted lifetime for `self`.
pub trait GraphSizeRef<'a>: GraphSize<'a> {
    fn nodes_iter(&self) -> Self::NodeIt;
    fn edges_iter(&self) -> Self::EdgeIt;

    fn nodes<'b>(&'b self) -> GraphIter<'b, Self, Self::NodeIt>
    where
        Self: Sized,
        'a: 'b,
    {
        GraphIter(GraphSizeRef::nodes_iter(self), self)
    }

    fn edges<'b>(&'b self) -> GraphIter<'b, Self, Self::EdgeIt>
    where
        Self: Sized,
        'a: 'b,
    {
        GraphIter(GraphSizeRef::edges_iter(self), self)
    }
}

/// A reference to an undirected graph.
///
/// This trait contains methods with a unrestricted lifetime for `self`.
pub trait UndirectedRef<'a>: Undirected<'a> + GraphSizeRef<'a> {
    fn enodes(&self, e: Self::Edge) -> (Self::Node, Self::Node);

    fn neigh_iter(&self, u: Self::Node) -> Self::NeighIt;
}

/// A reference to a digraph.
///
/// This trait contains methods with a unrestricted lifetime for `self`.
pub trait DirectedRef<'a>: Directed<'a> + UndirectedRef<'a> {
    fn src(&self, e: Self::Edge) -> Self::Node;
    fn snk(&self, e: Self::Edge) -> Self::Node;

    fn out_iter(&self, u: Self::Node) -> Self::OutIt;
    fn in_iter(&self, u: Self::Node) -> Self::InIt;
    fn incident_iter(&self, u: Self::Node) -> Self::IncidentIt;
}

/// A reference to an indexed graph.
///
/// This trait contains methods with a unrestricted lifetime for `self`.
pub trait IndexGraphRef<'a>: IndexGraph<'a> + UndirectedRef<'a> {
    fn id2node(&self, id: usize) -> Self::Node;
    fn id2edge(&self, id: usize) -> Self::Edge;
}