use crate::alphabet::{
donors, DP_CLASS, DP_DONOR_BASE, DP_PLAIN, DP_RSET_BASE, DP_STOP, IS_REPRESENTABLE,
RANK_ABSENT_ALL, RANK_PACKED,
};
#[cfg(not(feature = "simd"))]
use crate::alphabet::IDENTITY_ASCII;
use crate::constants::{
DP_SIGNAL_BASE, FILL_SIGNAL_BASE, MAX_DP_ANALYSIS_BYTES, MAX_DP_SEGMENT_CHARS, MAX_FILL_BYTES,
MAX_TAIL_ZEROS, MIN_FILL_BYTES, MIN_FILL_IN_SEGMENT_BYTES, MIN_PASSTHROUGH_BYTES,
MIN_TAIL_ZEROS, TAIL_SIGNAL_BASE,
};
use crate::digits::{value_to_5chars_32, value_to_5chars_64};
fn encode_capacity(n: usize) -> usize {
n + n / 4 + 16
}
#[inline]
fn reserve(out: &mut Vec<u8>, w: usize, need: usize) {
debug_assert!(w <= out.len(), "the cursor left the buffer");
if need > out.len() - w {
let want = w + need;
out.resize(want + want / 4, 0);
}
}
const LANE_HI: u64 = 0x8080_8080_8080_8080;
const LANE_ONES: u64 = 0x0101_0101_0101_0101;
#[inline]
fn lane_ge(x: u64, y: u64) -> u64 {
((x | LANE_HI) - y) & LANE_HI
}
#[inline]
fn lane_min(x: u64, y: u64) -> u64 {
let m = (lane_ge(x, y) >> 7) * 0xFF;
(x & !m) | (y & m)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DpPrefix {
pub len: usize,
pub mask: u16,
pub profile: u8,
}
struct DpScan {
mask: u16,
profile: u8,
k: u64,
min_donor: u64,
prev: (u16, u8),
prev_pos: usize,
}
impl DpScan {
fn new() -> Self {
DpScan {
mask: 0,
profile: 0,
k: 0,
min_donor: RANK_ABSENT_ALL,
prev: (0, 0),
prev_pos: usize::MAX,
}
}
#[inline]
fn account(&mut self, cls: u8, b: u8, pos: usize) -> bool {
if cls < DP_DONOR_BASE {
let viable = lane_ge(self.min_donor, (self.k + 1) * LANE_ONES);
if viable == 0 {
return false;
}
self.prev = (self.mask, self.profile);
self.prev_pos = pos;
self.profile = (viable.trailing_zeros() >> 3) as u8;
self.mask |= 1u16 << (cls - DP_RSET_BASE);
self.k += 1;
} else {
let new_min = lane_min(self.min_donor, RANK_PACKED[b as usize]);
if new_min == self.min_donor {
return true;
}
let viable = lane_ge(new_min, self.k * LANE_ONES);
if viable == 0 {
return false;
}
self.prev = (self.mask, self.profile);
self.prev_pos = pos;
self.profile = (viable.trailing_zeros() >> 3) as u8;
self.min_donor = new_min;
}
true
}
}
fn fold_first(st: &mut DpScan, seen: &mut u64, b: u8) -> bool {
let cls = DP_CLASS[b as usize];
let bit = 1u64 << cls;
if *seen & bit != 0 {
return true;
}
if cls == DP_STOP {
return false; }
*seen |= bit;
st.account(cls, b, 0)
}
pub fn scan_dp(window: &[u8]) -> DpPrefix {
let limit = window.len().min(MAX_DP_ANALYSIS_BYTES);
let mut st = DpScan::new();
let mut seen = 1u64 << DP_PLAIN;
#[cfg(feature = "simd")]
let mut skip = crate::simd::SkipSet::new();
let mut i = 0usize;
if limit > 0 && fold_first(&mut st, &mut seen, window[0]) {
#[cfg(feature = "simd")]
skip.account(window[0]);
i = 1;
let mut prev = window[0];
while i < limit {
#[cfg(feature = "simd")]
{
while i + crate::simd::LANES <= limit {
let w: &[u8; crate::simd::LANES + 1] = window[i - 1..i + crate::simd::LANES]
.try_into()
.expect("the loop bound keeps this window inside the scan");
let run = skip.skippable(w);
if run == 0 {
break;
}
i += run;
prev = window[i - 1];
if run < crate::simd::LANES {
break;
}
}
if i >= limit {
break;
}
}
let b = window[i];
if b == prev {
let start = i - 1;
let stop = limit.min(start + MIN_FILL_IN_SEGMENT_BYTES);
let mut e = i + 1;
while e < stop && window[e] == b {
e += 1;
}
if e - start >= MIN_FILL_IN_SEGMENT_BYTES {
let (mask, profile) = if st.prev_pos == start {
st.prev
} else {
(st.mask, st.profile)
};
return DpPrefix { len: start, mask, profile };
}
i = e;
continue;
}
let cls = DP_CLASS[b as usize];
let bit = 1u64 << cls;
if seen & bit == 0 {
if cls == DP_STOP {
break; }
if !st.account(cls, b, i) {
break;
}
seen |= bit;
#[cfg(feature = "simd")]
skip.account(b);
}
prev = b;
i += 1;
}
}
DpPrefix { len: i, mask: st.mask, profile: st.profile }
}
#[inline]
fn fill_run(window: &[u8]) -> usize {
let b = window[0];
let limit = window.len().min(MAX_FILL_BYTES);
let mut i = 1usize;
while i < limit && window[i] == b {
i += 1;
}
i
}
#[inline]
fn lanes_within(word: u64, lo: u64, hi: u64) -> bool {
let below = word.wrapping_sub(LANE_ONES * lo) & !word;
let above = word.wrapping_add(LANE_ONES * (127 - hi)) | word;
(below | above) & LANE_HI == 0
}
#[inline]
fn dp_gate(data: &[u8], q: usize) -> bool {
if let Some(w) = data.get(q..q + 8) {
let word = u64::from_le_bytes(w.try_into().expect("an eight-byte slice"));
if !lanes_within(word, 0x09, 0x7E) {
return false;
}
}
match data.get(q..q + 4) {
Some(g) => dp_table_gate(g),
None => false,
}
}
#[inline]
fn dp_table_gate(w: &[u8]) -> bool {
(IS_REPRESENTABLE[w[0] as usize]
& IS_REPRESENTABLE[w[1] as usize]
& IS_REPRESENTABLE[w[2] as usize]
& IS_REPRESENTABLE[w[3] as usize])
!= 0
}
#[cfg(not(test))]
#[inline(always)]
fn skip_enabled() -> bool {
true
}
#[cfg(test)]
thread_local! {
static SKIP_ENABLED: std::cell::Cell<bool> = const { std::cell::Cell::new(true) };
}
#[cfg(test)]
#[inline]
fn skip_enabled() -> bool {
SKIP_ENABLED.with(|on| on.get())
}
#[cfg(test)]
pub(crate) fn set_skip_enabled(on: bool) -> bool {
SKIP_ENABLED.with(|flag| flag.replace(on))
}
const WINDOW_BYTES: usize = 4 + MIN_PASSTHROUGH_BYTES;
#[inline]
fn window_may_hold(win: &[u8; WINDOW_BYTES]) -> (bool, bool) {
let fill = (win[2] == 0) | (win[6] == 0) | (win[0] == win[1]) | (win[4] == win[5]);
let word = u64::from_le_bytes(win[4..12].try_into().expect("an eight-byte slice"));
(lanes_within(word, 0x09, 0x7E), fill)
}
#[inline]
fn decision_at(data: &[u8], q: usize, dp_maybe: bool) -> bool {
let w: &[u8; MIN_PASSTHROUGH_BYTES] = data[q..q + MIN_PASSTHROUGH_BYTES]
.try_into()
.expect("the caller keeps this window inside the input");
if w[2] == 0 {
if w[..MIN_TAIL_ZEROS].iter().all(|&b| b == 0) {
return true;
}
if w[2..2 + MIN_TAIL_ZEROS].iter().all(|&b| b == 0) {
return true;
}
}
if w[1] == w[0] {
let mut e = 2;
while e < MIN_FILL_BYTES && w[e] == w[0] {
e += 1;
}
if e >= MIN_FILL_BYTES {
return true;
}
}
if dp_maybe && dp_table_gate(w) {
let mut e = 4;
while e < MIN_PASSTHROUGH_BYTES && IS_REPRESENTABLE[w[e] as usize] != 0 {
e += 1;
}
if e >= MIN_PASSTHROUGH_BYTES {
return true;
}
}
false
}
fn next_decision_point(data: &[u8], from: usize, limit: usize) -> usize {
let n = data.len();
let mut q = from;
let fast_end = limit.min(n.saturating_sub(MIN_PASSTHROUGH_BYTES));
#[cfg(feature = "simd")]
while q + crate::simd::SKIP_BYTES <= fast_end {
let w: &[u8; crate::simd::SKIP_WINDOW] = data[q..q + crate::simd::SKIP_WINDOW]
.try_into()
.expect("the loop bound keeps this window inside the input");
if !crate::simd::groups_may_hold(w) {
q += crate::simd::SKIP_BYTES;
continue;
}
let settled = q + crate::simd::SKIP_BYTES;
while q < settled {
if decision_at(data, q, true) {
return q;
}
q += 4;
}
}
while q + 4 <= fast_end {
let win: &[u8; WINDOW_BYTES] = data[q..q + WINDOW_BYTES]
.try_into()
.expect("the loop bound keeps this window inside the input");
let (dp, fill) = window_may_hold(win);
if !(dp | fill) {
q += 8;
continue;
}
if decision_at(data, q, dp) {
return q;
}
if decision_at(data, q + 4, dp) {
return q + 4;
}
q += 8;
}
while q < fast_end {
if decision_at(data, q, true) {
return q;
}
q += 4;
}
while q < limit {
if q + 2 < n && data[q + 2] == 0 {
let zeros_at = |s: usize| {
let limit = n.min(s + MIN_TAIL_ZEROS);
let mut e = s;
while e < limit && data[e] == 0 {
e += 1;
}
e - s >= MIN_TAIL_ZEROS
};
if zeros_at(q) && q + MIN_TAIL_ZEROS + 2 <= n {
return q;
}
if zeros_at(q + 2) {
return q;
}
}
if q + 1 < n && data[q + 1] == data[q] {
let limit = n.min(q + MIN_FILL_BYTES);
let mut e = q + 1;
while e < limit && data[e] == data[q] {
e += 1;
}
if e - q >= MIN_FILL_BYTES {
return q;
}
}
if IS_REPRESENTABLE[data[q] as usize] != 0 {
let limit = n.min(q + MIN_PASSTHROUGH_BYTES);
let mut e = q;
while e < limit && IS_REPRESENTABLE[data[e] as usize] != 0 {
e += 1;
}
if e - q >= MIN_PASSTHROUGH_BYTES {
return q;
}
}
q += 4;
}
limit
}
fn fill_signal(byte: u8, len: usize) -> [u8; 5] {
debug_assert!((MIN_FILL_BYTES..=MAX_FILL_BYTES).contains(&len));
let payload = ((byte as u64) << 11) | (len as u64 - 1);
value_to_5chars_64(FILL_SIGNAL_BASE + payload)
}
fn tail_signal(zeros: usize, order: u8, lit: [u8; 2]) -> [u8; 5] {
debug_assert!((MIN_TAIL_ZEROS..=MAX_TAIL_ZEROS).contains(&zeros));
let payload = ((order as u64) << 21)
| ((zeros as u64 - 1) << 16)
| ((lit[0] as u64) << 8)
| (lit[1] as u64);
value_to_5chars_64(TAIL_SIGNAL_BASE + payload)
}
struct FillChoice {
signal: [u8; 5],
covers: usize,
}
fn choose_fill(window: &[u8], run: usize) -> Option<FillChoice> {
let mut covers = if run >= MIN_FILL_BYTES { run } else { 0 };
let mut choice = None;
if window[0] == 0 {
let zeros = run.min(MAX_TAIL_ZEROS);
if zeros >= MIN_TAIL_ZEROS && zeros + 2 <= window.len() && zeros + 2 > covers {
covers = zeros + 2;
choice = Some(tail_signal(zeros, 0, [window[zeros], window[zeros + 1]]));
}
}
if window.len() >= 3 && window[2] == 0 {
let zeros = zero_run(&window[2..]);
if zeros >= MIN_TAIL_ZEROS && zeros + 2 > covers {
covers = zeros + 2;
choice = Some(tail_signal(zeros, 1, [window[0], window[1]]));
}
}
if covers == 0 {
return None;
}
Some(FillChoice {
signal: choice.unwrap_or_else(|| fill_signal(window[0], run)),
covers,
})
}
#[inline]
fn zero_run(window: &[u8]) -> usize {
let limit = window.len().min(MAX_TAIL_ZEROS);
let mut i = 0usize;
while i < limit && window[i] == 0 {
i += 1;
}
i
}
fn dp_signal(prefix: &DpPrefix) -> [u8; 5] {
debug_assert!((1..=MAX_DP_SEGMENT_CHARS).contains(&prefix.len));
let payload = ((prefix.profile as u64) << 24)
| ((prefix.mask as u64) << 11)
| (prefix.len as u64 - 1);
value_to_5chars_64(DP_SIGNAL_BASE + payload)
}
pub struct Part {
pub out: String,
pub points: Vec<(usize, usize)>,
pub end: usize,
}
pub fn encode(data: &[u8]) -> String {
encode_range(data, 0, data.len(), None, false).out
}
pub fn encode_range(
data: &[u8],
start: usize,
stop: usize,
meet: Option<&[(usize, usize)]>,
record: bool,
) -> Part {
let mut out: Vec<u8> = vec![0u8; encode_capacity(stop - start) + 16];
let mut w = 0usize;
let mut pos = start;
let mut points: Vec<(usize, usize)> = Vec::new();
#[cfg(not(feature = "simd"))]
let mut xlat = IDENTITY_ASCII;
#[cfg(feature = "simd")]
let (mut xlat_pairs, mut xlat_k) = ([(0u8, 0u8); crate::alphabet::RSET_LEN], 0usize);
let mut xlat_key = u32::MAX;
let mut block_start = usize::MAX;
while pos < data.len() {
if block_start == usize::MAX {
if pos >= stop {
break;
}
if let Some(list) = meet {
if pos > start && list.binary_search_by_key(&pos, |&(p, _)| p).is_ok() {
break;
}
}
if record {
points.push((pos, w));
}
}
let run = fill_run(&data[pos..]);
if let Some(fill) = choose_fill(&data[pos..], run) {
if block_start != usize::MAX {
w += flush_block(data, block_start, pos, &mut out, w);
block_start = usize::MAX;
}
reserve(&mut out, w, 5);
out[w..w + 5].copy_from_slice(&fill.signal);
w += 5;
pos += fill.covers;
continue;
}
let prefix = if data.len() - pos >= MIN_PASSTHROUGH_BYTES && dp_gate(data, pos) {
scan_dp(&data[pos..])
} else {
DpPrefix { len: 0, mask: 0, profile: 0 }
};
if prefix.len >= MIN_PASSTHROUGH_BYTES {
debug_assert!(5 + prefix.len <= prefix.len.div_ceil(4) * 5);
if block_start != usize::MAX {
w += flush_block(data, block_start, pos, &mut out, w);
block_start = usize::MAX;
}
reserve(&mut out, w, 5 + prefix.len);
out[w..w + 5].copy_from_slice(&dp_signal(&prefix));
w += 5;
let key = ((prefix.profile as u32) << 16) | prefix.mask as u32;
if xlat_key != key {
let (pairs, k) = donors(prefix.profile as usize, prefix.mask);
#[cfg(not(feature = "simd"))]
{
xlat = IDENTITY_ASCII;
for &(rset, donor) in &pairs[..k] {
xlat[rset as usize] = donor;
}
}
#[cfg(feature = "simd")]
{
xlat_pairs = pairs;
xlat_k = k;
}
xlat_key = key;
}
let src = &data[pos..pos + prefix.len];
let dst = &mut out[w..w + prefix.len];
#[cfg(feature = "simd")]
crate::simd::translate(&xlat_pairs[..xlat_k], src, dst);
#[cfg(not(feature = "simd"))]
for (o, &b) in dst.iter_mut().zip(src.iter()) {
debug_assert!(b < 128, "the scan accepts only ASCII bytes");
*o = xlat[(b & 0x7f) as usize];
}
w += prefix.len;
pos += prefix.len;
continue;
}
if block_start == usize::MAX {
block_start = pos;
}
pos += (data.len() - pos).min(4);
if skip_enabled() && pos < data.len() && IS_REPRESENTABLE[data[pos] as usize] == 0 {
let next = next_decision_point(data, pos, data.len().min(stop.max(pos)));
pos += ((next - pos) / 4) * 4;
}
if pos >= stop {
break;
}
}
if block_start != usize::MAX {
debug_assert!(
pos >= data.len() || (pos - block_start).is_multiple_of(4),
"a worker stopping at {pos} left {} bytes of a block-mode group pending",
(pos - block_start) % 4
);
w += flush_block(data, block_start, pos, &mut out, w);
}
debug_assert!(points.windows(2).all(|p| p[0].0 < p[1].0), "splice points out of order");
out.truncate(w);
debug_assert!(out.is_ascii());
Part {
out: String::from_utf8(out).expect("encoder emits only Alphabet-N characters"),
points,
end: pos,
}
}
pub const MIN_PARALLEL_CHUNK: usize = 1 << 20;
pub fn encode_parallel(data: &[u8], threads: usize) -> String {
let threads = threads.max(1);
if threads == 1 || data.len() < 2 * MIN_PARALLEL_CHUNK {
return encode(data);
}
parallel_with_chunk(data, (data.len() / threads).max(MIN_PARALLEL_CHUNK))
}
#[cfg(feature = "fuzzing")]
#[doc(hidden)]
pub fn __fuzz_encode_parallel_chunked(data: &[u8], chunk: usize) -> String {
parallel_with_chunk(data, chunk.max(1))
}
fn parallel_with_chunk(data: &[u8], chunk: usize) -> String {
let starts: Vec<usize> = (0..).map(|i| i * chunk).take_while(|&s| s < data.len()).collect();
let parts: Vec<Part> = std::thread::scope(|scope| {
let handles: Vec<_> = starts
.iter()
.enumerate()
.map(|(i, &begin)| {
let end = starts.get(i + 1).copied().unwrap_or(data.len());
scope.spawn(move || encode_range(data, begin, end, None, i > 0))
})
.collect();
handles.into_iter().map(|h| h.join().expect("worker panicked")).collect()
});
let mut out = String::with_capacity(encode_capacity(data.len()));
out.push_str(&parts[0].out);
let mut cursor = parts[0].end;
for part in &parts[1..] {
if cursor >= part.end {
continue;
}
if let Ok(i) = part.points.binary_search_by_key(&cursor, |&(p, _)| p) {
out.push_str(&part.out[part.points[i].1..]);
cursor = part.end;
continue;
}
let repair = encode_range(data, cursor, part.end, Some(&part.points), false);
out.push_str(&repair.out);
cursor = repair.end;
if let Ok(i) = part.points.binary_search_by_key(&cursor, |&(p, _)| p) {
out.push_str(&part.out[part.points[i].1..]);
cursor = part.end;
}
}
if cursor < data.len() {
out.push_str(&encode_range(data, cursor, data.len(), None, false).out);
}
out
}
fn flush_block(data: &[u8], start: usize, end: usize, out: &mut Vec<u8>, w: usize) -> usize {
let n = end - start;
let need = n / 4 * 5 + if n.is_multiple_of(4) { 0 } else { n % 4 + 1 };
reserve(out, w, need);
process_with_block_mode(&data[start..end], &mut out[w..])
}
fn process_with_block_mode(buf: &[u8], dst: &mut [u8]) -> usize {
let full_len = buf.len() - buf.len() % 4;
let groups = full_len / 4;
let quads = full_len / 16;
let (qin, _) = buf[..quads * 16].as_chunks::<16>();
let (qout, _) = dst[..quads * 20].as_chunks_mut::<20>();
for (src, chars) in qin.iter().zip(qout) {
let (groups4, _) = src.as_chunks::<4>();
let a = value_to_5chars_32(u32::from_be_bytes(groups4[0]));
let b = value_to_5chars_32(u32::from_be_bytes(groups4[1]));
let c = value_to_5chars_32(u32::from_be_bytes(groups4[2]));
let d = value_to_5chars_32(u32::from_be_bytes(groups4[3]));
chars[0..5].copy_from_slice(&a);
chars[5..10].copy_from_slice(&b);
chars[10..15].copy_from_slice(&c);
chars[15..20].copy_from_slice(&d);
}
let (ins, _) = buf[quads * 16..full_len].as_chunks::<4>();
let (outs, _) = dst[quads * 20..groups * 5].as_chunks_mut::<5>();
for (src, chars) in ins.iter().zip(outs) {
*chars = value_to_5chars_32(u32::from_be_bytes(*src));
}
let rem = buf.len() - full_len;
if rem > 0 {
let mut padded = [0u8; 4];
padded[..rem].copy_from_slice(&buf[full_len..]);
let group = value_to_5chars_32(u32::from_be_bytes(padded));
dst[groups * 5..groups * 5 + rem + 1].copy_from_slice(&group[..rem + 1]);
groups * 5 + rem + 1
} else {
groups * 5
}
}
#[cfg(test)]
mod lane_tests {
use super::*;
use crate::alphabet::{NOT_REPRESENTABLE, NUM_PROFILES, PROFILES, RSET_ASCII, RSET_LEN};
fn words(seed: u64) -> impl Iterator<Item = u64> {
let mut state = seed;
std::iter::repeat_with(move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
})
}
#[test]
fn lane_operations_agree_over_random_words() {
let masked = |w: u64| w & 0x7f7f_7f7f_7f7f_7f7f;
for (x, y) in words(0x9E37_79B9_7F4A_7C15).zip(words(0xD1B5_4A32_D192_ED03)).take(crate::tests::cases(100_000)) {
let (x, y) = (masked(x), masked(y));
let min = lane_min(x, y);
let ge = lane_ge(x, y);
for p in 0..8 {
let xp = (x >> (8 * p)) & 0xff;
let yp = (y >> (8 * p)) & 0xff;
assert_eq!((min >> (8 * p)) & 0xff, xp.min(yp), "lane {p} of {x:#018x}/{y:#018x}");
assert_eq!(
(ge >> (8 * p)) & 0x80 != 0,
xp >= yp,
"lane {p} of {x:#018x}/{y:#018x}"
);
}
}
}
#[test]
fn lanes_within_agrees_with_the_bytes_it_stands_for() {
let scalar = |w: u64, lo: u64, hi: u64| (0..8).all(|p| (lo..=hi).contains(&((w >> (8 * p)) & 0xff)));
let bounds = [(0x09u64, 0x7Eu64), (0x00, 0x7f), (0x20, 0x40)];
for &(lo, hi) in &bounds {
for background in [0x00u8, 0x09, 0x41, 0x7E, 0x7F, 0x80, 0xFF] {
for lane in 0..8 {
for value in 0..=255u8 {
let mut bytes = [background; 8];
bytes[lane] = value;
let w = u64::from_le_bytes(bytes);
assert_eq!(
lanes_within(w, lo, hi),
scalar(w, lo, hi),
"lane {lane} = {value:#04x}, background {background:#04x}, \
range {lo:#04x}..={hi:#04x}"
);
}
}
}
for w in words(0x2545_F491_4F6C_DD1D).take(crate::tests::cases(200_000)) {
assert_eq!(lanes_within(w, lo, hi), scalar(w, lo, hi), "{w:#018x}");
let inside = w & 0x3f3f_3f3f_3f3f_3f3f | 0x2020_2020_2020_2020;
if scalar(inside, lo, hi) {
assert!(lanes_within(inside, lo, hi), "{inside:#018x} was rejected");
}
}
}
}
#[test]
fn lane_operations_agree_with_per_lane_arithmetic() {
let cases = [0u64, 1, 12, 13, 127, RANK_ABSENT_ALL, 0x000d_0700_0102_0c0d];
for &x in &cases {
for &y in &cases {
let min = lane_min(x, y);
let ge = lane_ge(x, y);
for p in 0..8 {
let xp = (x >> (8 * p)) & 0xff;
let yp = (y >> (8 * p)) & 0xff;
assert!(xp < 128 && yp < 128, "test inputs stay in range");
assert_eq!((min >> (8 * p)) & 0xff, xp.min(yp));
assert_eq!((ge >> (8 * p)) & 0x80 != 0, xp >= yp);
}
}
}
}
#[test]
fn dp_classes_agree_with_the_tables_they_replace() {
let mut donor_slots = 0u8;
for b in 0..=255u8 {
let cls = DP_CLASS[b as usize];
let representable = IS_REPRESENTABLE[b as usize] != 0;
assert_eq!(
cls == DP_STOP,
!representable,
"{b:#04x}: DP_STOP must be exactly the non-representable bytes"
);
if cls == DP_STOP {
assert_eq!(RANK_PACKED[b as usize], NOT_REPRESENTABLE);
continue;
}
assert!(cls < 63, "{b:#04x}: every class but DP_STOP is a real bit");
if (DP_RSET_BASE..DP_DONOR_BASE).contains(&cls) {
assert_eq!(RSET_ASCII[(cls - DP_RSET_BASE) as usize], b);
} else if cls == DP_PLAIN {
assert_eq!(RANK_PACKED[b as usize], RANK_ABSENT_ALL);
assert!(PROFILES.iter().all(|p| !p.contains(&b)));
} else {
donor_slots += 1;
assert!(PROFILES.iter().any(|p| p.contains(&b)));
assert_ne!(RANK_PACKED[b as usize], RANK_ABSENT_ALL);
}
}
assert_eq!(donor_slots as usize + RSET_LEN, 35);
}
#[test]
fn packed_ranks_match_the_profile_table() {
for (p, profile) in PROFILES.iter().enumerate() {
for (rank, &c) in profile.iter().enumerate() {
let lane = (RANK_PACKED[c as usize] >> (8 * p)) & 0xff;
assert_eq!(lane, rank as u64, "{} in profile {}", c as char, p);
}
}
assert_eq!(RANK_PACKED[b'a' as usize], RANK_ABSENT_ALL);
assert_eq!(RANK_PACKED[0], NOT_REPRESENTABLE);
}
#[test]
fn profiles_are_thirteen_distinct_alphabet_characters() {
for (p, profile) in PROFILES.iter().enumerate() {
assert_eq!(profile.len(), RSET_LEN);
for (i, &c) in profile.iter().enumerate() {
assert!(
crate::alphabet::ALPHABET_VALUE[c as usize] >= 0,
"profile {} rank {} is not in Alphabet-N",
p,
i
);
assert!(
!profile[..i].contains(&c),
"profile {} repeats {}",
p,
c as char
);
}
}
assert_eq!(PROFILES.len(), NUM_PROFILES);
}
}