Remove limit of 1000 position per attribute

Instead of using an arbitrary limit we encode the absolute position in a u32
using one strong u16 for the field id and a weak u16 for the relative position in the attribute.
This commit is contained in:
many 2021-09-22 17:48:24 +02:00
parent 8f6b6c9042
commit 360c5ff3df
No known key found for this signature in database
GPG key ID: 2CEF23B75189EACA
6 changed files with 91 additions and 24 deletions

View file

@ -1,8 +1,7 @@
use std::cmp;
use crate::{Attribute, Position};
use crate::{relative_from_absolute_position, Position};
pub const ONE_ATTRIBUTE: u32 = 1000;
pub const MAX_DISTANCE: u32 = 8;
pub fn index_proximity(lhs: u32, rhs: u32) -> u32 {
@ -14,19 +13,15 @@ pub fn index_proximity(lhs: u32, rhs: u32) -> u32 {
}
pub fn positions_proximity(lhs: Position, rhs: Position) -> u32 {
let (lhs_attr, lhs_index) = extract_position(lhs);
let (rhs_attr, rhs_index) = extract_position(rhs);
let (lhs_attr, lhs_index) = relative_from_absolute_position(lhs);
let (rhs_attr, rhs_index) = relative_from_absolute_position(rhs);
if lhs_attr != rhs_attr {
MAX_DISTANCE
} else {
index_proximity(lhs_index, rhs_index)
index_proximity(lhs_index as u32, rhs_index as u32)
}
}
pub fn extract_position(position: Position) -> (Attribute, Position) {
(position / ONE_ATTRIBUTE, position % ONE_ATTRIBUTE)
}
pub fn path_proximity(path: &[Position]) -> u32 {
path.windows(2).map(|w| positions_proximity(w[0], w[1])).sum::<u32>()
}