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
use crate::data::YesData;
use crate::trie_node::{TrieNode, NodeAssociation};

pub type TrieDataNode<'a, D> = TrieNode<'a, D, YesData>;

/// Methods only on nodes that have data.
impl<'a, D> TrieDataNode<'a, D> {
    /// Recursive function that drops all children maps and collects data
    /// regardless of having multiple words branching from them or not.
    pub(crate) fn remove_all_words_collect(&mut self, found_data: &mut Vec<D>) {
        self.children.values_mut().for_each(|child| {
            child.remove_all_words_collect(found_data);
        });

        if let NodeAssociation::Data(data_vec) = self.clear_word_end_association(false) {
            found_data.extend(data_vec);
        }

        self.clear_children();
    }

    /// Recursive function finds every node that is an end of a word and appends
    /// it's data to the passed vector.
    pub(crate) fn generate_all_data<'b>(&'b self, found_data: &mut Vec<&'b D>) {
        if let NodeAssociation::Data(data_vec) = &self.word_end_association {
            found_data.extend(data_vec.iter());
        }

        self.children
            .values()
            .for_each(|x| x.generate_all_data(found_data));
    }

    /// Function pushes data to the association vector.
    pub(crate) fn push_data(&mut self, data: D) {
        if let NodeAssociation::Data(vec) = &mut self.word_end_association {
            vec.push(data);
        }
    }
}