Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
252c144431 | |||
3ffc40596a |
5 changed files with 377 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
# PPJ
|
# PPJ
|
||||||
|
|
||||||
This repository exists to archive PJATK tasks for the PPJ curriculum.
|
This branch contains the solution for the **1st task**.
|
||||||
|
|
||||||
Each task can be found under its **corresponding branch**: `task/n`.
|
This solution contains Rust code :333c
|
||||||
Example: `task/2`.
|
|
||||||
|
[Go back to main](/femsci/ppj/src/branch/nya)
|
||||||
|
|
243
j1-rs/Cargo.lock
generated
Normal file
243
j1-rs/Cargo.lock
generated
Normal file
|
@ -0,0 +1,243 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bytes"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cesu8"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "combine"
|
||||||
|
version = "4.6.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "j1-rs"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"jni",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jni"
|
||||||
|
version = "0.21.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97"
|
||||||
|
dependencies = [
|
||||||
|
"cesu8",
|
||||||
|
"cfg-if",
|
||||||
|
"combine",
|
||||||
|
"jni-sys",
|
||||||
|
"log",
|
||||||
|
"thiserror",
|
||||||
|
"walkdir",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jni-sys"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.78"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "same-file"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.52"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.57"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.57"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "walkdir"
|
||||||
|
version = "2.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||||
|
dependencies = [
|
||||||
|
"same-file",
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.45.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm",
|
||||||
|
"windows_aarch64_msvc",
|
||||||
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_gnullvm",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.42.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
|
10
j1-rs/Cargo.toml
Normal file
10
j1-rs/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "j1-rs"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate_type = ["cdylib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
jni = "0.21.1"
|
32
j1-rs/src/lib.rs
Normal file
32
j1-rs/src/lib.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
use jni::{objects::JClass, sys::jstring, JNIEnv};
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "system" fn Java_dev_femsci_pjatk1_Main_histogen(
|
||||||
|
env: JNIEnv,
|
||||||
|
_class: JClass,
|
||||||
|
data: i32,
|
||||||
|
) -> jstring {
|
||||||
|
let shift = |s: u8| ((data & (0xff << s)) >> s) as u8;
|
||||||
|
let (i0, i1, i2): (u8, u8, u8) = (shift(16), shift(8), shift(0));
|
||||||
|
let max = i0.max(i1.max(i2));
|
||||||
|
|
||||||
|
let mut buf = String::new();
|
||||||
|
|
||||||
|
for i in 0..max {
|
||||||
|
let f_ast = |num: u8| {
|
||||||
|
let idx = max - i;
|
||||||
|
match idx <= num {
|
||||||
|
true => '*',
|
||||||
|
false => ' ',
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
buf.push_str(&format!("{}{}{}\n", f_ast(i0), f_ast(i1), f_ast(i2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let str_f = env
|
||||||
|
.new_string(buf.trim_end())
|
||||||
|
.expect("Cannot create string.");
|
||||||
|
|
||||||
|
str_f.into_raw()
|
||||||
|
}
|
88
src/dev/femsci/pjatk1/Main.java
Normal file
88
src/dev/femsci/pjatk1/Main.java
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
package dev.femsci.pjatk1;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/*
|
||||||
|
by femsci
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
public Main() {
|
||||||
|
}
|
||||||
|
|
||||||
|
//jni ref
|
||||||
|
public static native String histogen(int data);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Task 1
|
||||||
|
|
||||||
|
the premise: under some constraints, print a histogram
|
||||||
|
|
||||||
|
solution:
|
||||||
|
- parse the input
|
||||||
|
- get the greatest input value, use it as histogram bar height
|
||||||
|
- iterate over the height, inverse printing bars (bottom up) when value >= index
|
||||||
|
|
||||||
|
// // // Rust > C# >>>>> java
|
||||||
|
*/
|
||||||
|
|
||||||
|
//some brief validation messages conveying low-quality reports
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if(args.length != 3) {
|
||||||
|
System.err.println("No valid arguments provided.");
|
||||||
|
System.exit(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte i0, i1, i2;
|
||||||
|
try {
|
||||||
|
i1 = Byte.parseByte(args[1]);
|
||||||
|
i0 = Byte.parseByte(args[0]);
|
||||||
|
i2 = Byte.parseByte(args[2]);
|
||||||
|
} catch(NumberFormatException _e) {
|
||||||
|
System.err.println("Provided arguments are invalid.");
|
||||||
|
System.exit(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("executing the task...");
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.loadLibrary("j1_rs");
|
||||||
|
long t1 = System.nanoTime();
|
||||||
|
via_jni(i0, i1, i2);
|
||||||
|
long t2 = System.nanoTime();
|
||||||
|
|
||||||
|
System.out.printf("operation took %dµs %n", Math.ceilDiv(t2 - t1, 1000));
|
||||||
|
} catch (UnsatisfiedLinkError _e) {
|
||||||
|
System.err.println("cannot locate the library to perform jni variant");
|
||||||
|
}
|
||||||
|
|
||||||
|
long t1 = System.nanoTime();
|
||||||
|
via_plain_java(i0, i1, i2);
|
||||||
|
long t2 = System.nanoTime();
|
||||||
|
|
||||||
|
System.out.printf("operation took %dµs %n", Math.ceilDiv(t2 - t1, 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void via_jni(byte i0, byte i1, byte i2) {
|
||||||
|
System.out.println("\nvia JNI to a Rust lib:");
|
||||||
|
int data = (i0 << 16) | (i1 << 8) | i2;
|
||||||
|
|
||||||
|
//can also directly write to stdout in the rust lib
|
||||||
|
System.out.println(histogen(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void via_plain_java(byte i0, byte i1, byte i2) {
|
||||||
|
System.out.println("\nvia the plain old Java:");
|
||||||
|
|
||||||
|
int max = Math.max(i0, Math.max(i1, i2));
|
||||||
|
|
||||||
|
//cant use primitives in lambdas
|
||||||
|
BiFunction<Integer, Byte, Character> ast = (idx, b) -> b > idx ? '*' : ' ';
|
||||||
|
|
||||||
|
//i hate java for lack of delegates and proper closures
|
||||||
|
for(int i = max - 1; i >= 0; --i) {
|
||||||
|
System.out.printf("%s%s%s%n", ast.apply(i, i0), ast.apply(i, i1), ast.apply(i, i2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue