Skip to main content

unbanished_game/behaviour/ai/
qsort.rs

1//! The C runtime's `qsort`, whose unstable order the game's sorting depends on.
2
3use super::*;
4
5/// The MSVC CRT `qsort` inlined for candidates, ordering by priority, highest first (comparator
6/// `b.priority - a.priority`). Median of three, short sort (selection of the maximum) for 8 or
7/// fewer elements, smaller partition first.
8///
9/// Original: `FUN_140030770`.
10pub fn crt_qsort(a: &mut [Candidate]) {
11    crt_qsort_by(a, |a: &Candidate, b: &Candidate| b.1 - a.1);
12}
13
14/// The Microsoft C runtime's `qsort` with a `less` comparator, as the game's templated copies use
15/// it: the same algorithm as [`crt_qsort_by`], with `cmp(a, b) > 0` being `less(b, a)` and
16/// equality neither way.
17///
18/// Original: `FUN_14002c9d0`, `FUN_140030c00`, `FUN_1400220f0`, `FUN_14001f120`, `FUN_140015c10`,
19/// `FUN_140015f10`, `FUN_140016ce0`, `FUN_140016fe0`.
20pub fn crt_qsort_less<T>(a: &mut [T], less: impl Fn(&T, &T) -> bool) {
21    crt_qsort_by(a, |x, y| {
22        if less(y, x) {
23            1
24        } else if less(x, y) {
25            -1
26        } else {
27            0
28        }
29    });
30}
31
32/// The same with a three-way comparator.
33pub fn crt_qsort_by<T>(a: &mut [T], cmp: impl Fn(&T, &T) -> i32) {
34    const CUTOFF: usize = 8;
35    if a.len() < 2 {
36        return;
37    }
38    let mut stack: Vec<(usize, usize)> = Vec::new();
39    let (mut lo, mut hi) = (0usize, a.len() - 1);
40    loop {
41        let size = hi - lo + 1;
42        if size <= CUTOFF {
43            // shortsort
44            let mut h = hi;
45            while h > lo {
46                let mut max = lo;
47                for p in lo + 1..=h {
48                    if cmp(&a[p], &a[max]) > 0 {
49                        max = p;
50                    }
51                }
52                a.swap(max, h);
53                h -= 1;
54            }
55        } else {
56            let mut mid = lo + size / 2;
57            if cmp(&a[lo], &a[mid]) > 0 {
58                a.swap(lo, mid);
59            }
60            if cmp(&a[lo], &a[hi]) > 0 {
61                a.swap(lo, hi);
62            }
63            if cmp(&a[mid], &a[hi]) > 0 {
64                a.swap(mid, hi);
65            }
66            let (mut loguy, mut higuy) = (lo as isize, hi as isize);
67            let m = |mid: usize| mid as isize;
68            loop {
69                if m(mid) > loguy {
70                    loop {
71                        loguy += 1;
72                        if !(loguy < m(mid) && cmp(&a[loguy as usize], &a[mid]) <= 0) {
73                            break;
74                        }
75                    }
76                }
77                if m(mid) <= loguy {
78                    loop {
79                        loguy += 1;
80                        if !(loguy <= hi as isize && cmp(&a[loguy as usize], &a[mid]) <= 0) {
81                            break;
82                        }
83                    }
84                }
85                loop {
86                    higuy -= 1;
87                    if !(higuy > m(mid) && cmp(&a[higuy as usize], &a[mid]) > 0) {
88                        break;
89                    }
90                }
91                if higuy < loguy {
92                    break;
93                }
94                a.swap(loguy as usize, higuy as usize);
95                if m(mid) == higuy {
96                    mid = loguy as usize;
97                }
98            }
99            higuy += 1;
100            if m(mid) < higuy {
101                loop {
102                    higuy -= 1;
103                    if !(higuy > m(mid) && cmp(&a[higuy as usize], &a[mid]) == 0) {
104                        break;
105                    }
106                }
107            }
108            if m(mid) >= higuy {
109                loop {
110                    higuy -= 1;
111                    if !(higuy > lo as isize && cmp(&a[higuy as usize], &a[mid]) == 0) {
112                        break;
113                    }
114                }
115            }
116            if higuy - lo as isize >= hi as isize - loguy {
117                if (lo as isize) < higuy {
118                    stack.push((lo, higuy as usize));
119                }
120                if loguy < hi as isize {
121                    lo = loguy as usize;
122                    continue;
123                }
124            } else {
125                if loguy < hi as isize {
126                    stack.push((loguy as usize, hi));
127                }
128                if (lo as isize) < higuy {
129                    hi = higuy as usize;
130                    continue;
131                }
132            }
133        }
134        match stack.pop() {
135            Some((l, h)) => {
136                lo = l;
137                hi = h;
138            }
139            None => return,
140        }
141    }
142}