forked from kdudka/predator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.cc
More file actions
2577 lines (2293 loc) · 80.3 KB
/
Copy pathRange.cc
File metadata and controls
2577 lines (2293 loc) · 80.3 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Daniela Ďuričeková, xduric00@stud.fit.vutbr.cz
* @file Range.cc
* @brief Implementation of class that represents the value range of the variable.
* @date 2012
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
// Enable assertions.
#undef NDEBUG
#include <cassert>
#include "Range.h"
using std::vector;
using std::sort;
using std::max;
using std::min;
using std::cout;
using std::endl;
using std::pair;
typedef Range::Interval Interval;
namespace {
/**
* @brief Compares two intervals according to their lower bounds.
*
* @param[in] i1 The first interval.
* @param[in] i2 The second interval.
* @return @c true if the first interval is lower than the second one, @c false
* otherwise.
*/
bool compareLowerBounds(const Range::Interval &i1, const Range::Interval &i2)
{
if (i1.first.isNotNumber())
return true;
else if (i2.first.isNotNumber())
return false;
return (i1.first < i2.first) ||
(i1.first == i2.first && i1.second < i2.second);
}
}
// Definition of static variables and constants.
const size_t Range::MAX_INTERVALS_IN_RANGE;
/**
* @brief Constructs an empty range.
*/
Range::Range()
{
}
/**
* @brief Constructs a range containing an interval [@a n, @a n] (a single point).
*
* @param[in] n Number that represents the lower bound and the upper bound of the
* interval that will be added to the range.
*/
Range::Range(Number n)
{
data.push_back(Interval(n,n));
}
/**
* @brief Constructs a range containing one interval.
*
* @param[in] i The first interval that will be added to the range.
*/
Range::Range(Interval i)
{
data.push_back(i);
normalize();
}
/**
* @brief Constructs a range containing two intervals.
*
* @param[in] i1 The first interval that will be added to the range.
* @param[in] i2 The second interval that will be added to the range.
*/
Range::Range(Interval i1, Interval i2)
{
data.push_back(i1);
data.push_back(i2);
normalize();
}
/**
* @brief Constructs a range containing three intervals.
*
* @param[in] i1 The first interval that will be added to the range.
* @param[in] i2 The second interval that will be added to the range.
* @param[in] i3 The third interval that will be added to the range.
*/
Range::Range(Interval i1, Interval i2, Interval i3)
{
data.push_back(i1);
data.push_back(i2);
data.push_back(i3);
normalize();
}
/**
* @brief Constructs a range containing four intervals.
*
* @param[in] i1 The first interval that will be added to the range.
* @param[in] i2 The second interval that will be added to the range.
* @param[in] i3 The third interval that will be added to the range.
* @param[in] i4 The fourth interval that will be added to the range.
*/
Range::Range(Interval i1, Interval i2, Interval i3, Interval i4)
{
data.push_back(i1);
data.push_back(i2);
data.push_back(i3);
data.push_back(i4);
normalize();
}
/**
* @brief Normalizes vector of intervals.
*
* It edits intervals, so the lower bounds are always lower or equal than upper
* bounds. It sorts intervals in the vector according to the numerical order and
* if intervals are overlapping or neighbouring it joins them together.
*/
void Range::normalize()
{
// It does not make sense to normalize empty vector.
if (data.size() == 0)
return;
// Only one NAN interval is kept.
vector<Interval> tmp;
bool isNanThere = false;
for (iterator it = data.begin(); it != data.end(); ++it) {
if (it->first.isNotNumber() || it->second.isNotNumber()) {
if (!isNanThere) {
// The first interval representing NAN is kept. Others are
// removed.
if (it->first.isNotNumber()) {
tmp.push_back(Interval(it->first, it->first));
} else {
tmp.push_back(Interval(it->second, it->second));
}
isNanThere = true;
}
} else {
// Everything except extra NAN intervals is kept.
tmp.push_back(*it);
}
}
swap(data, tmp);
// Pre-processing intervals.
tmp.clear();
for (iterator it = data.begin(); it != data.end(); ++it) {
if (it->first > it->second) {
// The lower bound is greater than the upper bound.
tmp.push_back(Interval(it->second.getMin(), it->second));
tmp.push_back(Interval(it->first, it->first.getMax()));
} else {
// The lower bound is lower or equal than the upper bound.
tmp.push_back(*it);
}
}
swap(data, tmp);
// Sorting intervals.
sort(data.begin(), data.end(), compareLowerBounds);
tmp.clear();
tmp.push_back(data.front());
// If the space between intervals is lower than or equal epsilon, they
// will be joined together.
Number epsilon(1, sizeof(int), true);
if (data.front().first.isFloatingPoint()) {
epsilon = Number(0.1, sizeof(double));
}
// Joining intervals.
for (iterator current = data.begin() + 1; current != data.end(); current++) {
Interval previous = tmp.back();
// The second condition is necessary because of overflowing of the Number.
if (current->first <= previous.second ||
current->first <= previous.second + epsilon) {
// The lower bound of currently processed interval is lower or equal
// than upper bound of the previous processed interval (intervals are
// neighbours or overlap).
Number lowerBound = std::min(previous.first, current->first);
Number upperBound = std::max(previous.second, current->second);
tmp.back() = Interval(lowerBound, upperBound);
} else {
// If it is not possible to join the last processed interval with the
// current one.
tmp.push_back(*current);
}
}
swap(data, tmp);
// If there are too many intervals, merge them.
if (data.size() > MAX_INTERVALS_IN_RANGE) {
mergeIntervalsInPlace();
}
}
/**
* @brief Merges the intervals in the range in place, meaning that the current
* range is changed.
*
* See the description of mergeIntervals() for more information.
*/
void Range::mergeIntervalsInPlace() {
// On ranges with no more than a single interval, there is nothing to do.
if (data.size() < 2) {
return;
}
if (isFloatingPoint() && containsNan()) {
// We have to handle the situation when there is NAN separately.
Number nan(data[0].first);
Number min(data[1].first);
Number max(data[data.size() - 1].second);
data.clear();
data.push_back(Interval(nan, nan));
data.push_back(Interval(min, max));
} else {
Number min(data[0].first);
Number max(data[data.size() - 1].second);
data.clear();
data.push_back(Interval(min, max));
}
}
/**
* @brief Splits range into intervals where the intervals (INF, INF) and (-INF, -INF)
* are always extra. Thus, there will be no intervals like (-INF, 5). This will
* be always split into (-INF, -INF) and (MIN, 5). It also splits all intervals
* that contains negative and positive numbers at the same time. For example,
* interval (-5, 5) will be split into intervals (-5, -1) and (0, 5). Also, for
* integral types (MIN, MIN) must be extra. Otherwise, it causes problems.
* Because, MIN * (-1) = MIN.
*/
Range Range::splitBySpecialValues() const
{
// I should always work with non-empty range.
assert(!empty());
Range result;
if (isFloatingPoint()) {
// Split floating-point range by special values.
for (Range::const_iterator it = begin(); it != end(); ++it) {
const Number &x = it->first;
const Number &y = it->second;
// In the next code, we need to have zero with the appropriate type.
Number zero = x.assign(Number(0, sizeof(char), true));
if (x.isNegativeInf() && y.isNumber() && y < zero) {
// For intervals in the form of (-INF, -number).
result.data.push_back(Interval(x, x));
result.data.push_back(Interval(x.getMin(), y));
} else if (x.isNegativeInf() && y.isNumber() && y >= zero) {
// For intervals in the form of (-INF, number).
result.data.push_back(Interval(x, x));
result.data.push_back(Interval(x.getMin(), zero));
result.data.push_back(Interval(zero, y));
} else if (x.isNegativeInf() && y.isPositiveInf()) {
// For intervals in the form of (-INF, INF).
result.data.push_back(Interval(x, x));
result.data.push_back(Interval(x.getMin(), zero));
result.data.push_back(Interval(zero, x.getMax()));
result.data.push_back(Interval(y, y));
} else if (x.isNumber() && x < zero && y.isNumber() && y >= zero) {
// For intervals in the form of (-number, number).
result.data.push_back(Interval(x, zero));
result.data.push_back(Interval(zero, y));
} else if (x.isNumber() && x < zero && y.isPositiveInf()) {
// For intervals in the form of (-number, INF)
result.data.push_back(Interval(x, zero));
result.data.push_back(Interval(zero, x.getMax()));
result.data.push_back(Interval(y, y));
} else if (x.isNumber() && x >= zero && y.isPositiveInf()) {
// For intervals in the form of (number, INF).
result.data.push_back(Interval(x, x.getMax()));
result.data.push_back(Interval(y, y));
} else {
// Otherwise, for intervals in the forms (-INF, -INF),
// (-number, -number), (number, number), (INF, INF), (NAN, NAN).
result.data.push_back(Interval(x, y));
}
}
} else if (isIntegral() && isSigned()) {
// Split signed integral ranges by special values.
for (Range::const_iterator it = begin(); it != end(); ++it) {
const Number &x = it->first;
const Number &y = it->second;
// In the next code, we need to have zero with the appropriate type.
Number zero = x.assign(Number(0, sizeof(char), true));
if (x.isMin() && y.isMin()) {
// For intervals in the form of (MIN, MIN).
result.data.push_back(Interval(x, y));
} else if (x.isMin() && y.isNumber() && y < zero) {
// For intervals in the form of (MIN, - number).
Number xPlusOne(x.getInt() + 1, x.getBitWidth(), x.getSign());
result.data.push_back(Interval(x, x));
result.data.push_back(Interval(xPlusOne, y));
} else if (x.isMin() && y.isNumber() && y >= zero) {
// For intervals in the form of (MIN, number) and (MIN, MAX).
Number xPlusOne(x.getInt() + 1, x.getBitWidth(), x.getSign());
result.data.push_back(Interval(x, x));
result.data.push_back(Interval(xPlusOne, zero));
result.data.push_back(Interval(zero, y));
} else if (x.isNumber() && x < zero && y.isNumber() && y >= zero) {
// For intervals in the form of (-number, number) and (-number, MAX).
result.data.push_back(Interval(x, zero));
result.data.push_back(Interval(zero, y));
} else {
// Otherwise, for intervals in the forms (MIN, MIN),
// (-number, -number), (number, number), (number, MAX), (MAX, MAX).
result.data.push_back(Interval(x, y));
}
}
} else if (isIntegral() && isUnsigned()) {
// There are no problems for unsigned integral ranges.
result = *this;
}
// Do not call the normalize() function here. It blends everything together
// and the resulted range will be same as the given one.
return result;
}
/**
* @brief Splits the range so that if it contains 0 or 0.0, then this value will
* be in a separate interval (0, 0) or (0.0, 0.0)
*
* @return Splitted range.
*/
Range Range::splitByZero() const {
// I should always work with non-empty range.
assert(!empty());
// In the next code, we need to have zero with the appropriate type.
Number zero = data[0].first.assign(Number(0, sizeof(char), true));
Range result;
for (Range::const_iterator it = begin(); it != end(); ++it) {
const Number &x = it->first;
const Number &y = it->second;
if (isFloatingPoint()) {
// Split floating-point range.
if (x == zero && y == zero) {
result.data.push_back(Interval(zero, zero));
} else if (x == zero) {
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(x.getEpsilon(), y));
} else if (y == zero) {
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(x, -y.getEpsilon()));
} else if (x < zero && y > zero) {
result.data.push_back(Interval(x, -y.getEpsilon()));
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(x.getEpsilon(), y));
} else {
result.data.push_back(Interval(x, y));
}
} else if (isIntegral() && isSigned()) {
// Split a signed integral range.
if (x == zero && y == zero) {
result.data.push_back(Interval(zero, zero));
} else if (x == zero) {
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(Number(1, x.getBitWidth(),
true), y));
} else if (y == zero) {
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(x, Number(-1, x.getBitWidth(),
true)));
} else if (x < zero && y > zero) {
result.data.push_back(Interval(x, Number(-1, x.getBitWidth(),
true)));
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(Number(1, x.getBitWidth(),
true), y));
} else {
result.data.push_back(Interval(x, y));
}
} else { // isIntegral() && isUnsigned()
assert(isIntegral() && isUnsigned());
// Split an unsigned integral range.
if (x == zero && y == zero) {
result.data.push_back(Interval(zero, zero));
} else if (x == zero) {
result.data.push_back(Interval(zero, zero));
result.data.push_back(Interval(Number(1, x.getBitWidth(),
false), y));
} else {
result.data.push_back(Interval(x, y));
}
}
}
// Do not call the normalize() function here. It blends everything together
// and the resulted range will be same as the given one.
return result;
}
/**
* @brief Returns @c true if current range has the same type as the range @a r, @c
* false otherwise.
*/
bool Range::hasSameTypeAs(const Range &r) const
{
assert(!empty() && !r.empty());
return (((isIntegral() && r.isIntegral()) &&
(isSigned() == r.isSigned()) &&
(data[0].first.getBitWidth() == r.data[0].first.getBitWidth())) ||
((isFloatingPoint() && r.isFloatingPoint()) &&
(data[0].first.getBitWidth() == r.data[0].first.getBitWidth())));
}
/**
* @brief Returns @c true if the current range contains just a single number, @c
* false otherwise.
*
* For example, the ranges [1, 1] and [4.5, 4.5] both contain a single number
* while the ranges [1, 2] and [3, 3][5, 7] contains more than a single number.
*/
bool Range::containsOnlySingleNumber() const
{
return size() == 1 && data[0].first == data[0].second;
}
/**
* @brief Returns @c true if the current range contains the minimal integral
* value, @c false otherwise.
*
* If the range is composed of floating-point numbers, this function returns @c
* false.
*/
bool Range::containsIntegralMin() const
{
if (empty() || !isIntegral()) {
return false;
}
// The minimal integral value should be in the first interval (if any).
return data[0].first.isMin();
}
/**
* @brief Returns @c true if the current range contains the intergral value @c
* -1, @c false otherwise.
*
* If the range is composed of floating-point numbers, this function
* returns @c false.
*/
bool Range::containsIntegralMinusOne() const
{
if (empty() || !isIntegral()) {
return false;
}
// Create a -1 value of a proper type.
Number minOne(-1, data[0].first.getBitWidth(), data[0].first.isSigned());
// Go through the range and try to find -1.
for (const_iterator it = begin(); it != end(); ++it) {
// Notice that the case when it->fist == -1 or it-second == -1 is also
// included in the following check.
if (it->first <= minOne && it->second >= minOne) {
return true;
}
}
return false;
}
/**
* @brief Returns the maximal number stored in the current range.
*/
Number Range::getMax() const
{
assert(!empty());
return data[size() - 1].second;
}
/**
* @brief Returns the minimal number stored in the current range.
*/
Number Range::getMin() const
{
assert(!empty());
if (data[0].first.isNotNumber() && size() > 1) {
return data[1].first;
}
else {
return data[0].first;
}
}
/**
* @brief Extends ranges according to the C99 standard.
*
* @param[in] r1 The first range that will be extended.
* @param[in] r2 The second range that will be extended.
*
* @return The pair of extended ranges.
*/
std::pair<Range, Range> Range::extensionByCRules(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
// This is done in order to get the result that contains the correct type.
const Number &firstOp = r1.data[0].first;
const Number &secondOp = r2.data[0].first;
Number res = firstOp * secondOp;
Range result1;
for (Range::const_iterator it = r1.begin(); it != r1.end(); ++it) {
// Converts the interval according to the type stored in res.
Interval tmp = Interval(res.assign(it->first), res.assign(it->second));
result1.data.push_back(tmp);
}
result1.normalize();
Range result2;
for (Range::const_iterator it = r2.begin(); it != r2.end(); ++it) {
// Converts the interval according to the type stored in res.
Interval tmp = Interval(res.assign(it->first), res.assign(it->second));
result2.data.push_back(tmp);
}
result2.normalize();
return std::pair<Range, Range>(result1, result2);
}
/**
* @brief Returns @c true if the current range contains @c NAN, @c false otherwise.
*/
bool Range::containsNan() const
{
// I should always work with non-empty range.
assert(!empty());
Interval i = data[0];
if (i.first.isNotNumber()) {
return true;
} else {
return false;
}
}
/**
* @brief Returns @c true if the current range contains @c INF, @c false otherwise.
*/
bool Range::containsPositiveInf() const
{
// I should always work with non-empty range.
assert(!empty());
// Ranges are sorted. So, if INF is present in the range, it is its last element.
Interval i = data[size() - 1];
if (i.second.isPositiveInf()) {
// We use second, because (number, INF) is correct interval.
return true;
} else {
return false;
}
}
/**
* @brief Returns @c true if the current range contains @c -INF, @c false otherwise.
*/
bool Range::containsNegativeInf() const
{
// I should always work with non-empty range.
assert(!empty());
// Ranges are sorted. So, if -INF is present in the range, it is its
// first or second element. It is second if (NAN, NAN) interval is present.
// Otherwise, it is first.
if (data[0].first.isNegativeInf() ||
(size() > 1 && data[1].first.isNegativeInf())) {
// We use second, because (number, INF) is correct interval.
return true;
} else {
return false;
}
}
/**
* @brief Returns @c true if the current range contains value convertible to @c
* true, @c false otherwise.
*/
bool Range::containsTrue() const
{
// I should always work with non-empty range.
assert(!empty());
for (Range::const_iterator it = begin(); it != end(); ++it) {
if (it->first.toBool() || it->second.toBool()) {
return true;
}
}
return false;
}
/**
* @brief Returns @c true if the current range contains value convertible to @c
* false, @c false otherwise.
*/
bool Range::containsFalse() const
{
// I should always work with non-empty range.
assert(!empty());
// Only zero is convertible to false.
return containsZero();
}
/**
* @brief Returns @c true if the current range contains a zero (@c 0 or @c 0.0),
* @c false otherwise.
*/
bool Range::containsZero() const
{
// I should always work with non-empty range.
assert(!empty());
Number zeroInt(0, sizeof(int), true);
Number zero(data[0].first.assign(zeroInt));
for (Range::const_iterator it = begin(); it != end(); ++it) {
if (!it->first.toBool() || !it->second.toBool() ||
((it->first < zero) && (zero < it->second))) {
return true;
}
}
return false;
}
/**
* @brief Returns the maximal range for the given number @a n according to the number
* type.
*/
Range Range::getMaxRange(const Number &n)
{
if (n.isIntegral()) {
return Range(Range::Interval(n.getMin(), n.getMax()));
} else { // n.isFloatingPoint()
return Range(Range::Interval(n.getNan(), n.getNan()),
Range::Interval(n.getNegativeInf(), n.getPositiveInf()));
}
}
/**
* @brief Computes logical @c not for range @a r.
*/
Range logicalNot(const Range &r)
{
// I should always work with non-empty range.
assert(!r.empty());
Range result;
Number zero = Number(0, sizeof(int), true);
Number one = Number(1, sizeof(int), true);
if (r.containsTrue() && r.containsFalse()) {
// ! [0,1]
result.data.push_back(Range::Interval(zero, one));
} else if (r.containsTrue()) {
// ! [1]
result.data.push_back(Range::Interval(zero, zero));
} else if (r.containsFalse()) {
// ! [0]
result.data.push_back(Range::Interval(one, one));
}
result.normalize();
return result;
}
/**
* @brief Computes logical @c and for ranges @a r1 and @a r2.
*/
Range logicalAnd(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
Range result;
// I use here int type because in C programs, integral promotion is applied.
// Hence, by using int type instead boolean, I cannot break anything.
Number zero = Number(0, sizeof(int), true);
Number one = Number(1, sizeof(int), true);
if (r1.containsFalse() || r2.containsFalse()) {
result.data.push_back(Range::Interval(zero, zero));
}
if (r1.containsTrue() && r2.containsTrue()) {
result.data.push_back(Range::Interval(one, one));
}
result.normalize();
return result;
}
/**
* @brief Computes logical @c or for ranges @a r1 and @a r2.
*/
Range logicalOr(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
Range result;
// I use here int type because in C programs, integral promotion is applied.
// Hence, by using int type instead boolean, I cannot break anything.
Number zero = Number(0, sizeof(int), true);
Number one = Number(1, sizeof(int), true);
if (r1.containsFalse() && r2.containsFalse()) {
result.data.push_back(Range::Interval(zero, zero));
}
if (r1.containsTrue() || r2.containsTrue()) {
result.data.push_back(Range::Interval(one, one));
}
result.normalize();
return result;
}
/**
* @brief Computes logical @c xor for ranges @a r1 and @a r2.
*/
Range logicalXor(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
Range result;
// I use here int type because in C programs, integral promotion is applied.
// Hence, by using int type instead boolean, I cannot break anything.
Number zero = Number(0, sizeof(int), true);
Number one = Number(1, sizeof(int), true);
if ((r1.containsTrue() && r2.containsTrue()) ||
(r1.containsFalse() && r2.containsFalse())) {
result.data.push_back(Range::Interval(zero, zero));
}
if ((r1.containsFalse() && r2.containsTrue()) ||
(r1.containsTrue() && r2.containsFalse())) {
result.data.push_back(Range::Interval(one, one));
}
result.normalize();
return result;
}
/**
* @brief Checks whether ranges are equal.
*
* @param[in] r1 The first range for comparison.
* @param[in] r2 The second range for comparison.
*
* @return @c true if ranges are equal, @c false otherwise.
*/
bool operator==(const Range &r1, const Range &r2)
{
if (r1.size() != r2.size()) {
// Different sizes of ranges. They are not equal.
return false;
}
if (r1.size() == 0) {
// Empty ranges must be treated separately because of assertions.
return true;
}
if (!r1.containsNan() && !r1.containsNan()) {
// If they do not contain NAN.
return r1.data == r2.data;
} else if (r1.containsNan() && r1.containsNan()) {
// If they contain NAN.
return std::equal(r1.begin() + 1, r1.end(), r2.begin() + 1);
} else {
// One range contain NAN. The other does not contain NAN.
return false;
}
}
/**
* @brief Checks whether ranges are not equal.
*
* @param[in] r1 The first range for comparison.
* @param[in] r2 The second range for comparison.
*
* @return @c true if ranges are not equal, @c false otherwise.
*/
bool operator!=(const Range &r1, const Range &r2)
{
return r1.data != r2.data;
}
/**
* @brief Checks whether ranges @a r1 and @a r2 are equal and returns the
* resulting range that can contain three types of intervals: [0,0],
* [1,1] and [0,1]. [0,0] means that no two values from these two ranges
* are equal. [0,1] means that there are values from these two ranges
* that are equal and also values that are not equal. [1,1] means that
* all values from these two rages are equal.
*/
Range logicalEq(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
// I will need this on different places to express interval [0,0].
Number falseNum(false, sizeof(int), true);
Range::Interval falseInt(falseNum, falseNum);
Range result;
if (r1.containsNan() || r2.containsNan()) {
// If at least one range contains NAN, it is necessary to add [0,0].
result.data.push_back(falseInt);
}
if ((r1.size() > 1 || r2.size() > 1) ||
(r1[0].second != r1[0].first) ||
(r2[0].second != r2[0].first)) {
// If at least one range represents more than one point, we have to add [0,0].
result.data.push_back(falseInt);
}
if ((r1.size() == 1 && r2.size() == 1) &&
(r1[0].second == r1[0].first) &&
(r2[0].second == r2[0].first) &&
(r1[0].first != r2[0].first)) {
// If both ranges represent one point but these points are different.
result.data.push_back(falseInt);
}
Range::const_iterator it = r1.begin();
Range::const_iterator jt = r2.begin();
while (it != r1.end() && jt != r2.end()) {
// We have to find common number. If it exists, we have to add [1,1].
if (it->first.isNotNumber()) {
// We never compare NAN to something else because it is always false.
++it;
continue;
}
if (jt->first.isNotNumber()) {
// We never compare NAN to something else because it is always false.
++jt;
continue;
}
if (it->second < jt->first) {
// The first range is strictly lower than the second.
++it;
continue;
}
if (jt->second < it->first) {
// The second interval is strictly lower than the first.
++jt;
continue;
}
// There is some overlapping of intervals in both ranges.
// Hence, r1 and r2 have common number. We have to add [1,1].
Number trueNum(true, sizeof(int), true);
Range::Interval trueInt(trueNum, trueNum);
result.data.push_back(trueInt);
break;
}
result.normalize();
return result;
}
/**
* @brief Checks whether ranges @a r1 and @a r2 are not equal and returns the
* resulting range that can contain three types of intervals: [0,0],
* [1,1] and [0,1]. [0,0] means that all values from these two ranges
* are equal. [0,1] means that there are values from these two ranges
* that are equal and also values that are not equal. [1,1] means that
* no values from these two rages are equal.
*/
Range logicalNeq(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
return logicalNot(logicalEq(r1, r2));
}
/**
* @brief Checks whether range @a r1 is lower than range @a r2 and returns the
* resulting range that can contain three types of intervals: [0,0],
* [1,1] and [0,1]. [0,0] means that no two values from these two
* ranges accomplish the condition for this comparison. [0,1] means that
* there are values from these two ranges that accomplish the condition for
* this comparison and also values that do not accomplish the condition for
* this comparison. [1,1] means that all values from these two ranges
* accomplish the condition for this comparison.
*/
Range logicalLt(const Range &r1, const Range &r2)
{
// I should always work with non-empty range.
assert(!r1.empty() && !r2.empty());
// I will need this on different places to express interval [0,0].
Number falseNum(false, sizeof(int), true);
Range::Interval falseInt(falseNum, falseNum);
Range result;
if (!r1.containsNan() && !r2.containsNan()) {
// There is no NAN in r1 and r2.
Number r1Min = r1[0].first;
Number r1Max = r1[r1.size() - 1].second;
Number r2Min = r2[0].first;
Number r2Max = r2[r2.size() - 1].second;
Number first = Number(r1Min < r2Max, sizeof(int), true);
Number second = Number(r1Max < r2Min, sizeof(int), true);
result.data.push_back(Range::Interval(first, first));
result.data.push_back(Range::Interval(second, second));
} else if (r1.containsNan() && r2.containsNan()) {
// There is NAN in both Ranges. Since there is NAN, we have to add [0,0].
result.data.push_back(falseInt);
if ((r1.size() > 1) && (r2.size() > 1)) {
// Both ranges have at least two intervals. There is no need to do
// something with ranges where at least one range has only NAN because
// if we compare NAN with anything the result is false.
Number r1Min = r1[1].first;
Number r1Max = r1[r1.size() - 1].second;
Number r2Min = r2[1].first;
Number r2Max = r2[r2.size() - 1].second;
Number first = Number(r1Min < r2Max, sizeof(int), true);
Number second = Number(r1Max < r2Min, sizeof(int), true);
result.data.push_back(Range::Interval(first, first));
result.data.push_back(Range::Interval(second, second));
}
} else if (r1.containsNan() && !r2.containsNan()) {
// The first range contains NAN and the second does not contain NAN.
// Since there is NAN, we have to add [0,0].
result.data.push_back(falseInt);
if (r1.size() > 1) {
// The first range has at least one interval apart from the NAN interval.
// For the second range, we have at least one interval - range cannot
// be empty.
Number r1Min = r1[1].first;
Number r1Max = r1[r1.size() - 1].second;
Number r2Min = r2[0].first;
Number r2Max = r2[r2.size() - 1].second;
Number first = Number(r1Min < r2Max, sizeof(int), true);
Number second = Number(r1Max < r2Min, sizeof(int), true);
result.data.push_back(Range::Interval(first, first));
result.data.push_back(Range::Interval(second, second));
}
} else if (!r1.containsNan() && r2.containsNan()) {
// The second range contains NAN and the first does not contain NAN.
// Since there is NAN, we have to add [0,0].
result.data.push_back(falseInt);
if (r2.size() > 1) {
// The second range has at least one interval apart from the NAN interval.
// For the first range, we have at least one interval - range cannot
// be empty.
Number r1Min = r1[0].first;
Number r1Max = r1[r1.size() - 1].second;
Number r2Min = r2[1].first;
Number r2Max = r2[r2.size() - 1].second;
Number first = Number(r1Min < r2Max, sizeof(int), true);