Skip to content

Commit

Permalink
Merge pull request #6 from byeongkeunahn/rewrite-io
Browse files Browse the repository at this point in the history
Rewrite io + Enable CI
  • Loading branch information
byeongkeunahn authored Oct 1, 2023
2 parents 6e81efd + 22b994e commit 2c7d505
Show file tree
Hide file tree
Showing 46 changed files with 1,477 additions and 803 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Check

on:
- push
- pull_request

env:
CARGO_NET_RETRY: 10
CI: 1
RUSTUP_MAX_RETRIES: 10

jobs:
build:
name: CI (Ubuntu)
runs-on: ubuntu-latest
strategy:
matrix:
target:
- x86_64-unknown-linux-gnu
- i686-unknown-linux-gnu
env:
CARGO_BUILD_TARGET: ${{ matrix.target }}
steps:
- uses: actions/checkout@v3
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
target: ${{ matrix.target }}
profile: minimal
override: true
components: clippy
- name: Install System packages
run: |
sudo apt update
sudo apt install gcc-multilib nasm
- name: Clippy
run: cargo clippy
env:
RUSTFLAGS: "-D warnings -A clippy::missing_safety_doc"
- name: Test
run: cargo test --lib -- --test-threads 1
env:
RUSTFLAGS: ""
- name: Check C (x86_64)
if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release.sh C 64 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
- name: Check C (x86)
if: ${{ matrix.target == 'i686-unknown-linux-gnu' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-32bit.sh C 32 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
- name: Check Rust (x86_64)
if: ${{ matrix.target == 'x86_64-unknown-linux-gnu' }}
run: |
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./src/solution.rs ./tests/boj_1000.in ./tests/boj_1000.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_1001.rs ./tests/boj_1001.in ./tests/boj_1001.out
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_2751.rs ./tests/boj_2751.in.zip ./tests/boj_2751.out.zip
python ./scripts/build-and-judge.py ${{ runner.temp }} ./release-rs.sh Rust 64 ./tests/boj_3745.rs ./tests/boj_3745.in ./tests/boj_3745.out
58 changes: 58 additions & 0 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Check

on:
- push
- pull_request

env:
CARGO_NET_RETRY: 10
CI: 1
RUSTUP_MAX_RETRIES: 10

jobs:
build:
name: CI (Windows)
runs-on: windows-latest
strategy:
matrix:
target:
- x86_64-pc-windows-msvc
env:
CARGO_BUILD_TARGET: ${{ matrix.target }}
steps:
- uses: actions/checkout@v3
- uses: ilammy/msvc-dev-cmd@v1
with:
arch: amd64
vsversion: 2022
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
target: ${{ matrix.target }}
profile: minimal
override: true
components: clippy
- name: Install System packages
run: |
python -m pip install pefile
- name: Clippy
run: cargo clippy
env:
RUSTFLAGS: "-D warnings -A clippy::missing_safety_doc"
- name: Test
run: cargo test --lib -- --test-threads 1
env:
RUSTFLAGS: ""
- name: Check C (x86_64)
run: |
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\src\solution.rs .\tests\boj_1000.in .\tests\boj_1000.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_1001.rs .\tests\boj_1001.in .\tests\boj_1001.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_2751.rs .\tests\boj_2751.in.zip .\tests\boj_2751.out.zip
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows.cmd C 64 .\tests\boj_3745.rs .\tests\boj_3745.in .\tests\boj_3745.out
- name: Check Rust (x86_64)
run: |
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\src\solution.rs .\tests\boj_1000.in .\tests\boj_1000.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_1001.rs .\tests\boj_1001.in .\tests\boj_1001.out
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_2751.rs .\tests\boj_2751.in.zip .\tests\boj_2751.out.zip
python .\scripts\build-and-judge.py ${{ runner.temp }} .\release-64bit-windows-rs.cmd Rust 64 .\tests\boj_3745.rs .\tests\boj_3745.in .\tests\boj_3745.out
45 changes: 0 additions & 45 deletions .github/workflows/build.yml

This file was deleted.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ path = "src/bin/basm-submit.rs"

[dependencies]
compiler_builtins = { version = "0.1.101", features = ["mem"] }
dtoa = "1.0.9"
libm = "0.2.7"
ryu = "1.0"

[profile.dev]
panic = "abort"
Expand All @@ -38,6 +38,3 @@ lto = true
overflow-checks = false
codegen-units = 1
strip = true

[profile.submit]
inherits = "release"
80 changes: 58 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ fn main() {
```rust
// src/solution.rs
use basm::io::{Reader, Writer};
use basm::platform::io::{Reader, Writer};
pub fn main() {
let mut reader: Reader = Default::default();
let mut writer: Writer = Default::default();
let a = reader.next_usize();
let b = reader.next_usize();
writer.write_usize(a + b);
let a = reader.usize();
let b = reader.usize();
writer.usize(a + b);
}
```

Expand All @@ -45,13 +45,13 @@ pub fn main() {
- 위의 예시 코드는 기본 설정에 따라 입출력 버퍼를 크게 할당합니다. 대부분의 상황에서는 기본 설정이 적절하지만, 156KB 메모리 사용량을 달성하려면 버퍼 크기를 줄여야 합니다. 다음 코드에서는 입출력 버퍼를 각각 128바이트로 설정하여 메모리 사용량을 줄입니다.
```rust
// src/solution.rs
use basm::io::{Reader, Writer};
use basm::platform::io::{Reader, Writer};
pub fn main() {
let mut reader = Reader::<128>::new();
let mut writer = Writer::<128>::new();
let a = reader.next_usize();
let b = reader.next_usize();
writer.write_usize(a + b);
let a = reader.usize();
let b = reader.usize();
writer.usize(a + b);
}
```

Expand Down Expand Up @@ -186,20 +186,19 @@ dashu = { git = "https://github.com/cmpute/dashu.git", rev = "22f3935", default-
src/solution.rs를 다음과 같이 수정합니다.

```rust
use basm::io::{Reader, Writer};
use crate::alloc::string::ToString;
use basm::platform::io::{Reader, Writer};
use alloc::string::ToString;
use core::str::FromStr;
use dashu::Integer as Int;
use dashu::Integer;

#[inline(always)]
pub fn main() {
let mut reader: Reader = Default::default();
let mut writer: Writer = Default::default();
let a = Int::from_str(&reader.next_string()).unwrap();
let b = Int::from_str(&reader.next_string()).unwrap();
let a = Integer::from_str(&reader.word()).unwrap();
let b = Integer::from_str(&reader.word()).unwrap();
let ans = &a + &b;
writer.write(ans.to_string().as_bytes());
writer.write(b"\n");
writer.str(&ans.to_string());
writer.byte(b'\n');
}
```

Expand Down Expand Up @@ -266,7 +265,7 @@ src/solution.rs를 다음과 같이 수정합니다.


```rust
use basm::io::{Reader, Writer};
use basm::platform::io::{Reader, Writer};
use alloc::string::ToString;
use core::str::FromStr;
use dashu::Integer;
Expand Down Expand Up @@ -314,17 +313,54 @@ fn expr(input: &str) -> IResult<&str, Integer> {
)(input)
}

#[inline(always)]
pub fn main() {
let mut reader: Reader = Default::default();
let mut writer: Writer = Default::default();
let input = reader.next_string();
let input = reader.word();
if let Ok((_, ans)) = all_consuming(expr)(&input) {
writer.write(ans.to_string().as_bytes());
writer.str(&ans.to_string());
} else {
writer.write(b"ROCK");
writer.str("ROCK");
}
writer.byte(b'\n');
}
```

이후 실행 과정은 위의 "큰 수 A+B"와 동일하게 진행하면 됩니다.

## 예제: 오름세([BOJ 3745](https://www.acmicpc.net/problem/3745))

이 예제는 하나의 입력 파일에 여러 개의 테스트 케이스가 있지만 개수가 따로 주어지지 않을 때 파일의 끝(end-of-file; EOF)을 검출하여 프로그램을 적절히 종료하는 방법을 보여줍니다.

이 프로젝트를 다운로드 또는 클론한 다음, 위의 "주의사항"에 나열된 대로 Nightly Rust를 셋업합니다.

src/solution.rs를 다음과 같이 수정합니다.

```rust
use basm::platform::io::{Reader, Writer, Print};
use core::cmp::max;
pub fn main() {
let mut reader: Reader = Default::default();
let mut writer: Writer = Default::default();
let mut x = [usize::MAX; 100_001]; // x[i] = minimum end-value of "len >= i" increasing seq.
while !reader.is_eof_skip_whitespace() {
let n = reader.usize();
let mut ans = 0;
x[0] = 0;
for i in 0..n {
x[i + 1] = usize::MAX;
let v = reader.usize();
let (mut lo, mut hi) = (0, i);
while lo < hi {
let mid = (lo + hi + 1) / 2;
if x[mid] < v { lo = mid; } else { hi = mid - 1; }
}
let ans_new = lo + 1;
x[ans_new] = v;
ans = max(ans, ans_new);
}
writer.println(ans);
}
writer.write(b"\n");
}
```

Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
link_args_basm.push("/ENTRY:_start");
link_args_basm.push("/BASE:0x0");
link_args_basm.push("/NXCOMPAT:NO");
link_args_basm.push("/STACK:268435456");
link_args_basm.push("/EMITTOOLVERSIONINFO:NO");
link_args_basm.push("/EMITPOGOPHASEINFO");
link_args_basm_submit.push("/ALIGN:128");
Expand Down
Empty file modified release-32bit.sh
100644 → 100755
Empty file.
Empty file modified release-64bit.sh
100644 → 100755
Empty file.
Empty file modified release-rs.sh
100644 → 100755
Empty file.
Empty file modified release.sh
100644 → 100755
Empty file.
Loading

0 comments on commit 2c7d505

Please sign in to comment.