From 30c779644e4825fae6d59a4b2b7ff2dc38917c07 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Thu, 11 Jan 2024 02:17:26 +0530 Subject: [PATCH] initial super simple version --- Cargo.lock | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7e0ab09 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,61 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "onebrc" +version = "0.1.0" +dependencies = [ + "rayon", +] + +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] diff --git a/Cargo.toml b/Cargo.toml index 7e2292a..3dddfcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rayon = "1.8.0" diff --git a/src/main.rs b/src/main.rs index e7a11a9..a80dce1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,59 @@ +use std::collections::HashMap; +use std::fs::File; +use std::io::Read; + fn main() { - println!("Hello, world!"); + let mut file = File::open("./measurements.txt").expect("error in opening file"); + + let mut map = HashMap::new(); + + let mut buf = Vec::new(); + + file.read_to_end(&mut buf) + .expect("error in reading file to memory"); + + for line in buf.split(|&x| x == b'\n') { + let l = line.len(); + if l == 0 { + continue; + } + + let loc = line.iter().position(|&x| x == b';').unwrap(); + + let (city, val) = line.split_at(loc); + let val = &val[1..]; + + let val = String::from_utf8_lossy(val); + + let val: f32 = val.parse().unwrap(); + + let (min, sum, max, counter) = + map.entry(city) + .or_insert((std::f32::MAX, 0.0, std::f32::MIN, 0)); + + *min = val.min(*min); + *max = val.max(*max); + *counter += 1; + *sum += val; + } + + let mut list: Vec<(&[u8], (f32, f32, f32, usize))> = map.into_iter().collect(); + list.sort_unstable_by(|(a, _), (b, _)| a.cmp(b)); + + let mut output = String::new(); + + let l = list.len(); + + for (i, (city, (min, mean, max, counter))) in list.into_iter().enumerate() { + output.push_str(&format!( + "{}={:.1}/{:.1}/{:.1}{}", + String::from_utf8_lossy(city), + min, + mean / counter as f32, + max, + if i == l - 1 { "" } else { ", " } + )); + } + + println!("{{{output}}}"); }