|
1 | 1 | use serde::{Deserialize, Serialize}; |
2 | 2 |
|
| 3 | +/// The `Path` represents as a path to reference to the node in the `NodeTree`. |
3 | 4 | #[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)] |
4 | 5 | pub struct Path(pub Vec<usize>); |
5 | 6 |
|
@@ -48,8 +49,56 @@ impl From<&[usize]> for Path { |
48 | 49 | } |
49 | 50 |
|
50 | 51 | impl Path { |
| 52 | + /// |
| 53 | + /// |
| 54 | + /// The path will be changed is |
| 55 | + /// # Arguments |
| 56 | + /// |
| 57 | + /// * `other`: |
| 58 | + /// * `offset`: represents the len of nodes referenced by this path |
| 59 | + /// |
| 60 | + /// returns: Path |
| 61 | + /// |
| 62 | + /// # Examples |
| 63 | + /// |
| 64 | + /// ``` |
| 65 | + /// |
| 66 | + /// ``` |
| 67 | + pub fn transform(&self, other: &Path, offset: usize) -> Path { |
| 68 | + if self.len() > other.len() { |
| 69 | + return other.clone(); |
| 70 | + } |
| 71 | + if self.is_empty() || other.is_empty() { |
| 72 | + return other.clone(); |
| 73 | + } |
| 74 | + for i in 0..(self.len() - 1) { |
| 75 | + if self.0[i] != other.0[i] { |
| 76 | + return other.clone(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Splits the `Path` into two part. The suffix will contain the last element of the `Path`. |
| 81 | + let second_last_index = self.0.len() - 1; |
| 82 | + let mut prefix: Vec<usize> = self.0[0..second_last_index].into(); |
| 83 | + let mut suffix: Vec<usize> = other.0[self.0.len()..].into(); |
| 84 | + let last_value = self.0.last().unwrap().clone(); |
| 85 | + |
| 86 | + let other_second_last_value = other.0[second_last_index]; |
| 87 | + |
| 88 | + // |
| 89 | + if last_value <= other_second_last_value { |
| 90 | + prefix.push(other_second_last_value + offset); |
| 91 | + } else { |
| 92 | + prefix.push(other_second_last_value); |
| 93 | + } |
| 94 | + |
| 95 | + // concat the prefix and suffix into a new path |
| 96 | + prefix.append(&mut suffix); |
| 97 | + Path(prefix) |
| 98 | + } |
| 99 | + |
51 | 100 | // delta is default to be 1 |
52 | | - pub fn transform(pre_insert_path: &Path, b: &Path, offset: i64) -> Path { |
| 101 | + pub fn transform3(pre_insert_path: &Path, b: &Path, offset: i64) -> Path { |
53 | 102 | if pre_insert_path.len() > b.len() { |
54 | 103 | return b.clone(); |
55 | 104 | } |
@@ -83,44 +132,44 @@ mod tests { |
83 | 132 | #[test] |
84 | 133 | fn path_transform_test_1() { |
85 | 134 | assert_eq!( |
86 | | - { Path::transform(&Path(vec![0, 1]), &Path(vec![0, 1]), 1) }.0, |
| 135 | + { Path::transform3(&Path(vec![0, 1]), &Path(vec![0, 1]), 1) }.0, |
87 | 136 | vec![0, 2] |
88 | 137 | ); |
89 | 138 |
|
90 | 139 | assert_eq!( |
91 | | - { Path::transform(&Path(vec![0, 1]), &Path(vec![0, 1]), 5) }.0, |
| 140 | + { Path::transform3(&Path(vec![0, 1]), &Path(vec![0, 1]), 5) }.0, |
92 | 141 | vec![0, 6] |
93 | 142 | ); |
94 | 143 | } |
95 | 144 |
|
96 | 145 | #[test] |
97 | 146 | fn path_transform_test_2() { |
98 | 147 | assert_eq!( |
99 | | - { Path::transform(&Path(vec![0, 1]), &Path(vec![0, 2]), 1) }.0, |
| 148 | + { Path::transform3(&Path(vec![0, 1]), &Path(vec![0, 2]), 1) }.0, |
100 | 149 | vec![0, 3] |
101 | 150 | ); |
102 | 151 | } |
103 | 152 |
|
104 | 153 | #[test] |
105 | 154 | fn path_transform_test_3() { |
106 | 155 | assert_eq!( |
107 | | - { Path::transform(&Path(vec![0, 1]), &Path(vec![0, 2, 7, 8, 9]), 1) }.0, |
| 156 | + { Path::transform3(&Path(vec![0, 1]), &Path(vec![0, 2, 7, 8, 9]), 1) }.0, |
108 | 157 | vec![0, 3, 7, 8, 9] |
109 | 158 | ); |
110 | 159 | } |
111 | 160 |
|
112 | 161 | #[test] |
113 | 162 | fn path_transform_no_changed_test() { |
114 | 163 | assert_eq!( |
115 | | - { Path::transform(&Path(vec![0, 1, 2]), &Path(vec![0, 0, 7, 8, 9]), 1) }.0, |
| 164 | + { Path::transform3(&Path(vec![0, 1, 2]), &Path(vec![0, 0, 7, 8, 9]), 1) }.0, |
116 | 165 | vec![0, 0, 7, 8, 9] |
117 | 166 | ); |
118 | 167 | assert_eq!( |
119 | | - { Path::transform(&Path(vec![0, 1, 2]), &Path(vec![0, 1]), 1) }.0, |
| 168 | + { Path::transform3(&Path(vec![0, 1, 2]), &Path(vec![0, 1]), 1) }.0, |
120 | 169 | vec![0, 1] |
121 | 170 | ); |
122 | 171 | assert_eq!( |
123 | | - { Path::transform(&Path(vec![1, 1]), &Path(vec![1, 0]), 1) }.0, |
| 172 | + { Path::transform3(&Path(vec![1, 1]), &Path(vec![1, 0]), 1) }.0, |
124 | 173 | vec![1, 0] |
125 | 174 | ); |
126 | 175 | } |
|
0 commit comments