#![deny(unsafe_op_in_unsafe_fn)]
pub trait StalinSortable {
unsafe fn stalin_sort(&self) -> Self;
unsafe fn stalin_sort_gtg(&self) -> (Self, Self)
where
Self: Sized;
}
impl<T> StalinSortable for Vec<T> where T : Clone + PartialOrd {
unsafe fn stalin_sort(&self) -> Self {
let mut result : Vec<T> = Vec::with_capacity(self.len());
if self.len() == 0 {
return result;
}
let mut max : T = self.first().unwrap().clone();
self.into_iter().for_each(|x| {
if *x > max {
result.push(x.clone());
max = x.clone();
}
});
result
}
unsafe fn stalin_sort_gtg(&self) -> (Self, Self) {
let mut result : Vec<T> = Vec::with_capacity(self.len());
let mut gulag : Vec<T> = Vec::with_capacity(self.len());
if self.len() == 0 {
return (result, gulag);
}
let mut max : T = self.first().unwrap().clone();
self.into_iter().for_each(|x| {
if *x >= max {
result.push(x.clone());
max = x.clone();
} else { gulag.push(x.clone()); }
});
(result, gulag)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stalin_sort() {
unsafe {
assert_eq!(vec![1, 2, 3].stalin_sort(), vec![1, 2, 3]);
assert_eq!(vec![1,2,1,4,5].stalin_sort(), vec![1,2,4,5]);
}
}
}