Formatting
This commit is contained in:
parent
34768d3ec1
commit
8eefe06e8b
@ -1,14 +1,10 @@
|
|||||||
#![feature(slice_split_once)]
|
#![feature(slice_split_once)]
|
||||||
|
|
||||||
use std::{
|
|
||||||
fs::File,
|
|
||||||
io::BufReader,
|
|
||||||
thread,
|
|
||||||
};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{BufRead, Seek, SeekFrom};
|
use std::io::{BufRead, Seek, SeekFrom};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
use std::{fs::File, io::BufReader, thread};
|
||||||
|
|
||||||
use onebrc::{hashstr, parse_temp, StationMeasurements};
|
use onebrc::{hashstr, parse_temp, StationMeasurements};
|
||||||
|
|
||||||
@ -18,7 +14,8 @@ fn main() {
|
|||||||
const FILE_PATH: &str = "../../../measurements.txt";
|
const FILE_PATH: &str = "../../../measurements.txt";
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
thread::scope(|s| {
|
thread::scope(|s| {
|
||||||
let mut stations: HashMap<usize, (String, StationMeasurements)> = HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
|
let mut stations: HashMap<usize, (String, StationMeasurements)> =
|
||||||
|
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
let cores = thread::available_parallelism().unwrap().into();
|
let cores = thread::available_parallelism().unwrap().into();
|
||||||
let file = File::open(FILE_PATH).expect("File measurements.txt not found");
|
let file = File::open(FILE_PATH).expect("File measurements.txt not found");
|
||||||
@ -30,9 +27,13 @@ fn main() {
|
|||||||
for i in 1..cores {
|
for i in 1..cores {
|
||||||
let mut reader = BufReader::new(&file);
|
let mut reader = BufReader::new(&file);
|
||||||
let mut byte_start = chunk_length * i;
|
let mut byte_start = chunk_length * i;
|
||||||
reader.seek(SeekFrom::Start(byte_start as u64)).expect("could not seek");
|
reader
|
||||||
|
.seek(SeekFrom::Start(byte_start as u64))
|
||||||
|
.expect("could not seek");
|
||||||
let mut line = Vec::with_capacity(108);
|
let mut line = Vec::with_capacity(108);
|
||||||
let line_len = reader.read_until(b'\n', &mut line).expect("could not read bytes");
|
let line_len = reader
|
||||||
|
.read_until(b'\n', &mut line)
|
||||||
|
.expect("could not read bytes");
|
||||||
byte_start += line_len;
|
byte_start += line_len;
|
||||||
bounds.push(byte_start as u64);
|
bounds.push(byte_start as u64);
|
||||||
}
|
}
|
||||||
@ -49,7 +50,9 @@ fn main() {
|
|||||||
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
|
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
|
||||||
let mut line = Vec::with_capacity(108);
|
let mut line = Vec::with_capacity(108);
|
||||||
loop {
|
loop {
|
||||||
let line_len = reader.read_until(b'\n', &mut line).expect("could not read bytes");
|
let line_len = reader
|
||||||
|
.read_until(b'\n', &mut line)
|
||||||
|
.expect("could not read bytes");
|
||||||
if line_len == 0 {
|
if line_len == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -89,7 +92,9 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut stations: Vec<String> = stations.iter().map(|(_, (station, measurements))| {
|
let mut stations: Vec<String> = stations
|
||||||
|
.iter()
|
||||||
|
.map(|(_, (station, measurements))| {
|
||||||
let measurements = measurements.to_string();
|
let measurements = measurements.to_string();
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
{
|
{
|
||||||
@ -99,7 +104,8 @@ fn main() {
|
|||||||
{
|
{
|
||||||
format!("{station}={measurements}")
|
format!("{station}={measurements}")
|
||||||
}
|
}
|
||||||
}).collect();
|
})
|
||||||
|
.collect();
|
||||||
stations.sort();
|
stations.sort();
|
||||||
let stations = stations.join(",");
|
let stations = stations.join(",");
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
|
@ -62,7 +62,10 @@ pub fn parse_temp(bytes: &[u8]) -> isize {
|
|||||||
(true, 5) => get_digit(bytes[1]) * 100 + get_digit(bytes[2]) * 10 + get_digit(bytes[4]),
|
(true, 5) => get_digit(bytes[1]) * 100 + get_digit(bytes[2]) * 10 + get_digit(bytes[4]),
|
||||||
(false, 3) => get_digit(bytes[0]) * 10 + get_digit(bytes[2]),
|
(false, 3) => get_digit(bytes[0]) * 10 + get_digit(bytes[2]),
|
||||||
(false, 4) => get_digit(bytes[0]) * 100 + get_digit(bytes[1]) * 10 + get_digit(bytes[3]),
|
(false, 4) => get_digit(bytes[0]) * 100 + get_digit(bytes[1]) * 10 + get_digit(bytes[3]),
|
||||||
_x => panic!("could not parse temp: is_negative = {is_negative}, length = {}", bytes.len()),
|
_x => panic!(
|
||||||
|
"could not parse temp: is_negative = {is_negative}, length = {}",
|
||||||
|
bytes.len()
|
||||||
|
),
|
||||||
};
|
};
|
||||||
if is_negative {
|
if is_negative {
|
||||||
-as_decimal
|
-as_decimal
|
||||||
@ -127,13 +130,11 @@ pub fn new_parse_temp(bytes: &[u8]) -> isize {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_pos(bytes: &[u8], find: u8) -> Option<u32> {
|
pub fn get_pos(bytes: &[u8], find: u8) -> Option<u32> {
|
||||||
let chunks = bytes.windows(4);
|
let chunks = bytes.windows(4);
|
||||||
let mut pos = 0;
|
for (pos, chunk) in chunks.enumerate() {
|
||||||
for chunk in chunks {
|
|
||||||
let inner_pos = get_pos_in_chunk(chunk, find);
|
let inner_pos = get_pos_in_chunk(chunk, find);
|
||||||
if inner_pos < chunk.len() as u32 {
|
if inner_pos < chunk.len() as u32 {
|
||||||
return Some(pos + inner_pos);
|
return Some(pos as u32 + inner_pos);
|
||||||
}
|
}
|
||||||
pos += 1;
|
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -193,7 +194,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_getpos() {
|
fn test_getpos() {
|
||||||
let semi_bytes = vec![0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, b';', 0_u8, 0_u8];
|
let semi_bytes = vec![
|
||||||
|
0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, b';', 0_u8, 0_u8,
|
||||||
|
];
|
||||||
let semi_bytes = semi_bytes.as_slice();
|
let semi_bytes = semi_bytes.as_slice();
|
||||||
let pos = get_pos(semi_bytes, b';').unwrap();
|
let pos = get_pos(semi_bytes, b';').unwrap();
|
||||||
assert_eq!(pos, 8);
|
assert_eq!(pos, 8);
|
||||||
|
Loading…
Reference in New Issue
Block a user