bio_seq/seq/
array.rs

1// Copyright 2021-2024 Jeff Knaggs
2// Licensed under the MIT license (http://opensource.org/licenses/MIT)
3// This file may not be copied, modified, or distributed
4// except according to those terms.
5
6use crate::codec::Codec;
7
8use crate::seq::slice::SeqSlice;
9//use crate::seq::Seq;
10
11use crate::{Ba, Bs};
12
13//use bitvec::field::BitField;
14
15//use core::fmt;
16use core::marker::PhantomData;
17use core::ops::Deref;
18use core::ptr;
19
20//use core::ops::{BitAnd, BitOr};
21//use std::hash::{Hash, Hasher};
22
23/// Static bit-packed sequence meant to be accessed as a `&'static SeqSlice`
24///
25/// ```
26/// # use bio_seq::prelude::*;
27/// // internally, `dna!` constructs a `SeqArray<Dna>`
28/// let seq: &'static SeqSlice<Dna> = dna!("ACGTGT");
29/// ```
30#[derive(Debug)]
31#[repr(transparent)]
32pub struct SeqArray<A: Codec, const N: usize, const W: usize> {
33    pub _p: PhantomData<A>,
34    pub ba: Ba<W>,
35}
36
37/*
38impl<A: Codec, const K: usize, const W: usize> Hash for SeqArray<A, K, W> {
39    fn hash<H: Hasher>(&self, state: &mut H) {
40        let bs: &SeqSlice<A> = self.as_ref();
41        bs.hash(state);
42    }
43}
44*/
45
46impl<A: Codec, const N: usize, const W: usize> Deref for SeqArray<A, N, W> {
47    type Target = SeqSlice<A>;
48
49    fn deref(&self) -> &Self::Target {
50        let bs: *const Bs = ptr::from_ref::<Bs>(&self.ba[..N * A::BITS as usize]);
51        unsafe { &*(bs as *const SeqSlice<A>) }
52    }
53}
54
55impl<A: Codec, const N: usize, const W: usize> AsRef<SeqSlice<A>> for SeqArray<A, N, W> {
56    fn as_ref(&self) -> &SeqSlice<A> {
57        self
58    }
59}
60/*
61impl<A: Codec, const N: usize> From<&SeqArray<A, N, 1>> for usize {
62    fn from(slice: &SeqArray<A, N, 1>) -> usize {
63        slice.bs.load_le::<usize>()
64    }
65}
66
67impl<A: Codec, const N: usize, const W: usize> fmt::Display for SeqArray<A, N, W> {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        fmt::Display::fmt(self.as_ref(), f)
70    }
71}
72
73impl<A: Codec, const N: usize, const W: usize> BitAnd for &SeqArray<A, N, W> {
74    type Output = Seq<A>;
75
76    fn bitand(self, rhs: Self) -> Self::Output {
77        let mut bv = self.ba.to_bitvec();
78        bv &= &rhs.ba;
79        bv.truncate(N * A::BITS as usize);
80
81        Seq::<A> {
82            bv,
83            _p: PhantomData,
84        }
85    }
86}
87
88impl<A: Codec, const N: usize, const W: usize> BitOr for &SeqArray<A, N, W> {
89    type Output = Seq<A>;
90
91    fn bitor(self, rhs: Self) -> Self::Output {
92        let mut bv = self.ba.to_bitvec();
93        bv |= &rhs.ba;
94        bv.truncate(N * A::BITS as usize);
95
96        Seq::<A> {
97            bv,
98            _p: PhantomData,
99        }
100    }
101}
102*/