2020-09-22 10:53:20 +02:00
|
|
|
use std::cmp;
|
2021-06-16 18:33:33 +02:00
|
|
|
|
2023-12-06 15:49:02 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-09-22 17:48:24 +02:00
|
|
|
use crate::{relative_from_absolute_position, Position};
|
2020-09-22 10:53:20 +02:00
|
|
|
|
2023-10-03 12:02:16 +02:00
|
|
|
pub const MAX_DISTANCE: u32 = 4;
|
2020-09-22 10:53:20 +02:00
|
|
|
|
|
|
|
pub fn index_proximity(lhs: u32, rhs: u32) -> u32 {
|
|
|
|
if lhs <= rhs {
|
|
|
|
cmp::min(rhs - lhs, MAX_DISTANCE)
|
|
|
|
} else {
|
|
|
|
cmp::min((lhs - rhs) + 1, MAX_DISTANCE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn positions_proximity(lhs: Position, rhs: Position) -> u32 {
|
2021-09-22 17:48:24 +02:00
|
|
|
let (lhs_attr, lhs_index) = relative_from_absolute_position(lhs);
|
|
|
|
let (rhs_attr, rhs_index) = relative_from_absolute_position(rhs);
|
2021-06-16 18:33:33 +02:00
|
|
|
if lhs_attr != rhs_attr {
|
|
|
|
MAX_DISTANCE
|
|
|
|
} else {
|
2021-09-22 17:48:24 +02:00
|
|
|
index_proximity(lhs_index as u32, rhs_index as u32)
|
2021-06-16 18:33:33 +02:00
|
|
|
}
|
2020-09-22 10:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_proximity(path: &[Position]) -> u32 {
|
|
|
|
path.windows(2).map(|w| positions_proximity(w[0], w[1])).sum::<u32>()
|
|
|
|
}
|
2023-12-06 15:49:02 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum ProximityPrecision {
|
|
|
|
#[default]
|
2023-12-14 16:31:00 +01:00
|
|
|
ByWord,
|
|
|
|
ByAttribute,
|
2023-12-06 15:49:02 +01:00
|
|
|
}
|