1use std::cmp::Ordering;
6use std::fmt;
7use std::ops::{Add, Neg, Sub};
8
9use super::traits::Score;
10use super::ScoreLevel;
11
12#[derive(Clone, Copy, PartialEq, Eq, Hash)]
34pub struct BendableScore<const H: usize, const S: usize> {
35 hard: [i64; H],
36 soft: [i64; S],
37}
38
39impl<const H: usize, const S: usize> BendableScore<H, S> {
40 pub const fn of(hard: [i64; H], soft: [i64; S]) -> Self {
43 BendableScore { hard, soft }
44 }
45
46 pub const fn zero() -> Self {
48 BendableScore {
49 hard: [0; H],
50 soft: [0; S],
51 }
52 }
53
54 pub const fn hard_levels_count(&self) -> usize {
56 H
57 }
58
59 pub const fn soft_levels_count(&self) -> usize {
61 S
62 }
63
64 pub const fn hard_score(&self, level: usize) -> i64 {
69 self.hard[level]
70 }
71
72 pub const fn soft_score(&self, level: usize) -> i64 {
77 self.soft[level]
78 }
79
80 pub const fn hard_scores(&self) -> &[i64; H] {
82 &self.hard
83 }
84
85 pub const fn soft_scores(&self) -> &[i64; S] {
87 &self.soft
88 }
89
90 pub const fn one_hard(level: usize) -> Self {
95 let mut hard = [0; H];
96 hard[level] = 1;
97 BendableScore { hard, soft: [0; S] }
98 }
99
100 pub const fn one_soft(level: usize) -> Self {
105 let mut soft = [0; S];
106 soft[level] = 1;
107 BendableScore { hard: [0; H], soft }
108 }
109}
110
111impl<const H: usize, const S: usize> Default for BendableScore<H, S> {
112 fn default() -> Self {
113 Self::zero()
114 }
115}
116
117impl<const H: usize, const S: usize> Score for BendableScore<H, S> {
118 fn is_feasible(&self) -> bool {
119 self.hard.iter().all(|&s| s >= 0)
120 }
121
122 fn zero() -> Self {
123 BendableScore::zero()
124 }
125
126 fn levels_count() -> usize {
127 H + S
128 }
129
130 fn level_number(&self, index: usize) -> i64 {
131 if index < H {
132 self.hard[index]
133 } else if index < H + S {
134 self.soft[index - H]
135 } else {
136 panic!(
137 "BendableScore<{}, {}> has {} levels, got index {}",
138 H,
139 S,
140 H + S,
141 index
142 )
143 }
144 }
145
146 fn from_level_numbers(levels: &[i64]) -> Self {
147 assert!(levels.len() >= H + S, "Not enough levels provided");
148 let mut hard = [0; H];
149 let mut soft = [0; S];
150 hard.copy_from_slice(&levels[..H]);
151 soft.copy_from_slice(&levels[H..H + S]);
152 BendableScore { hard, soft }
153 }
154
155 #[track_caller]
156 fn multiply(&self, multiplicand: f64) -> Self {
157 let (product, overflow) = self.overflowing_multiply(multiplicand);
158 if overflow {
159 super::float_overflow(multiplicand);
160 }
161 product
162 }
163
164 #[inline]
165 fn overflowing_multiply(&self, multiplicand: f64) -> (Self, bool) {
166 let mut overflow = !multiplicand.is_finite();
167 let product = BendableScore {
168 hard: self
169 .hard
170 .map(|level| super::float_level(level, multiplicand, &mut overflow)),
171 soft: self
172 .soft
173 .map(|level| super::float_level(level, multiplicand, &mut overflow)),
174 };
175 (product, overflow)
176 }
177
178 #[track_caller]
179 fn divide(&self, divisor: f64) -> Self {
180 let factor = 1.0 / divisor;
181 let mut overflow = !divisor.is_finite() || divisor == 0.0;
182 let quotient = BendableScore {
183 hard: self
184 .hard
185 .map(|level| super::float_level(level, factor, &mut overflow)),
186 soft: self
187 .soft
188 .map(|level| super::float_level(level, factor, &mut overflow)),
189 };
190 if overflow {
191 super::float_overflow(divisor);
192 }
193 quotient
194 }
195
196 #[inline]
197 fn scale(&self, factor: i64) -> Self {
198 BendableScore {
199 hard: self.hard.map(|level| super::scale_level(level, factor)),
200 soft: self.soft.map(|level| super::scale_level(level, factor)),
201 }
202 }
203
204 #[inline]
205 fn overflowing_scale(&self, factor: i64) -> (Self, bool) {
206 let mut overflow = false;
207 let mut scale =
208 |level: i64| super::track_overflow(level.overflowing_mul(factor), &mut overflow);
209 let hard = self.hard.map(&mut scale);
210 let soft = self.soft.map(&mut scale);
211 (BendableScore { hard, soft }, overflow)
212 }
213
214 #[track_caller]
215 fn abs(&self) -> Self {
216 Self::checked(
217 self.levels_overflowing(*self, |level, _, overflow| {
218 super::track_overflow(level.overflowing_abs(), overflow)
219 }),
220 "absolute value",
221 )
222 }
223
224 #[inline]
225 fn overflowing_add(self, other: Self) -> (Self, bool) {
226 self.levels_overflowing(other, super::add_level)
227 }
228
229 #[inline]
230 fn overflowing_sub(self, other: Self) -> (Self, bool) {
231 self.levels_overflowing(other, super::sub_level)
232 }
233
234 #[inline]
235 fn overflowing_neg(self) -> (Self, bool) {
236 self.levels_overflowing(self, |level, _, overflow| super::neg_level(level, overflow))
237 }
238
239 fn level_label(index: usize) -> ScoreLevel {
240 if index < H {
241 ScoreLevel::Hard
242 } else if index < H + S {
243 ScoreLevel::Soft
244 } else {
245 panic!(
246 "BendableScore<{}, {}> has {} levels, got index {}",
247 H,
248 S,
249 H + S,
250 index
251 )
252 }
253 }
254
255 #[inline]
256 fn to_scalar(&self) -> f64 {
257 let total_levels = H + S;
258 let mut scalar = 0.0;
259 for (i, &v) in self.hard.iter().enumerate() {
260 let weight = 10_f64.powi(((total_levels - 1 - i) * 6) as i32);
261 scalar += v as f64 * weight;
262 }
263 for (i, &v) in self.soft.iter().enumerate() {
264 let weight = 10_f64.powi(((S - 1 - i) * 6) as i32);
265 scalar += v as f64 * weight;
266 }
267 scalar
268 }
269}
270
271impl<const H: usize, const S: usize> Ord for BendableScore<H, S> {
272 fn cmp(&self, other: &Self) -> Ordering {
273 for i in 0..H {
275 match self.hard[i].cmp(&other.hard[i]) {
276 Ordering::Equal => continue,
277 ord => return ord,
278 }
279 }
280
281 for i in 0..S {
283 match self.soft[i].cmp(&other.soft[i]) {
284 Ordering::Equal => continue,
285 ord => return ord,
286 }
287 }
288
289 Ordering::Equal
290 }
291}
292
293impl<const H: usize, const S: usize> PartialOrd for BendableScore<H, S> {
294 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
295 Some(self.cmp(other))
296 }
297}
298
299impl<const H: usize, const S: usize> BendableScore<H, S> {
300 #[inline]
303 fn levels_overflowing(
304 self,
305 other: Self,
306 operation: impl Fn(i64, i64, &mut bool) -> i64,
307 ) -> (Self, bool) {
308 let mut overflow = false;
309 let mut hard = [0; H];
310 for (slot, (left, right)) in hard.iter_mut().zip(self.hard.iter().zip(&other.hard)) {
311 *slot = operation(*left, *right, &mut overflow);
312 }
313 let mut soft = [0; S];
314 for (slot, (left, right)) in soft.iter_mut().zip(self.soft.iter().zip(&other.soft)) {
315 *slot = operation(*left, *right, &mut overflow);
316 }
317 (BendableScore { hard, soft }, overflow)
318 }
319
320 #[inline]
322 #[track_caller]
323 fn checked((score, overflow): (Self, bool), name: &'static str) -> Self {
324 if overflow {
325 super::arithmetic_overflow(name);
326 }
327 score
328 }
329}
330
331impl<const H: usize, const S: usize> Add for BendableScore<H, S> {
332 type Output = Self;
333
334 #[inline]
335 #[track_caller]
336 fn add(self, other: Self) -> Self {
337 Self::checked(Score::overflowing_add(self, other), "addition")
338 }
339}
340
341impl<const H: usize, const S: usize> Sub for BendableScore<H, S> {
342 type Output = Self;
343
344 #[inline]
345 #[track_caller]
346 fn sub(self, other: Self) -> Self {
347 Self::checked(Score::overflowing_sub(self, other), "subtraction")
348 }
349}
350
351impl<const H: usize, const S: usize> Neg for BendableScore<H, S> {
352 type Output = Self;
353
354 #[inline]
355 #[track_caller]
356 fn neg(self) -> Self {
357 Self::checked(Score::overflowing_neg(self), "negation")
358 }
359}
360
361impl<const H: usize, const S: usize> fmt::Debug for BendableScore<H, S> {
362 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363 write!(
364 f,
365 "BendableScore(hard: {:?}, soft: {:?})",
366 self.hard, self.soft
367 )
368 }
369}
370
371impl<const H: usize, const S: usize> fmt::Display for BendableScore<H, S> {
372 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
373 let hard_str: Vec<String> = self.hard.iter().map(|s| s.to_string()).collect();
374 let soft_str: Vec<String> = self.soft.iter().map(|s| s.to_string()).collect();
375
376 write!(
377 f,
378 "[{}]hard/[{}]soft",
379 hard_str.join("/"),
380 soft_str.join("/")
381 )
382 }
383}