range_action_map/
range_area.rs1use super::{ArgsType, CutSet, DiffSet, IdentType, Segment};
4#[derive(Debug)]
5pub struct RangeArea<SegmentType: Segment> {
6 pub start: usize,
7 pub end: usize,
8 pub segment: SegmentType,
9}
10
11impl<SegmentType: Segment> RangeArea<SegmentType> {
12 #[inline]
13 pub fn contains(&self, pos: usize) -> bool {
15 self.start <= pos && pos < self.end
16 }
17 pub fn shrink_or_split_if_overlap(
22 &mut self,
23 start: usize,
24 end: usize,
25 args: ArgsType,
26 ) -> DiffSet<SegmentType> {
27 if end <= self.start || self.end <= start {
28 DiffSet::Unchanged
30 } else if start <= self.start && self.end <= end {
31 self.segment.remove(args);
33 DiffSet::Removed
34 } else if self.start < start && end < self.end {
35 let old_end = self.end;
37 self.end = start;
38 DiffSet::Splitted(Self {
39 start: end,
40 end: old_end,
41 segment: self.segment.split_and_remove_middle(start, end, args),
42 })
43 } else if end < self.end {
44 self.segment.shrink_to_right(end, args);
46 self.start = end;
47 DiffSet::Shrinked
48 } else {
49 assert!(self.start < start); self.segment.shrink_to_left(start, args);
52 self.end = start;
53 DiffSet::Shrinked
54 }
55 }
56 pub fn split_and_modify_if_overlap(
59 &mut self,
60 start: usize,
61 end: usize,
62 new_flag: IdentType,
63 args: ArgsType,
64 ) -> CutSet<SegmentType> {
65 if end <= self.start || self.end <= start {
66 CutSet::Unchanged
68 } else if start <= self.start && self.end <= end {
69 self.segment.modify(new_flag, args);
71 CutSet::WholeModified
72 } else if self.start < start && end < self.end {
73 let pos_left = start; let pos_right = end; let (seg_middle, seg_right) = self
77 .segment
78 .modify_middle(pos_left, pos_right, new_flag, args);
79 let old_end = self.end;
80 self.end = start;
81 CutSet::ModifiedMiddle(
82 Self {
83 start: start,
84 end: end,
85 segment: seg_middle,
86 },
87 Self {
88 start: end,
89 end: old_end,
90 segment: seg_right,
91 },
92 )
93 } else if end < self.end {
94 let old_end = self.end;
96 self.end = end;
97 CutSet::ModifiedLeft(Self {
99 start: end,
100 end: old_end,
101 segment: self.segment.modify_left(end, new_flag, args),
102 })
103 } else {
104 assert!(self.start < start); let old_end = self.end;
107 self.end = start;
108 CutSet::ModifiedRight(Self {
109 start: start,
110 end: old_end,
111 segment: self.segment.modify_right(start, new_flag, args),
112 })
113 }
114 }
115
116 pub fn is_overlap_with(&self, start: usize, end: usize) -> bool {
118 !(self.end <= start || self.start >= end)
119 }
120}