delta.rs 24 KB

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