Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# @help: https://git-scm.com/docs/gitignore

target/
3 changes: 3 additions & 0 deletions s3_dsa/p01/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @help: https://git-scm.com/docs/gitignore

*.bin
69 changes: 69 additions & 0 deletions s3_dsa/p01/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions s3_dsa/p01/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/cargo.json
# @help: https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
default-members = ["./src/bin/sort/"]
members = [
"./src/bin/gen/",
"./src/bin/sort/",
"./src/lib/nacci/",
"./src/lib/polyphase/",
]
resolver = "2"

[workspace.dependencies]
log = "0.4.22"

[workspace.package]
authors = ["fruzitent <fruzit@gmail.com>"]
description = "s3_dsa/p01"
edition = "2021"
license-file = "license.md"
readme = "readme.md"
repository = "git+https://github.com/fruzitent/kpi.git"
5 changes: 5 additions & 0 deletions s3_dsa/p01/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/rust-toolchain.json
# @help: https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file

[toolchain]
channel = "stable"
4 changes: 4 additions & 0 deletions s3_dsa/p01/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/rustfmt.json
# @help: https://rust-lang.github.io/rustfmt

max_width = 120
21 changes: 21 additions & 0 deletions s3_dsa/p01/src/bin/gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/cargo.json
# @help: https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]

[dependencies]
nanorand = "0.7.0"

[dev-dependencies]

[features]

[package]
authors.workspace = true
description.workspace = true
edition.workspace = true
license-file.workspace = true
name = "gen"
readme.workspace = true
repository.workspace = true
version = "0.1.0"
11 changes: 11 additions & 0 deletions s3_dsa/p01/src/bin/gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let path = std::path::Path::new("./input.bin");
let size = usize::pow(2, 30);

let mut data = vec![0; size];
let mut rng = nanorand::tls_rng();
nanorand::Rng::fill_bytes(&mut rng, &mut data);

let mut file = std::fs::File::create(path).unwrap();
std::io::Write::write_all(&mut file, &data).unwrap();
}
24 changes: 24 additions & 0 deletions s3_dsa/p01/src/bin/sort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/cargo.json
# @help: https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]

[dependencies]
fern = "0.7.0"
humantime = "2.1.0"
log.workspace = true
polyphase = { path = "../../lib/polyphase/" }

[dev-dependencies]

[features]

[package]
authors.workspace = true
description.workspace = true
edition.workspace = true
license-file.workspace = true
name = "sort"
readme.workspace = true
repository.workspace = true
version = "0.1.0"
24 changes: 24 additions & 0 deletions s3_dsa/p01/src/bin/sort/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fn setup_logger() -> Result<(), fern::InitError> {
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{} {} {}:{}] {}",
humantime::format_rfc3339_seconds(std::time::SystemTime::now()),
record.level(),
record.file().unwrap(),
record.line().unwrap(),
message
))
})
.level(log::LevelFilter::Debug)
.chain(std::io::stdout())
.apply()?;
Ok(())
}

fn main() {
setup_logger().unwrap();
let input_path = std::path::Path::new("./input.bin");
let output_path = std::path::Path::new("./output.bin");
polyphase::sort(input_path, output_path, false, 3);
}
21 changes: 21 additions & 0 deletions s3_dsa/p01/src/lib/nacci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/cargo.json
# @help: https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]

[dependencies]
log.workspace = true

[dev-dependencies]

[features]

[package]
authors.workspace = true
description.workspace = true
edition.workspace = true
license-file.workspace = true
name = "nacci"
readme.workspace = true
repository.workspace = true
version = "0.1.0"
80 changes: 80 additions & 0 deletions s3_dsa/p01/src/lib/nacci/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// @see: https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Fibonacci_numbers_of_higher_order
pub struct Nacci {
// TODO: replace usize with T
cache: Vec<usize>,
index: usize,
total: usize,
}

impl Nacci {
pub fn new(order: usize) -> Option<Self> {
if order < 2 {
// TODO: https://en.wikipedia.org/wiki/Negafibonacci_coding
return None;
}
let mut cache = vec![0; order];
cache[order - 1] = 1;
Some(Self {
cache,
index: 0,
total: 1,
})
}
}

impl Iterator for Nacci {
type Item = usize;

fn collect<B>(self) -> B
where
B: FromIterator<Self::Item>,
Self: Sized,
{
FromIterator::from_iter(self)
}

fn next(&mut self) -> Option<Self::Item> {
let result = self.cache[self.index];
self.cache[self.index] = self.total;
self.index = (self.index + 1) % self.cache.len();
self.total = self.total * 2 - result;
Some(result)
}
}

pub fn get_dist(length: usize, tape_count: usize) -> Vec<usize> {
let fib = Nacci::new(tape_count - 1).unwrap();
let mut levels: Vec<usize> = vec![0; tape_count - 1];
for (i, val) in fib.enumerate() {
levels[i % (tape_count - 1)] = val;
let total: usize = levels.iter().sum();
log::debug!("i={i:?}, val={val:?}, levels={levels:?}, total={total:?}", i = i + 1);
if total >= length {
break;
}
}
levels
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
assert!(Nacci::new(1).is_none());
assert_eq!(
Nacci::new(2).unwrap().take(16).collect::<Vec<_>>(),
vec![0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
);
assert_eq!(
Nacci::new(3).unwrap().take(16).collect::<Vec<_>>(),
vec![0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705]
);
assert_eq!(
Nacci::new(4).unwrap().take(16).collect::<Vec<_>>(),
vec![0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490]
);
assert_eq!(Nacci::new(5).unwrap().take(0).collect::<Vec<usize>>(), vec![]);
}
}
24 changes: 24 additions & 0 deletions s3_dsa/p01/src/lib/polyphase/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#:schema https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/cargo.json
# @help: https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]

[dependencies]
bytemuck = "1.19.0"
log.workspace = true
nacci = { path = "../nacci/" }

[dev-dependencies]

[features]
init_merge = []

[package]
authors.workspace = true
description.workspace = true
edition.workspace = true
license-file.workspace = true
name = "polyphase"
readme.workspace = true
repository.workspace = true
version = "0.1.0"
Loading