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
mod bubble_sort;
pub use bubble_sort::*;
mod select_sort;
pub use select_sort::*;
mod insert_sort;
pub use insert_sort::*;
mod merge_sort;
pub use merge_sort::*;

/// Generic interface to sorting algorithms
pub trait Sort<T: core::cmp::PartialOrd + Clone> {
    /// Get the internal data
    fn inner(&self) -> Vec<T>;

    /// Customizable comparison logic based on closures
    fn sort_by<F>(&mut self, f: F)
    where
        F: FnOnce(&T, &T) -> bool + core::marker::Copy;

    /// Sort by ascending order
    fn sort(&mut self) {
        self.sort_by(|v1, v2| v1 <= v2);
    }

    /// Determine if the sort is ascending
    fn is_sort(&self) -> bool {
        self.is_sort_by(|v1, v2| v1 <= v2)
    }

    /// Customized judgments are ordered
    fn is_sort_by<F>(&self, f: F) -> bool
    where
        F: FnOnce(&T, &T) -> bool + core::marker::Copy,
    {
        let array = self.inner();

        if array.is_empty() {
            return true;
        }

        for idx in 0..array.len() - 1 {
            if !f(&array[idx], &array[idx + 1]) {
                return false;
            }
        }
        true
    }
}

pub trait Infite {
    fn max_value() -> Self;

    fn min_value() -> Self;
}