bio_seq/seq/
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
// Copyright 2021-2024 Jeff Knaggs
// Licensed under the MIT license (http://opensource.org/licenses/MIT)
// This file may not be copied, modified, or distributed
// except according to those terms.

use crate::codec::Codec;

use crate::seq::slice::SeqSlice;
use crate::seq::Seq;

use crate::{Ba, Bs};

use bitvec::field::BitField;
//use bitvec::prelude::*;

use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;
use core::ptr;

use core::ops::{BitAnd, BitOr};
use std::hash::{Hash, Hasher};

#[derive(Debug)]
#[repr(transparent)]
pub struct SeqArray<A: Codec, const N: usize, const W: usize> {
    pub _p: PhantomData<A>,
    pub ba: Ba<W>,
}

impl<A: Codec, const K: usize, const W: usize> Hash for SeqArray<A, K, W> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let bs: &SeqSlice<A> = self.as_ref();
        bs.hash(state);
    }
}

impl<A: Codec, const N: usize, const W: usize> Deref for SeqArray<A, N, W> {
    type Target = SeqSlice<A>;

    fn deref(&self) -> &Self::Target {
        let bs: *const Bs = ptr::from_ref::<Bs>(&self.ba[..N * A::BITS as usize]);
        unsafe { &*(bs as *const SeqSlice<A>) }
    }
}

impl<A: Codec, const N: usize> From<&SeqArray<A, N, 1>> for usize {
    fn from(slice: &SeqArray<A, N, 1>) -> usize {
        slice.bs.load_le::<usize>()
    }
}

impl<A: Codec, const N: usize, const W: usize, const M: usize, const V: usize>
    PartialEq<SeqArray<A, N, W>> for SeqArray<A, M, V>
{
    fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
        if N == M {
            self.ba == other.ba
        } else {
            false
        }
    }
}

impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqSlice<A>> for SeqArray<A, N, W> {
    fn eq(&self, other: &SeqSlice<A>) -> bool {
        self.as_ref() == other
    }
}

impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqSlice<A>> for &SeqArray<A, N, W> {
    fn eq(&self, other: &SeqSlice<A>) -> bool {
        self.as_ref() == other
    }
}

impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqArray<A, N, W>> for &SeqSlice<A> {
    fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
        *self == other.as_ref()
    }
}

impl<A: Codec, const N: usize, const W: usize> PartialEq<SeqArray<A, N, W>> for SeqSlice<A> {
    fn eq(&self, other: &SeqArray<A, N, W>) -> bool {
        self == other.as_ref()
    }
}

impl<A: Codec, const N: usize, const W: usize> AsRef<SeqSlice<A>> for SeqArray<A, N, W> {
    fn as_ref(&self) -> &SeqSlice<A> {
        self
    }
}

impl<A: Codec, const N: usize, const W: usize> fmt::Display for SeqArray<A, N, W> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.as_ref(), f)
    }
}

impl<A: Codec, const N: usize, const W: usize> BitAnd for &SeqArray<A, N, W> {
    type Output = Seq<A>;

    fn bitand(self, rhs: Self) -> Self::Output {
        let mut bv = self.ba.to_bitvec();
        bv &= &rhs.ba;
        bv.truncate(N * A::BITS as usize);

        Seq::<A> {
            bv,
            _p: PhantomData,
        }
    }
}

impl<A: Codec, const N: usize, const W: usize> BitOr for &SeqArray<A, N, W> {
    type Output = Seq<A>;

    fn bitor(self, rhs: Self) -> Self::Output {
        let mut bv = self.ba.to_bitvec();
        bv |= &rhs.ba;
        bv.truncate(N * A::BITS as usize);

        Seq::<A> {
            bv,
            _p: PhantomData,
        }
    }
}