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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use cargo_snippet::snippet;
pub use std::cmp;

// not usable in macro export
// pub trait Reorder {
//     fn reorder(&mut self, w: &Vec<usize>);
// }

// impl<T: Copy> Reorder for Vec<T> {
//     fn reorder(&mut self, w: &Vec<usize>) {
//         let mut nv = Vec::new();
//         for i in w {
//             nv.push(self[*i]);
//         }
//         *self = nv;
//     }
// }

/// Sorts the given vectors by the given function.
/// Requires `use lib::util::vec::*;`
///
/// # Examples
///
/// ```rust
/// use lib::srt;
/// use lib::util::vec::*;
/// let mut x = vec![1, 3, 2];
/// let mut y = vec![3, 2, 1];
/// srt!(|i, j| x[i] < x[j], y, x);
/// assert_eq!(x, [1, 2, 3]);
/// assert_eq!(y, [3, 1, 2]);
/// ```
#[snippet("srt")]
#[macro_export]
macro_rules! srt {
    ($f:expr, $head:expr, $($tail:expr),*) => {
        fn reorder<T: Copy>(slf: &mut Vec<T>, w: &Vec<usize>) {
            let mut nv = Vec::new();
            for i in w {
                nv.push(slf[*i]);
            }
            *slf = nv;
        }

        let mut w: Vec<usize> = (0..$head.len()).collect();
        w.sort_unstable_by(|&i, &j| if $f(i, j) { cmp::Ordering::Less } else { cmp::Ordering::Greater });
        reorder(&mut $head, &w);
        $(
            reorder(&mut $tail, &w);
        )*
    };
}

#[snippet("smaller")]
pub trait Comparator {
    fn smaller(&self) -> impl Fn(usize, usize) -> bool;
    fn greater(&self) -> impl Fn(usize, usize) -> bool;
}
impl<T: Ord> Comparator for Vec<T> {
    fn smaller(&self) -> impl Fn(usize, usize) -> bool {
        move |i, j| self[i] < self[j]
    }
    fn greater(&self) -> impl Fn(usize, usize) -> bool {
        move |i, j| self[i] > self[j]
    }
}

pub trait Decrement {
    fn decrement(&mut self);
}

impl<T: Copy + std::ops::SubAssign<T> + From<u8>> Decrement for Vec<T> {
    fn decrement(&mut self) {
        for i in 0..self.len() {
            self[i] -= 1.into();
        }
    }
}

impl Decrement for usize {
    fn decrement(&mut self) {
        *self -= 1;
    }
}

impl Decrement for i64 {
    fn decrement(&mut self) {
        *self -= 1;
    }
}

impl Decrement for i32 {
    fn decrement(&mut self) {
        *self -= 1;
    }
}

/// Decrements the given number or vector.
/// Requires `use lib::util::vec::*;`
///
/// # Examples
///
/// ```rust
/// use lib::i0;
/// use lib::util::vec::*;
/// let mut l = 1;
/// let mut r = 3;
/// let mut a = vec![1, 3, 2];
/// i0!(l, a);
/// assert_eq!(l, 0);
/// assert_eq!(a, [0, 2, 1]);
/// ```
#[macro_export]
macro_rules! i0 {
    ($($x:expr),*) => {
        $(
            $x.decrement();
        )*
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_srt_0() {
        let mut x = vec![1, 3, 2];
        let mut y = vec!["c", "b", "aa"];
        srt!(|i, j| x[i] < x[j], y, x);
        assert_eq!(x, [1, 2, 3]);
        assert_eq!(y, ["c", "aa", "b"]);
    }

    #[test]
    fn test_srt_1() {
        let mut x = vec![1, 3, 2];
        let mut y = vec!["c", "b", "aa"];
        srt!(x.smaller(), y, x);
        assert_eq!(x, [1, 2, 3]);
        assert_eq!(y, ["c", "aa", "b"]);
    }

    #[test]
    fn test_i0() {
        let mut n = 3;
        let mut a = vec![1, 3, 2];
        i0!(n, a);
        assert_eq!(n, 2);
        assert_eq!(a, [0, 2, 1]);
    }
}