summaryrefslogtreecommitdiff
path: root/forest/src/default.rs
blob: d79c1c7f869729c437d721d5713a7c260d3c400e (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! This file provides a default implementation for the
//! [`Forest`][crate::Forest] trait.

use super::*;
use graph::{
    builder::BuilderMut, labelled::binary::PLGBuilderMut, Graph, LabelGraph, PLGraph, RedirectGraph,
};

use core::fmt::Display;

/// The type of errors for forest operations.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub enum Error {
    /// An index is out of bounds.
    ///
    /// The first component is the index that is out of bounds, and
    /// the second component is the current length of nodes.
    IndexOutOfBounds(usize, usize),
    /// The forest does not permit duplicate nodes but encounters a
    /// repeated node.
    DuplicatedNode(usize),
    /// A node has no labels while it is required to have one.
    NodeNoLabel(usize),
    /// Encounter an invalid error in converting from an error of
    /// graphs.
    InvalidGraphError(GError),
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::IndexOutOfBounds(index, bound) => {
                write!(f, "index {index} is out of bound {bound}")
            }
            Error::DuplicatedNode(n) => write!(f, "node {n} is duplicated"),
            Error::InvalidGraphError(ge) => write!(f, "invalid error: {ge}"),
            Error::NodeNoLabel(n) => write!(f, "node {n} has no labels, but it should have one"),
        }
    }
}

impl std::error::Error for Error {}

impl From<GError> for Error {
    fn from(ge: GError) -> Self {
        match ge {
            GError::IndexOutOfBounds(index, bound) => Self::IndexOutOfBounds(index, bound),
            GError::DuplicatedNode(n) => Self::DuplicatedNode(n),
            _ => Self::InvalidGraphError(ge),
        }
    }
}

/// A default implementation of forest.
#[derive(Debug, Clone)]
pub struct DefaultForest<T: GraphLabel> {
    graph: PLGraph<T>,
    root: Option<usize>,
}

impl<T: GraphLabel> Default for DefaultForest<T> {
    fn default() -> Self {
        let graph = Default::default();
        let root = None;

        Self { graph, root }
    }
}

impl<T: GraphLabel> AsRef<DefaultForest<T>> for DefaultForest<T> {
    fn as_ref(&self) -> &DefaultForest<T> {
        &self
    }
}

impl<T: GraphLabel> Graph for DefaultForest<T> {
    type Iter<'a> = <PLGraph<T> as Graph>::Iter<'a>
    where
        Self: 'a;

    #[inline]
    fn is_empty(&self) -> bool {
        self.graph.is_empty()
    }

    #[inline]
    fn nodes_len(&self) -> usize {
        self.graph.nodes_len()
    }

    #[inline]
    fn edges_len(&self) -> Option<usize> {
        self.graph.edges_len()
    }

    #[inline]
    fn children_of(&self, node_id: usize) -> Result<Self::Iter<'_>, GError> {
        self.graph.children_of(node_id)
    }

    #[inline]
    fn degree(&self, node_id: usize) -> Result<usize, GError> {
        self.graph.degree(node_id)
    }

    #[inline]
    fn is_empty_node(&self, node_id: usize) -> Result<bool, GError> {
        self.graph.is_empty_node(node_id)
    }

    #[inline]
    fn has_edge(&self, source: usize, target: usize) -> Result<bool, GError> {
        self.graph.has_edge(source, target)
    }

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

impl<T: GraphLabel> ParentsGraph for DefaultForest<T> {
    type Iter<'a>= <PLGraph<T> as ParentsGraph>::Iter<'a>
    where
        Self:'a;

    fn parents_of(&self, node_id: usize) -> Result<<Self as ParentsGraph>::Iter<'_>, GError> {
        self.graph.parents_of(node_id)
    }
}

impl<T: GraphLabel> LabelGraph<T> for DefaultForest<T> {
    type Iter<'a> = std::iter::Empty<usize>
    where
        Self: 'a;

    type LabelIter<'a> = std::iter::Empty<(&'a T, <Self as LabelGraph<T>>::Iter<'a>)>
    where
        Self: 'a,
        T: 'a;

    type EdgeLabelIter<'a> = std::iter::Empty<T>
    where
        Self: 'a,
        T: 'a;

    #[inline]
    fn query_label(&self, label: T) -> Option<usize> {
        self.graph.query_label(label)
    }

    #[inline]
    fn vertex_label(&self, node_id: usize) -> Result<Option<T>, GError> {
        self.graph.vertex_label(node_id)
    }

