mod ordering;
mod signature;
pub(crate) use signature::{Signature, set_digit};
use core::fmt::{self, Write};
use crate::MAX_LENGTH;
pub(crate) const CAPACITY: usize = MAX_LENGTH + 1;
#[derive(Clone, Copy)]
pub struct Position {
digits: [u16; CAPACITY],
length: u8,
measured: u8,
signature: Signature,
}
impl Position {
pub(crate) fn new(
signature: Signature,
n: impl Iterator<Item = u16>,
z: impl Iterator<Item = u16>,
) -> Self {
let mut digits = [0u16; CAPACITY];
let mut length = 0usize;
for (slot, digit) in digits.iter_mut().zip(n.chain(z)) {
*slot = digit;
length += 1;
}
Self {
digits,
length: length as u8,
measured: length as u8,
signature,
}
}
pub(crate) fn measured_as(mut self, length: usize) -> Self {
self.measured = length.min(u8::MAX as usize) as u8;
self
}
pub(crate) fn measured(&self) -> usize {
self.measured as usize
}
pub(crate) fn truncated(mut self, length: usize) -> Self {
self.length = self.length.min(length as u8);
self
}
pub(crate) fn signature(&self) -> &Signature {
&self.signature
}
pub fn n(&self) -> &[u16] {
&self.digits[..self.signature.ln as usize]
}
pub fn z(&self) -> &[u16] {
&self.digits[self.signature.ln as usize..self.length as usize]
}
pub fn digits(&self) -> &[u16] {
&self.digits[..self.length as usize]
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.length as usize
}
pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
self.digits()
.iter()
.map(|digit| self.signature.character(*digit))
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for ch in self.chars() {
f.write_char(ch)?;
}
Ok(())
}
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Position(\"{self}\")")
}
}