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
use std::fmt::{Debug, Write};
use std::iter::FromIterator;
use std::ops::{Index, IndexMut, Range};
pub trait Sequence<'a, Item: 'a, Subsequence: Sequence<'a, Item, Subsequence> + ?Sized>:
Index<usize, Output = Item> + Index<Range<usize>, Output = Subsequence>
{
type Iterator: DoubleEndedIterator<Item = &'a Item>;
fn prefix(&'a self, len: usize) -> &Subsequence {
debug_assert!(len < self.len());
&self[0..len]
}
fn suffix(&'a self, len: usize) -> &Subsequence {
debug_assert!(len < self.len());
&self[self.len() - len..self.len()]
}
fn iter(&'a self) -> Self::Iterator;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn first(&'a self) -> Option<&Item> {
self.iter().next()
}
fn last(&'a self) -> Option<&Item> {
self.iter().last()
}
fn is_proper_subsequence_of(&'a self, other: &Self) -> bool
where
Item: Eq,
{
if self.len() >= other.len() {
return false;
}
for start_index in 0..=other.len() - self.len() {
let mut found_subsequence = true;
for index in 0..self.len() {
if self[index] != other[start_index + index] {
found_subsequence = false;
break;
}
}
if found_subsequence {
return true;
}
}
false
}
fn contains(&'a self, item: &Item) -> bool
where
Item: Eq,
{
self.iter().any(|i| item == i)
}
fn forward_merge_iter_assume_mergeable(
&'a self,
suffix: &'a Self,
) -> std::iter::Chain<Self::Iterator, std::iter::Skip<Self::Iterator>>
where
Item: Eq,
{
let first_item_index = self
.iter()
.enumerate()
.filter(|(_, i)| *i == suffix.first().expect("The given sequence is empty."))
.map(|(i, _)| i)
.next()
.expect("This sequence does not contain the first item of the given sequence.");
self.iter()
.chain(suffix.iter().skip(self.len() - first_item_index))
}
fn backward_merge_iter_assume_mergeable(
&'a self,
suffix: &'a Self,
) -> std::iter::Chain<Self::Iterator, std::iter::Skip<Self::Iterator>>
where
Item: Eq,
{
suffix.forward_merge_iter_assume_mergeable(self)
}
fn to_debug_string(&'a self) -> String
where
Item: Debug,
{
let mut result = String::new();
write!(result, "[").unwrap();
let mut once = true;
for item in self.iter() {
if once {
once = false;
} else {
write!(result, ", ").unwrap();
}
write!(result, "{:?}", item).unwrap();
}
write!(result, "]").unwrap();
result
}
}
pub trait SequenceMut<'a, Item: 'a, Subsequence: SequenceMut<'a, Item, Subsequence> + ?Sized>:
Sequence<'a, Item, Subsequence>
+ IndexMut<usize, Output = Item>
+ IndexMut<Range<usize>, Output = Subsequence>
{
type IteratorMut: Iterator<Item = &'a mut Item>;
fn iter_mut(&'a mut self) -> Self::IteratorMut;
}
pub trait OwnedSequence<'a, Item: 'a, Subsequence: Sequence<'a, Item, Subsequence> + ?Sized>:
Sequence<'a, Item, Subsequence> + Sized
{
}
pub trait CloneableSequence<
'a,
Item: 'a + Clone,
Subsequence: CloneableSequence<'a, Item, Subsequence> + ?Sized,
>: ToOwned
{
}
pub trait EditableSequence<'a, Item: 'a, Subsequence: Sequence<'a, Item, Subsequence> + ?Sized>:
Sequence<'a, Item, Subsequence> + Extend<Item> + IntoIterator<Item = Item> + FromIterator<Item>
{
fn extend_into<
ExtensionItem: Into<Item>,
ExtensionSource: IntoIterator<Item = ExtensionItem>,
>(
&mut self,
extension: ExtensionSource,
) {
self.extend(extension.into_iter().map(Into::into));
}
}
#[cfg(test)]
mod tests {
use crate::interface::Sequence;
#[test]
fn test_merge_sequences_simple() {
let s1 = vec![0, 1, 2, 3, 4, 5];
let s2 = vec![3, 4, 5, 6, 7, 8];
let merged: Vec<_> = s1
.forward_merge_iter_assume_mergeable(&s2)
.copied()
.collect();
debug_assert_eq!(merged, vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);
}
}