graaf/op/
has_arc.rs

1//! Check whether an arc exists in a digraph.
2//!
3//! To check whether an edge exists between `u` to `v`, see [`HasEdge`].
4//!
5//! # Examples
6//!
7//! ```
8//! use graaf::{
9//!     AddArc,
10//!     AdjacencyList,
11//!     Empty,
12//!     HasArc,
13//! };
14//!
15//! let mut digraph = AdjacencyList::empty(3);
16//!
17//! digraph.add_arc(0, 1);
18//! digraph.add_arc(0, 2);
19//! digraph.add_arc(1, 0);
20//!
21//! assert!(digraph.has_arc(0, 1));
22//! assert!(digraph.has_arc(0, 2));
23//! assert!(digraph.has_arc(1, 0));
24//! assert!(!digraph.has_arc(1, 2));
25//! assert!(!digraph.has_arc(2, 0));
26//! assert!(!digraph.has_arc(2, 1));
27//! ```
28//!
29//! [`HasEdge`]: crate::HasEdge
30
31/// Check whether an arc exists in a digraph.
32pub trait HasArc {
33    /// Check whether an arc exists in the digraph.
34    ///
35    /// # Arguments
36    ///
37    /// * `u`: The tail vertex.
38    /// * `v`: The head vertex.
39    ///
40    /// # Panics
41    ///
42    /// `has_arc` may not panic if `u` and `v` aren't in the digraph.
43    ///
44    /// # Examples
45    ///
46    /// ```
47    /// use graaf::{
48    ///     AddArc,
49    ///     AdjacencyList,
50    ///     Empty,
51    ///     HasArc,
52    /// };
53    ///
54    /// let mut digraph = AdjacencyList::empty(3);
55    ///
56    /// digraph.add_arc(0, 1);
57    /// digraph.add_arc(0, 2);
58    /// digraph.add_arc(1, 0);
59    ///
60    /// assert!(digraph.has_arc(0, 1));
61    /// assert!(digraph.has_arc(0, 2));
62    /// assert!(digraph.has_arc(1, 0));
63    /// assert!(!digraph.has_arc(1, 2));
64    /// assert!(!digraph.has_arc(2, 0));
65    /// assert!(!digraph.has_arc(2, 1));
66    /// ```
67    #[must_use]
68    fn has_arc(&self, u: usize, v: usize) -> bool;
69}
70
71/// `HasArc` proptests
72#[macro_export]
73macro_rules! proptest_has_arc {
74    ($type:ty) => {
75        use {
76            proptest::proptest,
77            $crate::Empty,
78        };
79
80        proptest! {
81            #[test]
82            fn has_arc_out_of_bounds(order in 1..25_usize) {
83                let digraph = <$type>::empty(order);
84
85                for u in 0..order {
86                    assert!(!digraph.has_arc(u, order));
87                    assert!(!digraph.has_arc(order, u));
88                }
89            }
90        }
91    };
92}