I am working on a small part of a matching system that uses boolean conditional expressions.
These conditional expressions are contrained to a single variable and a single operator (with an edge case of an Inclusive Between).
I am interested in:
- Equal To “=”
- Greater than “>”
- Greater Than Or Equal To “>=”
- Less Than “<“
- Less Than Or Equal To “<=”
- Inclusive Between “>= AND <=”
I have a requirement to compare two conditional expressions and evaluate:
1) Is there an overlap of possible values?
Does “X > 1000” overlap with “X > 999”? Yes.
2) If there is an overlap, return the overlap:
The overlap of “X > 1000” with “X > 999” is “X > 1000”
3) Is a conditional expression constrained by another?
“X < 999” is constrained by “X < 1000” ; “X < 1001” is not constrained by “X < 1000”
What I have done so far is build up a truth table of all possible combinations and return the results, but I was wondering if there was an easier way to calculate these?
Any Theory / Reference material / C# libraries out there?
I haven’t heard of any, but you can easily do without them if you represent the constraints as intervals:
x > 1000 becomes (1000, double.Infinity)
x == 1000 becomes [1000, 1000]
etc.
This way you need only one class
With this code, you can answer the questions:
1) overlap:
a.Intersect(b).isEmpty()2) intersect:
a.Intersect(b)3) constrain:
a.Intersect(b).Equals(a)EDIT:
As @CodeInChaos suggests, you should consider replacing double with decimal. Mind that decimal lacks infinite values, so you should use decimal.MaxValue and decimal.MinValue instead.