2022-08-17 16:20:56 +08:00
|
|
|
#[derive(Clone)]
|
2022-08-16 16:25:52 +08:00
|
|
|
pub struct Position(pub Vec<usize>);
|
|
|
|
|
|
|
|
impl Position {
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
2022-08-19 14:43:11 +08:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Position {
|
|
|
|
// delta is default to be 1
|
2022-08-22 11:10:06 +08:00
|
|
|
pub fn transform(pre_insert_path: &Position, b: &Position, delta: i64) -> Position {
|
2022-08-19 14:43:11 +08:00
|
|
|
if pre_insert_path.len() > b.len() {
|
|
|
|
return b.clone();
|
|
|
|
}
|
|
|
|
if pre_insert_path.is_empty() || b.is_empty() {
|
|
|
|
return b.clone();
|
|
|
|
}
|
|
|
|
// check the prefix
|
|
|
|
for i in 0..(pre_insert_path.len()) {
|
|
|
|
if pre_insert_path.0[i] != b.0[i] {
|
|
|
|
return b.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut prefix: Vec<usize> = pre_insert_path.0[0..(pre_insert_path.len() - 1)].into();
|
|
|
|
let mut suffix: Vec<usize> = b.0[pre_insert_path.0.len()..].into();
|
|
|
|
let prev_insert_last: usize = *pre_insert_path.0.last().unwrap();
|
|
|
|
let b_at_index = b.0[pre_insert_path.0.len() - 1];
|
|
|
|
if prev_insert_last <= b_at_index {
|
2022-08-22 11:10:06 +08:00
|
|
|
prefix.push(((b_at_index as i64) + delta) as usize);
|
2022-08-19 14:43:11 +08:00
|
|
|
} else {
|
|
|
|
prefix.push(b_at_index);
|
|
|
|
}
|
|
|
|
prefix.append(&mut suffix);
|
|
|
|
return Position(prefix);
|
|
|
|
}
|
2022-08-16 16:25:52 +08:00
|
|
|
}
|
2022-08-19 12:04:43 +08:00
|
|
|
|
|
|
|
impl From<Vec<usize>> for Position {
|
|
|
|
fn from(v: Vec<usize>) -> Self {
|
|
|
|
Position(v)
|
|
|
|
}
|
|
|
|
}
|