Skip to content

Commit

Permalink
Merge pull request #120 from iamgamja/main
Browse files Browse the repository at this point in the history
feat: add Nonwhite struct, add Deref for Line & Nonwhite
  • Loading branch information
byeongkeunahn authored Jan 2, 2025
2 parents 39abbc6 + 19093c6 commit aff8a9b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
55 changes: 29 additions & 26 deletions basm-std/src/platform/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ pub trait ReaderTrait: Sized {
fn u128(&mut self) -> u128;
fn usize(&mut self) -> usize;
fn f64(&mut self) -> f64;
fn byte(&mut self) -> u8;
fn word(&mut self) -> String;
fn line(&mut self) -> String;
fn skip_whitespace(&mut self) -> usize;
fn next<T: Readable>(&mut self) -> T {
T::read(self)
}
Expand Down Expand Up @@ -174,23 +176,7 @@ impl<const N: usize> Reader<N> {
}
consumed
}
// We do not use avx2 for this function since most of the time
// we only skip a few whitespaces.
pub fn skip_whitespace(&mut self) -> usize {
let mut len = 0;
'outer: loop {
while self.off < self.len {
if unsafe { self.buf[self.off].assume_init() } > b' ' {
break 'outer len;
}
self.off += 1;
len += 1;
}
if self.try_refill(1) == 0 {
break len;
}
}
}

pub fn skip_until_whitespace(&mut self) -> usize {
let mut len = 0;
'outer: loop {
Expand Down Expand Up @@ -246,15 +232,6 @@ impl<const N: usize> Reader<N> {
}
}

pub fn ascii(&mut self) -> u8 {
self.try_refill(1);
let mut out = 0u8;
if self.off < self.len {
out = unsafe { self.buf[self.off].assume_init() };
self.off += 1;
}
out
}
pub fn word_buf(&mut self, buf: &mut [u8]) -> usize {
self.skip_whitespace();
let mut len = 0;
Expand Down Expand Up @@ -501,6 +478,32 @@ impl<const N: usize> ReaderTrait for Reader<N> {
if let Ok(ans) = out { ans } else { f64::NAN }
}
}
fn byte(&mut self) -> u8 {
self.try_refill(1);
let mut out = 0u8;
if self.off < self.len {
out = unsafe { self.buf[self.off].assume_init() };
self.off += 1;
}
out
}
// We do not use avx2 for this function since most of the time
// we only skip a few whitespaces.
fn skip_whitespace(&mut self) -> usize {
let mut len = 0;
'outer: loop {
while self.off < self.len {
if unsafe { self.buf[self.off].assume_init() } > b' ' {
break 'outer len;
}
self.off += 1;
len += 1;
}
if self.try_refill(1) == 0 {
break len;
}
}
}
}

/*
Expand Down
26 changes: 26 additions & 0 deletions basm-std/src/platform/io/reader_traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Readable, ReaderTrait};
use alloc::string::String;
use core::ops::Deref;

macro_rules! impl_primitive {
($($ty:ident)*) => {
Expand Down Expand Up @@ -30,6 +31,31 @@ impl Readable for Line {
}
}

impl Deref for Line {
type Target = String;

fn deref(&self) -> &Self::Target {
&self.0
}
}

pub struct Nonwhite(pub u8);

impl Readable for Nonwhite {
fn read(reader: &mut impl ReaderTrait) -> Self {
reader.skip_whitespace();
Self(reader.byte())
}
}

impl Deref for Nonwhite {
type Target = u8;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T: Readable, const N: usize> Readable for [T; N] {
fn read(reader: &mut impl ReaderTrait) -> Self {
core::array::from_fn(|_| T::read(reader))
Expand Down
2 changes: 1 addition & 1 deletion tests/boj_14939.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn main() {
for i in 0..n {
for j in 0..n {
reader.skip_whitespace();
let c = reader.ascii();
let c = reader.byte();
if c == b'O' {
board[i] |= 1u32 << j;
}
Expand Down

0 comments on commit aff8a9b

Please sign in to comment.