2 using Psychlops.Internal;
\r
8 public struct Interval {
\r
9 public enum OPERATOR { CLOSE, OPEN };
\r
10 public const OPERATOR CLOSE = OPERATOR.CLOSE, OPEN = OPERATOR.OPEN;
\r
14 public VAL(double v, OPERATOR o)
\r
19 public bool bounded()
\r
21 return !Double.IsNaN(val) && (!Double.IsInfinity(val) || op == OPERATOR.OPEN);
\r
24 public VAL begin, end;
\r
29 begin = new VAL { val = Double.PositiveInfinity, op = OPERATOR.CLOSE };
\r
30 end = new VAL { val = Double.NegativeInfinity, op = OPERATOR.CLOSE };
\r
33 public Interval(double floor_val, double ceil_val)
\r
35 begin.val = floor_val;
\r
36 begin.op = OPERATOR.CLOSE;
\r
38 end.op = OPERATOR.CLOSE;
\r
40 public Interval(double floor_val, OPERATOR floor_op, double ceil_val, OPERATOR ceil_op)
\r
42 begin.val = floor_val;
\r
43 begin.op = floor_op;
\r
49 public int int_floor();
\r
50 public int int_floor(int minval);
\r
51 public int int_ceil();
\r
52 public int int_ceil(int maxval);
\r
54 bool includes(double val)
\r
56 bool result = false;
\r
58 case OPERATOR.CLOSE:
\r
59 result = begin.val<=val ? true : false;
\r
62 result = begin.val<val ? true : false;
\r
66 case OPERATOR.CLOSE:
\r
67 result = result && ( end.val>=val ? true : false );
\r
70 result = result && ( end.val>val ? true : false );
\r
76 public bool bounded()
\r
78 return begin.bounded() && end.bounded();
\r
81 System.Collections.Generic.IEnumerable<double> step(double steps)
\r
83 if (steps > 0) throw new Exception("Interval: step must be a positive");
\r
84 // return new IntervalIEnumerable(this, steps);
\r
86 long front_step = (it.begin.op == Interval.OPERATOR.CLOSE ? -1 : 0);
\r
87 long back_step = (long)System.Math.Floor((it.end.val - it.begin.val) / steps);
\r
88 if (it.end.op == Interval.OPERATOR.OPEN && 0 == System.Math.IEEERemainder(it.end.val - it.begin.val, steps))
\r
92 while (front_step <= back_step)
\r
93 yield return steps * front_step + it.begin.val;
\r
97 #region accessor generation
\r
99 public static IntervalAcc operator <(double val, Interval rng)
\r
101 return new IntervalAcc { instance = new Interval(val, OPERATOR.OPEN, Double.PositiveInfinity, OPERATOR.CLOSE) };
\r
103 public static IntervalAcc operator <=(double val, Interval rng)
\r
105 return new IntervalAcc { instance = new Interval(val, OPERATOR.CLOSE, Double.PositiveInfinity, OPERATOR.CLOSE) };
\r
107 public static IntervalAcc operator >(double val, Interval rng)
\r
109 return new IntervalAcc { instance = new Interval(Double.NegativeInfinity, OPERATOR.CLOSE, val, OPERATOR.OPEN) };
\r
111 public static IntervalAcc operator >=(double val, Interval rng)
\r
113 return new IntervalAcc { instance = new Interval(Double.NegativeInfinity, OPERATOR.CLOSE, val, OPERATOR.CLOSE) };
\r
115 public static IntervalAcc operator <(Interval rng, double val)
\r
117 return new IntervalAcc { instance = new Interval(Double.NegativeInfinity, OPERATOR.CLOSE, val, OPERATOR.OPEN) };
\r
119 public static IntervalAcc operator <=(Interval rng, double val)
\r
121 return new IntervalAcc { instance = new Interval(Double.NegativeInfinity, OPERATOR.CLOSE, val, OPERATOR.CLOSE) };
\r
123 public static IntervalAcc operator >(Interval rng, double val)
\r
125 return new IntervalAcc { instance = new Interval(val, OPERATOR.OPEN, Double.PositiveInfinity, OPERATOR.CLOSE) };
\r
127 public static IntervalAcc operator >=(Interval rng, double val)
\r
129 return new IntervalAcc { instance = new Interval(val, OPERATOR.CLOSE, Double.PositiveInfinity, OPERATOR.CLOSE) };
\r
138 #region accessor definition
\r
140 public struct IntervalAcc
\r
142 public Interval instance;
\r
144 public static IntervalAcc operator <(double val, IntervalAcc rng)
\r
146 return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.OPEN, rng.instance.end.val, rng.instance.end.op) };
\r
148 public static IntervalAcc operator <=(double val, IntervalAcc rng)
\r
150 return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.CLOSE, rng.instance.end.val, rng.instance.end.op) };
\r
152 public static IntervalAcc operator >(double val, IntervalAcc rng)
\r
154 return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.OPEN) };
\r
156 public static IntervalAcc operator >=(double val, IntervalAcc rng)
\r
158 return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.CLOSE) };
\r
160 public static IntervalAcc operator <(IntervalAcc rng, double val)
\r
162 return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.OPEN) };
\r
164 public static IntervalAcc operator <=(IntervalAcc rng, double val)
\r
166 return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.CLOSE) };
\r
168 public static IntervalAcc operator >(IntervalAcc rng, double val)
\r
170 return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.OPEN, rng.instance.end.val, rng.instance.end.op) };
\r
172 public static IntervalAcc operator >=(IntervalAcc rng, double val)
\r
174 return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.CLOSE, rng.instance.end.val, rng.instance.end.op) };
\r
177 public static implicit operator Interval(IntervalAcc rhs)
\r
179 return rhs.instance;
\r