    fn edge_label(
        &self,
        _source: usize,
        _target: usize,
    ) -> Result<Self::EdgeLabelIter<'_>, GError> {
        unimplemented!("edges have no labels")
    }

    fn find_children_with_label(
        &self,
        _node_id: usize,
        _label: &T,
    ) -> Result<<Self as LabelGraph<T>>::Iter<'_>, GError> {
        unimplemented!("edges have no labels")
    }

    fn labels_of(&self, _node_id: usize) -> Result<Self::LabelIter<'_>, GError> {
        unimplemented!("edges have no labels")
    }

    fn has_edge_label(&self, _node_id: usize, _label: &T, _target: usize) -> Result<bool, GError> {
        unimplemented!("edges have no labels")
    }
}

impl<T: GraphLabel> Forest<T> for DefaultForest<T> {
    type Error = Error;

    fn root(&self) -> Option<usize> {
        self.root
    }

    fn new_leaf(label: T) -> Self {
        let mut graph = PLGraph::default();

        let mut builder = PLGBuilderMut::from_graph_mut(&mut graph);

        let root = Some(builder.add_vertex(label));

        Self { graph, root }
    }

    fn is_prefix<F>(&self, node_id: usize, fragment: F) -> Result<bool, Self::Error>
    where
        F: AsRef<Self>,
    {
        if !self.has_node(node_id) {
            return Err(Error::IndexOutOfBounds(node_id, self.nodes_len()));
        }

        // We do a depth-first traversal to determine if every node
        // encountered has the same set of children (labels taken into
        // the consideration).

        let fragment = fragment.as_ref();

        let mut frag_stack = Vec::with_capacity(fragment.nodes_len());

        let mut self_stack = Vec::with_capacity(fragment.nodes_len());

        let frag_root = if let Some(root) = fragment.root() {
            root
        } else {
            // an empty forest is a prefix of any forest.
            return Ok(true);
        };

        frag_stack.push(frag_root);
        self_stack.push(node_id);

        // defer popping
        while let (Some(frag_top), Some(self_top)) =
            (frag_stack.last().copied(), self_stack.last().copied())
        {
            frag_stack.pop();
            self_stack.pop();

            if fragment.vertex_label(frag_top)? != self.vertex_label(self_top)? {
                // not a prefix
                return Ok(false);
            }

            let mut self_children = self.children_of(self_top)?;

            for child in fragment.children_of(frag_top)? {
                if let Some(self_child) = self_children.next() {
                    frag_stack.push(child);
                    self_stack.push(self_child);
                } else {
                    // too few children
                    return Ok(false);
                }
            }
        }

        // Check both stacks are empty at the end.
        Ok(frag_stack.is_empty() && self_stack.is_empty())
    }

    fn plant<F>(&mut self, node_id: usize, fragment: F) -> Result<(), Self::Error>
    where
        F: AsRef<Self>,
    {
        // Convert self to a builder_mut, and traverse fragment in a
        // depth-first manner and adjoin corresponding nodes along the
        // way.

        if !self.has_node(node_id) {
            return Err(Error::IndexOutOfBounds(node_id, self.nodes_len()));
        }

        let fragment = fragment.as_ref();

        let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph);

        let root = if let Some(root) = fragment.root() {
            root
        } else {
            // Nothing to do to plant an empty forest.
            return Ok(());
        };

        // Just a dummy label for use in adding edges.
        //
        // REVIEW: I probably should refactor the API for builder_mut.
        let root_label = fragment
            .vertex_label(root)?
            .ok_or(Error::NodeNoLabel(root))?;

        let nodes_len = fragment.nodes_len();

        // First adjoin those nodes and join the edges later.

        for node in 0..nodes_len {
            let label = fragment
                .vertex_label(node)?
                .ok_or(Error::NodeNoLabel(node))?;

            builder.add_vertex(label);
        }

        // If the fragment root has a duplicate label, the forest will
        // not grow, so we use the label to find the adjoined node
        // index.

        // the nodes hava already been added to the forest, so it is
        // safe to call unwrap.
        macro_rules! conversion (
            ($node:expr) => {
                {
                    builder
                        .query_label(
                            fragment
                                .vertex_label($node)?
                                .ok_or(Error::NodeNoLabel($node))?
                        ).unwrap()
                }
            }
        );

        // Don't forget to join the new sub-forest to the original
        // forest, at the specified position.

        builder.add_edge(node_id, conversion!(root), root_label)?;

