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
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};

use crate::BitBlock;

/// [`BitBlock`]s that use Little Endian order instead of Big Endian Order. \
/// # Example
/// ```
/// # use bitmap32::BitMap;
/// # use bitmap32::LE;
/// let map = BitMap::from_raw_le([0b00000001u8, 0b01000000]);
///
/// assert_eq!(map.test(0), Some(true));
/// assert_eq!(map.test(9), Some(false));
/// assert_eq!(map.test(14), Some(true));
/// assert_eq!(map.test(7), Some(false));
/// ```
#[cfg_attr(feature = "alloc", derive(Debug))]
#[cfg_attr(feature = "std", derive(Hash))]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct LE<B: BitBlock>(pub B);

macro_rules! impl_le_bit_block {
    {$type:ty} => {
        unsafe impl BitBlock for LE<$type> {
            const ZERO: Self = LE(0);

            const UPSTREAM_ONE: Self = LE(1);

            const BITS: usize = <$type as BitBlock>::BITS;

            #[inline(always)]
            fn wrapping_shift_downstream(self, n: usize) -> Self {
                LE(self.0.wrapping_shift_upstream(n))
            }

            #[inline(always)]
            fn wrapping_shift_upstream(self, n: usize) -> Self {
                LE(self.0.wrapping_shift_downstream(n))
            }

            #[inline(always)]
            fn leading_zeros(self) -> usize {
                self.0.trailing_zeros() as usize
            }

            #[inline(always)]
            fn leading_ones(self) -> usize {
                self.0.trailing_ones() as usize
            }
        }
    }
}

impl_le_bit_block! {u8}
impl_le_bit_block! {u16}
impl_le_bit_block! {u32}
impl_le_bit_block! {u64}
impl_le_bit_block! {u128}

impl<B: BitBlock> Not for LE<B> {
    type Output = LE<<B as Not>::Output>;

    #[inline(always)]
    fn not(self) -> Self::Output {
        Self(!self.0)
    }
}

impl<B: BitBlock> BitOr for LE<B> {
    type Output = LE<<B as BitOr>::Output>;

    #[inline(always)]
    fn bitor(self, rhs: Self) -> Self::Output {
        LE(self.0.bitor(rhs.0))
    }
}

impl<B: BitBlock> BitAnd for LE<B> {
    type Output = LE<<B as BitAnd>::Output>;

    #[inline(always)]
    fn bitand(self, rhs: Self) -> Self::Output {
        LE(self.0.bitand(rhs.0))
    }
}

impl<B: BitBlock> BitAndAssign for LE<B> {
    #[inline(always)]
    fn bitand_assign(&mut self, rhs: Self) {
        self.0.bitand_assign(rhs.0)
    }
}

impl<B: BitBlock> BitOrAssign for LE<B> {
    #[inline(always)]
    fn bitor_assign(&mut self, rhs: Self) {
        self.0.bitor_assign(rhs.0)
    }
}