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 std::ops::{
    BitAnd, BitAndAssign,
    BitOr, BitOrAssign,
    BitXor, BitXorAssign,
    Not,
    RangeInclusive,
};
use rand::Rng;

use crate::{
    core::prelude::*,
    numeric::prelude::*,
};

/// Boolean Numeric type for array
pub trait BoolNumeric: Numeric + Not +
BitAnd + BitAndAssign +
BitOr + BitOrAssign +
BitXor + BitXorAssign {}

impl ArrayElement for bool {

    fn zero() -> Self {
        false
    }

    fn one() -> Self {
        true
    }

    fn is_nan(&self) -> bool {
        false
    }
}

impl Numeric for bool {

    fn rand(_: RangeInclusive<Self>) -> Self {
        let mut rng = rand::thread_rng();
        rng.gen::<bool>()
    }

    fn from_usize(value: usize) -> Self {
        value == 1
    }

    fn from_f64(value: f64) -> Self {
        value == 1.
    }

    fn to_usize(&self) -> usize {
        *self as usize
    }

    fn to_i32(&self) -> i32 {
        *self as i32
    }

    fn to_f64(&self) -> f64 {
        self.to_usize() as f64
    }

    fn is_inf(&self) -> bool {
        false
    }

    fn max(&self) -> Self {
        true
    }

    fn bitwise_and(&self, other: &Self) -> Self {
        self & other
    }

    fn bitwise_or(&self, other: &Self) -> Self {
        self | other
    }

    fn bitwise_xor(&self, other: &Self) -> Self {
        self ^ other
    }

    fn bitwise_not(&self) -> Self {
        !*self
    }

    fn left_shift(&self, other: &Self) -> Self {
        (self.to_usize() << other.to_usize()) == 1
    }

    fn right_shift(&self, other: &Self) -> Self {
        (self.to_usize() >> other.to_usize()) == 1
    }

    fn binary_repr(&self) -> String {
        format!("{:b}", *self as usize)
    }
}

impl BoolNumeric for bool {}