#![warn(missing_docs)]
pub mod indices;
pub mod printing;
pub mod vecops;
pub mod mutops;
use std::io;
use std::io::Write;
use std::fs::File;
use printing::*;
use std::fmt;
use std::error::Error;
use std::thread::AccessError;
use std::fmt::{Debug,Display};
use core::ops::Range;
#[derive(Debug)]
pub enum IErr<T> where T:Sized+Debug {
NoDataError(T),
DataError(T),
ArithError(T),
OtherError(T)
}
pub type IE = IErr<&'static str>;
impl<T> Error for IErr<T> where T:Sized+Debug+Display {}
impl<T> fmt::Display for IErr<T> where T:Sized+Debug+Display {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
IErr::NoDataError(s) => write!(f,"Missing or insufficient data: {}",s),
IErr::DataError(s) => write!(f,"Wrong data: {}",s),
IErr::ArithError(s) => write!(f,"Arithmetic error: {}",s),
IErr::OtherError(s) => write!(f,"Converted from {}",s)
}
}
}
impl From<AccessError> for IErr<& 'static str> {
fn from(_: AccessError) -> Self {
IErr::OtherError("AccessError")
}
}
#[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]
)
}};
}
pub fn inf64<T>(arg:T) -> f64 where F64:From<T> {
let F64(res) = F64::from(arg);
res
}
#[derive(Default)]
pub struct MinMax<T> {
pub min: T,
pub minindex: usize,
pub max: T,
pub maxindex: usize,
}
impl<T> std::fmt::Display for MinMax<T>
where
T: Copy + std::fmt::Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"min: {GR}{}{UN}, minindex: {YL}{}{UN}, max: {GR}{}{UN}, maxindex: {YL}{}{UN}",
self.min,
self.minindex,
self.max,
self.maxindex
)
}
}
pub trait Printing<T> where Self: Sized {
fn rd(self) -> String { format!("{RD}{}{UN}",self.to_str()) }
fn gr(self) -> String { format!("{GR}{}{UN}",self.to_str()) }
fn bl(self) -> String { format!("{BL}{}{UN}",self.to_str()) }
fn yl(self) -> String { format!("{YL}{}{UN}",self.to_str()) }
fn mg(self) -> String { format!("{MG}{}{UN}",self.to_str()) }
fn cy(self) -> String { format!("{CY}{}{UN}",self.to_str()) }
fn wvec(self,f:&mut File) -> Result<(), io::Error> {
Ok(write!(*f,"{} ", self.to_plainstr())?)
}
fn pvec(self) { print!("{} ", self.to_plainstr()) }
fn to_str(self) -> String;
fn to_plainstr(self) -> String;
}
pub trait Indices {
fn newindex(n:usize) -> Vec<usize> { Vec::from_iter(0..n) }
fn invindex(self) -> Vec<usize>;
fn complindex(self) -> Vec<usize>;
fn unindex<T>(self, v: &[T], ascending: bool) -> Vec<T> where T:Copy;
fn ucorrelation(self, v: &[usize]) -> f64;
fn indx_to_f64(self) -> Vec<f64>;
}
pub trait Vecops<T> {
fn tof64(self) -> Vec<f64> where T: Copy, f64: From<T>;
fn maxt(self) -> T where T: PartialOrd+Copy;
fn mint(self) -> T where T: PartialOrd+Copy;
fn minmaxt(self) -> (T, T) where T: PartialOrd+Copy;
fn minmax(self) -> MinMax<T> where T: PartialOrd+Copy;
fn minmax_slice(self,i:usize, n:usize) -> MinMax<T> where T: PartialOrd+Copy;
fn minmax_indexed(self, idx:&[usize], i:usize, n:usize) -> MinMax<T>
where T: PartialOrd+Copy;
fn revs(self) -> Vec<T> where T: Copy;
fn sansrepeat(self) -> Vec<T> where T: PartialEq+Copy;
fn member(self, m:T, forward:bool) -> Option<usize> where T: PartialEq+Copy;
fn binsearch(self, val:&T, ascending:bool) -> Range<usize> where T: PartialOrd;
fn binsearch_indexed(self, idx:&[usize], val:&T, ascending:bool) -> Range<usize> where T: PartialOrd;
fn occurs(self, val:T) -> usize where T: PartialOrd;
fn unite_unsorted(self, v: &[T]) -> Vec<T> where T: Clone;
fn unite_indexed(self, ix1: &[usize], v2: &[T], ix2: &[usize]) -> Vec<T>
where T: PartialOrd+Copy;
fn intersect(self, v2: &[T]) -> Vec<T> where T: PartialOrd+Copy;
fn intersect_indexed(self, ix1: &[usize], v2: &[T], ix2: &[usize]) -> Vec<T>
where T: PartialOrd+Copy;
fn diff(self, v2: &[T]) -> Vec<T> where T: PartialOrd+Copy;
fn diff_indexed(self, ix1: &[usize], v2: &[T], ix2: &[usize]) -> Vec<T>
where T: PartialOrd+Copy;
fn partition(self, pivot:T) -> (Vec<T>, Vec<T>, Vec<T>)
where T: PartialOrd+Copy;
fn partition_indexed(self, pivot: T) -> (Vec<usize>, Vec<usize>, Vec<usize>)
where T: PartialOrd+Copy;
fn merge(self, v2: &[T]) -> Vec<T> where T: PartialOrd+Copy;
fn merge_indexed(self, idx1: &[usize], v2: &[T], idx2: &[usize]) -> (Vec<T>, Vec<usize>)
where T: PartialOrd+Copy;
fn merge_indices(self, idx1: &[usize], idx2: &[usize]) -> Vec<usize>
where T: PartialOrd+Copy;
fn mergesort_indexed(self) -> Vec<usize> where T:PartialOrd+Copy;
fn mergesortslice(self, i: usize, n: usize) -> Vec<usize>
where T: PartialOrd+Copy;
fn sortm(self, ascending: bool) -> Vec<T> where T: PartialOrd+Copy;
fn rank(self, ascending: bool) -> Vec<usize> where T: PartialOrd+Copy;
fn isorttwo(self, idx: &mut[usize], i0: usize, i1: usize) -> bool where T:PartialOrd;
fn isortthree(self, idx: &mut[usize], i0: usize, i1:usize, i2:usize) where T: PartialOrd;
fn hashsort_indexed(self) -> Vec<usize>
where T: PartialOrd+Copy,F64:From<T>;
fn hashsortslice(self, idx: &mut[usize], i: usize, n: usize, min:T, max:T)
where T: PartialOrd+Copy,F64:From<T>;
fn sorth(self, ascending: bool) -> Vec<T>
where T: PartialOrd+Copy,F64:From<T>;
}
pub struct F64(pub f64);
impl From<&str> for F64 {
fn from(s:&str) -> F64 {
if s.is_empty() { return F64(0_f64) };
let bytes = s.as_bytes();
let mut res = bytes[0] as f64;
for i in 1..7 {
res *= 256.;
if i < bytes.len() { res += bytes[i] as f64; }
};
F64(res)
}
}
impl From<usize> for F64 {
fn from(s:usize) -> F64 { F64(s as f64) }
}
impl From<u8> for F64 {
fn from(s:u8) -> F64 { F64(s as f64)}
}
impl From<u16> for F64 {
fn from(s:u16) -> F64 { F64(s as f64) }
}
impl From<u32> for F64 {
fn from(s:u32) -> F64 { F64(s as f64) }
}
impl From<u64> for F64 {
fn from(s:u64) -> F64 { F64(s as f64) }
}
impl From<i8> for F64 {
fn from(s:i8) -> F64 { F64(s as f64)}
}
impl From<i16> for F64 {
fn from(s:i16) -> F64 { F64(s as f64) }
}
impl From<i32> for F64 {
fn from(s:i32) -> F64 { F64(s as f64) }
}
impl From<i64> for F64 {
fn from(s:i64) -> F64 { F64(s as f64) }
}
impl From<f32> for F64 {
fn from(s:f32) -> F64 { F64(s as f64) }
}
impl From<f64> for F64 {
fn from(s:f64) -> F64 { F64(s) }
}
pub trait Mutops<T> {
fn mutquicksort(self) where T: PartialOrd;
fn mutrevs(self);
fn mutsorttwo(self, i0:usize, i1:usize) -> bool
where T: PartialOrd;
fn mutsortthree(self, i0:usize, i1:usize, i2:usize)
where T: PartialOrd;
fn muthashsort(self)
where T: PartialOrd+Copy, F64:From<T>;
fn muthashsortslice(self, i:usize, n:usize, min:T, max:T)
where T: PartialOrd+Copy, F64:From<T>;
}