Skip to main content

solverforge_solver/planning/scalar/
candidate.rs

1use std::marker::PhantomData;
2
3use super::ScalarGroupLimits;
4
5#[derive(Debug)]
6pub struct ScalarEdit<S> {
7    descriptor_index: usize,
8    entity_index: usize,
9    variable_name: &'static str,
10    to_value: Option<usize>,
11    _phantom: PhantomData<fn() -> S>,
12}
13
14impl<S> Clone for ScalarEdit<S> {
15    fn clone(&self) -> Self {
16        *self
17    }
18}
19
20impl<S> Copy for ScalarEdit<S> {}
21
22impl<S> PartialEq for ScalarEdit<S> {
23    fn eq(&self, other: &Self) -> bool {
24        self.descriptor_index == other.descriptor_index
25            && self.entity_index == other.entity_index
26            && self.variable_name == other.variable_name
27            && self.to_value == other.to_value
28    }
29}
30
31impl<S> Eq for ScalarEdit<S> {}
32
33impl<S> std::hash::Hash for ScalarEdit<S> {
34    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
35        self.descriptor_index.hash(state);
36        self.entity_index.hash(state);
37        self.variable_name.hash(state);
38        self.to_value.hash(state);
39    }
40}
41
42impl<S> ScalarEdit<S> {
43    #[doc(hidden)]
44    pub const fn from_descriptor_index(
45        descriptor_index: usize,
46        entity_index: usize,
47        variable_name: &'static str,
48        to_value: Option<usize>,
49    ) -> Self {
50        Self {
51            descriptor_index,
52            entity_index,
53            variable_name,
54            to_value,
55            _phantom: PhantomData,
56        }
57    }
58
59    #[doc(hidden)]
60    #[inline]
61    pub fn descriptor_index(&self) -> usize {
62        self.descriptor_index
63    }
64
65    #[doc(hidden)]
66    #[inline]
67    pub fn entity_index(&self) -> usize {
68        self.entity_index
69    }
70
71    #[doc(hidden)]
72    #[inline]
73    pub fn variable_name(&self) -> &'static str {
74        self.variable_name
75    }
76
77    #[doc(hidden)]
78    #[inline]
79    pub fn to_value(&self) -> Option<usize> {
80        self.to_value
81    }
82}
83
84#[derive(Debug)]
85pub struct ScalarCandidate<S> {
86    reason: &'static str,
87    edits: Vec<ScalarEdit<S>>,
88    construction_slot_key: Option<usize>,
89    construction_entity_order_key: Option<i64>,
90    construction_value_order_key: Option<i64>,
91}
92
93impl<S> Clone for ScalarCandidate<S> {
94    fn clone(&self) -> Self {
95        Self {
96            reason: self.reason,
97            edits: self.edits.clone(),
98            construction_slot_key: self.construction_slot_key,
99            construction_entity_order_key: self.construction_entity_order_key,
100            construction_value_order_key: self.construction_value_order_key,
101        }
102    }
103}
104
105impl<S> PartialEq for ScalarCandidate<S> {
106    fn eq(&self, other: &Self) -> bool {
107        self.reason == other.reason
108            && self.edits == other.edits
109            && self.construction_slot_key == other.construction_slot_key
110            && self.construction_entity_order_key == other.construction_entity_order_key
111            && self.construction_value_order_key == other.construction_value_order_key
112    }
113}
114
115impl<S> Eq for ScalarCandidate<S> {}
116
117impl<S> std::hash::Hash for ScalarCandidate<S> {
118    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
119        self.reason.hash(state);
120        self.edits.hash(state);
121        self.construction_slot_key.hash(state);
122        self.construction_entity_order_key.hash(state);
123        self.construction_value_order_key.hash(state);
124    }
125}
126
127impl<S> ScalarCandidate<S> {
128    pub fn new(reason: &'static str, edits: Vec<ScalarEdit<S>>) -> Self {
129        Self {
130            reason,
131            edits,
132            construction_slot_key: None,
133            construction_entity_order_key: None,
134            construction_value_order_key: None,
135        }
136    }
137
138    pub fn with_construction_slot_key(mut self, key: usize) -> Self {
139        self.construction_slot_key = Some(key);
140        self
141    }
142
143    pub fn with_construction_entity_order_key(mut self, key: i64) -> Self {
144        self.construction_entity_order_key = Some(key);
145        self
146    }
147
148    pub fn with_construction_value_order_key(mut self, key: i64) -> Self {
149        self.construction_value_order_key = Some(key);
150        self
151    }
152
153    #[doc(hidden)]
154    #[inline]
155    pub fn reason(&self) -> &'static str {
156        self.reason
157    }
158
159    #[doc(hidden)]
160    #[inline]
161    pub fn edits(&self) -> &[ScalarEdit<S>] {
162        &self.edits
163    }
164
165    #[doc(hidden)]
166    #[inline]
167    pub fn into_edits(self) -> Vec<ScalarEdit<S>> {
168        self.edits
169    }
170
171    #[doc(hidden)]
172    #[inline]
173    pub fn construction_slot_key(&self) -> Option<usize> {
174        self.construction_slot_key
175    }
176
177    #[doc(hidden)]
178    #[inline]
179    pub fn construction_entity_order_key(&self) -> Option<i64> {
180        self.construction_entity_order_key
181    }
182
183    #[doc(hidden)]
184    #[inline]
185    pub fn construction_value_order_key(&self) -> Option<i64> {
186        self.construction_value_order_key
187    }
188}
189
190pub type ScalarCandidateProvider<S> = fn(&S, ScalarGroupLimits) -> Vec<ScalarCandidate<S>>;