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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use core::cmp::{Ordering};
use core::marker::{PhantomData};
use super::{Extract, ExtractResult, Span, Stream};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct View<'s, T, P> where T: Extract<'s, P> + Span, P: Copy {
bytes: &'s [u8],
values: usize,
parameter: P,
_marker: PhantomData<*const T>,
}
impl<'s, T, P> View<'s, T, P> where T: Extract<'s, P> + Span, P: Copy {
#[inline]
pub fn bytes(&self) -> &'s [u8] {
self.bytes
}
#[inline]
pub fn len(&self) -> usize {
self.values
}
#[inline]
pub fn is_empty(&self) -> bool {
self.values == 0
}
#[inline]
pub fn get(&self, index: usize) -> Option<&'s [u8]> {
if index < self.values {
let start = index * T::SPAN;
let end = start + T::SPAN;
Some(&self.bytes[start..end])
} else {
None
}
}
#[inline]
pub fn extract(&self, index: usize) -> Option<T> {
Some(Stream(self.get(index)?).extract(self.parameter).unwrap())
}
#[inline]
pub fn try_extract(&self, index: usize) -> Option<ExtractResult<'s, T>> {
Some(Stream(self.get(index)?).extract(self.parameter))
}
#[inline]
pub fn iter(self) -> ViewIter<'s, T, P> {
let values = self.values;
ViewIter { view: self, index: 0, values }
}
#[inline]
pub fn binary_search(&self, value: &T) -> Option<usize> where T: Ord {
self.binary_search_by(|v| v.cmp(value)).map(|(i, _)| i)
}
#[inline]
pub fn binary_search_by<F>(
&self, mut f: F
) -> Option<(usize, T)> where F: FnMut(&T) -> Ordering {
if self.values == 0 {
return None;
}
let mut low = 0isize;
let mut high = self.values as isize - 1;
while low <= high {
let middle = low + ((high - low) / 2);
let value = self.extract(middle as usize).unwrap();
match f(&value) {
Ordering::Equal => return Some((middle as usize, value)),
Ordering::Less => low = middle + 1,
Ordering::Greater => high = middle - 1,
}
}
None
}
#[inline]
pub fn binary_search_by_key<U, F>(
&self, key: &U, mut f: F
) -> Option<(usize, T)> where U: Ord, F: FnMut(&T) -> U {
self.binary_search_by(|v| f(v).cmp(key))
}
#[inline]
pub fn binary_search_bytes(&self, bytes: &[u8]) -> Option<usize> {
self.binary_search_bytes_by(|b| b.cmp(bytes)).map(|(i, _)| i)
}
#[inline]
pub fn binary_search_bytes_by<F>(
&self, mut f: F
) -> Option<(usize, &'s [u8])> where
F: FnMut(&'s [u8]) -> Ordering
{
if self.values == 0 {
return None;
}
let mut low = 0isize;
let mut high = self.values as isize - 1;
while low <= high {
let middle = low + ((high - low) / 2);
let bytes = self.get(middle as usize).unwrap();
match f(&bytes) {
Ordering::Equal => return Some((middle as usize, bytes)),
Ordering::Less => low = middle + 1,
Ordering::Greater => high = middle - 1,
}
}
None
}
#[inline]
pub fn binary_search_bytes_by_key<U, F>(
&self, key: &U, mut f: F
) -> Option<(usize, &'s [u8])> where
U: Ord, F: FnMut(&'s [u8]) -> U
{
self.binary_search_bytes_by(|b| f(b).cmp(key))
}
}
impl<'s, T, P> Extract<'s, (usize, P)> for View<'s, T, P> where
T: Extract<'s, P> + Span,
P: Copy
{
#[inline]
fn extract(
stream: &mut Stream<'s>, (values, parameter): (usize, P)
) -> ExtractResult<'s, Self> {
let bytes = stream.extract(values * T::SPAN)?;
Ok(View { bytes, values, parameter, _marker: PhantomData })
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ViewIter<'s, T, P> where T: Extract<'s, P> + Span, P: Copy {
view: View<'s, T, P>,
index: usize,
values: usize,
}
impl<'s, T, P> Iterator for ViewIter<'s, T, P> where
T: Extract<'s, P> + Span,
P: Copy
{
type Item = T;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.values - self.index;
(size, Some(size))
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.values {
let value = self.view.extract(self.index);
self.index += 1;
value
} else {
None
}
}
}
impl<'s, T, P> DoubleEndedIterator for ViewIter<'s, T, P> where
T: Extract<'s, P> + Span,
P: Copy
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.index < self.values {
let value = self.view.extract(self.values - 1);
self.values -= 1;
value
} else {
None
}
}
}
impl<'s, T, P> ExactSizeIterator for ViewIter<'s, T, P> where
T: Extract<'s, P> + Span,
P: Copy { }
#[cfg(test)]
mod test {
use super::*;
use super::super::Endianness::*;
#[test]
fn test_extract_view() {
let mut stream = Stream(&[1, 2, 3, 4, 5, 6, 7, 8]);
let view: View<u16, _> = stream.extract((4, Little)).unwrap();
assert_eq!(view.extract(0), Some(0x0201));
assert_eq!(view.extract(1), Some(0x0403));
assert_eq!(view.extract(2), Some(0x0605));
assert_eq!(view.extract(3), Some(0x0807));
assert_eq!(view.extract(4), None);
let mut iterator = view.iter();
assert_eq!(iterator.len(), 4);
assert_eq!(iterator.next_back(), Some(0x0807));
assert_eq!(iterator.len(), 3);
assert_eq!(iterator.next(), Some(0x0201));
assert_eq!(iterator.len(), 2);
assert_eq!(iterator.next_back(), Some(0x0605));
assert_eq!(iterator.len(), 1);
assert_eq!(iterator.next(), Some(0x0403));
assert_eq!(iterator.len(), 0);
assert_eq!(iterator.next_back(), None);
assert_eq!(view.binary_search(&0x0000), None);
assert_eq!(view.binary_search(&0x0201), Some(0));
assert_eq!(view.binary_search(&0x0403), Some(1));
assert_eq!(view.binary_search(&0x0605), Some(2));
assert_eq!(view.binary_search(&0x0807), Some(3));
assert_eq!(view.binary_search(&0xFFFF), None);
assert_eq!(view.binary_search_bytes(&[0, 0]), None);
assert_eq!(view.binary_search_bytes(&[1, 2]), Some(0));
assert_eq!(view.binary_search_bytes(&[3, 4]), Some(1));
assert_eq!(view.binary_search_bytes(&[5, 6]), Some(2));
assert_eq!(view.binary_search_bytes(&[7, 8]), Some(3));
assert_eq!(view.binary_search_bytes(&[255, 255]), None);
}
}