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
#![no_std]
#![feature(const_fn, const_if_match, const_loop)]

/// This macro takes the following parameters in the order they're listed:
/// type to sort, index to start at, index to end at, total array length, and name of existing
/// const / static array variable (or directly-passed "anonymous" array).
#[macro_export]
macro_rules! staticsort {
  ($type:ty, $low:expr, $high:expr, $len:expr, $values:expr) => {{
    match $len {
      0 => $values,
      _ => {
        #[inline]
        const fn static_sort(
          mut values: [$type; $len],
          mut low: isize,
          mut high: isize,
        ) -> [$type; $len]
        {
          if high - low <= 0 {
            return values;
          }
          loop {
            let mut i = low;
            let mut j = high;
            let p = values[(low + ((high - low) >> 1)) as usize];
            loop {
              while p > values[i as usize] {
                i += 1;
              }
              while p < values[j as usize] {
                j -= 1;
              }
              if i <= j {
                if i != j {
                  let q = values[i as usize];
                  values[i as usize] = values[j as usize];
                  values[j as usize] = q;
                }
                i += 1;
                j -= 1;
              }
              if i > j {
                break;
              }
            }
            if j - low < high - i {
              if low < j {
                values = static_sort(values, low, j);
              }
              low = i;
            } else {
              if i < high {
                values = static_sort(values, i, high)
              }
              high = j;
            }
            if low >= high {
              break;
            }
          }
          values
        }
        static_sort($values, $low, $high)
      }
    }
  };};
}