1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pub mod indices;
pub mod merge;
use std::fmt::Write;

/// macro `here!()` gives `&str` with the current `file:line path::function` for error messages.
#[macro_export]
macro_rules! here {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            std::any::type_name::<T>()
        }
        let name = type_name_of(f); 
        format!("\n{}:{} {}", file!(), line!(), &name[..name.len()-3])
    }}
}

/// Helper function `write vector`. Formats Vec<T> as 
/// space separated values (ssv)  
/// that can be Displayed without recourse to Debug. 
/// Saves space by using ssv instead of csv. 
/// This must be done in Rust item by item, hence the iteration.
/// You can remove the green colour incantations at the beginning
/// and at the end, if not wanted.
pub fn wv<T>(v: &[T]) -> String where T:Copy+std::fmt::Display {
    let s =
        v.iter().fold(String::from("\x1B[01;92m[ "),
        |mut s,&n| {write!(s,"{} ",n).ok(); s} )
        +"]\x1B[0m";
    s
}

/// Helper function to `write tuple`, as produced by `minmax`
pub fn wt<T>(&(mi,mix,mx,mxi):&(T,usize,T,usize)) -> String where T:Copy+std::fmt::Display {
    let mut s = String::new();
    write!(s,"\x1B[01;92m({},{},{},{})\x1B[0m",mi,mix,mx,mxi).ok();
    s 
}

/// Helper function to format in green a single item. 
pub fn wi<T>(item: &T) -> String where T:std::fmt::Display {
    let s = String::from("\x1B[01;92m");
    s + &item.to_string() + "\x1B[0m"
}

/// Methods to manipulate indices of `Vec<usize>` type.
pub trait Indices { 
    /// Reverse an index slice by simple reverse iteration.
    fn revindex(self) -> Vec<usize>; 
    /// Invert an index.
    fn invindex(self) -> Vec<usize>;
    /// complement of the index - turns ranks from/to ascending/descending
    fn complindex(self) -> Vec<usize>;
    /// Collect values from `v` in the order of indices in self.
    fn unindex<T: Copy>(self, v:&[T], ascending:bool) -> Vec<T>;
    /// Pearson's correlation coefficient of two slices, typically the ranks.  
    fn ucorrelation(self, v: &[usize]) -> f64; 
    /// Potentially useful clone-recast of &[usize] to Vec<f64> 
    fn indx_to_f64 (self) -> Vec<f64>;
}