use std::cmp::Ordering;
use std::fmt;
use super::traits::Score;
use super::ScoreLevel;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HardSoftScore {
hard: i64,
soft: i64,
}
impl HardSoftScore {
pub const ZERO: HardSoftScore = HardSoftScore { hard: 0, soft: 0 };
pub const ONE_HARD: HardSoftScore = HardSoftScore { hard: 1, soft: 0 };
pub const ONE_SOFT: HardSoftScore = HardSoftScore { hard: 0, soft: 1 };
#[inline]
pub const fn of(hard: i64, soft: i64) -> Self {
HardSoftScore { hard, soft }
}
#[inline]
pub const fn of_hard(hard: i64) -> Self {
HardSoftScore { hard, soft: 0 }
}
#[inline]
pub const fn of_soft(soft: i64) -> Self {
HardSoftScore { hard: 0, soft }
}
#[inline]
pub const fn hard(&self) -> i64 {
self.hard
}
#[inline]
pub const fn soft(&self) -> i64 {
self.soft
}
pub const fn hard_score(&self) -> HardSoftScore {
HardSoftScore::of_hard(self.hard)
}
pub const fn soft_score(&self) -> HardSoftScore {
HardSoftScore::of_soft(self.soft)
}
}
impl Score for HardSoftScore {
#[inline]
fn is_feasible(&self) -> bool {
self.hard >= 0
}
#[inline]
fn zero() -> Self {
HardSoftScore::ZERO
}
#[inline]
fn levels_count() -> usize {
2
}
fn to_level_numbers(&self) -> Vec<i64> {
vec![self.hard, self.soft]
}
fn from_level_numbers(levels: &[i64]) -> Self {
assert_eq!(levels.len(), 2, "HardSoftScore requires exactly 2 levels");
HardSoftScore::of(levels[0], levels[1])
}
impl_score_scale!(HardSoftScore { hard, soft } => of);
fn level_label(index: usize) -> ScoreLevel {
match index {
0 => ScoreLevel::Hard,
1 => ScoreLevel::Soft,
_ => panic!("HardSoftScore has 2 levels, got index {}", index),
}
}
#[inline]
fn to_scalar(&self) -> f64 {
self.hard as f64 * 1_000_000.0 + self.soft as f64
}
}
impl Ord for HardSoftScore {
fn cmp(&self, other: &Self) -> Ordering {
match self.hard.cmp(&other.hard) {
Ordering::Equal => self.soft.cmp(&other.soft),
other => other,
}
}
}
impl_score_ops!(HardSoftScore { hard, soft } => of);
impl fmt::Debug for HardSoftScore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "HardSoftScore({}, {})", self.hard, self.soft)
}
}
impl fmt::Display for HardSoftScore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}hard/{}soft", self.hard, self.soft)
}
}
impl_score_parse!(HardSoftScore { hard => "hard", soft => "soft" } => of);