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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::fmt::Debug;
use std::ops::{AddAssign, SubAssign};
use crate::ContentTraits;
pub trait TreeIndex<E: ContentTraits> where Self: Debug + Copy + Clone + PartialEq + Eq {
type IndexUpdate: Debug + Default + PartialEq + Eq;
type IndexValue: Copy + Clone + Default + Debug + AddAssign + SubAssign + PartialEq + Eq + Sized;
fn increment_marker(marker: &mut Self::IndexUpdate, entry: &E);
fn decrement_marker(marker: &mut Self::IndexUpdate, entry: &E);
fn decrement_marker_by_val(marker: &mut Self::IndexUpdate, val: &Self::IndexValue);
fn update_offset_by_marker(offset: &mut Self::IndexValue, by: &Self::IndexUpdate);
fn increment_offset(offset: &mut Self::IndexValue, by: &E);
fn increment_offset_partial(offset: &mut Self::IndexValue, by: &E, at: usize) {
let mut e = *by;
if at < e.len() { e.truncate(at); }
Self::increment_offset(offset, &e);
}
const CAN_COUNT_ITEMS: bool = false;
fn count_items(_idx: Self::IndexValue) -> usize { panic!("Index cannot count items") }
}
pub trait FindContent<E: ContentTraits + ContentLength>: TreeIndex<E> {
fn index_to_content(offset: Self::IndexValue) -> usize;
}
pub trait FindOffset<E: ContentTraits>: TreeIndex<E> {
fn index_to_offset(offset: Self::IndexValue) -> usize;
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ContentIndex;
impl<E: ContentTraits + ContentLength> TreeIndex<E> for ContentIndex {
type IndexUpdate = isize;
type IndexValue = u32;
fn increment_marker(marker: &mut Self::IndexUpdate, entry: &E) {
*marker += entry.content_len() as isize;
}
fn decrement_marker(marker: &mut Self::IndexUpdate, entry: &E) {
*marker -= entry.content_len() as isize;
}
fn decrement_marker_by_val(marker: &mut Self::IndexUpdate, val: &Self::IndexValue) {
*marker -= *val as isize;
}
fn update_offset_by_marker(offset: &mut Self::IndexValue, by: &Self::IndexUpdate) {
*offset = offset.wrapping_add(*by as u32);
}
fn increment_offset(offset: &mut Self::IndexValue, by: &E) {
*offset += by.content_len() as u32;
}
fn increment_offset_partial(offset: &mut Self::IndexValue, by: &E, at: usize) {
*offset += by.content_len().min(at) as u32;
}
}
impl<E: ContentTraits + ContentLength> FindContent<E> for ContentIndex {
fn index_to_content(offset: Self::IndexValue) -> usize { offset as usize }
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct RawPositionIndex;
impl<E: ContentTraits> TreeIndex<E> for RawPositionIndex {
type IndexUpdate = isize;
type IndexValue = u32;
fn increment_marker(marker: &mut Self::IndexUpdate, entry: &E) {
*marker += entry.len() as isize;
}
fn decrement_marker(marker: &mut Self::IndexUpdate, entry: &E) {
*marker -= entry.len() as isize;
}
fn decrement_marker_by_val(marker: &mut Self::IndexUpdate, val: &Self::IndexValue) {
*marker -= *val as isize;
}
fn update_offset_by_marker(offset: &mut Self::IndexValue, by: &Self::IndexUpdate) {
*offset = offset.wrapping_add(*by as u32);
}
fn increment_offset(offset: &mut Self::IndexValue, by: &E) {
*offset += by.len() as u32;
}
fn increment_offset_partial(offset: &mut Self::IndexValue, by: &E, at: usize) {
*offset += by.len().min(at) as u32;
}
const CAN_COUNT_ITEMS: bool = true;
fn count_items(idx: Self::IndexValue) -> usize { idx as usize }
}
impl<E: ContentTraits> FindOffset<E> for RawPositionIndex {
fn index_to_offset(offset: Self::IndexValue) -> usize { offset as usize }
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct FullIndex;
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub struct Pair<V: Copy + Clone + Default + AddAssign + SubAssign + PartialEq + Eq>(pub V, pub V);
impl<V: Copy + Clone + Default + AddAssign + SubAssign + PartialEq + Eq> AddAssign for Pair<V> {
#[inline]
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
self.1 += rhs.1;
}
}
impl<V: Copy + Clone + Default + AddAssign + SubAssign + PartialEq + Eq> SubAssign for Pair<V> {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
self.1 -= rhs.1;
}
}
impl<E: ContentTraits + ContentLength> TreeIndex<E> for FullIndex {
type IndexUpdate = Pair<i32>;
type IndexValue = Pair<u32>;
fn increment_marker(marker: &mut Self::IndexUpdate, entry: &E) {
marker.0 += entry.len() as i32;
marker.1 += entry.content_len() as i32;
}
fn decrement_marker(marker: &mut Self::IndexUpdate, entry: &E) {
marker.0 -= entry.len() as i32;
marker.1 -= entry.content_len() as i32;
}
fn decrement_marker_by_val(marker: &mut Self::IndexUpdate, val: &Self::IndexValue) {
marker.0 -= val.0 as i32;
marker.1 -= val.1 as i32;
}
fn update_offset_by_marker(offset: &mut Self::IndexValue, by: &Self::IndexUpdate) {
offset.0 = offset.0.wrapping_add(by.0 as u32);
offset.1 = offset.1.wrapping_add(by.1 as u32);
}
fn increment_offset(offset: &mut Self::IndexValue, entry: &E) {
offset.0 += entry.len() as u32;
offset.1 += entry.content_len() as u32;
}
fn increment_offset_partial(offset: &mut Self::IndexValue, by: &E, at: usize) {
offset.0 += at as u32;
offset.1 += by.content_len().min(at) as u32;
}
const CAN_COUNT_ITEMS: bool = true;
fn count_items(idx: Self::IndexValue) -> usize { idx.0 as usize }
}
impl<E: ContentTraits + ContentLength> FindContent<E> for FullIndex {
fn index_to_content(offset: Self::IndexValue) -> usize {
offset.1 as usize
}
}
impl<E: ContentTraits + ContentLength> FindOffset<E> for FullIndex {
fn index_to_offset(offset: Self::IndexValue) -> usize {
offset.0 as usize
}
}
pub trait ContentLength {
fn content_len(&self) -> usize;
}
pub trait Toggleable {
fn is_activated(&self) -> bool;
fn is_deactivated(&self) -> bool {
!self.is_activated()
}
fn mark_activated(&mut self);
fn mark_deactivated(&mut self);
}