riichi_elements/
utils.rs

1//! Collection of misc utilities...
2
3pub fn sort2<T: Ord>(a: T, b: T) -> (T, T) {
4    if a < b { (a, b) } else { (b, a) }
5}
6
7pub fn sort3<T: Ord>(a: T, b: T, c: T) -> (T, T, T) {
8    let (a1, b1) = sort2(a, b);
9    let (b2, c2) = sort2(b1, c);
10    let (a3, b3) = sort2(a1, b2);
11    (a3, b3, c2)
12}
13
14pub const fn unpack4(x: u8) -> (bool, bool, bool, bool) {
15    (
16        (x & 0b0001) > 0,
17        (x & 0b0010) > 0,
18        (x & 0b0100) > 0,
19        (x & 0b1000) > 0,
20    )
21}
22
23pub const fn pack4(a: bool, b: bool, c: bool, d: bool) -> u8 {
24    (a as u8) | ((b as u8) << 1) | ((c as u8) << 2) | ((d as u8) << 3)
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn sort3_test() {
33        for x in [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] {
34            assert_eq!(sort3(x[0], x[1], x[2]), (1, 2, 3));
35        }
36    }
37}