This repository was archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray.rs
321 lines (283 loc) · 7.92 KB
/
array.rs
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
pub trait ArrayContent {
fn validate_bytes(bytes: &[u8]);
fn expected_contents() -> &'static str;
}
/// Fixed-sized container for a short DNA sequence or quality.
/// The capacity is determined by the type `N` and the contents are validates based on type `T`
/// Typically used as a convenient container for barcode or UMI sequences or quality.
#[derive(Clone, Copy, PartialOrd, Ord, Eq)]
pub struct ByteArray<T, const N: usize>
where
T: ArrayContent,
{
bytes: [u8; N],
length: u8,
phantom: PhantomData<T>,
}
impl<T, const N: usize> ByteArray<T, N>
where
T: ArrayContent,
{
pub fn new() -> Self {
ByteArray {
length: 0,
bytes: [0; N],
phantom: PhantomData,
}
}
/// Caller needs to ensure that the bytes are valid
pub fn push_unchecked(&mut self, src: &[u8]) {
let len = self.length as usize;
assert!(src.len() <= (N - len), "Input slice has length {} which exceeds the remaining capacity of {} bytes in the ByteArray", src.len(), N-len);
self.bytes[len..len + src.len()].copy_from_slice(src);
self.length += src.len() as u8;
}
pub fn push(&mut self, src: &[u8]) {
T::validate_bytes(src);
self.push_unchecked(src);
}
/// Remove the element at pos and return it
pub fn remove(&mut self, pos: usize) -> u8 {
let val = self[pos];
self.as_mut_bytes().split_at_mut(pos).1.rotate_left(1);
self.length -= 1;
self.bytes[self.length as usize] = 0;
val
}
///Insert the element at pos without validating
///
/// Panics if the capacity is full or if the pos is > self.len()
pub fn insert_unchecked(&mut self, pos: usize, byte: u8) {
let curr_len = self.len();
assert!(
curr_len <= (N - 1),
"No remaining capacity to insert a byte into ByteArray (N={N}).",
);
assert!(
pos <= curr_len,
"Cannot insert at position {pos} that is more that the array length {curr_len}.",
);
self.length += 1;
self.as_mut_bytes().split_at_mut(pos).1.rotate_right(1);
self[pos] = byte;
}
/// Create a new ByteArray from the given byte slice
/// The byte slice should contain only valid alphabets as defined by ArrayContent trait
/// otherwise this function will panic
pub fn from_bytes(src: &[u8]) -> Self {
let mut arr = Self::new();
arr.push(src);
arr
}
/// Create a new ByteArray from the given byte slice
/// Caller needs to ensure that the byte slice contains only valid alphabets as defined by ArrayContent trait
pub fn from_bytes_unchecked(src: &[u8]) -> Self {
let mut arr = Self::new();
arr.push_unchecked(src);
arr
}
pub fn from_iter_unchecked<C, D>(src: D) -> Self
where
C: Borrow<u8>,
D: IntoIterator<Item = C>,
{
let mut src = src.into_iter().fuse();
let mut bytes = [0; N];
let mut len = 0;
for (l, r) in bytes.iter_mut().zip(&mut src) {
*l = *r.borrow();
len += 1;
}
assert!(
src.next().is_none(),
"Error: Input iter exceeds capacity of {} bytes.",
bytes.len()
);
ByteArray {
length: len,
bytes,
phantom: PhantomData,
}
}
/// Returns a byte slice of the contents.
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[0..self.length as usize]
}
/// Returns a str of the contents.
pub fn as_str(&self) -> &str {
std::str::from_utf8(self.as_bytes()).unwrap()
}
/// Returns a mutable byte slice of the contents.
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
&mut self.bytes[0..self.length as usize]
}
/// Returns the length of this sequence, in bytes.
pub fn len(&self) -> usize {
self.length as usize
}
/// Returns true if self has a length of zero bytes.
pub fn is_empty(&self) -> bool {
self.length == 0
}
/// Returns an iterator over the bytes.
pub fn iter(&self) -> std::slice::Iter<'_, u8> {
self.as_bytes().iter()
}
}
impl<T, const N: usize> Default for ByteArray<T, N>
where
T: ArrayContent,
{
fn default() -> Self {
Self::new()
}
}
impl<T, C, const N: usize> FromIterator<C> for ByteArray<T, N>
where
T: ArrayContent,
C: Borrow<u8>,
{
fn from_iter<D: IntoIterator<Item = C>>(src: D) -> Self
where
C: Borrow<u8>,
{
let array = ByteArray::from_iter_unchecked(src);
T::validate_bytes(array.as_bytes());
array
}
}
impl<T, const N: usize> fmt::Display for ByteArray<T, N>
where
T: ArrayContent,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<T, const N: usize> fmt::Debug for ByteArray<T, N>
where
T: ArrayContent,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self, f)
}
}
impl<T, const N: usize> Index<usize> for ByteArray<T, N>
where
T: ArrayContent,
{
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
assert!(index < self.length as usize, "index out of bounds");
&self.bytes[index]
}
}
impl<T, const N: usize> IndexMut<usize> for ByteArray<T, N>
where
T: ArrayContent,
{
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
assert!(index < self.length as usize, "index out of bounds");
&mut self.bytes[index]
}
}
impl<T, const N: usize> AsRef<[u8]> for ByteArray<T, N>
where
T: ArrayContent,
{
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl<T, const N: usize> From<ByteArray<T, N>> for String
where
T: ArrayContent,
{
fn from(v: ByteArray<T, N>) -> String {
String::from(v.as_str())
}
}
impl<T, const N: usize> Borrow<[u8]> for ByteArray<T, N>
where
T: ArrayContent,
{
fn borrow(&self) -> &[u8] {
self.as_bytes()
}
}
impl<T, const N: usize> Hash for ByteArray<T, N>
where
T: ArrayContent,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_bytes().hash(state);
}
}
impl<T, const N: usize> PartialEq for ByteArray<T, N>
where
T: ArrayContent,
{
fn eq(&self, other: &Self) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl<T, const N: usize> IntoIterator for ByteArray<T, N>
where
T: ArrayContent,
{
type Item = u8;
type IntoIter = std::iter::Take<std::array::IntoIter<u8, N>>;
fn into_iter(self) -> Self::IntoIter {
IntoIterator::into_iter(self.bytes).take(self.length as usize)
}
}
impl<T, const N: usize> Serialize for ByteArray<T, N>
where
T: ArrayContent,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, T, const N: usize> Deserialize<'de> for ByteArray<T, N>
where
T: ArrayContent,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(ByteArrayVisitor {
phantom_t: PhantomData,
})
}
}
struct ByteArrayVisitor<T, const N: usize> {
phantom_t: PhantomData<[T; N]>,
}
impl<'de, T, const N: usize> Visitor<'de> for ByteArrayVisitor<T, N>
where
T: ArrayContent,
{
type Value = ByteArray<T, N>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(T::expected_contents())
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(ByteArray::from_bytes(value.as_bytes()))
}
}