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
/*
 This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0. If a copy of the MPL was not distributed with this
 file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use std::collections::{BTreeMap, HashMap};

use super::keywords::Keyword;
use super::lrobject::LrObject;

/// Keyword tree
/// Operate as a hash multimap of parent -> Vec<child>
#[derive(Default)]
pub struct KeywordTree {
    // HashMap. Key is the parent id. Values: the children ids.
    map: HashMap<i64, Vec<i64>>,
}

impl KeywordTree {
    pub fn new() -> KeywordTree {
        KeywordTree::default()
    }

    /// Get children for keyword with `id`
    pub fn children_for(&self, id: i64) -> Vec<i64> {
        if let Some(children) = self.map.get(&id) {
            return children.clone();
        }
        vec![]
    }

    fn add_child(&mut self, keyword: &Keyword) {
        self.map.entry(keyword.parent).or_insert_with(Vec::new);
        self.map
            .get_mut(&keyword.parent)
            .unwrap()
            .push(keyword.id());
    }

    /// Add children to the tree node.
    pub fn add_children(&mut self, children: &BTreeMap<i64, Keyword>) {
        for child in children.values() {
            self.add_child(child);
        }
    }

    #[cfg(test)]
    pub fn test() {
        let mut keywords: BTreeMap<i64, Keyword> = BTreeMap::new();
        keywords.insert(1, Keyword::new(1, 0, "", ""));
        keywords.insert(2, Keyword::new(2, 1, "", ""));
        keywords.insert(3, Keyword::new(3, 2, "", ""));
        keywords.insert(4, Keyword::new(4, 0, "", ""));
        keywords.insert(5, Keyword::new(5, 2, "", ""));

        let mut tree = KeywordTree::new();
        tree.add_children(&keywords);

        assert_eq!(tree.map.len(), 3);
        assert_eq!(tree.map[&0].len(), 2);
        assert_eq!(tree.map[&1].len(), 1);
        assert_eq!(tree.map[&2].len(), 2);

        let children = tree.children_for(0);
        assert_eq!(children, vec![1, 4]);
    }
}

#[cfg(test)]
#[test]
fn keyword_tree_test() {
    KeywordTree::test();
}