summaryrefslogtreecommitdiff
path: root/forest/src/default.rs
blob: 5e438d45e4b4f9bc128c04ada43501249b5d1cde (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! This file provides a default implementation for the
//! [`Forest`][crate::Forest] trait.

use super::*;
use graph::{error::Error as GraphError, ALGraph, Graph};

use std::collections::{hash_set::Iter, HashMap, HashSet};

#[derive(Debug, Clone)]
pub struct DefaultForest<NodeLabel: GraphLabel, EdgeLabel: GraphLabel> {
    graph: ALGraph,
    vertex_labels: HashMap<usize, NodeLabel>,
    edge_labels: HashMap<(usize, usize), EdgeLabel>,
    plugins: HashSet<usize>,
    plugouts: HashSet<usize>,
}

impl<NodeLabel: GraphLabel, EdgeLabel: GraphLabel> Default for DefaultForest<NodeLabel, EdgeLabel> {
    fn default() -> Self {
        Self {
            graph: Default::default(),
            vertex_labels: Default::default(),
            edge_labels: Default::default(),
            plugins: Default::default(),
            plugouts: Default::default(),
        }
    }
}

impl<NodeLabel: GraphLabel, EdgeLabel: GraphLabel> Graph for DefaultForest<NodeLabel, EdgeLabel> {
    type Iter<'a> = <ALGraph as Graph>::Iter<'a>
    where
        Self: 'a;

    fn is_empty(&self) -> bool {
        self.graph.is_empty()
    }

    fn nodes_len(&self) -> usize {
        self.graph.nodes_len()
    }

    fn children_of(&self, node_id: usize) -> Result<Self::Iter<'_>, GraphError> {
        self.graph.children_of(node_id)
    }

    fn degree(&self, node_id: usize) -> Result<usize, GraphError> {
        self.graph.degree(node_id)
    }

    fn is_empty_node(&self, node_id: usize) -> Result<bool, GraphError> {
        self.graph.is_empty_node(node_id)
    }

    fn has_edge(&self, source: usize, target: usize) -> Result<bool, GraphError> {
        self.graph.has_edge(source, target)
    }

    fn replace_by_builder(&mut self, _builder: impl graph::Builder<Result = Self>) {
        unimplemented!()
    }
}

#[derive(Debug)]
pub struct LabelIter<'a> {
    set_iter: Iter<'a, usize>,
}

impl<'a> Iterator for LabelIter<'a> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        self.set_iter.next().copied()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.set_iter.size_hint()
    }
}

impl<'a> ExactSizeIterator for LabelIter<'a> {
    fn len(&self) -> usize {
        self.set_iter.len()
    }
}

impl<'a> From<Iter<'a, usize>> for LabelIter<'a> {
    fn from(set_iter: Iter<'a, usize>) -> Self {
        Self { set_iter }
    }
}

impl<NodeLabel: GraphLabel, EdgeLabel: GraphLabel> Forest<NodeLabel, EdgeLabel>
    for DefaultForest<NodeLabel, EdgeLabel>
{
    type PluginIter<'a> = LabelIter<'a>
    where
        Self: 'a;

    type PlugoutIter<'a> = LabelIter<'a>
    where
        Self: 'a;

    fn plugins(&self) -> Self::PluginIter<'_> {
        self.plugins.iter().into()
    }

    fn plugouts(&self) -> Self::PlugoutIter<'_> {
        self.plugouts.iter().into()
    }

    fn plug(&mut self, other: &Self) -> Result<(), Error> {
        // PLAN: Produce a BuilderMut, adjust the indices for edges,
        // and then add edges between plugs.
        //
        // Since I cannot touch the underlying nodes directly, I have
        // to add the nodes and edges individually.

        todo!()
    }

    fn vertex_label(&self, node_id: usize) -> Result<Option<NodeLabel>, Error> {
        if node_id >= self.nodes_len() {
            return Err(Error::IndexOutOfBounds(node_id, self.nodes_len()));
        }

        Ok(self.vertex_labels.get(&node_id).copied())
    }

    fn edge_label(&self, source: usize, target: usize) -> Result<Option<EdgeLabel>, Error> {
        if source >= self.nodes_len() {
            return Err(Error::IndexOutOfBounds(source, self.nodes_len()));
        }

        if target >= self.nodes_len() {
            return Err(Error::IndexOutOfBounds(target, self.nodes_len()));
        }

        Ok(self.edge_labels.get(&(source, target)).copied())
    }
}