remove rust solution
This commit is contained in:
parent
cd9c0f52e9
commit
5a16ea9ff6
1
lab1a/rust/.gitignore
vendored
1
lab1a/rust/.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
target
|
|
122
lab1a/rust/Cargo.lock
generated
122
lab1a/rust/Cargo.lock
generated
@ -1,122 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "a-rust"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"anyhow",
|
|
||||||
"crossbeam-channel",
|
|
||||||
"crossbeam-utils",
|
|
||||||
"serde_json",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "anyhow"
|
|
||||||
version = "1.0.75"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cfg-if"
|
|
||||||
version = "1.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "crossbeam-channel"
|
|
||||||
version = "0.5.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"crossbeam-utils",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "crossbeam-utils"
|
|
||||||
version = "0.8.16"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "itoa"
|
|
||||||
version = "1.0.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "1.0.67"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
|
|
||||||
dependencies = [
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "quote"
|
|
||||||
version = "1.0.33"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ryu"
|
|
||||||
version = "1.0.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde"
|
|
||||||
version = "1.0.188"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
|
|
||||||
dependencies = [
|
|
||||||
"serde_derive",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.188"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "serde_json"
|
|
||||||
version = "1.0.107"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
|
|
||||||
dependencies = [
|
|
||||||
"itoa",
|
|
||||||
"ryu",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "2.0.36"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "91e02e55d62894af2a08aca894c6577281f76769ba47c94d5756bec8ac6e7373"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-ident"
|
|
||||||
version = "1.0.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
|
@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "a-rust"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
anyhow = "1.0.75"
|
|
||||||
crossbeam-channel = "0.5.8"
|
|
||||||
crossbeam-utils = "0.8.16"
|
|
||||||
serde_json = "1.0.107"
|
|
@ -1,3 +0,0 @@
|
|||||||
# Lab1a
|
|
||||||
|
|
||||||
THIS IS NOT FINISHED
|
|
@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DataEntry {
|
|
||||||
pub name: String,
|
|
||||||
pub sugar: f32,
|
|
||||||
pub criteria: i32
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
use crate::common::DataEntry;
|
|
||||||
|
|
||||||
pub struct LimitedBuffer {
|
|
||||||
items: Vec<DataEntry>,
|
|
||||||
capacity: usize,
|
|
||||||
|
|
||||||
estimated_size: i32,
|
|
||||||
consumed_count: u32,
|
|
||||||
processed_count: u32
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LimitedBuffer {
|
|
||||||
pub fn new(capacity: usize) -> LimitedBuffer {
|
|
||||||
return LimitedBuffer{
|
|
||||||
items: Vec::with_capacity(capacity),
|
|
||||||
capacity,
|
|
||||||
estimated_size: 0,
|
|
||||||
consumed_count: 0,
|
|
||||||
processed_count: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn try_insert(&mut self, entry: DataEntry) -> bool {
|
|
||||||
if self.items.len() == self.capacity {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
self.items.push(entry);
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(&mut self) -> Option<DataEntry> {
|
|
||||||
if self.items.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(self.items.remove(0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_done(&self) -> bool {
|
|
||||||
return (self.estimated_size as u32) == self.processed_count
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn consume_estimated(&mut self) -> bool {
|
|
||||||
if (self.estimated_size as u32) == self.consumed_count {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
self.consumed_count += 1;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_estimated(&mut self, change: i32) {
|
|
||||||
self.estimated_size += change
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mark_processed(&mut self) {
|
|
||||||
self.processed_count += 1
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,118 +0,0 @@
|
|||||||
use std::{env::args, process::exit, thread, time::Duration, sync::{Arc, Mutex}};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
mod limited_buffer;
|
|
||||||
mod common;
|
|
||||||
|
|
||||||
use common::DataEntry;
|
|
||||||
use limited_buffer::LimitedBuffer;
|
|
||||||
|
|
||||||
// This was a mistake
|
|
||||||
|
|
||||||
const INPUT_BUFFER_SIZE: usize = 2;
|
|
||||||
const INPUT_MONITOR_COUNT: i32 = 2;
|
|
||||||
|
|
||||||
const OUTPUT_BUFFER_SIZE: usize = 10;
|
|
||||||
const OUTPUT_MONITOR_COUNT: i32 = 1;
|
|
||||||
|
|
||||||
fn filter_entry(entry: &DataEntry) -> bool {
|
|
||||||
thread::sleep(Duration::from_millis(100 + entry.criteria as u64));
|
|
||||||
return entry.sugar > entry.criteria as f32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn output_entry(output_list: &mut Vec<DataEntry>, entry: DataEntry) {
|
|
||||||
thread::sleep(Duration::from_millis(200));
|
|
||||||
output_list.push(entry);
|
|
||||||
output_list.sort_by(|a, b| a.sugar.total_cmp(&b.sugar))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn filter_entries(input: Arc<Mutex<LimitedBuffer>>, output: Arc<Mutex<LimitedBuffer>>) {
|
|
||||||
while !input.lock().unwrap().is_done() {
|
|
||||||
if input.lock().unwrap().consume_estimated() {
|
|
||||||
let entry = input.lock().unwrap().remove();
|
|
||||||
if entry.is_none() { continue; }
|
|
||||||
let entry = entry.unwrap();
|
|
||||||
|
|
||||||
println!("Started to filter: {}", entry.name);
|
|
||||||
let is_filtered = filter_entry(&entry);
|
|
||||||
println!("Finished to filter: {}", entry.name);
|
|
||||||
|
|
||||||
if is_filtered {
|
|
||||||
// output.lock().unwrap().update_estimated(1);
|
|
||||||
// while !output.lock().unwrap().try_insert(entry) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
input.lock().unwrap().mark_processed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ouput_entries(input: Arc<Mutex<LimitedBuffer>>, output: Arc<Mutex<LimitedBuffer>>, output_list: Arc<Mutex<Vec<DataEntry>>>) {
|
|
||||||
while !input.lock().unwrap().is_done() {
|
|
||||||
let entry = input.lock().unwrap().remove();
|
|
||||||
if entry.is_none() { continue; }
|
|
||||||
let entry = entry.unwrap();
|
|
||||||
output.lock().unwrap().consume_estimated();
|
|
||||||
|
|
||||||
let entry_name = entry.name.clone();
|
|
||||||
println!("Started to output: {}", entry_name);
|
|
||||||
{
|
|
||||||
let mut output_list = output_list.lock().unwrap();
|
|
||||||
output_entry(&mut output_list, entry);
|
|
||||||
}
|
|
||||||
println!("Finished to output: {}", entry_name);
|
|
||||||
|
|
||||||
output.lock().unwrap().mark_processed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
|
||||||
let args = args().collect::<Vec<_>>();
|
|
||||||
if args.len() != 3 {
|
|
||||||
eprintln!("Usage: {} <data-file> <output-file>", args[0]);
|
|
||||||
exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
let entries = vec![
|
|
||||||
DataEntry{ name: "foo1".into(), sugar: 100.0, criteria: 100 },
|
|
||||||
DataEntry{ name: "foo2".into(), sugar: 100.0, criteria: 100 },
|
|
||||||
DataEntry{ name: "foo3".into(), sugar: 100.0, criteria: 100 },
|
|
||||||
DataEntry{ name: "foo4".into(), sugar: 100.0, criteria: 100 },
|
|
||||||
DataEntry{ name: "foo5".into(), sugar: 100.0, criteria: 100 }
|
|
||||||
];
|
|
||||||
|
|
||||||
let mut input_threads = vec![];
|
|
||||||
// let mut output_threads = vec![];
|
|
||||||
|
|
||||||
// let output_list = Arc::new(Mutex::new(Vec::new()));
|
|
||||||
let input_buffer = Arc::new(Mutex::new(LimitedBuffer::new(INPUT_BUFFER_SIZE)));
|
|
||||||
let output_buffer = Arc::new(Mutex::new(LimitedBuffer::new(OUTPUT_BUFFER_SIZE)));
|
|
||||||
input_buffer.lock().unwrap().update_estimated(entries.len() as i32);
|
|
||||||
|
|
||||||
for _ in 0..INPUT_MONITOR_COUNT {
|
|
||||||
let input_buffer = input_buffer.clone();
|
|
||||||
let output_buffer = output_buffer.clone();
|
|
||||||
input_threads.push(thread::spawn(|| filter_entries(input_buffer, output_buffer)));
|
|
||||||
}
|
|
||||||
// for _ in 0..OUTPUT_MONITOR_COUNT {
|
|
||||||
// let input_buffer = input_buffer.clone();
|
|
||||||
// let output_buffer = output_buffer.clone();
|
|
||||||
// let output_list = output_list.clone();
|
|
||||||
// output_threads.push(thread::spawn(|| ouput_entries(input_buffer, output_buffer, output_list)));
|
|
||||||
// }
|
|
||||||
|
|
||||||
for entry in entries {
|
|
||||||
input_buffer.lock().unwrap().try_insert(entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
for handle in input_threads {
|
|
||||||
handle.join().expect("Failed to join input thread");
|
|
||||||
}
|
|
||||||
// for handle in output_threads {
|
|
||||||
// handle.join().expect("Failed to join output thread");
|
|
||||||
// }
|
|
||||||
|
|
||||||
println!("Finished");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user