graaf/op/
is_oriented.rs

1//! Check whether a digraph is oriented.
2//!
3//! An oriented digraph has no cycle of length 2.
4//!
5//! # Examples
6//!
7//! ```
8//! use graaf::{
9//!     AdjacencyList,
10//!     Circuit,
11//!     IsOriented,
12//! };
13//!
14//! assert!(!AdjacencyList::circuit(2).is_oriented());
15//! assert!(AdjacencyList::circuit(3).is_oriented());
16//! ```
17
18use crate::{
19    Arcs,
20    HasArc,
21};
22
23/// Check whether a digraph is oriented.
24pub trait IsOriented {
25    /// Check whether the digraph is oriented.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use graaf::{
31    ///     AdjacencyList,
32    ///     Circuit,
33    ///     IsOriented,
34    /// };
35    ///
36    /// assert!(!AdjacencyList::circuit(2).is_oriented());
37    /// assert!(AdjacencyList::circuit(3).is_oriented());
38    /// ```
39    #[must_use]
40    fn is_oriented(&self) -> bool;
41}
42
43impl<D> IsOriented for D
44where
45    D: Arcs + HasArc,
46{
47    fn is_oriented(&self) -> bool {
48        self.arcs().all(|(u, v)| !self.has_arc(v, u))
49    }
50}
51
52/// `IsOriented` tests
53#[macro_export]
54macro_rules! test_is_oriented {
55    ($fixture:path) => {
56        use $fixture::{
57            bang_jensen_196,
58            bang_jensen_34,
59            bang_jensen_94,
60            kattis_builddeps,
61            kattis_cantinaofbabel_1,
62            kattis_cantinaofbabel_2,
63            kattis_escapewallmaria_1,
64            kattis_escapewallmaria_2,
65            kattis_escapewallmaria_3,
66        };
67
68        #[test]
69        fn is_oriented_bang_jensen_196() {
70            assert!(!bang_jensen_196().is_oriented());
71        }
72
73        #[test]
74        fn is_oriented_bang_jensen_34() {
75            assert!(bang_jensen_34().is_oriented());
76        }
77
78        #[test]
79        fn is_oriented_bang_jensen_94() {
80            assert!(bang_jensen_94().is_oriented());
81        }
82
83        #[test]
84        fn is_oriented_kattis_builddeps() {
85            assert!(kattis_builddeps().is_oriented());
86        }
87
88        #[test]
89        fn is_oriented_kattis_cantinaofbabel_1() {
90            assert!(!kattis_cantinaofbabel_1().is_oriented());
91        }
92
93        #[test]
94        fn is_oriented_kattis_cantinaofbabel_2() {
95            assert!(!kattis_cantinaofbabel_2().is_oriented());
96        }
97
98        #[test]
99        fn is_oriented_kattis_escapewallmaria_1() {
100            assert!(!kattis_escapewallmaria_1().is_oriented());
101        }
102
103        #[test]
104        fn is_oriented_kattis_escapewallmaria_2() {
105            assert!(!kattis_escapewallmaria_2().is_oriented());
106        }
107
108        #[test]
109        fn is_oriented_kattis_escapewallmaria_3() {
110            assert!(!kattis_escapewallmaria_3().is_oriented());
111        }
112    };
113}