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
use crate::{AnyGraph, Edge, Key, Value, Vertex};

use std::collections::HashMap;

/// An interface for getting the successors and predecessors of each [`Vertex`].
pub trait Kinship<K, V>: AnyGraph<K, V>
where
    K: Key,
    V: Value,
{
    /// Get the successors of each vertex.
    fn successors(&self) -> HashMap<Vertex<K, V>, Vec<Edge<K>>>;

    /// Get the predecessors of each vertex.
    fn predecessors(&self) -> HashMap<Vertex<K, V>, Vec<Edge<K>>>;

    /// Get the successors of each vertex where the key is a [`Key`].
    fn successors_as_key_and_edges(&self) -> HashMap<K, Vec<Edge<K>>> {
        self.successors()
            .iter()
            .fold(HashMap::new(), |mut acc, (vertex, edges)| {
                acc.insert(vertex.key().clone(), edges.iter().cloned().collect());
                acc
            })
    }

    /// Get the predecessors of each vertex where the key is a [`Key`].
    fn predecessors_as_key_and_edges(&self) -> HashMap<K, Vec<Edge<K>>> {
        self.predecessors()
            .iter()
            .fold(HashMap::new(), |mut acc, (vertex, edges)| {
                acc.insert(vertex.key().clone(), edges.iter().cloned().collect());
                acc
            })
    }

    /// Get the map of key and vertex.
    fn key_vertex_map(&self) -> HashMap<K, Vertex<K, V>> {
        self.vertices()
            .iter()
            .fold(HashMap::new(), |mut acc, vertex| {
                acc.insert(vertex.key().clone(), vertex.clone());
                acc
            })
    }
}