initial super simple version

This commit is contained in:
Ishan Jain 2024-01-11 02:17:26 +05:30
parent a93abe3f45
commit 30c779644e
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
3 changed files with 119 additions and 1 deletions

61
Cargo.lock generated Normal file
View File

@ -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",
]

View File

@ -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"

View File

@ -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}}}");
}