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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
pub trait Zero {
    fn zero() -> Self;
}

pub trait One {
    fn one() -> Self;
}

macro_rules! zero {
    ($($t:ty),*) => {
        $(
        impl Zero for $t {
    fn zero() -> Self {
        0 as $t
    }
        })*
    };
}

macro_rules! one {
    ($($t:ty),*) => {
        $(
        impl One for $t {
    fn one() -> Self {
        1 as $t
    }
        })*
    };
}

zero!(isize, i8, i16, i32, i64, i128, usize, u8, u16, u32, u64, u128, f32, f64);
one!(isize, i8, i16, i32, i64, i128, usize, u8, u16, u32, u64, u128, f32, f64);

pub trait Gcd {
    fn gcd(a: Self, b: Self) -> Self;
    fn lcm(a: Self, b: Self) -> Self;
}

// NOTE(lubo): adapted https://rosettacode.org/wiki/Least_common_multiple#Rust
macro_rules! gcd {
    ($($t:ty),*) => {
        $(
        impl Gcd for $t {
            fn gcd(a: Self, b: Self) -> Self {
                match ((a, b), (a & 1, b & 1)) {
                    ((x, y), _) if x == y => y,
                    ((0, x), _) | ((x, 0), _) => x,
                    ((x, y), (0, 1)) | ((y, x), (1, 0)) => Self::gcd(x >> 1, y),
                    ((x, y), (0, 0)) => Self::gcd(x >> 1, y >> 1) << 1,
                    ((x, y), (1, 1)) => {
                        let (x, y) = (std::cmp::min(x, y), std::cmp::max(x, y));
                        Self::gcd((y - x) >> 1, x)
                    }
                    _ => unreachable!(),
                }
            }
            fn lcm(a: Self, b: Self) -> Self {
                a * (b / Self::gcd(a, b))
            }
        })*
    };
}

gcd!(usize, i32, i64);

pub trait AbsoluteValue
where
    Self: Sized,
{
    fn abs(&self) -> Option<Self>;
}

macro_rules! checked_absolute_value {
    ($($t:ty),*) => {
        $(
        impl AbsoluteValue for $t {
    fn abs(&self) -> Option<Self> {
        self.checked_abs()
    }
        })*
    };
}

checked_absolute_value!(i32);

macro_rules! identity_absolute_value {
    ($($t:ty),*) => {
        $(
impl AbsoluteValue for $t {
    fn abs(&self) -> Option<Self> {
        Some(*self)
    }
        })*
    };
}

identity_absolute_value!(usize);

pub trait InclusiveMin<T> {
    fn inclusive_min(&self) -> &T;
}
pub trait InclusiveMax<T> {
    fn inclusive_max(&self) -> &T;
}
pub trait ExclusiveMax<T> {
    fn exclusive_max(&self) -> &T;
}


pub fn triangle_numbers(n: i32) -> i32 {
    // n * (n + 1) / 2
    if n & 0b1 > 0 {
        n * ((n + 1) / 2)
    } else {
        (n / 2) * (n + 1)
    }
}

#[cfg(test)]
mod tests {
    use crate::interval::Interval;

    #[test]
    fn interval_overlap_abab() {
        let a = 0..2;
        let b = 1..3;
        assert_eq!(a.interval_union(&b), Some(0..3));
        assert_eq!(b.interval_union(&a), Some(0..3));
        assert_eq!(a.interval_intersection(&b), Some(1..2));
        assert_eq!(b.interval_intersection(&a), Some(1..2));
    }
    #[test]
    fn interval_overlap_abba() {
        let a = 0..3;
        let b = 1..2;
        assert_eq!(a.interval_union(&b), Some(0..3));
        assert_eq!(b.interval_union(&a), Some(0..3));
        assert_eq!(a.interval_intersection(&b), Some(1..2));
        assert_eq!(b.interval_intersection(&a), Some(1..2));
    }
    #[test]
    fn interval_overlap_aabb() {
        let a = 0..1;
        let b = 2..3;
        assert_eq!(a.interval_union(&b), None);
        assert_eq!(b.interval_union(&a), None);
        assert_eq!(a.interval_intersection(&b), None);
        assert_eq!(b.interval_intersection(&a), None);
    }
    #[test]
    fn interval_overlap_abx() {
        let a = 0..2;
        let b = 1..2;
        assert_eq!(a.interval_union(&b), Some(0..2));
        assert_eq!(b.interval_union(&a), Some(0..2));
        assert_eq!(a.interval_intersection(&b), Some(1..2));
        assert_eq!(b.interval_intersection(&a), Some(1..2));
    }
    #[test]
    fn interval_overlap_xab() {
        let a = 0..3;
        let b = 0..2;
        assert_eq!(a.interval_union(&b), Some(0..3));
        assert_eq!(b.interval_union(&a), Some(0..3));
        assert_eq!(a.interval_intersection(&b), Some(0..2));
        assert_eq!(b.interval_intersection(&a), Some(0..2));
    }
    #[test]
    fn interval_overlap_axb() {
        let a = 0..2;
        let b = 2..3;
        assert_eq!(a.interval_union(&b), Some(0..3));
        assert_eq!(b.interval_union(&a), Some(0..3));

        assert_eq!(a.interval_intersection(&b), None);
        assert_eq!(b.interval_intersection(&a), None);
        // assert_eq!(a.interval_intersection(&b), Some(2..2));
        // assert_eq!(b.interval_intersection(&a), Some(2..2));
        // assert!(a.interval_intersection(&b).is_none() || a.interval_intersection(&b) == Some(2..2));
        // assert!(b.interval_intersection(&a).is_none() || b.interval_intersection(&a) == Some(2..2));
    }
}