cassowary/
operators.rs

1use ::std::ops;
2use {
3    Term,
4    Variable,
5    Expression,
6    WeightedRelation,
7    PartialConstraint,
8    Constraint
9};
10
11// Relation
12
13impl ops::BitOr<WeightedRelation> for f64 {
14    type Output = PartialConstraint;
15    fn bitor(self, r: WeightedRelation) -> PartialConstraint {
16        PartialConstraint(self.into(), r)
17    }
18}
19impl ops::BitOr<WeightedRelation> for f32 {
20    type Output = PartialConstraint;
21    fn bitor(self, r: WeightedRelation) -> PartialConstraint {
22        (self as f64).bitor(r)
23    }
24}
25impl ops::BitOr<WeightedRelation> for Variable {
26    type Output = PartialConstraint;
27    fn bitor(self, r: WeightedRelation) -> PartialConstraint {
28        PartialConstraint(self.into(), r)
29    }
30}
31impl ops::BitOr<WeightedRelation> for Term {
32    type Output = PartialConstraint;
33    fn bitor(self, r: WeightedRelation) -> PartialConstraint {
34        PartialConstraint(self.into(), r)
35    }
36}
37impl ops::BitOr<WeightedRelation> for Expression {
38    type Output = PartialConstraint;
39    fn bitor(self, r: WeightedRelation) -> PartialConstraint {
40        PartialConstraint(self.into(), r)
41    }
42}
43
44impl ops::BitOr<f64> for PartialConstraint {
45    type Output = Constraint;
46    fn bitor(self, rhs: f64) -> Constraint {
47        let (op, s) = self.1.into();
48        Constraint::new(self.0 - rhs, op, s)
49    }
50}
51impl ops::BitOr<f32> for PartialConstraint {
52    type Output = Constraint;
53    fn bitor(self, rhs: f32) -> Constraint {
54        self.bitor(rhs as f64)
55    }
56}
57impl ops::BitOr<Variable> for PartialConstraint {
58    type Output = Constraint;
59    fn bitor(self, rhs: Variable) -> Constraint {
60        let (op, s) = self.1.into();
61        Constraint::new(self.0 - rhs, op, s)
62    }
63}
64impl ops::BitOr<Term> for PartialConstraint {
65    type Output = Constraint;
66    fn bitor(self, rhs: Term) -> Constraint {
67        let (op, s) = self.1.into();
68        Constraint::new(self.0 - rhs, op, s)
69    }
70}
71impl ops::BitOr<Expression> for PartialConstraint {
72    type Output = Constraint;
73    fn bitor(self, rhs: Expression) -> Constraint {
74        let (op, s) = self.1.into();
75        Constraint::new(self.0 - rhs, op, s)
76    }
77}
78
79// Variable
80
81impl ops::Add<f64> for Variable {
82    type Output = Expression;
83    fn add(self, v: f64) -> Expression {
84        Expression::new(vec![Term::new(self, 1.0)], v)
85    }
86}
87
88impl ops::Add<f32> for Variable {
89    type Output = Expression;
90    fn add(self, v: f32) -> Expression {
91        self.add(v as f64)
92    }
93}
94
95impl ops::Add<Variable> for f64 {
96    type Output = Expression;
97    fn add(self, v: Variable) -> Expression {
98        Expression::new(vec![Term::new(v, 1.0)], self)
99    }
100}
101
102impl ops::Add<Variable> for f32 {
103    type Output = Expression;
104    fn add(self, v: Variable) -> Expression {
105        (self as f64).add(v)
106    }
107}
108
109impl ops::Add<Variable> for Variable {
110    type Output = Expression;
111    fn add(self, v: Variable) -> Expression {
112        Expression::new(vec![Term::new(self, 1.0), Term::new(v, 1.0)], 0.0)
113    }
114}
115
116impl ops::Add<Term> for Variable {
117    type Output = Expression;
118    fn add(self, t: Term) -> Expression {
119        Expression::new(vec![Term::new(self, 1.0), t], 0.0)
120    }
121}
122
123impl ops::Add<Variable> for Term {
124    type Output = Expression;
125    fn add(self, v: Variable) -> Expression {
126        Expression::new(vec![self, Term::new(v, 1.0)], 0.0)
127    }
128}
129
130impl ops::Add<Expression> for Variable {
131    type Output = Expression;
132    fn add(self, mut e: Expression) -> Expression {
133        e.terms.push(Term::new(self, 1.0));
134        e
135    }
136}
137
138impl ops::Add<Variable> for Expression {
139    type Output = Expression;
140    fn add(mut self, v: Variable) -> Expression {
141        self.terms.push(Term::new(v, 1.0));
142        self
143    }
144}
145
146impl ops::Neg for Variable {
147    type Output = Term;
148    fn neg(self) -> Term {
149        Term::new(self, -1.0)
150    }
151}
152
153impl ops::Sub<f64> for Variable {
154    type Output = Expression;
155    fn sub(self, v: f64) -> Expression {
156        Expression::new(vec![Term::new(self, 1.0)], -v)
157    }
158}
159
160impl ops::Sub<f32> for Variable {
161    type Output = Expression;
162    fn sub(self, v: f32) -> Expression {
163        self.sub(v as f64)
164    }
165}
166
167impl ops::Sub<Variable> for f64 {
168    type Output = Expression;
169    fn sub(self, v: Variable) -> Expression {
170        Expression::new(vec![Term::new(v, -1.0)], self)
171    }
172}
173
174impl ops::Sub<Variable> for f32 {
175    type Output = Expression;
176    fn sub(self, v: Variable) -> Expression {
177        (self as f64).sub(v)
178    }
179}
180
181impl ops::Sub<Variable> for Variable {
182    type Output = Expression;
183    fn sub(self, v: Variable) -> Expression {
184        Expression::new(vec![Term::new(self, 1.0), Term::new(v, -1.0)], 0.0)
185    }
186}
187
188impl ops::Sub<Term> for Variable {
189    type Output = Expression;
190    fn sub(self, t: Term) -> Expression {
191        Expression::new(vec![Term::new(self, 1.0), -t], 0.0)
192    }
193}
194
195impl ops::Sub<Variable> for Term {
196    type Output = Expression;
197    fn sub(self, v: Variable) -> Expression {
198        Expression::new(vec![self, Term::new(v, -1.0)], 0.0)
199    }
200}
201
202impl ops::Sub<Expression> for Variable {
203    type Output = Expression;
204    fn sub(self, mut e: Expression) -> Expression {
205        e.negate();
206        e.terms.push(Term::new(self, 1.0));
207        e
208    }
209}
210
211impl ops::Sub<Variable> for Expression {
212    type Output = Expression;
213    fn sub(mut self, v: Variable) -> Expression {
214        self.terms.push(Term::new(v, -1.0));
215        self
216    }
217}
218
219impl ops::Mul<f64> for Variable {
220    type Output = Term;
221    fn mul(self, v: f64) -> Term {
222        Term::new(self, v)
223    }
224}
225
226impl ops::Mul<f32> for Variable {
227    type Output = Term;
228    fn mul(self, v: f32) -> Term {
229        self.mul(v as f64)
230    }
231}
232
233impl ops::Mul<Variable> for f64 {
234    type Output = Term;
235    fn mul(self, v: Variable) -> Term {
236        Term::new(v, self)
237    }
238}
239
240impl ops::Mul<Variable> for f32 {
241    type Output = Term;
242    fn mul(self, v: Variable) -> Term {
243        (self as f64).mul(v)
244    }
245}
246
247impl ops::Div<f64> for Variable {
248    type Output = Term;
249    fn div(self, v: f64) -> Term {
250        Term::new(self, 1.0 / v)
251    }
252}
253
254impl ops::Div<f32> for Variable {
255    type Output = Term;
256    fn div(self, v: f32) -> Term {
257        self.div(v as f64)
258    }
259}
260
261// Term
262
263impl ops::Mul<f64> for Term {
264    type Output = Term;
265    fn mul(mut self, v: f64) -> Term {
266        self.coefficient *= v;
267        self
268    }
269}
270
271impl ops::Mul<f32> for Term {
272    type Output = Term;
273    fn mul(self, v: f32) -> Term {
274        self.mul(v as f64)
275    }
276}
277
278impl ops::Mul<Term> for f64 {
279    type Output = Term;
280    fn mul(self, mut t: Term) -> Term {
281        t.coefficient *= self;
282        t
283    }
284}
285
286impl ops::Mul<Term> for f32 {
287    type Output = Term;
288    fn mul(self, t: Term) -> Term {
289        (self as f64).mul(t)
290    }
291}
292
293impl ops::Div<f64> for Term {
294    type Output = Term;
295    fn div(mut self, v: f64) -> Term {
296        self.coefficient /= v;
297        self
298    }
299}
300
301impl ops::Div<f32> for Term {
302    type Output = Term;
303    fn div(self, v: f32) -> Term {
304        self.div(v as f64)
305    }
306}
307
308impl ops::Add<f64> for Term {
309    type Output = Expression;
310    fn add(self, v: f64) -> Expression {
311        Expression::new(vec![self], v)
312    }
313}
314
315impl ops::Add<f32> for Term {
316    type Output = Expression;
317    fn add(self, v: f32) -> Expression {
318        self.add(v as f64)
319    }
320}
321
322impl ops::Add<Term> for f64 {
323    type Output = Expression;
324    fn add(self, t: Term) -> Expression {
325        Expression::new(vec![t], self)
326    }
327}
328
329impl ops::Add<Term> for f32 {
330    type Output = Expression;
331    fn add(self, t: Term) -> Expression {
332        (self as f64).add(t)
333    }
334}
335
336impl ops::Add<Term> for Term {
337    type Output = Expression;
338    fn add(self, t: Term) -> Expression {
339        Expression::new(vec![self, t], 0.0)
340    }
341}
342
343impl ops::Add<Expression> for Term {
344    type Output = Expression;
345    fn add(self, mut e: Expression) -> Expression {
346        e.terms.push(self);
347        e
348    }
349}
350
351impl ops::Add<Term> for Expression {
352    type Output = Expression;
353    fn add(mut self, t: Term) -> Expression {
354        self.terms.push(t);
355        self
356    }
357}
358
359impl ops::Neg for Term {
360    type Output = Term;
361    fn neg(mut self) -> Term {
362        self.coefficient = -self.coefficient;
363        self
364    }
365}
366
367impl ops::Sub<f64> for Term {
368    type Output = Expression;
369    fn sub(self, v: f64) -> Expression {
370        Expression::new(vec![self], -v)
371    }
372}
373
374impl ops::Sub<f32> for Term {
375    type Output = Expression;
376    fn sub(self, v: f32) -> Expression {
377        self.sub(v as f64)
378    }
379}
380
381impl ops::Sub<Term> for f64 {
382    type Output = Expression;
383    fn sub(self, t: Term) -> Expression {
384        Expression::new(vec![-t], self)
385    }
386}
387
388impl ops::Sub<Term> for f32 {
389    type Output = Expression;
390    fn sub(self, t: Term) -> Expression {
391        (self as f64).sub(t)
392    }
393}
394
395impl ops::Sub<Term> for Term {
396    type Output = Expression;
397    fn sub(self, t: Term) -> Expression {
398        Expression::new(vec![self, -t], 0.0)
399    }
400}
401
402impl ops::Sub<Expression> for Term {
403    type Output = Expression;
404    fn sub(self, mut e: Expression) -> Expression {
405        e.negate();
406        e.terms.push(self);
407        e
408    }
409}
410
411impl ops::Sub<Term> for Expression {
412    type Output = Expression;
413    fn sub(mut self, t: Term) -> Expression {
414        self.terms.push(-t);
415        self
416    }
417}
418
419// Expression
420
421impl ops::Mul<f64> for Expression {
422    type Output = Expression;
423    fn mul(mut self, v: f64) -> Expression {
424        self.constant *= v;
425        for t in &mut self.terms {
426            *t = *t * v;
427        }
428        self
429    }
430}
431
432impl ops::Mul<f32> for Expression {
433    type Output = Expression;
434    fn mul(self, v: f32) -> Expression {
435        self.mul(v as f64)
436    }
437}
438
439impl ops::Mul<Expression> for f64 {
440    type Output = Expression;
441    fn mul(self, mut e: Expression) -> Expression {
442        e.constant *= self;
443        for t in &mut e.terms {
444            *t = *t * self;
445        }
446        e
447    }
448}
449
450impl ops::Mul<Expression> for f32 {
451    type Output = Expression;
452    fn mul(self, e: Expression) -> Expression {
453        (self as f64).mul(e)
454    }
455}
456
457impl ops::Div<f64> for Expression {
458    type Output = Expression;
459    fn div(mut self, v: f64) -> Expression {
460        self.constant /= v;
461        for t in &mut self.terms {
462            *t = *t / v;
463        }
464        self
465    }
466}
467
468impl ops::Div<f32> for Expression {
469    type Output = Expression;
470    fn div(self, v: f32) -> Expression {
471        self.div(v as f64)
472    }
473}
474
475impl ops::Add<f64> for Expression {
476    type Output = Expression;
477    fn add(mut self, v: f64) -> Expression {
478        self.constant += v;
479        self
480    }
481}
482
483impl ops::Add<f32> for Expression {
484    type Output = Expression;
485    fn add(self, v: f32) -> Expression {
486        self.add(v as f64)
487    }
488}
489
490impl ops::Add<Expression> for f64 {
491    type Output = Expression;
492    fn add(self, mut e: Expression) -> Expression {
493        e.constant += self;
494        e
495    }
496}
497
498impl ops::Add<Expression> for f32 {
499    type Output = Expression;
500    fn add(self, e: Expression) -> Expression {
501        (self as f64).add(e)
502    }
503}
504
505impl ops::Add<Expression> for Expression {
506    type Output = Expression;
507    fn add(mut self, mut e: Expression) -> Expression {
508        self.terms.append(&mut e.terms);
509        self.constant += e.constant;
510        self
511    }
512}
513
514impl ops::Neg for Expression {
515    type Output = Expression;
516    fn neg(mut self) -> Expression {
517        self.negate();
518        self
519    }
520}
521
522impl ops::Sub<f64> for Expression {
523    type Output = Expression;
524    fn sub(mut self, v: f64) -> Expression {
525        self.constant -= v;
526        self
527    }
528}
529
530impl ops::Sub<f32> for Expression {
531    type Output = Expression;
532    fn sub(self, v: f32) -> Expression {
533        self.sub(v as f64)
534    }
535}
536
537impl ops::Sub<Expression> for f64 {
538    type Output = Expression;
539    fn sub(self, mut e: Expression) -> Expression {
540        e.negate();
541        e.constant += self;
542        e
543    }
544}
545
546impl ops::Sub<Expression> for f32 {
547    type Output = Expression;
548    fn sub(self, e: Expression) -> Expression {
549        (self as f64).sub(e)
550    }
551}
552
553impl ops::Sub<Expression> for Expression {
554    type Output = Expression;
555    fn sub(mut self, mut e: Expression) -> Expression {
556        e.negate();
557        self.terms.append(&mut e.terms);
558        self.constant += e.constant;
559        self
560    }
561}