aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/red.rs
blob: 71212a08115c62d46b9a26de0201ab55840595b9 (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
use std::sync::{Arc, RwLock};
use {
    TextUnit,
    yellow::{Ptr, GreenNode, TextLen}
};

#[derive(Debug)]
pub(crate) struct RedNode {
    green: Arc<GreenNode>,
    parent: Option<ParentData>,
    children: RwLock<Vec<Option<Arc<RedNode>>>>,
}

#[derive(Debug)]
struct ParentData {
    parent: Ptr<RedNode>,
    start_offset: TextUnit,
    index_in_parent: usize,
}

impl RedNode {
    pub fn new_root(
        green: Arc<GreenNode>,
    ) -> RedNode {
        RedNode::new(green, None)
    }

    fn new_child(
        green: Arc<GreenNode>,
        parent: Ptr<RedNode>,
        start_offset: TextUnit,
        index_in_parent: usize
    ) -> RedNode {
        let parent_data = ParentData {
            parent,
            start_offset,
            index_in_parent
        };
        RedNode::new(green, Some(parent_data))
    }

    fn new(
        green: Arc<GreenNode>,
        parent: Option<ParentData>,
    ) -> RedNode {
        let children = vec![None; green.n_children()];
        RedNode { green, parent, children: RwLock::new(children) }
    }

    pub(crate) fn green(&self) -> &GreenNode {
        &self.green
    }

    pub(crate) fn start_offset(&self) -> TextUnit {
        match &self.parent {
            None => 0.into(),
            Some(p) => p.start_offset,
        }
    }

    pub(crate) fn n_children(&self) -> usize {
        self.green.n_children()
    }

    pub(crate) fn nth_child(&self, me: Ptr<RedNode>, n: usize) -> Arc<RedNode> {
        match &self.children.read().unwrap()[n] {
            Some(child) => return child.clone(),
            None => (),
        }
        let mut children = self.children.write().unwrap();
        if children[n].is_none() {
            let start_offset = {
                let mut acc = self.start_offset();
                for i in 0..n {
                    acc += self.green.nth_trivias(i).text_len();
                    acc += self.green.nth_child(i).text_len();
                }
                acc += self.green.nth_trivias(n).text_len();
                acc
            };
            let green = self.green.nth_child(n).clone();
            let child = RedNode::new_child(green, me, start_offset, n);
            children[n] = Some(Arc::new(child))
        }
        children[n].as_ref().unwrap().clone()
    }
}