oscoin / osrank-rs

A pre-alpha osrank implementation in Rust.
http://oscoin.io/
3 stars 3 forks source link

Add the Rank data structure to sort pageranks #5

Closed adinapoli-mndc closed 5 years ago

adinapoli-mndc commented 5 years ago

cc @MeBrei

This PR provides a Rank data structure as part of a new osrank::collections module, which implements an Iterator to iterate over the labeled elements of a data structure. It allows things like these:

    #[test]
    fn test_rank_into_iter() {
        let mtrx = arr2(&[[1], [9], [7]]);

        let rows = vec!["foo", "bar", "baz"]
            .into_iter()
            .map(String::from)
            .collect::<Vec<String>>();
        let cols = vec!["quux", "lorem", "ipsum"]
            .into_iter()
            .map(String::from)
            .collect::<Vec<String>>();
        let labels = (rows.as_slice(), cols.as_slice());

        let lbld = mtrx.labeled(labels);

        let rank = Rank::from(lbld).unwrap_or_else(|e| panic!(e));

        assert_eq!(
            rank.into_iter()
                .sorted_by(|(_, v1), (_, v2)| v2.cmp(v1))
                .take(2)
                .collect::<Vec<(&str, &i32)>>(),
            vec![("bar", &9), ("baz", &7)]
        );
    }

Thanks to this I have managed to sort the page ranks and to display them labeled, but there is something fishy going on, as papito cannot possibly be the most influential project in the Rust ecosystem :

winapi 0.04760741947253987332855373892926
rand 0.02447552165000656601567818881904
serde 0.02034980182889630645481915394157
libc 0.01874767780299204517069355802050
log 0.01613704060469288437129620206179
num-traits 0.01563045437343842797295323521212
serde_derive 0.01430165464574848291356978791100
compiletest_rs 0.01375802001704208230026260650902
proc-macro2 0.01030119260966721496386444556492
rustc-serialize 0.00987997677542114781379378740667
clippy 0.00953403789545404813099427343559
regex 0.00879273734899923935237620042926
quote 0.00860599974101563733319686377854
lazy_static 0.00841001816710117144904046426745
rustc-ap-rustc_data_structures 0.00796875949198406346996037541430
quickcheck 0.00747318951747357254622450284387
num 0.00708266159981826154412587470688
term 0.00702357424386592255755035196785
syn 0.00678246767408268499272017137969
serde_json 0.00634508284883164488249240875462
kernel32-sys 0.00571477519474944059552079522746
rustc-ap-serialize 0.00566998830611966601195073067743
byteorder 0.00564042329070288915582320754538
rustc-ap-rustc_cratesio_shim 0.00498932601652565289873653142649
serde_test 0.00472009660243220049297141471811
tempdir 0.00470869909738269327298398891912
rand_core 0.00445253684519347336240313595113
[...]

Looks reasonable!