flange_flat_tree/navigator/
neighbors.rs

1/** Structure containing neighboring information for a node.
2*
3* Internally (for the navigator) this structure is used with
4* A=usize to store how neighboring and (therby navigaiton)
5* information for nodes (which noce is the parent node i.E.).
6*
7* But it can also be used (and that is why it is public)
8* for other neighboring information, like all the neighboring
9* values of nodes.
10*/
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Neighbors<A> {
13    // The node itself
14    pub me: Option<A>,
15    // The node above us
16    pub parent: Option<A>,
17    // The first of out child nodes
18    pub first_child: Option<A>,
19    // The prev node on the same level
20    pub next_sibling: Option<A>,
21    // The next node on the same level
22    pub prev_sibling: Option<A>,
23}
24
25impl<A> Neighbors<A> {
26    /** Map all the neigbors (which are not None)
27     * to some other value using the provided function.
28     *
29     * # Arguments
30     *
31     * - f - The function used for mapping the values.
32     *
33     * # Result
34     *
35     * A neighbors structure, where the neighbors are exchanged
36     * by the corresponding result of f.
37     */
38    pub fn map<B, F>(&self, f: F) -> Neighbors<B>
39    where
40        F: Fn(&A) -> B,
41    {
42        Neighbors {
43            me: self.me.as_ref().map(&f),
44            parent: self.parent.as_ref().map(&f),
45            first_child: self.first_child.as_ref().map(&f),
46            next_sibling: self.next_sibling.as_ref().map(&f),
47            prev_sibling: self.prev_sibling.as_ref().map(&f),
48        }
49    }
50
51    /** Same as map, but removes double "Option" for a
52     * mapping function that returns an option.
53     */
54    pub fn map_and_then<B, F>(&self, f: F) -> Neighbors<B>
55    where
56        F: Fn(&A) -> Option<B>,
57    {
58        Neighbors {
59            me: self.me.as_ref().and_then(&f),
60            first_child: self.first_child.as_ref().and_then(&f),
61            parent: self.parent.as_ref().and_then(&f),
62            next_sibling: self.next_sibling.as_ref().and_then(&f),
63            prev_sibling: self.prev_sibling.as_ref().and_then(&f),
64        }
65    }
66}
67
68impl<A> Neighbors<&A> {
69    /** Clone the contained values (which have to be references)*/
70    pub fn cloned(&self) -> Neighbors<A>
71    where
72        A: Clone,
73    {
74        Neighbors {
75            me: self.me.cloned(),
76            first_child: self.first_child.cloned(),
77            parent: self.parent.cloned(),
78            next_sibling: self.next_sibling.cloned(),
79            prev_sibling: self.prev_sibling.cloned(),
80        }
81    }
82}
83
84impl Neighbors<usize> {
85    /** Do `map_end_then` but don't use a mapping function but
86     * take the values directly form an vec.*/
87    pub fn map_and_then_with_values<'a, B>(&self, v: &'a [Option<B>]) -> Neighbors<&'a B> {
88        self.map_and_then(|i| v.get(*i).and_then(|v| v.as_ref()))
89    }
90}