cursor.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. use crate::{
  2. core::{Delta, Interval, Operation},
  3. errors::{ErrorBuilder, OTError, OTErrorCode},
  4. };
  5. use std::{cmp::min, iter::Enumerate, slice::Iter};
  6. #[derive(Debug)]
  7. pub struct Cursor<'a> {
  8. pub(crate) delta: &'a Delta,
  9. pub(crate) origin_iv: Interval,
  10. pub(crate) next_iv: Interval,
  11. pub(crate) c_index: usize,
  12. pub(crate) o_index: usize,
  13. iter: Enumerate<Iter<'a, Operation>>,
  14. next_op: Option<Operation>,
  15. }
  16. impl<'a> Cursor<'a> {
  17. pub fn new(delta: &'a Delta, interval: Interval) -> Cursor<'a> {
  18. // debug_assert!(interval.start <= delta.target_len);
  19. let mut cursor = Self {
  20. delta,
  21. origin_iv: interval,
  22. next_iv: interval,
  23. c_index: 0,
  24. o_index: 0,
  25. iter: delta.ops.iter().enumerate(),
  26. next_op: None,
  27. };
  28. cursor.descend(0);
  29. cursor
  30. }
  31. pub fn next_interval(&self) -> Interval { self.next_op_interval_with_constraint(None) }
  32. pub fn next_op_with_len(&mut self, force_len: Option<usize>) -> Option<Operation> {
  33. let mut find_op = None;
  34. let next_op = self.next_op.take();
  35. let mut next_op = next_op.as_ref();
  36. if next_op.is_none() {
  37. next_op = find_next_op(self);
  38. }
  39. let old_c_index = self.c_index;
  40. while find_op.is_none() && next_op.is_some() {
  41. let op = next_op.take().unwrap();
  42. let interval = self.next_op_interval_with_constraint(force_len);
  43. if interval.is_empty() {
  44. self.next_op = Some(op.clone());
  45. break;
  46. }
  47. find_op = op.shrink(interval);
  48. let suffix = Interval::new(0, op.length()).suffix(interval);
  49. if !suffix.is_empty() {
  50. self.next_op = op.shrink(suffix);
  51. }
  52. self.c_index += interval.end;
  53. self.next_iv.start = self.c_index;
  54. if find_op.is_none() {
  55. next_op = find_next_op(self);
  56. }
  57. }
  58. if find_op.is_some() {
  59. let last = self.c_index - old_c_index;
  60. let force_len = force_len.unwrap_or(0);
  61. if force_len > last {
  62. let len = force_len - last;
  63. return self.next_op_with_len(Some(len));
  64. }
  65. }
  66. return find_op;
  67. }
  68. pub fn next_op(&mut self) -> Option<Operation> { self.next_op_with_len(None) }
  69. pub fn has_next(&self) -> bool { self.next_iter_op().is_some() }
  70. fn descend(&mut self, index: usize) {
  71. self.next_iv.start += index;
  72. if self.c_index >= self.next_iv.start {
  73. return;
  74. }
  75. while let Some((o_index, op)) = self.iter.next() {
  76. self.o_index = o_index;
  77. let start = self.c_index;
  78. let end = start + op.length();
  79. let intersect = Interval::new(start, end).intersect(self.next_iv);
  80. if intersect.is_empty() {
  81. self.c_index += op.length();
  82. } else {
  83. self.next_op = Some(op.clone());
  84. break;
  85. }
  86. }
  87. }
  88. pub fn next_iter_op(&self) -> Option<&Operation> {
  89. let mut next_op = self.next_op.as_ref();
  90. if next_op.is_none() {
  91. let mut offset = 0;
  92. for op in &self.delta.ops {
  93. offset += op.length();
  94. if offset > self.c_index {
  95. next_op = Some(op);
  96. break;
  97. }
  98. }
  99. }
  100. next_op
  101. }
  102. fn next_op_interval_with_constraint(&self, force_len: Option<usize>) -> Interval {
  103. let next_op = self.next_iter_op();
  104. if next_op.is_none() {
  105. return Interval::new(0, 0);
  106. }
  107. let op = next_op.unwrap();
  108. let start = self.c_index;
  109. let end = match force_len {
  110. None => self.c_index + op.length(),
  111. Some(force_len) => self.c_index + min(force_len, op.length()),
  112. };
  113. let intersect = Interval::new(start, end).intersect(self.next_iv);
  114. let interval = intersect.translate_neg(start);
  115. interval
  116. }
  117. }
  118. fn find_next_op<'a>(cursor: &mut Cursor<'a>) -> Option<&'a Operation> {
  119. match cursor.iter.next() {
  120. None => None,
  121. Some((o_index, op)) => {
  122. cursor.o_index = o_index;
  123. Some(op)
  124. },
  125. }
  126. }
  127. type SeekResult = Result<(), OTError>;
  128. pub trait Metric {
  129. fn seek(cursor: &mut Cursor, index: usize) -> SeekResult;
  130. }
  131. pub struct OpMetric {}
  132. impl Metric for OpMetric {
  133. fn seek(cursor: &mut Cursor, index: usize) -> SeekResult {
  134. let _ = check_bound(cursor.o_index, index)?;
  135. let mut temp_cursor = Cursor::new(cursor.delta, cursor.origin_iv);
  136. let mut offset = 0;
  137. while let Some((_, op)) = temp_cursor.iter.next() {
  138. offset += op.length();
  139. if offset > index {
  140. break;
  141. }
  142. }
  143. cursor.descend(offset);
  144. Ok(())
  145. }
  146. }
  147. pub struct CharMetric {}
  148. impl Metric for CharMetric {
  149. fn seek(cursor: &mut Cursor, index: usize) -> SeekResult {
  150. let _ = check_bound(cursor.c_index, index)?;
  151. let _ = cursor.next_op_with_len(Some(index));
  152. Ok(())
  153. }
  154. }
  155. fn check_bound(current: usize, target: usize) -> Result<(), OTError> {
  156. if current > target {
  157. let msg = format!("{} should be greater than current: {}", target, current);
  158. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength)
  159. .msg(&msg)
  160. .build());
  161. }
  162. Ok(())
  163. }