1#![deny(unsafe_op_in_unsafe_fn)]
9
10pub trait StalinSortable {
11 unsafe fn stalin_sort(&self) -> Self;
16 unsafe fn stalin_sort_gtg(&self) -> (Self, Self)
22 where
23 Self: Sized;
24}
25
26impl<T> StalinSortable for Vec<T> where T : Clone + PartialOrd {
27 unsafe fn stalin_sort(&self) -> Self {
28 let mut result : Vec<T> = Vec::with_capacity(self.len());
29
30 if self.len() == 0 {
31 return result;
32 }
33
34 let mut max : T = self.first().unwrap().clone();
35
36 self.into_iter().for_each(|x| {
37 if *x > max {
38 result.push(x.clone());
39 max = x.clone();
40 }
41 });
42
43 result
44 }
45
46 unsafe fn stalin_sort_gtg(&self) -> (Self, Self) {
47
48 let mut result : Vec<T> = Vec::with_capacity(self.len());
49
50 let mut gulag : Vec<T> = Vec::with_capacity(self.len());
51
52 if self.len() == 0 {
53 return (result, gulag);
54 }
55
56 let mut max : T = self.first().unwrap().clone();
57
58 self.into_iter().for_each(|x| {
59 if *x >= max {
60 result.push(x.clone());
61 max = x.clone();
62 } else { gulag.push(x.clone()); }
63 });
64
65 (result, gulag)
66 }
67}
68
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_stalin_sort() {
76 unsafe {
77 assert_eq!(vec![1, 2, 3].stalin_sort(), vec![1, 2, 3]);
78 assert_eq!(vec![1,2,1,4,5].stalin_sort(), vec![1,2,4,5]);
79 }
80 }
81}