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 130 131 132 133 134
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Kleen logic: https://en.wikipedia.org/wiki/Three-valued_logic#Kleene_and_Priest_logics
/// A "trilean" value based on Kleen logic.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KleeneValue {
/// False
False = 0,
/// True
True = 1,
/// Either true or false, but we’re not sure which yet.
Unknown,
}
impl From<bool> for KleeneValue {
fn from(b: bool) -> Self {
if b {
Self::True
} else {
Self::False
}
}
}
impl KleeneValue {
/// Turns this Kleene value to a bool, taking the unknown value as an
/// argument.
pub fn to_bool(self, unknown: bool) -> bool {
match self {
Self::True => true,
Self::False => false,
Self::Unknown => unknown,
}
}
/// Return true if any result of f() is true. Otherwise, return the strongest value seen.
/// Returns false if empty, like that of `Iterator`.
#[inline(always)]
pub fn any<T>(
iter: impl Iterator<Item = T>,
f: impl FnMut(T) -> Self,
) -> Self {
Self::any_value(iter, Self::True, Self::False, f)
}
/// Return false if any results of f() is false. Otherwise, return the strongest value seen.
/// Returns true if empty, opposite of `Iterator`.
#[inline(always)]
pub fn any_false<T>(
iter: impl Iterator<Item = T>,
f: impl FnMut(T) -> Self,
) -> Self {
Self::any_value(iter, Self::False, Self::True, f)
}
#[inline(always)]
fn any_value<T>(
iter: impl Iterator<Item = T>,
value: Self,
on_empty: Self,
mut f: impl FnMut(T) -> Self,
) -> Self {
let mut result = None;
for item in iter {
let r = f(item);
if r == value {
return r;
}
if let Some(v) = result.as_mut() {
*v = *v & r;
} else {
result = Some(r);
}
}
result.unwrap_or(on_empty)
}
}
impl std::ops::Not for KleeneValue {
type Output = Self;
fn not(self) -> Self {
match self {
Self::True => Self::False,
Self::False => Self::True,
Self::Unknown => Self::Unknown,
}
}
}
// Implements the logical and operation.
impl std::ops::BitAnd for KleeneValue {
type Output = Self;
fn bitand(self, other: Self) -> Self {
if self == Self::False || other == Self::False {
return Self::False;
}
if self == Self::Unknown || other == Self::Unknown {
return Self::Unknown;
}
Self::True
}
}
// Implements the logical or operation.
impl std::ops::BitOr for KleeneValue {
type Output = Self;
fn bitor(self, other: Self) -> Self {
if self == Self::True || other == Self::True {
return Self::True;
}
if self == Self::Unknown || other == Self::Unknown {
return Self::Unknown;
}
Self::False
}
}
impl std::ops::BitOrAssign for KleeneValue {
fn bitor_assign(&mut self, other: Self) {
*self = *self | other;
}
}
impl std::ops::BitAndAssign for KleeneValue {
fn bitand_assign(&mut self, other: Self) {
*self = *self & other;
}
}