use crate::variables::VarId;
use crate::model::Model;
pub trait ComparisonOp {
fn eq_op(&self, model: &mut Model, other: VarId);
fn ne_op(&self, model: &mut Model, other: VarId);
fn lt_op(&self, model: &mut Model, other: VarId);
fn le_op(&self, model: &mut Model, other: VarId);
fn gt_op(&self, model: &mut Model, other: VarId);
fn ge_op(&self, model: &mut Model, other: VarId);
}
pub trait BooleanOp {
fn and_op(&self, model: &mut Model, other: VarId);
fn or_op(&self, model: &mut Model, other: VarId);
fn not_op(&self, model: &mut Model);
}
impl ComparisonOp for VarId {
fn eq_op(&self, model: &mut Model, other: VarId) {
model.props.equals(*self, other);
}
fn ne_op(&self, model: &mut Model, other: VarId) {
model.props.not_equals(*self, other);
}
fn lt_op(&self, model: &mut Model, other: VarId) {
model.props.less_than(*self, other);
}
fn le_op(&self, model: &mut Model, other: VarId) {
model.props.less_than_or_equals(*self, other);
}
fn gt_op(&self, model: &mut Model, other: VarId) {
model.props.greater_than(*self, other);
}
fn ge_op(&self, model: &mut Model, other: VarId) {
model.props.greater_than_or_equals(*self, other);
}
}
impl BooleanOp for VarId {
fn and_op(&self, model: &mut Model, other: VarId) {
let _result = model.bool_and(&[*self, other]);
}
fn or_op(&self, model: &mut Model, other: VarId) {
let _result = model.bool_or(&[*self, other]);
}
fn not_op(&self, model: &mut Model) {
let _result = model.bool_not(*self);
}
}
impl Model {
pub fn eq_op(&mut self, left: VarId, right: VarId) {
self.props.equals(left, right);
}
pub fn ne_op(&mut self, left: VarId, right: VarId) {
self.props.not_equals(left, right);
}
pub fn lt_op(&mut self, left: VarId, right: VarId) {
self.props.less_than(left, right);
}
pub fn le_op(&mut self, left: VarId, right: VarId) {
self.props.less_than_or_equals(left, right);
}
pub fn gt_op(&mut self, left: VarId, right: VarId) {
self.props.greater_than(left, right);
}
pub fn ge_op(&mut self, left: VarId, right: VarId) {
self.props.greater_than_or_equals(left, right);
}
pub fn and_op(&mut self, left: VarId, right: VarId) {
let _result = self.bool_and(&[left, right]);
}
pub fn or_op(&mut self, left: VarId, right: VarId) {
let _result = self.bool_or(&[left, right]);
}
pub fn not_op(&mut self, var: VarId) {
let _result = self.bool_not(var);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;
#[test]
fn test_comparison_operators() {
let mut m = Model::default();
let x = m.int(1, 10);
let y = m.int(1, 10);
x.eq_op(&mut m, y);
x.ne_op(&mut m, y);
x.lt_op(&mut m, y);
x.le_op(&mut m, y);
x.gt_op(&mut m, y);
x.ge_op(&mut m, y);
}
#[test]
fn test_boolean_operators() {
let mut m = Model::default();
let a = m.int(0, 1); let b = m.int(0, 1);
a.and_op(&mut m, b);
a.or_op(&mut m, b);
a.not_op(&mut m);
}
#[test]
fn test_model_operator_extensions() {
let mut m = Model::default();
let x = m.int(1, 10);
let y = m.int(1, 10);
m.eq_op(x, y);
m.ne_op(x, y);
m.lt_op(x, y);
m.le_op(x, y);
m.gt_op(x, y);
m.ge_op(x, y);
let a = m.int(0, 1); let b = m.int(0, 1); m.and_op(a, b);
m.or_op(a, b);
m.not_op(a);
}
}