Skip to main content

pumpkin_core/engine/variables/
constant.rs

1use enumset::EnumSet;
2use pumpkin_checking::CheckerVariable;
3use pumpkin_checking::IntExt;
4
5use crate::engine::Assignments;
6use crate::predicates::Predicate;
7use crate::predicates::PredicateConstructor;
8use crate::propagation::DomainEvent;
9use crate::propagation::EventDispatcher;
10use crate::propagation::EventTarget;
11use crate::propagation::LocalId;
12use crate::variables::IntegerVariable;
13use crate::variables::TransformableVariable;
14
15impl EventTarget for i32 {
16    fn register(&self, _: &mut impl EventDispatcher, _: EnumSet<DomainEvent>, _: LocalId) {}
17}
18
19impl IntegerVariable for i32 {
20    type AffineView = i32;
21
22    fn lower_bound(&self, _assignment: &Assignments) -> i32 {
23        *self
24    }
25
26    fn lower_bound_at_trail_position(
27        &self,
28        _assignment: &Assignments,
29        _trail_position: usize,
30    ) -> i32 {
31        *self
32    }
33
34    fn upper_bound(&self, _assignment: &Assignments) -> i32 {
35        *self
36    }
37
38    fn upper_bound_at_trail_position(
39        &self,
40        _assignment: &Assignments,
41        _trail_position: usize,
42    ) -> i32 {
43        *self
44    }
45
46    fn contains(&self, _assignment: &Assignments, value: i32) -> bool {
47        value == *self
48    }
49
50    fn contains_at_trail_position(
51        &self,
52        _assignment: &Assignments,
53        value: i32,
54        _trail_position: usize,
55    ) -> bool {
56        value == *self
57    }
58
59    fn iterate_domain(&self, _assignment: &Assignments) -> impl Iterator<Item = i32> {
60        std::iter::once(*self)
61    }
62
63    fn unwatch_all(&self, _watchers: &mut crate::engine::notifications::Watchers<'_>) {}
64
65    fn watch_all_backtrack(
66        &self,
67        _watchers: &mut crate::engine::notifications::Watchers<'_>,
68        _events: EnumSet<DomainEvent>,
69    ) {
70    }
71
72    fn unpack_event(&self, _event: crate::propagation::OpaqueDomainEvent) -> DomainEvent {
73        unreachable!()
74    }
75
76    fn get_holes_at_current_checkpoint(
77        &self,
78        _assignments: &Assignments,
79    ) -> impl Iterator<Item = i32> {
80        std::iter::empty()
81    }
82
83    fn get_holes(&self, _assignments: &Assignments) -> impl Iterator<Item = i32> {
84        std::iter::empty()
85    }
86}
87
88impl TransformableVariable<i32> for i32 {
89    fn scaled(&self, scale: i32) -> i32 {
90        *self * scale
91    }
92
93    fn offset(&self, offset: i32) -> i32 {
94        *self + offset
95    }
96}
97
98impl PredicateConstructor for i32 {
99    type Value = i32;
100
101    fn equality_predicate(&self, bound: Self::Value) -> Predicate {
102        if bound == *self {
103            Predicate::trivially_true()
104        } else {
105            Predicate::trivially_false()
106        }
107    }
108
109    fn lower_bound_predicate(&self, bound: Self::Value) -> Predicate {
110        if bound <= *self {
111            Predicate::trivially_true()
112        } else {
113            Predicate::trivially_false()
114        }
115    }
116
117    fn upper_bound_predicate(&self, bound: Self::Value) -> Predicate {
118        if bound >= *self {
119            Predicate::trivially_true()
120        } else {
121            Predicate::trivially_false()
122        }
123    }
124
125    fn disequality_predicate(&self, bound: Self::Value) -> Predicate {
126        if bound != *self {
127            Predicate::trivially_true()
128        } else {
129            Predicate::trivially_false()
130        }
131    }
132}
133
134impl CheckerVariable<Predicate> for i32 {
135    fn does_atomic_constrain_self(&self, _atomic: &Predicate) -> bool {
136        false
137    }
138
139    fn atomic_less_than(&self, value: i32) -> Predicate {
140        if value >= *self {
141            Predicate::trivially_true()
142        } else {
143            Predicate::trivially_false()
144        }
145    }
146
147    fn atomic_greater_than(&self, value: i32) -> Predicate {
148        if value <= *self {
149            Predicate::trivially_true()
150        } else {
151            Predicate::trivially_false()
152        }
153    }
154
155    fn atomic_equal(&self, value: i32) -> Predicate {
156        if value == *self {
157            Predicate::trivially_true()
158        } else {
159            Predicate::trivially_false()
160        }
161    }
162
163    fn atomic_not_equal(&self, value: i32) -> Predicate {
164        if value != *self {
165            Predicate::trivially_true()
166        } else {
167            Predicate::trivially_false()
168        }
169    }
170
171    fn induced_lower_bound(
172        &self,
173        _variable_state: &pumpkin_checking::VariableState<Predicate>,
174    ) -> IntExt {
175        IntExt::Int(*self)
176    }
177
178    fn induced_upper_bound(
179        &self,
180        _variable_state: &pumpkin_checking::VariableState<Predicate>,
181    ) -> IntExt {
182        IntExt::Int(*self)
183    }
184
185    fn induced_fixed_value(
186        &self,
187        _variable_state: &pumpkin_checking::VariableState<Predicate>,
188    ) -> Option<i32> {
189        Some(*self)
190    }
191
192    fn induced_domain_contains(
193        &self,
194        _variable_state: &pumpkin_checking::VariableState<Predicate>,
195        value: i32,
196    ) -> bool {
197        value == *self
198    }
199
200    fn induced_holes<'this, 'state>(
201        &'this self,
202        _variable_state: &'state pumpkin_checking::VariableState<Predicate>,
203    ) -> impl Iterator<Item = i32> + 'state
204    where
205        'this: 'state,
206    {
207        std::iter::empty()
208    }
209
210    fn iter_induced_domain<'this, 'state>(
211        &'this self,
212        _variable_state: &'state pumpkin_checking::VariableState<Predicate>,
213    ) -> Option<impl Iterator<Item = i32> + 'state>
214    where
215        'this: 'state,
216    {
217        Some(std::iter::once(*self))
218    }
219}