#[cfg(not(feature = "portable-atomic"))]
use alloc::sync::Arc;
use core::hash::{Hash, Hasher};
use core::ops;
#[cfg(feature = "portable-atomic")]
use portable_atomic_util::Arc;
use crate::{Expression, RelationalOperator, Strength, Term, Variable, WeightedRelation};
#[derive(Debug)]
struct Inner {
expression: Expression,
strength: Strength,
operator: RelationalOperator,
}
#[derive(Clone, Debug)]
pub struct Constraint {
inner: Arc<Inner>,
}
impl Constraint {
pub fn new(
expression: Expression,
operator: RelationalOperator,
strength: Strength,
) -> Constraint {
Constraint {
inner: Arc::new(Inner {
expression,
operator,
strength,
}),
}
}
pub fn expr(&self) -> &Expression {
&self.inner.expression
}
pub fn op(&self) -> RelationalOperator {
self.inner.operator
}
pub fn strength(&self) -> Strength {
self.inner.strength
}
}
impl Hash for Constraint {
fn hash<H: Hasher>(&self, hasher: &mut H) {
use core::ops::Deref;
hasher.write_usize(self.inner.deref() as *const _ as usize);
}
}
impl PartialEq for Constraint {
fn eq(&self, other: &Constraint) -> bool {
use core::ops::Deref;
core::ptr::eq(self.inner.deref(), other.inner.deref())
}
}
impl Eq for Constraint {}
pub struct PartialConstraint {
expression: Expression,
relation: WeightedRelation,
}
impl PartialConstraint {
pub const fn new(expression: Expression, relation: WeightedRelation) -> PartialConstraint {
PartialConstraint {
expression,
relation,
}
}
}
impl ops::BitOr<f64> for PartialConstraint {
type Output = Constraint;
fn bitor(self, rhs: f64) -> Constraint {
let (operator, strength) = self.relation.into();
#[allow(clippy::suspicious_arithmetic_impl)]
Constraint::new(self.expression - rhs, operator, strength)
}
}
impl ops::BitOr<f32> for PartialConstraint {
type Output = Constraint;
fn bitor(self, rhs: f32) -> Constraint {
self.bitor(rhs as f64)
}
}
impl ops::BitOr<Variable> for PartialConstraint {
type Output = Constraint;
fn bitor(self, rhs: Variable) -> Constraint {
let (operator, strength) = self.relation.into();
#[allow(clippy::suspicious_arithmetic_impl)]
Constraint::new(self.expression - rhs, operator, strength)
}
}
impl ops::BitOr<Term> for PartialConstraint {
type Output = Constraint;
fn bitor(self, rhs: Term) -> Constraint {
let (operator, strength) = self.relation.into();
#[allow(clippy::suspicious_arithmetic_impl)]
Constraint::new(self.expression - rhs, operator, strength)
}
}
impl ops::BitOr<Expression> for PartialConstraint {
type Output = Constraint;
fn bitor(self, rhs: Expression) -> Constraint {
let (operator, strength) = self.relation.into();
#[allow(clippy::suspicious_arithmetic_impl)]
Constraint::new(self.expression - rhs, operator, strength)
}
}