Skip to content

Commit

Permalink
with stdin, always supply a vec with no capacity
Browse files Browse the repository at this point in the history
Signed-off-by: Lance-Drane <[email protected]>
  • Loading branch information
Lance-Drane committed Jul 13, 2024
1 parent 094da7d commit e3c7a82
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
11 changes: 3 additions & 8 deletions src/bin/0_cses_template_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ pub mod itoap {
}
}

/// Size of the output buffer.
/// Size of the output buffer. Larger capacities don't seem to help, note that this is OS dependent.
const BUF_SIZE: usize = 32_768;

/// Custom buffer around a writer
Expand Down Expand Up @@ -913,8 +913,6 @@ impl_float!((f32, i32));

// problem //

const MAX_INPUT_BYTES: usize = 18; // number can be up to 8 bytes (-1000000), two numbers, two whitespace chars

/// Given two numbers A and B, calculate their sum A+B.
///
/// <b>Input</b>
Expand Down Expand Up @@ -952,12 +950,9 @@ fn solve<W: std::io::Write>(scan: &[u8], out: &mut W) {
// entrypoints //

fn main() {
// you may want to allocate full capacity here (determine maximum buffer size from input constraints), but partial allocations seem to be slower
// so if you don't want to compute the vec size, just use a Vector without initialized capacity
let mut buf_str = Vec::with_capacity(MAX_INPUT_BYTES);
// use the APIs which work off of Vec<u8>
// When reading from STDIN, it's fastest to use a vec with no capacity.
let mut buf_str = vec![];
std::io::stdin().lock().read_to_end(&mut buf_str).unwrap();
// larger capacities don't seem to help, note that this is somewhat OS dependent.
let mut out = std::io::stdout().lock();
solve(&buf_str, &mut out);
}
Expand Down
4 changes: 1 addition & 3 deletions src/bin/mathematics_josephus_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,6 @@ impl_int!(for u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

// problem //

const MAX_INPUT_BYTES: usize = 2_200_007; // 7 max bytes for first line, 22 max bytes for each query line

/// Consider a game where there are n children (numbered 1,2,...,n) in a circle. During the game, every other child is removed from the circle until there are no children left.
///
/// Your task is to process q queries of the form: "when there are n children, who is the kth child that will be removed?"
Expand Down Expand Up @@ -710,7 +708,7 @@ fn solve<W: std::io::Write>(scan: &[u8], out: &mut W) {
// entrypoints //

fn main() {
let mut buf_str = Vec::with_capacity(MAX_INPUT_BYTES);
let mut buf_str = vec![];
std::io::stdin().lock().read_to_end(&mut buf_str).unwrap();
let mut out = std::io::stdout().lock();
solve(&buf_str, &mut out);
Expand Down

0 comments on commit e3c7a82

Please sign in to comment.