[ create a new paste ] login | about

Link: http://codepad.org/ihXAaJzx    [ raw code | fork ]

C, pasted on May 28:
/*
Cargo.toml contains:

[dependencies]
ordermap = "0.2.10"
memmap = "0.5.2"

[profile.release]
opt-level = 3
lto = true
panic = "abort"

Compiled with:
cargo build --release
*/

extern crate ordermap;
extern crate memmap;

fn main() {
    use std::env::args;
    use std::str::{from_utf8, from_utf8_unchecked};
    use std::cmp::max;
    use ordermap::OrderMap;

    let file_name = args().nth(1).expect("supply name of data file");
    let key_field_index = args().nth(2).unwrap().parse().unwrap();
    let value_field_index = args().nth(3).unwrap().parse().unwrap();

    let mmap = memmap::Mmap::open_path(file_name, memmap::Protection::Read).unwrap();
    let all_data = unsafe { mmap.as_slice() };

    let mut sum_by_key = OrderMap::<&[u8], i64>::new();
    let max_field_index = max(key_field_index, value_field_index);
    let delim = b'\t';

    for line in all_data.split(|&b| b == b'\n') {
        let mut key = None;
        let mut field_value = None;

        for (i, field) in line.splitn(max_field_index + 2, |&b| b == delim).enumerate() {
            if i == key_field_index {
                key = Some(field.into());
            } else if i == value_field_index {
                let field = unsafe { from_utf8_unchecked(&field) };
                field_value = Some(field.parse().unwrap());
            }
        }

        if let (Some(key), Some(field_value)) = (key, field_value) {
            *sum_by_key.entry(key).or_insert(0) += field_value;
        }
    }

    let (max_key, max_val) = sum_by_key.iter().max_by_key(|&(_, v)| v).unwrap();
    println!("max_key: {} sum: {}", from_utf8(&max_key).unwrap(), max_val);
}


Create a new paste based on this one


Comments: