Skip to content

Commit

Permalink
go back to using SplitDigitIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
mztikk committed Nov 4, 2024
1 parent f2d5bf1 commit 3c19e91
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ license = "MIT"
description = "library to parse and interpret poetic source code"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
lto = true

[dependencies]
rand = "0.8.5"
getrandom = { version = "0.2.15", features = ["js"], optional = true }
split-digits = "0.2.2"

[features]
wasm = ["dep:getrandom"]
Expand All @@ -22,3 +25,4 @@ criterion = "0.5.1"
[[bench]]
name = "benchmarks"
harness = false
lto = true
21 changes: 2 additions & 19 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use split_digits::SplitDigitIterator;
use std::collections::VecDeque;

fn split_digits_vec_deque(d: usize) -> Vec<u8> {
Expand Down Expand Up @@ -36,26 +37,8 @@ fn split_digits_vec_push_reverse(d: usize) -> Vec<u8> {
digits
}

fn split_digits(mut num: usize) -> impl Iterator<Item = usize> {
let mut divisor = 1;
while num >= divisor * 10 {
divisor *= 10;
}

std::iter::from_fn(move || {
if divisor == 0 {
None
} else {
let v = num / divisor;
num %= divisor;
divisor /= 10;
Some(v)
}
})
}

fn split_digits_iterator(d: usize) -> Vec<u8> {
split_digits(d).map(|x| x as u8).collect::<Vec<u8>>()
SplitDigitIterator::new(d).collect()
}

fn benchmark_split_digits(c: &mut Criterion) {
Expand Down
27 changes: 5 additions & 22 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::instruction::Instruction;
use split_digits::SplitDigitIterator;
use std::{cmp::Ordering, fmt::Display};

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -39,22 +40,8 @@ impl Display for ParseError {
pub struct Parser {}

impl Parser {
fn split_digits(mut num: usize) -> impl Iterator<Item = usize> {
let mut divisor = 1;
while num >= divisor * 10 {
divisor *= 10;
}

std::iter::from_fn(move || {
if divisor == 0 {
None
} else {
let v = num / divisor;
num %= divisor;
divisor /= 10;
Some(v)
}
})
fn split_digits(d: usize) -> Vec<u8> {
SplitDigitIterator::new(d).collect()
}

/// Any character that is not an alphabetic character or apostrophe is ignored, and treated as whitespace
Expand All @@ -78,11 +65,7 @@ impl Parser {
.for_each(|d| match d.cmp(&10) {
Ordering::Less => result.push(d as u8),
Ordering::Equal => result.push(0),
Ordering::Greater => result.append(
&mut Parser::split_digits(d)
.map(|x| x as u8)
.collect::<Vec<u8>>(),
),
Ordering::Greater => result.append(&mut Parser::split_digits(d)),
});

result
Expand Down Expand Up @@ -426,7 +409,7 @@ mod test {

#[test]
fn test_parser_split_digits() {
let digits = Parser::split_digits(568764567).collect::<Vec<usize>>();
let digits = Parser::split_digits(568764567);
assert_eq!(digits, vec![5, 6, 8, 7, 6, 4, 5, 6, 7]);
}

Expand Down

0 comments on commit 3c19e91

Please sign in to comment.