        // We can try to calculate the depth of fragments, if we need
        // to lower the memory usage.  But in our use cases, we
        // usually deal with fragments where each node has at most one
        // child, so the depth is supposed to be equal to the length
        // in this case.
        let mut stack = Vec::with_capacity(fragment.nodes_len());

        stack.push(root);

        while let Some(top) = stack.pop() {
            for child in fragment.children_of(top)? {
                builder.add_edge(conversion!(top), conversion!(child), root_label)?;
            }
        }

        Ok(())
    }

    fn clone_node<F>(&mut self, node_id: usize, clone_transform: F) -> Result<usize, Self::Error>
    where
        F: Fn(T) -> T,
    {
        let mut builder = PLGBuilderMut::from_graph_mut(&mut self.graph);

        let old_label = builder
            .vertex_label(node_id)?
            .ok_or(Error::NodeNoLabel(node_id))?;

        let new_label = clone_transform(old_label);

        // Make a new node
        let new_index = builder.add_vertex(new_label);

        // Re-direct parents to the new node.
        //
        // This must be done before pointing the new node to the old
        // node, otherwise that edge will be redirected as well.

        // Unfortunately I cannot loop through parents and mutate them
        // at the same time, so I first collect them into a vector.
        let parents: Vec<_> = builder.parents_of(node_id)?.collect();

        for parent in parents.into_iter() {
            builder.redirect(parent.node(), parent.edge(), new_index)?;
        }

        // Point the new node to the old node.  OLD_LABEL is just a
        // place holder.

        builder.add_edge(new_index, node_id, old_label)?;

        Ok(new_index)
    }
}

#[cfg(test)]
mod forest_test {
    use super::*;

    macro_rules! leaf (
        ($label:expr, $type:tt) =>{
            DefaultForest::<$type>::new_leaf($label)
        };
        ($label:expr) => {
            DefaultForest::<usize>::new_leaf($label)
        }
    );

    #[test]
    fn test_forest_api() -> Result<(), Box<dyn std::error::Error>> {
        let forest: DefaultForest<usize> = Default::default();

        // empty forest

        assert!(forest.is_empty());

        // leaf forest

        let mut forest = leaf!(0, usize);

        assert_eq!(forest.nodes_len(), 1);
        assert_eq!(forest.root(), Some(0));

        // add some child

        forest.plant(0, leaf!(1))?;

        assert_eq!(forest.nodes_len(), 2);
        let mut children = forest.children_of(0)?;
        assert_eq!(children.next(), Some(1));
        assert_eq!(children.next(), None);

        // add more children

        forest.plant(0, leaf!(2))?;
        forest.plant(0, leaf!(3))?;
        forest.plant(0, leaf!(4))?;
        forest.plant(2, leaf!(5))?;

        assert_eq!(forest.nodes_len(), 6);
        let mut children = forest.children_of(0)?;
        assert_eq!(children.next(), Some(1));
        assert_eq!(children.next(), Some(2));
        assert_eq!(children.next(), Some(3));
        assert_eq!(children.next(), Some(4));
        let mut children = forest.children_of(2)?;
        assert_eq!(children.next(), Some(5));
        assert_eq!(children.next(), None);

        let mut test_forest = leaf!(0);
        test_forest.plant(0, leaf!(1))?;
        test_forest.plant(0, leaf!(2))?;
        test_forest.plant(0, leaf!(3))?;
        test_forest.plant(2, leaf!(5))?;

        assert!(forest.is_prefix(0, &test_forest)?);

        let mut test_forest = leaf!(0);
        test_forest.plant(0, leaf!(1))?;
        test_forest.plant(0, leaf!(2))?;
        // this child of the root should have label 3 in order to be a
        // prefix.
        test_forest.plant(0, leaf!(4))?;
        test_forest.plant(2, leaf!(5))?;

        assert!(!forest.is_prefix(0, &test_forest)?);

        let mut test_forest = leaf!(2);
        test_forest.plant(0, leaf!(5))?;

        assert!(forest.is_prefix(2, &test_forest)?);

        // now test cloning

        // add a duplicate label
        forest.plant(3, leaf!(5))?;

        let len = forest.nodes_len();

        let clone_transform = |label| label + len;

        forest.clone_node(5, clone_transform)?;

        assert_eq!(forest.nodes_len(), 7);

        #[cfg(feature = "test-print-viz")]
        forest.print_viz("forest.gv")?;

        Ok(())
    }
}