delta.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. use crate::{
  2. core::{attributes::*, operation::*, DeltaIter, Interval, MAX_IV_LEN},
  3. errors::{ErrorBuilder, OTError, OTErrorCode},
  4. };
  5. use bytecount::num_chars;
  6. use std::{
  7. cmp::{min, Ordering},
  8. fmt,
  9. iter::FromIterator,
  10. str::FromStr,
  11. };
  12. #[derive(Clone, Debug, PartialEq)]
  13. pub struct Delta {
  14. pub ops: Vec<Operation>,
  15. pub base_len: usize,
  16. pub target_len: usize,
  17. }
  18. impl Default for Delta {
  19. fn default() -> Self {
  20. Self {
  21. ops: Vec::new(),
  22. base_len: 0,
  23. target_len: 0,
  24. }
  25. }
  26. }
  27. impl FromStr for Delta {
  28. type Err = ();
  29. fn from_str(s: &str) -> Result<Delta, Self::Err> {
  30. let mut delta = Delta::with_capacity(1);
  31. delta.add(Operation::Insert(s.into()));
  32. Ok(delta)
  33. }
  34. }
  35. impl<T: AsRef<str>> From<T> for Delta {
  36. fn from(s: T) -> Delta { Delta::from_str(s.as_ref()).unwrap() }
  37. }
  38. impl fmt::Display for Delta {
  39. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  40. // f.write_str(&serde_json::to_string(self).unwrap_or("".to_owned()))?;
  41. f.write_str("[ ")?;
  42. for op in &self.ops {
  43. f.write_fmt(format_args!("{} ", op))?;
  44. }
  45. f.write_str("]")?;
  46. Ok(())
  47. }
  48. }
  49. impl FromIterator<Operation> for Delta {
  50. fn from_iter<T: IntoIterator<Item = Operation>>(ops: T) -> Self {
  51. let mut operations = Delta::default();
  52. for op in ops {
  53. operations.add(op);
  54. }
  55. operations
  56. }
  57. }
  58. impl Delta {
  59. pub fn new() -> Self { Self::default() }
  60. #[inline]
  61. pub fn with_capacity(capacity: usize) -> Self {
  62. Self {
  63. ops: Vec::with_capacity(capacity),
  64. base_len: 0,
  65. target_len: 0,
  66. }
  67. }
  68. pub fn add(&mut self, op: Operation) {
  69. match op {
  70. Operation::Delete(i) => self.delete(i),
  71. Operation::Insert(i) => self.insert(&i.s, i.attributes),
  72. Operation::Retain(r) => self.retain(r.n, r.attributes),
  73. }
  74. }
  75. pub fn delete(&mut self, n: usize) {
  76. if n == 0 {
  77. return;
  78. }
  79. self.base_len += n as usize;
  80. if let Some(Operation::Delete(n_last)) = self.ops.last_mut() {
  81. *n_last += n;
  82. } else {
  83. self.ops.push(OpBuilder::delete(n).build());
  84. }
  85. }
  86. pub fn insert(&mut self, s: &str, attrs: Attributes) {
  87. if s.is_empty() {
  88. return;
  89. }
  90. self.target_len += num_chars(s.as_bytes());
  91. let new_last = match self.ops.as_mut_slice() {
  92. [.., Operation::Insert(insert)] => {
  93. //
  94. insert.merge_or_new_op(s, attrs)
  95. },
  96. [.., Operation::Insert(pre_insert), Operation::Delete(_)] => {
  97. //
  98. pre_insert.merge_or_new_op(s, attrs)
  99. },
  100. [.., op_last @ Operation::Delete(_)] => {
  101. let new_last = op_last.clone();
  102. *op_last = OpBuilder::insert(s).attributes(attrs).build();
  103. Some(new_last)
  104. },
  105. _ => Some(OpBuilder::insert(s).attributes(attrs).build()),
  106. };
  107. match new_last {
  108. None => {},
  109. Some(new_last) => self.ops.push(new_last),
  110. }
  111. }
  112. pub fn retain(&mut self, n: usize, attrs: Attributes) {
  113. if n == 0 {
  114. return;
  115. }
  116. self.base_len += n as usize;
  117. self.target_len += n as usize;
  118. if let Some(Operation::Retain(retain)) = self.ops.last_mut() {
  119. if let Some(new_op) = retain.merge_or_new_op(n, attrs) {
  120. self.ops.push(new_op);
  121. }
  122. } else {
  123. self.ops
  124. .push(OpBuilder::retain(n).attributes(attrs).build());
  125. }
  126. }
  127. /// Merges the operation with `other` into one operation while preserving
  128. /// the changes of both. Or, in other words, for each input string S and a
  129. /// pair of consecutive operations A and B.
  130. /// `apply(apply(S, A), B) = apply(S, compose(A, B))`
  131. /// must hold.
  132. pub fn compose(&self, other: &Self) -> Result<Self, OTError> {
  133. let mut new_delta = Delta::default();
  134. let mut iter = DeltaIter::new(self);
  135. let mut other_iter = DeltaIter::new(other);
  136. while iter.has_next() || other_iter.has_next() {
  137. if other_iter.is_next_insert() {
  138. new_delta.add(other_iter.next_op().unwrap());
  139. continue;
  140. }
  141. if iter.is_next_delete() {
  142. new_delta.add(iter.next_op().unwrap());
  143. continue;
  144. }
  145. let length = min(
  146. iter.next_op_len().unwrap_or(MAX_IV_LEN),
  147. other_iter.next_op_len().unwrap_or(MAX_IV_LEN),
  148. );
  149. let op = iter
  150. .next_op_with_len(length)
  151. .unwrap_or(OpBuilder::retain(length).build());
  152. let other_op = other_iter
  153. .next_op_with_len(length)
  154. .unwrap_or(OpBuilder::retain(length).build());
  155. debug_assert_eq!(op.len(), other_op.len());
  156. match (&op, &other_op) {
  157. (Operation::Retain(retain), Operation::Retain(other_retain)) => {
  158. let composed_attrs = compose_attributes(
  159. retain.attributes.clone(),
  160. other_retain.attributes.clone(),
  161. );
  162. new_delta.add(
  163. OpBuilder::retain(retain.n)
  164. .attributes(composed_attrs)
  165. .build(),
  166. )
  167. },
  168. (Operation::Insert(insert), Operation::Retain(other_retain)) => {
  169. let mut composed_attrs = compose_attributes(
  170. insert.attributes.clone(),
  171. other_retain.attributes.clone(),
  172. );
  173. composed_attrs.remove_empty();
  174. new_delta.add(
  175. OpBuilder::insert(op.get_data())
  176. .attributes(composed_attrs)
  177. .build(),
  178. )
  179. },
  180. (Operation::Retain(_), Operation::Delete(_)) => {
  181. new_delta.add(other_op);
  182. },
  183. (a, b) => {
  184. debug_assert_eq!(a.is_insert(), true);
  185. debug_assert_eq!(b.is_delete(), true);
  186. continue;
  187. },
  188. }
  189. }
  190. Ok(new_delta)
  191. }
  192. #[deprecated(
  193. note = "The same as compose except it requires the target_len of self must equal to other's base_len"
  194. )]
  195. pub fn compose2(&self, other: &Self) -> Result<Self, OTError> {
  196. if self.target_len != other.base_len {
  197. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  198. }
  199. let mut new_delta = Delta::default();
  200. let mut ops1 = self.ops.iter().cloned();
  201. let mut ops2 = other.ops.iter().cloned();
  202. let mut next_op1 = ops1.next();
  203. let mut next_op2 = ops2.next();
  204. loop {
  205. match (&next_op1, &next_op2) {
  206. (None, None) => break,
  207. (Some(Operation::Delete(i)), _) => {
  208. new_delta.delete(*i);
  209. next_op1 = ops1.next();
  210. },
  211. (_, Some(Operation::Insert(o_insert))) => {
  212. new_delta.insert(&o_insert.s, o_insert.attributes.clone());
  213. next_op2 = ops2.next();
  214. },
  215. (None, _) | (_, None) => {
  216. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  217. },
  218. (Some(Operation::Retain(retain)), Some(Operation::Retain(o_retain))) => {
  219. let composed_attrs = compose_operation(&next_op1, &next_op2);
  220. log::debug!(
  221. "[retain:{} - retain:{}]: {:?}",
  222. retain.n,
  223. o_retain.n,
  224. composed_attrs
  225. );
  226. match retain.cmp(&o_retain) {
  227. Ordering::Less => {
  228. new_delta.retain(retain.n, composed_attrs);
  229. next_op2 = Some(
  230. OpBuilder::retain(o_retain.n - retain.n)
  231. .attributes(o_retain.attributes.clone())
  232. .build(),
  233. );
  234. next_op1 = ops1.next();
  235. },
  236. std::cmp::Ordering::Equal => {
  237. new_delta.retain(retain.n, composed_attrs);
  238. next_op1 = ops1.next();
  239. next_op2 = ops2.next();
  240. },
  241. std::cmp::Ordering::Greater => {
  242. new_delta.retain(o_retain.n, composed_attrs);
  243. next_op1 = Some(OpBuilder::retain(retain.n - o_retain.n).build());
  244. next_op2 = ops2.next();
  245. },
  246. }
  247. },
  248. (Some(Operation::Insert(insert)), Some(Operation::Delete(o_num))) => {
  249. match (num_chars(insert.as_bytes()) as usize).cmp(o_num) {
  250. Ordering::Less => {
  251. next_op2 = Some(
  252. OpBuilder::delete(*o_num - num_chars(insert.as_bytes()) as usize)
  253. .attributes(insert.attributes.clone())
  254. .build(),
  255. );
  256. next_op1 = ops1.next();
  257. },
  258. Ordering::Equal => {
  259. next_op1 = ops1.next();
  260. next_op2 = ops2.next();
  261. },
  262. Ordering::Greater => {
  263. next_op1 = Some(
  264. OpBuilder::insert(
  265. &insert.chars().skip(*o_num as usize).collect::<String>(),
  266. )
  267. .build(),
  268. );
  269. next_op2 = ops2.next();
  270. },
  271. }
  272. },
  273. (Some(Operation::Insert(insert)), Some(Operation::Retain(o_retain))) => {
  274. let mut composed_attrs = compose_operation(&next_op1, &next_op2);
  275. composed_attrs.remove_empty();
  276. log::debug!(
  277. "compose: [{} - {}], composed_attrs: {}",
  278. insert,
  279. o_retain,
  280. composed_attrs
  281. );
  282. match (insert.num_chars()).cmp(o_retain) {
  283. Ordering::Less => {
  284. new_delta.insert(&insert.s, composed_attrs.clone());
  285. next_op2 = Some(
  286. OpBuilder::retain(o_retain.n - insert.num_chars())
  287. .attributes(o_retain.attributes.clone())
  288. .build(),
  289. );
  290. next_op1 = ops1.next();
  291. },
  292. Ordering::Equal => {
  293. new_delta.insert(&insert.s, composed_attrs);
  294. next_op1 = ops1.next();
  295. next_op2 = ops2.next();
  296. },
  297. Ordering::Greater => {
  298. let chars = &mut insert.chars();
  299. new_delta.insert(
  300. &chars.take(o_retain.n as usize).collect::<String>(),
  301. composed_attrs,
  302. );
  303. next_op1 = Some(OpBuilder::insert(&chars.collect::<String>()).build());
  304. next_op2 = ops2.next();
  305. },
  306. }
  307. },
  308. (Some(Operation::Retain(retain)), Some(Operation::Delete(o_num))) => {
  309. match retain.cmp(&o_num) {
  310. Ordering::Less => {
  311. new_delta.delete(retain.n);
  312. next_op2 = Some(OpBuilder::delete(*o_num - retain.n).build());
  313. next_op1 = ops1.next();
  314. },
  315. Ordering::Equal => {
  316. new_delta.delete(*o_num);
  317. next_op2 = ops2.next();
  318. next_op1 = ops1.next();
  319. },
  320. Ordering::Greater => {
  321. new_delta.delete(*o_num);
  322. next_op1 = Some(OpBuilder::retain(retain.n - *o_num).build());
  323. next_op2 = ops2.next();
  324. },
  325. }
  326. },
  327. };
  328. }
  329. Ok(new_delta)
  330. }
  331. /// Transforms two operations A and B that happened concurrently and
  332. /// produces two operations A' and B' (in an array) such that
  333. /// `apply(apply(S, A), B') = apply(apply(S, B), A')`.
  334. /// This function is the heart of OT.
  335. ///
  336. /// # Error
  337. ///
  338. /// Returns an `OTError` if the operations cannot be transformed due to
  339. /// length conflicts.
  340. pub fn transform(&self, other: &Self) -> Result<(Self, Self), OTError> {
  341. if self.base_len != other.base_len {
  342. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  343. }
  344. let mut a_prime = Delta::default();
  345. let mut b_prime = Delta::default();
  346. let mut ops1 = self.ops.iter().cloned();
  347. let mut ops2 = other.ops.iter().cloned();
  348. let mut next_op1 = ops1.next();
  349. let mut next_op2 = ops2.next();
  350. loop {
  351. match (&next_op1, &next_op2) {
  352. (None, None) => break,
  353. (Some(Operation::Insert(insert)), _) => {
  354. // let composed_attrs = transform_attributes(&next_op1, &next_op2, true);
  355. a_prime.insert(&insert.s, insert.attributes.clone());
  356. b_prime.retain(insert.num_chars(), insert.attributes.clone());
  357. next_op1 = ops1.next();
  358. },
  359. (_, Some(Operation::Insert(o_insert))) => {
  360. let composed_attrs = transform_operation(&next_op1, &next_op2);
  361. a_prime.retain(o_insert.num_chars(), composed_attrs.clone());
  362. b_prime.insert(&o_insert.s, composed_attrs);
  363. next_op2 = ops2.next();
  364. },
  365. (None, _) => {
  366. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  367. },
  368. (_, None) => {
  369. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  370. },
  371. (Some(Operation::Retain(retain)), Some(Operation::Retain(o_retain))) => {
  372. let composed_attrs = transform_operation(&next_op1, &next_op2);
  373. match retain.cmp(&o_retain) {
  374. Ordering::Less => {
  375. a_prime.retain(retain.n, composed_attrs.clone());
  376. b_prime.retain(retain.n, composed_attrs.clone());
  377. next_op2 = Some(OpBuilder::retain(o_retain.n - retain.n).build());
  378. next_op1 = ops1.next();
  379. },
  380. Ordering::Equal => {
  381. a_prime.retain(retain.n, composed_attrs.clone());
  382. b_prime.retain(retain.n, composed_attrs.clone());
  383. next_op1 = ops1.next();
  384. next_op2 = ops2.next();
  385. },
  386. Ordering::Greater => {
  387. a_prime.retain(o_retain.n, composed_attrs.clone());
  388. b_prime.retain(o_retain.n, composed_attrs.clone());
  389. next_op1 = Some(OpBuilder::retain(retain.n - o_retain.n).build());
  390. next_op2 = ops2.next();
  391. },
  392. };
  393. },
  394. (Some(Operation::Delete(i)), Some(Operation::Delete(j))) => match i.cmp(&j) {
  395. Ordering::Less => {
  396. next_op2 = Some(OpBuilder::delete(*j - *i).build());
  397. next_op1 = ops1.next();
  398. },
  399. Ordering::Equal => {
  400. next_op1 = ops1.next();
  401. next_op2 = ops2.next();
  402. },
  403. Ordering::Greater => {
  404. next_op1 = Some(OpBuilder::delete(*i - *j).build());
  405. next_op2 = ops2.next();
  406. },
  407. },
  408. (Some(Operation::Delete(i)), Some(Operation::Retain(o_retain))) => {
  409. match i.cmp(&o_retain) {
  410. Ordering::Less => {
  411. a_prime.delete(*i);
  412. next_op2 = Some(OpBuilder::retain(o_retain.n - *i).build());
  413. next_op1 = ops1.next();
  414. },
  415. Ordering::Equal => {
  416. a_prime.delete(*i);
  417. next_op1 = ops1.next();
  418. next_op2 = ops2.next();
  419. },
  420. Ordering::Greater => {
  421. a_prime.delete(o_retain.n);
  422. next_op1 = Some(OpBuilder::delete(*i - o_retain.n).build());
  423. next_op2 = ops2.next();
  424. },
  425. };
  426. },
  427. (Some(Operation::Retain(retain)), Some(Operation::Delete(j))) => {
  428. match retain.cmp(&j) {
  429. Ordering::Less => {
  430. b_prime.delete(retain.n);
  431. next_op2 = Some(OpBuilder::delete(*j - retain.n).build());
  432. next_op1 = ops1.next();
  433. },
  434. Ordering::Equal => {
  435. b_prime.delete(retain.n);
  436. next_op1 = ops1.next();
  437. next_op2 = ops2.next();
  438. },
  439. Ordering::Greater => {
  440. b_prime.delete(*j);
  441. next_op1 = Some(OpBuilder::retain(retain.n - *j).build());
  442. next_op2 = ops2.next();
  443. },
  444. };
  445. },
  446. }
  447. }
  448. Ok((a_prime, b_prime))
  449. }
  450. /// Applies an operation to a string, returning a new string.
  451. ///
  452. /// # Error
  453. ///
  454. /// Returns an error if the operation cannot be applied due to length
  455. /// conflicts.
  456. pub fn apply(&self, s: &str) -> Result<String, OTError> {
  457. if num_chars(s.as_bytes()) != self.base_len {
  458. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  459. }
  460. let mut new_s = String::new();
  461. let chars = &mut s.chars();
  462. for op in &self.ops {
  463. match &op {
  464. Operation::Retain(retain) => {
  465. for c in chars.take(retain.n as usize) {
  466. new_s.push(c);
  467. }
  468. },
  469. Operation::Delete(delete) => {
  470. for _ in 0..*delete {
  471. chars.next();
  472. }
  473. },
  474. Operation::Insert(insert) => {
  475. new_s += &insert.s;
  476. },
  477. }
  478. }
  479. Ok(new_s)
  480. }
  481. /// Computes the inverse of an operation. The inverse of an operation is the
  482. /// operation that reverts the effects of the operation
  483. pub fn invert_str(&self, s: &str) -> Self {
  484. let mut inverted = Delta::default();
  485. let chars = &mut s.chars();
  486. for op in &self.ops {
  487. match &op {
  488. Operation::Retain(retain) => {
  489. inverted.retain(retain.n, Attributes::default());
  490. // TODO: use advance_by instead, but it's unstable now
  491. // chars.advance_by(retain.num)
  492. for _ in 0..retain.n {
  493. chars.next();
  494. }
  495. },
  496. Operation::Insert(insert) => {
  497. inverted.delete(insert.num_chars());
  498. },
  499. Operation::Delete(delete) => {
  500. inverted.insert(
  501. &chars.take(*delete as usize).collect::<String>(),
  502. op.get_attributes(),
  503. );
  504. },
  505. }
  506. }
  507. inverted
  508. }
  509. pub fn invert(&self, other: &Delta) -> Delta {
  510. let mut inverted = Delta::default();
  511. if other.is_empty() {
  512. return inverted;
  513. }
  514. log::debug!("🌜Calculate invert delta");
  515. log::debug!("current: {}", self);
  516. log::debug!("other: {}", other);
  517. let mut index = 0;
  518. for op in &self.ops {
  519. let len: usize = op.len() as usize;
  520. match op {
  521. Operation::Delete(n) => {
  522. invert_from_other(&mut inverted, other, op, index, index + *n);
  523. index += len;
  524. },
  525. Operation::Retain(_) => {
  526. match op.has_attribute() {
  527. true => invert_from_other(&mut inverted, other, op, index, index + len),
  528. false => {
  529. log::debug!(
  530. "invert retain: {} by retain {} {}",
  531. op,
  532. len,
  533. op.get_attributes()
  534. );
  535. inverted.retain(len as usize, op.get_attributes())
  536. },
  537. }
  538. index += len;
  539. },
  540. Operation::Insert(_) => {
  541. log::debug!("invert insert: {} by delete {}", op, len);
  542. inverted.delete(len as usize);
  543. },
  544. }
  545. }
  546. log::debug!("🌛invert result: {}", inverted);
  547. inverted
  548. }
  549. /// Checks if this operation has no effect.
  550. #[inline]
  551. pub fn is_noop(&self) -> bool {
  552. match self.ops.as_slice() {
  553. [] => true,
  554. [Operation::Retain(_)] => true,
  555. _ => false,
  556. }
  557. }
  558. pub fn is_empty(&self) -> bool { self.ops.is_empty() }
  559. pub fn to_json(&self) -> String { serde_json::to_string(self).unwrap_or("".to_owned()) }
  560. pub fn extend(&mut self, other: Self) { other.ops.into_iter().for_each(|op| self.add(op)); }
  561. }
  562. fn invert_from_other(
  563. base: &mut Delta,
  564. other: &Delta,
  565. operation: &Operation,
  566. start: usize,
  567. end: usize,
  568. ) {
  569. log::debug!("invert op: {} [{}:{}]", operation, start, end);
  570. let other_ops = DeltaIter::from_interval(other, Interval::new(start, end)).ops();
  571. other_ops.into_iter().for_each(|other_op| match operation {
  572. Operation::Delete(n) => {
  573. log::debug!("invert delete: {} by add {}", n, other_op);
  574. base.add(other_op);
  575. },
  576. Operation::Retain(retain) => {
  577. log::debug!(
  578. "invert attributes: {:?}, {:?}",
  579. operation.get_attributes(),
  580. other_op.get_attributes()
  581. );
  582. let inverted_attrs =
  583. invert_attributes(operation.get_attributes(), other_op.get_attributes());
  584. log::debug!("invert attributes result: {:?}", inverted_attrs);
  585. log::debug!(
  586. "invert retain: {} by retain len: {}, {}",
  587. retain,
  588. other_op.len(),
  589. inverted_attrs
  590. );
  591. base.retain(other_op.len(), inverted_attrs);
  592. },
  593. Operation::Insert(_) => {
  594. log::error!("Impossible to here. Insert operation should be treated as delete")
  595. },
  596. });
  597. }