Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.Analysis.Numerical / IntervalContext.cs
1 // 
2 // IntervalContext.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2012 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using Mono.CodeContracts.Static.Lattices;
30
31 namespace Mono.CodeContracts.Static.Analysis.Numerical {
32         class IntervalContext : IntervalRationalContextBase<Interval> {
33                 public static readonly IntervalContext Instance = new IntervalContext ();
34
35                 IntervalContext ()
36                 {
37                 }
38
39                 public override Interval TopValue { get { return Interval.TopValue; } }
40                 public override Interval BottomValue { get { return Interval.BottomValue; } }
41
42                 public override Interval Zero { get { return Interval.For (Rational.Zero); } }
43                 public override Interval One { get { return Interval.For (Rational.One); } }
44
45                 public override Interval Positive { get { return Interval.For (0, Rational.PlusInfinity); } }
46                 public override Interval Negative { get { return Interval.For (Rational.MinusInfinity, 0); } }
47
48                 public override Interval GreaterEqualThanMinusOne { get { return Interval.For (Rational.MinusOne, Rational.PlusInfinity); } }
49
50                 public override Interval For (long value)
51                 {
52                         return Interval.For (value);
53                 }
54
55                 public override Interval For (long lower, long upper)
56                 {
57                         return Interval.For (lower, upper);
58                 }
59
60                 public override Interval For (long lower, Rational upper)
61                 {
62                         return Interval.For (lower, upper);
63                 }
64
65                 public override Interval For (Rational lower, long upper)
66                 {
67                         return Interval.For (lower, upper);
68                 }
69
70                 public override Interval For (Rational value)
71                 {
72                         return Interval.For (value);
73                 }
74
75                 public override Interval For (Rational lower, Rational upper)
76                 {
77                         return Interval.For (lower, upper);
78                 }
79
80                 public override Interval LeftOpen (Rational upperBound)
81                 {
82                         return Interval.For (Rational.MinusInfinity, upperBound);
83                 }
84
85                 public override Interval RightOpen (Rational lowerBound)
86                 {
87                         return Interval.For (lowerBound, Rational.PlusInfinity);
88                 }
89
90                 public override Interval Add (Interval a, Interval b)
91                 {
92                         return a + b;
93                 }
94
95                 public override Interval Sub (Interval a, Interval b)
96                 {
97                         return a - b;
98                 }
99
100                 public override Interval Div (Interval a, Interval b)
101                 {
102                         return a / b;
103                 }
104
105                 public override Interval Mul (Interval a, Interval b)
106                 {
107                         return a * b;
108                 }
109
110                 public override Interval Not (Interval value)
111                 {
112                         if (!value.IsNormal ())
113                                 return value;
114
115                         int intValue;
116                         if (value.TryGetSingletonFiniteInt32 (out intValue))
117                                 return Interval.For (intValue != 0 ? 0 : 1);
118
119                         return Interval.TopValue;
120                 }
121
122                 public override Interval Rem (Interval a, Interval b)
123                 {
124                         return TopValue;
125                 }
126
127                 public override Interval UnaryMinus (Interval value)
128                 {
129                         return -value;
130                 }
131
132                 public override Interval ApplyConversion (ExpressionOperator conv, Interval intv)
133                 {
134                         return Interval.ApplyConversion (conv, intv);
135                 }
136         }
137 }