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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
use std::fmt::Debug;
use std::hash::Hash;
use std::hash::Hasher;
use enumset::EnumSet;
use fnv::FnvBuildHasher;
use indexmap::Equivalent;
use indexmap::IndexSet;
use indexmap::set::MutableValues;
use super::PredicateIdAssignments;
use super::PredicateValue;
use crate::basic_types::PredicateId;
use crate::containers::StorageKey;
use crate::engine::TrailedInteger;
use crate::engine::TrailedValues;
use crate::predicates::Predicate;
use crate::predicates::PredicateType;
use crate::pumpkin_assert_eq_simple;
use crate::pumpkin_assert_moderate;
use crate::pumpkin_assert_simple;
use crate::variables::DomainId;
/// A generic structure for keeping track of the polarity of [`Predicate`]s.
///
/// This structure keeps track of all different [`PredicateType`]s.
#[derive(Debug, Clone)]
pub(crate) struct PredicateTracker {
/// The [`DomainId`] which the tracker is tracking the polarity for.
domain_id: DomainId,
/// `smaller[i]` is the index of the element with the largest value such that it is smaller
/// than `values[i]`
smaller: Vec<u32>,
/// `greater[i]` is the index of the element with the smallest value such that it is larger
/// than `values[i]`
greater: Vec<u32>,
/// A [`TrailedInteger`] which points to the largest lowest value which is assigned.
///
/// For example, if we have the values `x in [1, 5, 7, 9]` and we know that `[x >= 6]` holds,
/// then [`PredicateTracker::min_assigned`] will point to index 1.
min_assigned: TrailedInteger,
/// A [`TrailedInteger`] which points to the smallest largest value which is assigned.
///
/// For example, if we have the values `x in [1, 5, 7, 9]` and we know that `[x <= 8]` holds,
/// then [`PredicateTracker::min_assigned`] will point to index 3.
max_assigned: TrailedInteger,
/// A [`TrailedInteger`] which points to the largest lowest value which is assigned but not
/// equal to the value.
///
/// For example, if we have the values `x in [1, 6, 7, 9]` and we know that `[x >= 6]` holds,
/// then [`PredicateTracker::min_assigned`] will point to index 1.
min_assigned_strict: TrailedInteger,
/// A [`TrailedInteger`] which points to the smallest largest value which is assigned but not
/// equal to the value.
///
/// For example, if we have the values `x in [1, 5, 8, 9]` and we know that `[x <= 8]` holds,
/// then [`PredicateTracker::min_assigned`] will point to index 3.
max_assigned_strict: TrailedInteger,
/// The values which are currently being tracked by this [`PredicateTracker`].
///
/// We want quick membership queries but a hash-based set cannot be used since we require the
/// indices to remain consistent (since they are, for example, stored in [`Self::smaller`] and
/// [`Self::greater`]). Thus, we use an [`IndexSet`] which allows us to perform efficient
/// membership queries while also allowing us to index into the set.
///
/// Note that these values are not sorted in any way.
values: IndexSet<TrackedValue, FnvBuildHasher>,
/// The [`PredicateId`]s corresponding to the predicates for each value in
/// [`PredicateTracker::values`].
ids: Vec<Vec<PredicateId>>,
/// The [`PredicateType`]s tracked by this [`PredicateTracker`].
tracked: EnumSet<PredicateType>,
}
// A value tracked by the [`PredicateTracker`], keeps track of the values in the lowest 4 bits.
#[derive(Clone, Copy, Debug)]
struct TrackedValue {
value: i32,
flags: EnumSet<PredicateType>,
}
impl TrackedValue {
/// Creates a new [`TrackedValue`].
fn new(value: i32) -> Self {
Self {
value,
flags: EnumSet::new(),
}
}
/// Store the provided [`PredicateType`] in the [`TrackedValue`].
fn track_predicate_type(&mut self, predicate_type: PredicateType) {
self.flags |= predicate_type;
}
/// Returns whether the provided [`PredicateType`] is tracked by this [`TrackedValue`].
fn does_track_predicate_type(&self, predicate_type: PredicateType) -> bool {
self.flags.contains(predicate_type)
}
/// Return the [`PredicateType`]s which are stored in this [`TrackedValue`].
///
/// These are always returned in a pre-defined order, not the order in which they were
/// inserted.
fn get_predicate_types(&self) -> impl Iterator<Item = PredicateType> {
self.flags.iter()
}
/// Returns the value which is stored in this [`TrackedValue`].
fn get_value(&self) -> i32 {
self.value
}
}
impl PartialEq for TrackedValue {
fn eq(&self, other: &Self) -> bool {
self.get_value().eq(&other.get_value())
}
}
impl Eq for TrackedValue {}
impl PartialOrd for TrackedValue {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for TrackedValue {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.get_value().cmp(&other.get_value())
}
}
impl Hash for TrackedValue {
fn hash<H: Hasher>(&self, state: &mut H) {
self.get_value().hash(state)
}
}
impl Equivalent<TrackedValue> for i32 {
fn equivalent(&self, key: &TrackedValue) -> bool {
*self == key.get_value()
}
}
impl PredicateTracker {
pub(super) fn new() -> Self {
Self {
domain_id: DomainId::new(0),
// We do not want to create the trailed integers until necessary
min_assigned: TrailedInteger::create_from_index(0),
max_assigned: TrailedInteger::create_from_index(0),
min_assigned_strict: TrailedInteger::create_from_index(0),
max_assigned_strict: TrailedInteger::create_from_index(0),
smaller: Vec::default(),
greater: Vec::default(),
values: Default::default(),
ids: Vec::default(),
tracked: EnumSet::default(),
}
}
pub(super) fn initialise(
&mut self,
domain_id: DomainId,
initial_lower_bound: i32,
initial_upper_bound: i32,
trailed_values: &mut TrailedValues,
) {
if !self.values.is_empty() {
// The structures has been initialised previously
return;
}
self.min_assigned = trailed_values.grow(0);
self.max_assigned = trailed_values.grow(1);
self.min_assigned_strict = trailed_values.grow(0);
self.max_assigned_strict = trailed_values.grow(1);
// We set the tracking domain id
self.domain_id = domain_id;
// Then we place some sentinels for simplicity's sake which are always true
//
// It is _probably_ okay to note use the `-1` and `+1`
let _ = self.insert_value(initial_lower_bound - 1);
let _ = self.insert_value(initial_upper_bound + 1);
// These should never be queried so we provide a placeholder
self.ids.push(vec![]);
self.ids.push(vec![]);
// Then we place the sentinels into the `smaller` structure
//
// For the first element (containing the lower-bound), there is no smaller element
self.smaller.push(u32::MAX);
// For the second element (containing the upper-bound), the smaller element will currently
// point to the lower-bound element
self.smaller.push(0);
// Then we place the sentinels into the `greater` structure
//
// For the first element (containing the lower-bound), the greater element will currently
// point to the upper-bound element
self.greater.push(1);
// For the second element (containing the upper-bound), there is no greater element
self.greater.push(u32::MAX);
}
/// Returns whether any [`PredicateType::Equal`] or [`PredicateType::NotEqual`] types are being
/// tracked.
pub(super) fn can_be_updated_by_disequality(&self) -> bool {
self.tracked.contains(PredicateType::Equal)
|| self.tracked.contains(PredicateType::NotEqual)
}
/// Returns whether no more updates can take place due to the bounds not being able to be
/// moved.
pub(super) fn is_fixed(&self, trailed_values: &TrailedValues) -> bool {
if self.tracked.is_empty() {
// If it is empty, then it is trivially fixed
return true;
}
// The idea is to use the `min_assigned` and `max_assigned` fields to infer whether any
// updates can take place.
//
// Let's first look at an example for a variable `x`, imagine we have the following values
// [0, 10, 5, 2, 3, 1] where `x \in [0, 10]` (i.e. the first two values are fixed);
// we know that `min_assigned = 0` and `max_assigned = 1`; now we update the domain
// of `x` to be `[4, 4]`. We know that `min_assigned = 4` (pointing to value 3), and
// `max_assigned = 2` (pointing to value 5).
//
// If we now look at the successor of `min_assigned` (with index 2 and value 5) and the
// predecessor of `max_assigned` (with index 5 and value 3), then we can see that
// these are already assigned (according to `min_assigned` and `max_assigned`
// respectively).
//
// Thus, we simply need to check whether either:
// - The successor of `min_assigned` is equal to `max_assigned`
// - The predecessor of `max_assigned` is equal to `min_assigned`
let min_assigned_index = trailed_values.read(self.min_assigned) as usize;
let min_unassigned_index = self.greater[min_assigned_index] as usize;
pumpkin_assert_simple!(self.values[min_assigned_index] < self.values[min_unassigned_index]);
let max_assigned_index = trailed_values.read(self.max_assigned) as usize;
let max_unassigned_index = self.smaller[max_assigned_index] as usize;
pumpkin_assert_simple!(self.values[max_assigned_index] > self.values[max_unassigned_index]);
self.values[min_unassigned_index] >= self.values[max_assigned_index]
|| self.values[max_unassigned_index] <= self.values[min_assigned_index]
}
/// Inserts the value into the internal structures.
fn insert_value(&mut self, value: i32) -> usize {
let index = self.values.len();
let result = self.values.insert(TrackedValue::new(value));
assert!(result);
index
}
/// Returns the value at the provided index.
///
/// If the index is out of bounds, this method will panic.
fn get_value_at_index(&self, index: usize) -> TrackedValue {
*self
.values
.get_index(index)
.expect("Expected provided index to exist")
}
/// Returns all of the values currently present.
fn get_all_values(&self) -> impl Iterator<Item = TrackedValue> {
self.values.iter().copied()
}
/// Returns the index of the provided value if it is present.
fn get_index_of_value(&self, value: i32) -> Option<usize> {
self.values.get_index_of(&value)
}
/// Allows the [`PredicateTracker`] to indicate that a tracked [`Predicate`] has been satisfied.
fn predicate_has_been_satisfied(
&self,
index: usize,
predicate_index: usize,
predicate_id_assignments: &mut PredicateIdAssignments,
) {
let predicate_id = self.ids[index][predicate_index];
if predicate_id.id == u32::MAX {
// If it is a placeholder then we ignore it
return;
}
predicate_id_assignments.store_predicate(predicate_id, PredicateValue::AssignedTrue);
}
/// Allows the [`PredicateTracker`] to indicate that a tracked [`Predicate`] has been falsified.
fn predicate_has_been_falsified(
&self,
index: usize,
predicate_index: usize,
predicate_id_assignments: &mut PredicateIdAssignments,
) {
let predicate_id = self.ids[index][predicate_index];
if predicate_id.id == u32::MAX {
return;
}
predicate_id_assignments.store_predicate(predicate_id, PredicateValue::AssignedFalse);
}
/// Tracks a [`Predicate`] with a provided `value` and [`PredicateId`].
///
/// Returns true if it was not already tracked and false otherwise.
pub(super) fn track(&mut self, predicate: Predicate, predicate_id: PredicateId) -> bool {
pumpkin_assert_simple!(
!self.values.is_empty(),
"Initialise should have been called previously"
);
self.tracked |= predicate.get_predicate_type();
let value = predicate.get_right_hand_side();
// We check whether it is already tracked
if let Some((index, tracked_value)) = self.values.get_full_mut2(&value) {
// Then we check whether this particular predicate type has already been tracked
if !tracked_value.does_track_predicate_type(predicate.get_predicate_type()) {
let current_mask = predicate.get_predicate_type() as u8;
// We keep the predicate ids in the same order as they are returned by the
// TrackedValue
if let Some(pos) = tracked_value
.get_predicate_types()
.position(|predicate_type| predicate_type as u8 > current_mask)
{
self.ids[index].insert(pos, predicate_id);
} else {
self.ids[index].push(predicate_id);
}
tracked_value.track_predicate_type(predicate.get_predicate_type());
return true;
}
return false;
}
// Then we track the information for updating `smaller`; recall that we place a sentinel
// node with the smallest possible value at index 0
let index_largest_value_smaller_than;
// And we track the information for updating `greater`; recall that we place a sentinel
// node with the largest possible value at index 1
let index_smallest_value_larger_than;
// Then we go over each value to determine where to place the element in the linked list.
//
// Note that the element at the 1st index has the largest value
let mut index = 1;
loop {
let index_value = self.get_value_at_index(index);
pumpkin_assert_simple!(index_value.get_value() != value,);
// As soon as we have found a value smaller than the to track value, we can stop
if index_value.get_value() < value {
index_largest_value_smaller_than = index as u32;
index_smallest_value_larger_than = self.greater[index];
break;
}
index = self.smaller[index] as usize;
}
pumpkin_assert_eq_simple!(
self.get_value_at_index(index_largest_value_smaller_than as usize),
self.get_all_values()
.filter(|&stored_value| stored_value.get_value() < value)
.max()
.unwrap(),
);
pumpkin_assert_eq_simple!(
self.get_value_at_index(index_smallest_value_larger_than as usize),
self.get_all_values()
.filter(|&stored_value| stored_value.get_value() > value)
.min()
.unwrap()
);
let new_index = self.insert_value(value);
self.values
.get_index_mut2(new_index)
.unwrap()
.track_predicate_type(predicate.get_predicate_type());
self.greater[index_largest_value_smaller_than as usize] = new_index as u32;
self.smaller[index_smallest_value_larger_than as usize] = new_index as u32;
// Then we update the other structures
self.smaller.push(index_largest_value_smaller_than);
self.greater.push(index_smallest_value_larger_than);
self.ids.push(vec![predicate_id]);
true
}
pub(super) fn on_update(
&mut self,
predicate: Predicate,
trailed_values: &mut TrailedValues,
predicate_id_assignments: &mut PredicateIdAssignments,
) {
// If there are no tracked predicate types, then we don't need to perform any updates
if self.tracked.is_empty() {
return;
}
let value = predicate.get_right_hand_side();
// Then we update our internal structures
//
// The updates which can occur depend on the predicate type
if predicate.is_lower_bound_predicate() {
// We have a lower-bound predicate, so we move our min indices
//
// First, we move `min_assigned_strict` by checking whether the greater predicate is
// also satisfied
let mut greater_strict =
self.greater[trailed_values.read(self.min_assigned_strict) as usize];
while greater_strict != u32::MAX
&& value > self.values[greater_strict as usize].get_value()
{
// Now we go over all tracked predicate types and update them
for (predicate_index, predicate_type) in self.values[greater_strict as usize]
.get_predicate_types()
.enumerate()
{
match predicate_type {
PredicateType::UpperBound | PredicateType::Equal => {
self.predicate_has_been_falsified(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::NotEqual => {
self.predicate_has_been_satisfied(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::LowerBound => {
self.predicate_has_been_satisfied(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
}
}
// Note that we can move both instances since, if an update has a value `>` a
// tracked value, then it is also necessarily `>=`
trailed_values.assign(self.min_assigned_strict, greater_strict as i64);
trailed_values.assign(self.min_assigned, greater_strict as i64);
greater_strict = self.greater[greater_strict as usize];
}
// Now we move the `>=` index as well.
let mut greater = self.greater[trailed_values.read(self.min_assigned) as usize];
while greater != u32::MAX && value >= self.values[greater as usize].get_value() {
// In this case, we can only have a lower-bound update, because all of the other
// predicate types require a strictly larger value
if let Some(predicate_index) = self.values[greater as usize]
.get_predicate_types()
.position(|predicate_type| predicate_type == PredicateType::LowerBound)
{
self.predicate_has_been_satisfied(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
trailed_values.assign(self.min_assigned, greater as i64);
greater = self.greater[greater as usize];
}
} else if predicate.is_upper_bound_predicate() {
// We have an upper-bound predicate, so we move our max indices
//
// First, we move `max_assigned_strict` by checking whether the smaller predicate is
// also satisfied
let mut smaller_strict =
self.smaller[trailed_values.read(self.max_assigned_strict) as usize];
while smaller_strict != u32::MAX
&& value < self.values[smaller_strict as usize].get_value()
{
// Now we go over all tracked predicate types and update them
for (predicate_index, predicate_type) in self.values[smaller_strict as usize]
.get_predicate_types()
.enumerate()
{
match predicate_type {
PredicateType::LowerBound | PredicateType::Equal => {
self.predicate_has_been_falsified(
smaller_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::NotEqual => {
self.predicate_has_been_satisfied(
smaller_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::UpperBound => {
self.predicate_has_been_satisfied(
smaller_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
}
}
// Note that we can move both instances since, if an update has a value `<` a
// tracked value, then it is also necessarily `<=`
trailed_values.assign(self.max_assigned_strict, smaller_strict as i64);
trailed_values.assign(self.max_assigned, smaller_strict as i64);
smaller_strict = self.smaller[smaller_strict as usize];
}
// Now we move the `>=` index as well.
let mut smaller = self.smaller[trailed_values.read(self.max_assigned) as usize];
while smaller != u32::MAX && value <= self.values[smaller as usize].get_value() {
// In this case, we can only have a upper-bound update, because all of the other
// predicate types require a strictly smaller value
if let Some(predicate_index) = self.values[smaller as usize]
.get_predicate_types()
.position(|predicate_type| predicate_type == PredicateType::UpperBound)
{
self.predicate_has_been_satisfied(
smaller as usize,
predicate_index,
predicate_id_assignments,
);
}
trailed_values.assign(self.max_assigned, smaller as i64);
smaller = self.smaller[smaller as usize];
}
} else if predicate.is_not_equal_predicate() {
// If the right-hand side of the disequality predicate is smaller than the value
// pointed to by `min_assigned_strict` then no updates can take place
if value
<= self.values[trailed_values.read(self.min_assigned_strict) as usize].get_value()
{
return;
}
// If the right-hand side of the disequality predicate is larger than the value
// pointed to by `max_assigned_strict` then no updates can take place
if value
>= self.values[trailed_values.read(self.max_assigned_strict) as usize].get_value()
{
return;
}
// Now we check whether the value of the right-hand side of the disequality is tracked.
//
// If it is, and a disequality or equality predicate type are tracked, then we can
// update them accordingly
if let Some(index) = self.get_index_of_value(value) {
for (predicate_index, predicate_type) in
self.values[index].get_predicate_types().enumerate()
{
match predicate_type {
PredicateType::NotEqual => self.predicate_has_been_satisfied(
index,
predicate_index,
predicate_id_assignments,
),
PredicateType::Equal => self.predicate_has_been_falsified(
index,
predicate_index,
predicate_id_assignments,
),
_ => {}
}
}
}
} else if predicate.is_equality_predicate() {
// First update the lower-bound if necessary
//
// We have an equality predicate, so we move our min indices
//
// First, we move `min_assigned_strict` by checking whether the greater predicate is
// also satisfied
let mut greater_strict =
self.greater[trailed_values.read(self.min_assigned_strict) as usize];
while greater_strict != u32::MAX
&& value > self.values[greater_strict as usize].get_value()
{
// Now we go over all tracked predicate types and update them
for (predicate_index, predicate_type) in self.values[greater_strict as usize]
.get_predicate_types()
.enumerate()
{
match predicate_type {
PredicateType::UpperBound | PredicateType::Equal => {
self.predicate_has_been_falsified(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::NotEqual => {
self.predicate_has_been_satisfied(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::LowerBound => {
self.predicate_has_been_satisfied(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
}
}
// Note that we can move both instances since, if an update has a value `>` a
// tracked value, then it is also necessarily `>=`
trailed_values.assign(self.min_assigned_strict, greater_strict as i64);
trailed_values.assign(self.min_assigned, greater_strict as i64);
greater_strict = self.greater[greater_strict as usize];
}
// Now we move the `>=` index as well.
let mut greater = self.greater[trailed_values.read(self.min_assigned) as usize];
while greater != u32::MAX && value >= self.values[greater as usize].get_value() {
// In this case, we can only have a lower-bound update, because all of the other
// predicate types require a strictly larger value
if let Some(predicate_index) = self.values[greater as usize]
.get_predicate_types()
.position(|predicate_type| predicate_type == PredicateType::LowerBound)
{
self.predicate_has_been_satisfied(
greater_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
trailed_values.assign(self.min_assigned, greater as i64);
greater = self.greater[greater as usize];
}
// Then the upper-bound if necessary
//
// We have an equality predicate, so we move our max indices
//
// First, we move `max_assigned_strict` by checking whether the smaller predicate is
// also satisfied
let mut smaller_strict =
self.smaller[trailed_values.read(self.max_assigned_strict) as usize];
while smaller_strict != u32::MAX
&& value < self.values[smaller_strict as usize].get_value()
{
// Now we go over all tracked predicate types and update them
for (predicate_index, predicate_type) in self.values[smaller_strict as usize]
.get_predicate_types()
.enumerate()
{
match predicate_type {
PredicateType::LowerBound | PredicateType::Equal => {
self.predicate_has_been_falsified(
smaller_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::NotEqual => {
self.predicate_has_been_satisfied(
smaller_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::UpperBound => {
self.predicate_has_been_satisfied(
smaller_strict as usize,
predicate_index,
predicate_id_assignments,
);
}
}
}
// Note that we can move both instances since, if an update has a value `<` a
// tracked value, then it is also necessarily `<=`
trailed_values.assign(self.max_assigned_strict, smaller_strict as i64);
trailed_values.assign(self.max_assigned, smaller_strict as i64);
smaller_strict = self.smaller[smaller_strict as usize];
}
// Now we move the `>=` index as well.
let mut smaller = self.smaller[trailed_values.read(self.max_assigned) as usize];
while smaller != u32::MAX && value <= self.values[smaller as usize].get_value() {
// In this case, we can only have a upper-bound update, because all of the other
// predicate types require a strictly smaller value
if let Some(predicate_index) = self.values[smaller as usize]
.get_predicate_types()
.position(|predicate_type| predicate_type == PredicateType::UpperBound)
{
self.predicate_has_been_satisfied(
smaller as usize,
predicate_index,
predicate_id_assignments,
);
}
trailed_values.assign(self.max_assigned, smaller as i64);
smaller = self.smaller[smaller as usize];
}
// Now that we have moved the indices, we want to check whether it has become true
//
// We check whether min_assigned_strict and max_assigned_strict point to each other and
// that the next value is equal to the value
let greater = self.greater[trailed_values.read(self.min_assigned_strict) as usize];
if greater == self.smaller[trailed_values.read(self.max_assigned_strict) as usize]
&& self.values[greater as usize].get_value() == value
{
for (predicate_index, predicate_type) in self.values[greater as usize]
.get_predicate_types()
.enumerate()
{
match predicate_type {
PredicateType::NotEqual => {
self.predicate_has_been_falsified(
greater as usize,
predicate_index,
predicate_id_assignments,
);
}
PredicateType::Equal => {
self.predicate_has_been_satisfied(
greater as usize,
predicate_index,
predicate_id_assignments,
);
}
_ => {}
}
}
} else {
pumpkin_assert_moderate!(
!self.values.contains(&value)
|| (!self.values[self.get_index_of_value(value).unwrap()]
.does_track_predicate_type(PredicateType::NotEqual)
&& !self.values[self.get_index_of_value(value).unwrap()]
.does_track_predicate_type(PredicateType::Equal))
);
}
}
}
}
#[cfg(test)]
mod tests {
use crate::engine::Assignments;
use crate::engine::TrailedValues;
use crate::engine::notifications::predicate_notification::PredicateIdAssignments;
use crate::engine::notifications::predicate_notification::predicate_tracker::PredicateTracker;
use crate::engine::notifications::predicate_notification::predicate_tracker::TrackedValue;
use crate::predicate;
use crate::predicates::PredicateIdGenerator;
use crate::predicates::PredicateType;
#[test]
fn test_update_lower_bound() {
let mut assignments = Assignments::default();
let mut id_generator = PredicateIdGenerator::default();
let mut trailed_values = TrailedValues::default();
let mut predicate_id_assignments = PredicateIdAssignments::default();
let x = assignments.grow(0, 10);
let mut tracker = PredicateTracker::new();
tracker.initialise(x, 0, 10, &mut trailed_values);
let predicate = predicate!(x >= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(!added);
let predicate = predicate!(x <= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x != 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x == 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
tracker.on_update(
predicate!(x >= 5),
&mut trailed_values,
&mut predicate_id_assignments,
);
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x >= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x <= 5))));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x == 5))));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x != 5))));
tracker.on_update(
predicate!(x >= 6),
&mut trailed_values,
&mut predicate_id_assignments,
);
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x >= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x <= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x == 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x != 5)),
&assignments,
&mut id_generator
));
}
#[test]
fn test_update_upper_bound() {
let mut assignments = Assignments::default();
let mut id_generator = PredicateIdGenerator::default();
let mut trailed_values = TrailedValues::default();
let mut predicate_id_assignments = PredicateIdAssignments::default();
let x = assignments.grow(0, 10);
let mut tracker = PredicateTracker::new();
tracker.initialise(x, 0, 10, &mut trailed_values);
let predicate = predicate!(x >= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(!added);
let predicate = predicate!(x <= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x != 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x == 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
tracker.on_update(
predicate!(x <= 5),
&mut trailed_values,
&mut predicate_id_assignments,
);
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x <= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x >= 5))));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x == 5))));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x != 5))));
tracker.on_update(
predicate!(x <= 4),
&mut trailed_values,
&mut predicate_id_assignments,
);
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x <= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x >= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x == 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x != 5)),
&assignments,
&mut id_generator
));
}
#[test]
fn test_update_not_equals() {
let mut assignments = Assignments::default();
let mut id_generator = PredicateIdGenerator::default();
let mut trailed_values = TrailedValues::default();
let mut predicate_id_assignments = PredicateIdAssignments::default();
let x = assignments.grow(0, 10);
let mut tracker = PredicateTracker::new();
tracker.initialise(x, 0, 10, &mut trailed_values);
let predicate = predicate!(x >= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(!added);
let predicate = predicate!(x <= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x != 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x == 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
tracker.on_update(
predicate!(x != 5),
&mut trailed_values,
&mut predicate_id_assignments,
);
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x != 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x == 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x >= 5))));
assert!(predicate_id_assignments.is_unknown(id_generator.get_id(predicate!(x <= 5))));
}
#[test]
fn test_update_equals() {
let mut assignments = Assignments::default();
let mut id_generator = PredicateIdGenerator::default();
let mut trailed_values = TrailedValues::default();
let mut predicate_id_assignments = PredicateIdAssignments::default();
let x = assignments.grow(0, 10);
let mut tracker = PredicateTracker::new();
tracker.initialise(x, 0, 10, &mut trailed_values);
let predicate = predicate!(x >= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(!added);
let predicate = predicate!(x <= 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x != 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x == 5);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x == 6);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
let predicate = predicate!(x != 6);
let added = tracker.track(predicate, id_generator.get_id(predicate));
assert!(added);
tracker.on_update(
predicate!(x == 6),
&mut trailed_values,
&mut predicate_id_assignments,
);
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x == 6)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x != 6)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x != 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x == 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_satisfied(
id_generator.get_id(predicate!(x >= 5)),
&assignments,
&mut id_generator
));
assert!(predicate_id_assignments.is_falsified(
id_generator.get_id(predicate!(x <= 5)),
&assignments,
&mut id_generator
));
}
#[test]
fn pack_negative_value() {
let x = -255;
let mut value = TrackedValue::new(x);
assert_eq!(value.get_value(), x);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
Vec::<PredicateType>::new()
);
value.track_predicate_type(PredicateType::Equal);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![PredicateType::Equal]
);
assert_eq!(value.get_value(), x);
value.track_predicate_type(PredicateType::LowerBound);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![PredicateType::LowerBound, PredicateType::Equal]
);
assert_eq!(value.get_value(), x);
value.track_predicate_type(PredicateType::NotEqual);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![
PredicateType::LowerBound,
PredicateType::NotEqual,
PredicateType::Equal,
]
);
assert_eq!(value.get_value(), x);
value.track_predicate_type(PredicateType::UpperBound);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![
PredicateType::LowerBound,
PredicateType::NotEqual,
PredicateType::Equal,
PredicateType::UpperBound,
]
);
assert_eq!(value.get_value(), x);
}
#[test]
fn pack_positive_value() {
let x = 255;
let mut value = TrackedValue::new(x);
assert_eq!(value.get_value(), x);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
Vec::<PredicateType>::new()
);
value.track_predicate_type(PredicateType::Equal);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![PredicateType::Equal]
);
assert_eq!(value.get_value(), x);
value.track_predicate_type(PredicateType::LowerBound);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![PredicateType::LowerBound, PredicateType::Equal]
);
assert_eq!(value.get_value(), x);
value.track_predicate_type(PredicateType::NotEqual);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![
PredicateType::LowerBound,
PredicateType::NotEqual,
PredicateType::Equal,
]
);
assert_eq!(value.get_value(), x);
value.track_predicate_type(PredicateType::UpperBound);
assert_eq!(
value.get_predicate_types().collect::<Vec<_>>(),
vec![
PredicateType::LowerBound,
PredicateType::NotEqual,
PredicateType::Equal,
PredicateType::UpperBound,
]
);
assert_eq!(value.get_value(), x);
}
}