Replace indirect indexing

230 byte overhead is worth it to avoid conversions and potential
indexing errors
This commit is contained in:
2022-12-06 18:23:43 +01:00
parent 391bba24c5
commit 7c7c69255d

View File

@@ -1,19 +1,15 @@
use anyhow::Result;
fn find_first(input: &[u8], unique: usize) -> Result<usize> {
#[inline]
const fn index(c: u8) -> usize {
(c - b'a') as usize
}
let mut seen = [false; 26];
let mut seen = [false; 256];
let mut first = 0;
// Loop invariant: input[first..last] contains only unique characters
for (last, &c) in input.iter().enumerate() {
if seen[index(c)] {
if seen[c as usize] {
while input[first] != c {
seen[index(input[first])] = false;
seen[input[first] as usize] = false;
first += 1;
}
first += 1;
@@ -23,7 +19,7 @@ fn find_first(input: &[u8], unique: usize) -> Result<usize> {
return Ok(first + unique);
}
seen[index(c)] = true;
seen[c as usize] = true;
}
}