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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
//! Return an arc's weight.
//!
//! # Examples
//!
//! ```
//! use graaf::{
//! adjacency_list_weighted::Digraph,
//! gen::Empty,
//! op::{
//! AddArcWeighted,
//! ArcWeight,
//! },
//! };
//!
//! // 0 -> {1 (2), 2 (3)}
//! // 1 -> {0 (4)}
//! // 2 -> {0 (7), 1 (8)}
//!
//! let mut digraph = Digraph::<usize>::empty(3);
//!
//! digraph.add_arc_weighted(0, 1, 2);
//! digraph.add_arc_weighted(0, 2, 3);
//! digraph.add_arc_weighted(1, 0, 4);
//! digraph.add_arc_weighted(2, 0, 7);
//! digraph.add_arc_weighted(2, 1, 8);
//!
//! assert_eq!(digraph.arc_weight(0, 0), None);
//! assert_eq!(digraph.arc_weight(0, 1), Some(&2));
//! assert_eq!(digraph.arc_weight(0, 2), Some(&3));
//! assert_eq!(digraph.arc_weight(1, 0), Some(&4));
//! assert_eq!(digraph.arc_weight(1, 1), None);
//! assert_eq!(digraph.arc_weight(2, 0), Some(&7));
//! assert_eq!(digraph.arc_weight(2, 1), Some(&8));
//! assert_eq!(digraph.arc_weight(2, 2), None);
//! ```
/// Return an arc's weight.
///
/// # Implementing `ArcWeight`
///
/// Provide an implementation of `arc_weight` that returns the weight of the
/// arc.
///
/// ```
/// use {
/// graaf::op::ArcWeight,
/// std::collections::BTreeMap,
/// };
///
/// struct Digraph {
/// arcs: Vec<BTreeMap<usize, usize>>,
/// }
///
/// impl ArcWeight<usize> for Digraph {
/// fn arc_weight(&self, u: usize, v: usize) -> Option<&usize> {
/// self.arcs.get(u).and_then(|m| m.get(&v))
/// }
/// }
/// ```
///
/// # Examples
///
/// ```
/// use graaf::{
/// adjacency_list_weighted::Digraph,
/// gen::Empty,
/// op::{
/// AddArcWeighted,
/// ArcWeight,
/// },
/// };
///
/// // 0 -> {1 (2), 2 (3)}
/// // 1 -> {0 (4)}
/// // 2 -> {0 (7), 1 (8)}
///
/// let mut digraph = Digraph::<usize>::empty(3);
///
/// digraph.add_arc_weighted(0, 1, 2);
/// digraph.add_arc_weighted(0, 2, 3);
/// digraph.add_arc_weighted(1, 0, 4);
/// digraph.add_arc_weighted(2, 0, 7);
/// digraph.add_arc_weighted(2, 1, 8);
///
/// assert_eq!(digraph.arc_weight(0, 0), None);
/// assert_eq!(digraph.arc_weight(0, 1), Some(&2));
/// assert_eq!(digraph.arc_weight(0, 2), Some(&3));
/// assert_eq!(digraph.arc_weight(1, 0), Some(&4));
/// assert_eq!(digraph.arc_weight(1, 1), None);
/// assert_eq!(digraph.arc_weight(2, 0), Some(&7));
/// assert_eq!(digraph.arc_weight(2, 1), Some(&8));
/// assert_eq!(digraph.arc_weight(2, 2), None);
/// ```
pub trait ArcWeight<W> {
/// Returns the weight of the arc from `u` to `v` if it exists in the
/// digraph.
///
/// # Arguments
///
/// * `u`: The tail vertex.
/// * `v`: The head vertex.
#[must_use]
fn arc_weight(&self, u: usize, v: usize) -> Option<&W>;
}