#![warn(missing_docs)]
pub mod indices;
pub mod mutops;
pub mod printing;
pub mod search;
pub mod vecops;
use core::{
cmp::{Ordering, Reverse},
ops::Range,
};
use printing::*;
use std::{collections::BinaryHeap, fs::File, io, io::Write};
#[macro_export]
macro_rules! here {
($msg:expr) => {{
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],
$msg
)
}};
}
#[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: Clone + 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 fn deref_vec<T>(v: &[&T]) -> Vec<T>
where
T: Clone,
{
v.iter().map(|&x| x.clone()).collect()
}
pub fn qsortf64(v: &mut [f64]) {
v.sort_unstable_by(|a, b| a.total_cmp(b))
}
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 Search<T> {
fn binary_by(self, cmpr: impl Fn(T) -> Ordering) -> Result<T, T>;
fn binary_any(&self, cmpr: impl Fn(T) -> Ordering) -> (T, Range<T>);
fn binary_all(&self, cmpr: impl Fn(T) -> Ordering) -> Range<T>;
}
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: Clone;
fn ucorrelation(self, v: &[usize]) -> f64;
fn indx_to_f64(self) -> Vec<f64>;
}
pub trait Vecops<'a, T> {
fn tof64(self) -> Vec<f64>
where
T: Clone,
f64: From<T>;
fn maxt(self) -> T
where
T: PartialOrd + Clone;
fn mint(self) -> T
where
T: PartialOrd + Clone;
fn minmaxt(self) -> (T, T)
where
T: PartialOrd + Clone;
fn minmax(self) -> MinMax<T>
where
T: PartialOrd + Clone;
fn minmax_slice(self, i: usize, n: usize) -> MinMax<T>
where
T: PartialOrd + Clone;
fn minmax_indexed(self, idx: &[usize], i: usize, n: usize) -> MinMax<T>
where
T: PartialOrd + Clone;
fn revs(self) -> Vec<T>
where
T: Clone;
fn sansrepeat(self) -> Vec<T>
where
T: PartialEq + Clone;
fn member(self, m: T, forward: bool) -> Option<usize>
where
T: PartialEq + Clone;
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 + Clone;
fn intersect(self, v2: &[T]) -> Vec<T>
where
T: PartialOrd + Clone;
fn intersect_indexed(self, ix1: &[usize], v2: &[T], ix2: &[usize]) -> Vec<T>
where
T: PartialOrd + Clone;
fn diff(self, v2: &[T]) -> Vec<T>
where
T: PartialOrd + Clone;
fn diff_indexed(self, ix1: &[usize], v2: &[T], ix2: &[usize]) -> Vec<T>
where
T: PartialOrd + Clone;
fn partition(self, pivot: &T) -> (Vec<T>, Vec<T>, Vec<T>)
where
T: PartialOrd + Clone;
fn partition_indexed(self, pivot: &T) -> (Vec<usize>, Vec<usize>, Vec<usize>)
where
T: PartialOrd + Clone;
fn binsearch(self, target: &T) -> Range<usize>
where
T: PartialOrd + Copy;
fn binsearch_indexed(self, idx: &[usize], target: &T) -> Range<usize>
where
T: PartialOrd + Copy;
fn merge(self, v2: &[T]) -> Vec<T>
where
T: PartialOrd + Clone;
fn merge_indexed(self, idx1: &[usize], v2: &[T], idx2: &[usize]) -> (Vec<T>, Vec<usize>)
where
T: PartialOrd + Clone;
fn merge_indices(self, idx1: &[usize], idx2: &[usize]) -> Vec<usize>
where
T: PartialOrd + Clone;
fn mergesort_indexed(self) -> Vec<usize>
where
T: PartialOrd + Clone;
fn mergesortslice(self, i: usize, n: usize) -> Vec<usize>
where
T: PartialOrd + Clone;
fn sortm(self, ascending: bool) -> Vec<T>
where
T: PartialOrd + Clone;
fn rank(self, ascending: bool) -> Vec<usize>
where
T: PartialOrd + Clone;
fn isorttwo(self, idx: &mut [usize], i0: usize, i1: usize)
where
T: PartialOrd;
fn isortthree(self, idx: &mut [usize], i0: usize, i1: usize, i2: usize)
where
T: PartialOrd;
fn hashsort_indexed(self, quantify: impl Copy + Fn(&T) -> f64) -> Vec<usize>
where
T: PartialOrd + Clone;
fn hashsortslice(
self,
idx: &mut [usize],
i: usize,
n: usize,
min: f64,
max: f64,
quantify: impl Copy + Fn(&T) -> f64,
) where
T: PartialOrd + Clone;
fn sorth(self, quantify: impl Copy + Fn(&T) -> f64, ascending: bool) -> Vec<T>
where
T: PartialOrd + Clone;
fn smallest_k(&self, k: usize) -> BinaryHeap<&T>
where
T: Ord;
fn biggest_k(&self, k: usize) -> BinaryHeap<Reverse<&T>>
where
T: Ord;
fn isort_indexed<F>(self, rng: Range<usize>, c: F) -> Vec<usize>
where
F: Fn(&T, &T) -> Ordering;
fn isort_refs<F>(self, rng: Range<usize>, c: F) -> Vec<&'a T>
where
F: Fn(&T, &T) -> Ordering;
fn best_k<F>(self, k: usize, rng: Range<usize>, c: F) -> Vec<&'a T>
where
F: Fn(&T, &T) -> Ordering;
}
pub trait Mutops<T> {
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, quantify: impl Copy + Fn(&T) -> f64)
where
T: PartialOrd + Clone;
fn muthashsortslice(
self,
i: usize,
n: usize,
min: f64,
max: f64,
quantify: impl Copy + Fn(&T) -> f64,
) where
T: PartialOrd + Clone;
fn mutisort<F>(self, rng: Range<usize>, c: F)
where
T: Copy,
F: Fn(&T, &T) -> Ordering;
}