graaf/op/arcs_weighted.rs
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 73 74 75 76 77 78
//! Iterate over a digraph's weighted arcs.
//!
//! # Examples
//!
//! ```
//! use graaf::{
//!     AddArcWeighted,
//!     AdjacencyListWeighted,
//!     ArcsWeighted,
//!     Empty,
//! };
//!
//! let mut digraph = AdjacencyListWeighted::empty(3);
//!
//! digraph.add_arc_weighted(0, 1, 2);
//! digraph.add_arc_weighted(1, 2, 3);
//! digraph.add_arc_weighted(2, 0, 4);
//!
//! assert!(digraph
//!     .arcs_weighted()
//!     .eq([(0, 1, &2), (1, 2, &3), (2, 0, &4)]));
//! ```
/// Iterate over a digraph's weighted arcs.
///
/// # Implementing [`ArcsWeighted`] for a custom type
///
/// Provide an implementation of [`arcs_weighted`](ArcsWeighted) that returns
/// an iterator over a digraph's arcs and their weights.
///
/// ```
/// use graaf::ArcsWeighted;
///
/// struct AdjacencyListWeighted {
///     arcs: Vec<(usize, usize, usize)>,
/// }
///
/// impl ArcsWeighted for AdjacencyListWeighted {
///     type Weight = usize;
///
///     fn arcs_weighted(
///         &self,
///     ) -> impl Iterator<Item = (usize, usize, &Self::Weight)> {
///         self.arcs.iter().map(|&(u, v, ref w)| (u, v, w))
///     }
/// }
/// ```
pub trait ArcsWeighted {
    /// The weight of an arc.
    type Weight;
    /// Return an iterator over a digraphs' weighted arcs.
    ///
    /// # Examples
    ///
    /// ```
    /// use graaf::{
    ///     AddArcWeighted,
    ///     AdjacencyListWeighted,
    ///     ArcsWeighted,
    ///     Empty,
    /// };
    ///
    /// let mut digraph = AdjacencyListWeighted::empty(3);
    ///
    /// digraph.add_arc_weighted(0, 1, 2);
    /// digraph.add_arc_weighted(1, 2, 3);
    /// digraph.add_arc_weighted(2, 0, 4);
    ///
    /// assert!(digraph
    ///     .arcs_weighted()
    ///     .eq([(0, 1, &2), (1, 2, &3), (2, 0, &4)]));
    /// ```
    #[must_use]
    fn arcs_weighted(
        &self,
    ) -> impl Iterator<Item = (usize, usize, &Self::Weight)>;
}