arr_rs/boolean/types/
mod.rs1use std::ops::{
2 BitAnd, BitAndAssign,
3 BitOr, BitOrAssign,
4 BitXor, BitXorAssign,
5 Not,
6 RangeInclusive,
7};
8use rand::Rng;
9
10use crate::{
11 core::prelude::*,
12 numeric::prelude::*,
13};
14
15pub trait BoolNumeric: Numeric + Not +
17BitAnd + BitAndAssign +
18BitOr + BitOrAssign +
19BitXor + BitXorAssign {}
20
21impl ArrayElement for bool {
22
23 fn zero() -> Self {
24 false
25 }
26
27 fn one() -> Self {
28 true
29 }
30
31 fn is_nan(&self) -> bool {
32 false
33 }
34}
35
36impl Numeric for bool {
37
38 fn rand(_: RangeInclusive<Self>) -> Self {
39 let mut rng = rand::thread_rng();
40 rng.gen::<Self>()
41 }
42
43 fn from_usize(value: usize) -> Self {
44 value == 1
45 }
46
47 fn from_f64(value: f64) -> Self {
48 (value - 1.).abs() < 1e-24
49 }
50
51 fn to_usize(&self) -> usize {
52 <usize as From<_>>::from(*self)
53 }
54
55 fn to_isize(&self) -> isize {
56 <isize as From<_>>::from(*self)
57 }
58
59 fn to_i32(&self) -> i32 {
60 <i32 as From<_>>::from(*self)
61 }
62
63 fn to_f64(&self) -> f64 {
64 <f64 as From<_>>::from(<u16 as From<_>>::from(*self))
65 }
66
67 fn is_inf(&self) -> bool {
68 false
69 }
70
71 fn max(&self) -> Self {
72 true
73 }
74
75 fn bitwise_and(&self, other: &Self) -> Self {
76 self & other
77 }
78
79 fn bitwise_or(&self, other: &Self) -> Self {
80 self | other
81 }
82
83 fn bitwise_xor(&self, other: &Self) -> Self {
84 self ^ other
85 }
86
87 fn bitwise_not(&self) -> Self {
88 !*self
89 }
90
91 fn left_shift(&self, other: &Self) -> Self {
92 (self.to_usize() << other.to_usize()) == 1
93 }
94
95 fn right_shift(&self, other: &Self) -> Self {
96 (self.to_usize() >> other.to_usize()) == 1
97 }
98
99 fn binary_repr(&self) -> String {
100 format!("{:b}", self.to_usize())
101 }
102}
103
104impl BoolNumeric for bool {}