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
pub struct Node<K, V> {
    key: K,
    value: V,
    children: Vec<Box<Node<K, V>>>,
}

pub enum Error {
    ParentNotFound,
}

impl<K, V> Node<K, V>
where
    K: PartialEq,
{
    pub fn new(key: K, value: V) -> Self {
        Self {
            key,
            value,
            children: Vec::new(),
        }
    }

    pub fn add_child(&mut self, child: Node<K, V>) -> Option<Box<Node<K, V>>> {
        let child = Box::new(child);

        let mut old_value = None;
        for i in 0..self.children.len() {
            if self.children[i] == child {
                old_value = Some(self.children.remove(i));
                break;
            }
        }

        self.children.push(child);

        old_value
    }

    pub fn add_child_at_path(
        &mut self,
        path: &[&K],
        child: Node<K, V>,
    ) -> Result<Option<Box<Node<K, V>>>, Error> {
        match self.get_mut_child(path) {
            Some(parent) => Ok(parent.add_child(child)),
            None => Err(Error::ParentNotFound),
        }
    }

    /// Get a child node based on a list of keys.
    ///
    /// # Examples
    ///
    /// ```
    /// use generic_tree::Node;
    ///
    /// # let mut grandparent = Node::new("grandparent".to_owned(), 1);
    /// # let mut parent = Node::new("parent".to_owned(), 2);
    /// # let child = Node::new("child".to_owned(), 3);
    ///
    /// # assert!(parent.add_child(child).is_none());
    /// # assert!(grandparent.add_child(parent).is_none());
    ///
    /// // Tree structure:
    /// // grandparent -> parent -> child
    /// assert!(grandparent.get_child(&["parent", "child"]).is_some());
    /// ```
    pub fn get_child<Q>(&self, path: &[Q]) -> Option<&Node<K, V>>
    where
        Q: PartialEq<K>,
    {
        if path.is_empty() {
            return Some(self);
        }

        let child = &path[0];
        let path = &path[1..];

        for entry in &self.children {
            if child == entry.key() {
                return entry.get_child(path);
            }
        }

        None
    }

    pub fn get_mut_child(&mut self, path: &[&K]) -> Option<&mut Node<K, V>> {
        if path.is_empty() {
            return Some(self);
        }

        let child = path[0];
        let path = &path[1..];

        for entry in &mut self.children {
            if entry.key() == child {
                return entry.get_mut_child(path);
            }
        }

        None
    }

    pub fn take_child(&mut self, path: &[&K]) -> Option<Node<K, V>> {
        if path.is_empty() {
            return None;
        }

        let child = path[0];
        let path = &path[1..];

        for entry in &mut self.children {
            if entry.key() == child {
                return entry.take_child(path);
            }
        }

        None
    }

    pub fn value(&self) -> &V {
        &self.value
    }

    pub fn mut_value(&mut self) -> &mut V {
        &mut self.value
    }

    pub fn key(&self) -> &K {
        &self.key
    }
}

impl<K, V> PartialEq for Node<K, V>
where
    K: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}

impl<K, V> PartialEq<K> for Node<K, V>
where
    K: PartialEq,
{
    fn eq(&self, other: &K) -> bool {
        &self.key == other
    }
}