diff options
author | Aleksey Kladov <[email protected]> | 2018-07-29 14:19:16 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-29 14:19:16 +0100 |
commit | 13c6a5c4b02d6436e4197c3ca93a8a5c3112a967 (patch) | |
tree | efaeaedca21c1727976a1aa67e2c76b13feccd18 /src/yellow/red.rs | |
parent | 415c891d641fa305e7ddbbbcc78db990dd5d3564 (diff) |
Avoid optimizing trivia for now
Diffstat (limited to 'src/yellow/red.rs')
-rw-r--r-- | src/yellow/red.rs | 43 |
1 files changed, 18 insertions, 25 deletions
diff --git a/src/yellow/red.rs b/src/yellow/red.rs index 71212a081..e2dbceeae 100644 --- a/src/yellow/red.rs +++ b/src/yellow/red.rs | |||
@@ -1,12 +1,12 @@ | |||
1 | use std::sync::{Arc, RwLock}; | 1 | use std::sync::{Arc, RwLock}; |
2 | use { | 2 | use { |
3 | TextUnit, | 3 | TextUnit, |
4 | yellow::{Ptr, GreenNode, TextLen} | 4 | yellow::{Ptr, GreenNode}, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | #[derive(Debug)] | 7 | #[derive(Debug)] |
8 | pub(crate) struct RedNode { | 8 | pub(crate) struct RedNode { |
9 | green: Arc<GreenNode>, | 9 | green: GreenNode, |
10 | parent: Option<ParentData>, | 10 | parent: Option<ParentData>, |
11 | children: RwLock<Vec<Option<Arc<RedNode>>>>, | 11 | children: RwLock<Vec<Option<Arc<RedNode>>>>, |
12 | } | 12 | } |
@@ -20,30 +20,30 @@ struct ParentData { | |||
20 | 20 | ||
21 | impl RedNode { | 21 | impl RedNode { |
22 | pub fn new_root( | 22 | pub fn new_root( |
23 | green: Arc<GreenNode>, | 23 | green: GreenNode, |
24 | ) -> RedNode { | 24 | ) -> RedNode { |
25 | RedNode::new(green, None) | 25 | RedNode::new(green, None) |
26 | } | 26 | } |
27 | 27 | ||
28 | fn new_child( | 28 | fn new_child( |
29 | green: Arc<GreenNode>, | 29 | green: GreenNode, |
30 | parent: Ptr<RedNode>, | 30 | parent: Ptr<RedNode>, |
31 | start_offset: TextUnit, | 31 | start_offset: TextUnit, |
32 | index_in_parent: usize | 32 | index_in_parent: usize, |
33 | ) -> RedNode { | 33 | ) -> RedNode { |
34 | let parent_data = ParentData { | 34 | let parent_data = ParentData { |
35 | parent, | 35 | parent, |
36 | start_offset, | 36 | start_offset, |
37 | index_in_parent | 37 | index_in_parent, |
38 | }; | 38 | }; |
39 | RedNode::new(green, Some(parent_data)) | 39 | RedNode::new(green, Some(parent_data)) |
40 | } | 40 | } |
41 | 41 | ||
42 | fn new( | 42 | fn new( |
43 | green: Arc<GreenNode>, | 43 | green: GreenNode, |
44 | parent: Option<ParentData>, | 44 | parent: Option<ParentData>, |
45 | ) -> RedNode { | 45 | ) -> RedNode { |
46 | let children = vec![None; green.n_children()]; | 46 | let children = vec![None; green.children().len()]; |
47 | RedNode { green, parent, children: RwLock::new(children) } | 47 | RedNode { green, parent, children: RwLock::new(children) } |
48 | } | 48 | } |
49 | 49 | ||
@@ -59,29 +59,22 @@ impl RedNode { | |||
59 | } | 59 | } |
60 | 60 | ||
61 | pub(crate) fn n_children(&self) -> usize { | 61 | pub(crate) fn n_children(&self) -> usize { |
62 | self.green.n_children() | 62 | self.green.children().len() |
63 | } | 63 | } |
64 | 64 | ||
65 | pub(crate) fn nth_child(&self, me: Ptr<RedNode>, n: usize) -> Arc<RedNode> { | 65 | pub(crate) fn nth_child(&self, me: Ptr<RedNode>, idx: usize) -> Arc<RedNode> { |
66 | match &self.children.read().unwrap()[n] { | 66 | match &self.children.read().unwrap()[idx] { |
67 | Some(child) => return child.clone(), | 67 | Some(child) => return child.clone(), |
68 | None => (), | 68 | None => (), |
69 | } | 69 | } |
70 | let mut children = self.children.write().unwrap(); | 70 | let mut children = self.children.write().unwrap(); |
71 | if children[n].is_none() { | 71 | if children[idx].is_none() { |
72 | let start_offset = { | 72 | let green_children = self.green.children(); |
73 | let mut acc = self.start_offset(); | 73 | let start_offset = self.start_offset() |
74 | for i in 0..n { | 74 | + green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>(); |
75 | acc += self.green.nth_trivias(i).text_len(); | 75 | let child = RedNode::new_child(green_children[idx].clone(), me, start_offset, idx); |
76 | acc += self.green.nth_child(i).text_len(); | 76 | children[idx] = Some(Arc::new(child)) |
77 | } | ||
78 | acc += self.green.nth_trivias(n).text_len(); | ||
79 | acc | ||
80 | }; | ||
81 | let green = self.green.nth_child(n).clone(); | ||
82 | let child = RedNode::new_child(green, me, start_offset, n); | ||
83 | children[n] = Some(Arc::new(child)) | ||
84 | } | 77 | } |
85 | children[n].as_ref().unwrap().clone() | 78 | children[idx].as_ref().unwrap().clone() |
86 | } | 79 | } |
87 | } | 80 | } |