range_action_map/
segment.rs

1pub use super::defs::*;
2
3/// 维护一段相同权限的地址区间的线段
4pub trait Segment: Sized {
5    /// 删除这段区间
6    fn remove(&mut self, args: ArgsType);
7    /// 拆分这段区间。self 结构变成左半边,返回右半边
8    ///
9    /// 注意pos 参数为绝对位置而非相对位置
10    fn split(&mut self, pos: usize, args: ArgsType) -> Self;
11    /// 修改区间的属性
12    fn modify(&mut self, new_flag: IdentType, args: ArgsType);
13
14    /// 按 pos 拆分区间,只保留左半边
15    ///
16    /// 注意pos 参数为绝对位置而非相对位置
17    fn shrink_to_left(&mut self, pos: usize, args: ArgsType) {
18        self.split(pos, args).remove(args);
19    }
20    /// 按 pos 拆分区间,只保留右半边
21    ///
22    /// 注意pos 参数为绝对位置而非相对位置
23    fn shrink_to_right(&mut self, pos: usize, args: ArgsType) {
24        let right = self.split(pos, args);
25        self.remove(args);
26        *self = right;
27    }
28    /// 按 pos_left 和 pos_right 把区间拆成三段,**默认不检查 pos_left <= pos_right**。
29    /// 然后 self 结构变成左半边区间,删除中间的区间,返回右半边区间
30    ///
31    /// 注意pos 参数为绝对位置而非相对位置
32    fn split_and_remove_middle(
33        &mut self,
34        pos_left: usize,
35        pos_right: usize,
36        args: ArgsType,
37    ) -> Self {
38        let right = self.split(pos_right, args);
39        self.split(pos_left, args).remove(args);
40        right
41    }
42    /// 按 pos 拆分区间,并将左半边区间的属性修改为 new_flag。
43    /// self 结构变成左半边,返回右半边
44    ///
45    /// 注意pos 参数为绝对位置而非相对位置
46    fn modify_left(&mut self, pos: usize, new_flag: IdentType, args: ArgsType) -> Self {
47        let right = self.split(pos, args);
48        self.modify(new_flag, args);
49        right
50    }
51    /// 按 pos 拆分区间,并将右半边区间的属性修改为 new_flag。
52    /// self 结构变成左半边,返回右半边
53    ///
54    /// 注意pos 参数为绝对位置而非相对位置
55    fn modify_right(&mut self, pos: usize, new_flag: IdentType, args: ArgsType) -> Self {
56        let mut right = self.split(pos, args);
57        right.modify(new_flag, args);
58        right
59    }
60    /// 按 pos_left 和 pos_right 把区间拆成三段,**默认不检查 pos_left <= pos_right**,
61    /// 然后修改中间一段的属性。
62    /// self 结构变成左半边,返回中间和右半边
63    ///
64    /// 注意pos 参数为绝对位置而非相对位置
65    fn modify_middle(
66        &mut self,
67        pos_left: usize,
68        pos_right: usize,
69        new_flag: IdentType,
70        args: ArgsType,
71    ) -> (Self, Self) {
72        let right = self.split(pos_right, args);
73        let mut middle = self.split(pos_left, args);
74        middle.modify(new_flag, args);
75        (middle, right)
76    }
77}