1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use rle::SplitableSpan;
use crate::{Toggleable, ContentLength};
use rle::Searchable;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct TestRange {
pub id: u32,
pub len: u32,
pub is_activated: bool,
}
impl Default for TestRange {
fn default() -> Self {
Self {
id: u32::MAX,
len: u32::MAX,
is_activated: false
}
}
}
impl SplitableSpan for TestRange {
fn len(&self) -> usize { self.len as usize }
fn truncate(&mut self, at: usize) -> Self {
assert!(at > 0 && at < self.len as usize);
let other = Self {
id: self.id + at as u32,
len: self.len - at as u32,
is_activated: self.is_activated
};
self.len = at as u32;
other
}
fn truncate_keeping_right(&mut self, at: usize) -> Self {
let mut other = *self;
*self = other.truncate(at);
other
}
fn can_append(&self, other: &Self) -> bool {
other.id == self.id + self.len && other.is_activated == self.is_activated
}
fn append(&mut self, other: Self) {
assert!(self.can_append(&other));
self.len += other.len;
}
fn prepend(&mut self, other: Self) {
assert!(other.can_append(&self));
self.len += other.len;
self.id = other.id;
}
}
impl Toggleable for TestRange {
fn is_activated(&self) -> bool {
self.is_activated
}
fn mark_activated(&mut self) {
assert!(!self.is_activated);
self.is_activated = true;
}
fn mark_deactivated(&mut self) {
assert!(self.is_activated);
self.is_activated = false;
}
}
impl ContentLength for TestRange {
fn content_len(&self) -> usize {
if self.is_activated { self.len() } else { 0 }
}
}
impl Searchable for TestRange {
type Item = (u32, bool);
fn contains(&self, loc: Self::Item) -> Option<usize> {
if self.is_activated == loc.1 && loc.0 >= self.id && loc.0 < (self.id + self.len) {
Some((loc.0 - self.id) as usize)
} else { None }
}
fn at_offset(&self, offset: usize) -> Self::Item {
(offset as u32 + self.id, self.is_activated)
}
}