iterator.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. use super::cursor::*;
  2. use crate::core::{Attributes, Delta, Interval, Operation};
  3. use std::ops::{Deref, DerefMut};
  4. pub(crate) const MAX_IV_LEN: usize = i32::MAX as usize;
  5. pub struct DeltaIter<'a> {
  6. cursor: Cursor<'a>,
  7. }
  8. impl<'a> DeltaIter<'a> {
  9. pub fn new(delta: &'a Delta) -> Self {
  10. let interval = Interval::new(0, MAX_IV_LEN);
  11. Self::from_interval(delta, interval)
  12. }
  13. pub fn from_interval(delta: &'a Delta, interval: Interval) -> Self {
  14. let cursor = Cursor::new(delta, interval);
  15. Self { cursor }
  16. }
  17. pub fn ops(&mut self) -> Vec<Operation> { self.collect::<Vec<_>>() }
  18. pub fn next_op(&mut self) -> Option<Operation> { self.cursor.next_op() }
  19. pub fn next_op_len(&self) -> Option<usize> {
  20. let interval = self.cursor.next_iv();
  21. if interval.is_empty() {
  22. None
  23. } else {
  24. Some(interval.size())
  25. }
  26. }
  27. pub fn next_op_before(&mut self, index: usize) -> Option<Operation> {
  28. self.cursor.next_op_before(Some(index))
  29. }
  30. pub fn seek<M: Metric>(&mut self, index: usize) {
  31. match M::seek(&mut self.cursor, index) {
  32. Ok(_) => {},
  33. Err(e) => log::error!("Seek fail: {:?}", e),
  34. }
  35. }
  36. pub fn has_next(&self) -> bool { self.cursor.has_next() }
  37. pub fn is_next_insert(&self) -> bool {
  38. match self.cursor.next_iter_op() {
  39. None => false,
  40. Some(op) => op.is_insert(),
  41. }
  42. }
  43. pub fn is_next_retain(&self) -> bool {
  44. match self.cursor.next_iter_op() {
  45. None => false,
  46. Some(op) => op.is_retain(),
  47. }
  48. }
  49. pub fn is_next_delete(&self) -> bool {
  50. match self.cursor.next_iter_op() {
  51. None => false,
  52. Some(op) => op.is_delete(),
  53. }
  54. }
  55. }
  56. impl<'a> Iterator for DeltaIter<'a> {
  57. type Item = Operation;
  58. fn next(&mut self) -> Option<Self::Item> { self.next_op() }
  59. }
  60. pub struct AttributesIter<'a> {
  61. delta_iter: DeltaIter<'a>,
  62. }
  63. impl<'a> AttributesIter<'a> {
  64. pub fn new(delta: &'a Delta) -> Self {
  65. let interval = Interval::new(0, usize::MAX);
  66. Self::from_interval(delta, interval)
  67. }
  68. pub fn from_interval(delta: &'a Delta, interval: Interval) -> Self {
  69. let delta_iter = DeltaIter::from_interval(delta, interval);
  70. Self { delta_iter }
  71. }
  72. pub fn next_or_empty(&mut self) -> Attributes {
  73. match self.next() {
  74. None => Attributes::default(),
  75. Some((_, attributes)) => attributes,
  76. }
  77. }
  78. }
  79. impl<'a> Deref for AttributesIter<'a> {
  80. type Target = DeltaIter<'a>;
  81. fn deref(&self) -> &Self::Target { &self.delta_iter }
  82. }
  83. impl<'a> DerefMut for AttributesIter<'a> {
  84. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.delta_iter }
  85. }
  86. impl<'a> Iterator for AttributesIter<'a> {
  87. type Item = (usize, Attributes);
  88. fn next(&mut self) -> Option<Self::Item> {
  89. let next_op = self.delta_iter.next();
  90. if next_op.is_none() {
  91. return None;
  92. }
  93. let mut length: usize = 0;
  94. let mut attributes = Attributes::new();
  95. match next_op.unwrap() {
  96. Operation::Delete(_n) => {},
  97. Operation::Retain(retain) => {
  98. log::debug!("extend retain attributes with {} ", &retain.attributes);
  99. attributes.extend(retain.attributes.clone());
  100. length = retain.n;
  101. },
  102. Operation::Insert(insert) => {
  103. log::debug!("extend insert attributes with {} ", &insert.attributes);
  104. attributes.extend(insert.attributes.clone());
  105. length = insert.num_chars();
  106. },
  107. }
  108. Some((length, attributes))
  109. }
  110. }