-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiboolean.cpp
167 lines (145 loc) · 4.14 KB
/
iboolean.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Simple interval library from Luc JAULIN, with minor modifications from Fabrice LE BARS and Jeremy NICOLA.
#include "iboolean.h"
#ifdef __GNUC__
// Disable some GCC warnings.
#if (__GNUC__ >= 9)
#pragma GCC diagnostic ignored "-Wdeprecated-copy"
#endif // (__GNUC__ >= 9)
#if (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#pragma GCC diagnostic push
#endif // (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#endif // __GNUC__
iboolean::iboolean()
{
value = iperhaps;
}
iboolean::iboolean(bool b)
{
if (b) value = itrue; else value = ifalse;
}
iboolean::iboolean(IBOOLEAN tt)
{
value = tt;
}
iboolean::iboolean(const iboolean& t)
{
*this = t;
}
iboolean operator&&(iboolean x, iboolean y)
{
return And(x,y);
}
iboolean operator||(iboolean x, iboolean y)
{
return Or(x,y);
}
bool operator==(iboolean x, iboolean y)
{
return (x.value == y.value);
}
bool operator!=(iboolean x, iboolean y)
{
return (x.value != y.value);
}
iboolean operator!(iboolean x)
{
if (x.value == itrue) return iboolean(ifalse);
if (x.value == ifalse) return iboolean(itrue);
return x;
}
iboolean Not(iboolean x)
{
if (x.value == iperhaps) return iperhaps;
if (x.value == itrue) return ifalse;
if (x.value == ifalse) return itrue;
return iempty;
}
iboolean Inter(iboolean x, iboolean y)
{
if (x.value == y.value) return x.value;
if (x.value == iperhaps) return y.value;
if (y.value == iperhaps) return x.value;
return iboolean(iempty);
}
iboolean Union(iboolean x, iboolean y)
{
if (x.value == iempty) return y.value;
if (y.value == iempty) return x.value;
if (x.value == y.value) return x.value;
return iboolean(iperhaps);
}
iboolean Xor(iboolean x, iboolean y)
{
if (x.value == iempty) return iempty;
if (y.value == iempty) return iempty;
if (x.value == iperhaps) return iperhaps;
if (y.value == iperhaps) return iperhaps;
if (x.value == y.value) return ifalse;
return iboolean(itrue);
}
iboolean And(iboolean x, iboolean y)
{
if ((x.value == iempty)||(y.value == iempty)) return iboolean(iempty);
if ((x.value == ifalse)||(y.value == ifalse)) return iboolean(ifalse);
if ((x.value == iperhaps)||(y.value == iperhaps)) return iboolean(iperhaps);
return iboolean(itrue);
}
// Project the constraint x>=y with respect to x
iboolean geq(iboolean x, iboolean y)
{
iboolean r;
r = iboolean(iperhaps);
if (y.value == iempty) r = iboolean(iempty);
if (y.value == itrue) r = iboolean(itrue);
return Inter(x,r);
}
// Project the constraint x<=y with respect to x
iboolean leq(iboolean x, iboolean y)
{
iboolean r;
r = iboolean(iperhaps);
if (y.value == iempty) r = iboolean(iempty);
if (y.value == ifalse) r = iboolean(ifalse);
return Inter(x,r);
/* a && (implique b)
1*0=\iempty 1 && 0
1*1=1 1 && [0,1]
1*[0,1]=1 1 && [0,1]
0*0=0 0 && 0
0*1=0 0 && [0,1]
0*[0,1]=0 0 && [0,1]
[0,1]*0=0 [0,1] && 0
[0,1]*1=[0,1] [0,1] && [0,1]
[0,1]*[0,1]=[0,1] [0,1] && [0,1]
*/
}
// return x and !y
iboolean Restrict(iboolean x, iboolean y)
{
return And(x,!y);
}
iboolean Or(iboolean x, iboolean y)
{
if ((x.value == iempty)||(y.value == iempty)) return iboolean(iempty);
if ((x.value == itrue)||(y.value == itrue)) return iboolean(itrue);
if ((x.value == iperhaps)||(y.value == iperhaps)) return iboolean(iperhaps);
return iboolean(ifalse);
}
std::ostream& operator<<(std::ostream& os, const iboolean& a)
{
if (a.value == itrue) os << "itrue";
if (a.value == ifalse) os << "ifalse";
if (a.value == iperhaps) os << "iperhaps";
if (a.value == iempty) os << "iempty";
return os;
}
#ifdef __GNUC__
// Restore the GCC warnings previously disabled.
#if (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#pragma GCC diagnostic pop
#else
//#if (__GNUC__ >= 9)
//#pragma GCC diagnostic warning "-Wdeprecated-copy"
//#endif // (__GNUC__ >= 9)
#endif // (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#endif // __GNUC__