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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use super::*;

use std::ops::Bound;
use std::ops::Neg;
use std::ops::RangeBounds;

pub trait Int: Clone + Copy + Debug + Default + Display + Eq + FromStr + Hash + Ord
		+ IntConv + IntOps {
	type Signed: IntSigned;
	type Unsigned: IntUnsigned;
	const BITS: u32;
	const ZERO: Self;
	const ONE: Self;
	const TWO: Self;
	const THREE: Self;
	const FOUR: Self;
	const FIVE: Self;
	const SIX: Self;
	const SEVEN: Self;
	const EIGHT: Self;
	const NINE: Self;
	const MIN: Self;
	const MAX: Self;
	fn unsigned_abs (self) -> Self::Unsigned;
	fn signum (self) -> Self::Signed;

	/// Signed difference between two numbers
	///
	/// # Errors
	///
	/// Returns `Err (Overflow)` if the result can't be represented, ie if the difference is too
	/// high
	///
	fn signed_diff (self, other: Self) -> NumResult <Self::Signed>;

	/// Unsigned difference between two numbers
	///
	/// # Errors
	///
	/// Returns `Err (Overflow)` if the result can't be represented, ie if the second number is
	/// greater than the first
	///
	fn unsigned_diff (self, other: Self) -> NumResult <Self::Unsigned>;

	/// Add a signed number
	///
	/// # Errors
	///
	/// Returns `Err (Overflow)` if the result can't be represented
	///
	fn add_signed (self, other: Self::Signed) -> NumResult <Self>;

	/// Subtract a signed number
	///
	/// # Errors
	///
	/// Returns `Err (Overflow)` if the result can't be represented
	///
	fn sub_signed (self, other: Self::Signed) -> NumResult <Self>;

	/// Generic wrapper for primitive `count_ones` function.
	///
	fn gen_count_ones (self) -> u32;

	#[ inline (always) ]
	fn check_bit (self, bit: u32) -> bool {
		self & (Self::ONE << bit) != Self::ZERO
	}

	#[ inline (always) ]
	fn bit_set_assign (& mut self, bit: u32) {
		* self |= Self::ONE << bit;
	}

	#[ inline (always) ]
	fn bit_clear_assign (& mut self, bit: u32) {
		* self &= ! (Self::ONE << bit);
	}

	#[ inline (always) ]
	fn bound_start_assign (& mut self, other: Self) {
		* self = cmp::max (* self, other);
	}

	#[ inline (always) ]
	fn bound_end_assign (& mut self, other: Self) {
		* self = cmp::min (* self, other);
	}

	#[ inline (always) ]
	fn bounds_assign (& mut self, bounds: impl RangeBounds <Self>) {
		let start = match bounds.start_bound () {
			Bound::Included (& bound) => Some (bound),
			Bound::Excluded (& bound) => Some (bound + Self::ONE),
			Bound::Unbounded => None,
		};
		let end = match bounds.end_bound () {
			Bound::Included (& bound) => Some (bound),
			Bound::Excluded (& bound) => Some (bound - Self::ONE),
			Bound::Unbounded => None,
		};
		if let (Some (start), Some (end)) = (start, end) {
			assert! (start <= end);
		}
		if let Some (start) = start { self.bound_start_assign (start); }
		if let Some (end) = end { self.bound_end_assign (end); }
	}

	#[ inline (always) ]
	fn lcm (num_0: Self, num_1: Self) -> Self {
		let gcd = Self::gcd (num_0, num_1);
		num_0 * num_1 / gcd
	}

	#[ inline (always) ]
	fn gcd (mut num_0: Self, mut num_1: Self) -> Self {
		loop {
			let rem = num_0 % num_1;
			if rem == Self::ZERO { return num_1 }
			num_0 = num_1;
			num_1 = rem;
		}
	}

}

macro_rules! int_impl {
	( $signed:ident , $unsigned:ident, $bits:literal ) => {

		impl Int for $signed {

			type Signed = $signed;
			type Unsigned = $unsigned;

			const BITS: u32 = $signed::BITS;

			const ZERO: $signed = 0;
			const ONE: $signed = 1;
			const TWO: $signed = 2;
			const THREE: $signed = 3;
			const FOUR: $signed = 4;
			const FIVE: $signed = 5;
			const SIX: $signed = 6;
			const SEVEN: $signed = 7;
			const EIGHT: $signed = 8;
			const NINE: $signed = 9;

			const MIN: $signed = $signed::MIN;
			const MAX: $signed = $signed::MAX;

			#[ inline (always) ]
			fn unsigned_abs (self) -> $unsigned { $signed::unsigned_abs (self) }

			#[ inline (always) ]
			fn signum (self) -> $signed { $signed::signum (self) }

			#[ inline (always) ]
			fn signed_diff (self, other: Self) -> NumResult <$signed> {
				$signed::checked_sub (self, other).ok_or (Overflow)
			}

			#[ inline (always) ]
			fn unsigned_diff (self, other: Self) -> NumResult <$unsigned> {
				(other <= self).then (|| $signed::abs_diff (self, other)).ok_or (Overflow)
			}

			#[ inline (always) ]
			fn add_signed (self, other: $signed) -> NumResult <$signed> {
				$signed::checked_add (self, other).ok_or (Overflow)
			}

			#[ inline (always) ]
			fn sub_signed (self, other: $signed) -> NumResult <$signed> {
				$signed::checked_sub (self, other).ok_or (Overflow)
			}

			#[ inline (always) ]
			fn gen_count_ones (self) -> u32 {
				self.count_ones ()
			}

		}

		impl Int for $unsigned {

			type Signed = $signed;
			type Unsigned = $unsigned;

			const BITS: u32 = $signed::BITS;
			const ZERO: $unsigned = 0;
			const ONE: $unsigned = 1;
			const TWO: $unsigned = 2;
			const THREE: $unsigned = 3;
			const FOUR: $unsigned = 4;
			const FIVE: $unsigned = 5;
			const SIX: $unsigned = 6;
			const SEVEN: $unsigned = 7;
			const EIGHT: $unsigned = 8;
			const NINE: $unsigned = 9;
			const MIN: $unsigned = $unsigned::MIN;
			const MAX: $unsigned = $unsigned::MAX;

			#[ inline (always) ]
			fn unsigned_abs (self) -> $unsigned { self }

			#[ inline (always) ]
			fn signum (self) -> $signed {
				if self > 0 { Self::Signed::ONE } else { Self::Signed::ZERO }
			}

			#[ inline (always) ]
			fn signed_diff (self, other: Self) -> NumResult <$signed> {
				if other < self {
					(self - other).try_into ().ok ().ok_or (Overflow)
				} else {
					(other - self).try_into ().map ($signed::neg).ok ().ok_or (Overflow)
				}
			}

			#[ inline (always) ]
			fn unsigned_diff (self, other: Self) -> NumResult <$unsigned> {
				$unsigned::checked_sub (self, other).ok_or (Overflow)
			}

			#[ inline (always) ]
			fn add_signed (self, other: $signed) -> NumResult <$unsigned> {
				if other >= Self::Signed::ZERO {
					$unsigned::checked_add (self, $signed::unsigned_abs (other)).ok_or (Overflow)
				} else {
					$unsigned::checked_sub (self, $signed::unsigned_abs (other)).ok_or (Overflow)
				}
			}

			#[ inline (always) ]
			fn sub_signed (self, other: $signed) -> NumResult <$unsigned> {
				if other >= Self::Signed::ZERO {
					$unsigned::checked_sub (self, $signed::unsigned_abs (other)).ok_or (Overflow)
				} else {
					$unsigned::checked_add (self, $signed::unsigned_abs (other)).ok_or (Overflow)
				}
			}

			#[ inline (always) ]
			fn gen_count_ones (self) -> u32 {
				self.count_ones ()
			}

		}

		impl IntOpsSafe for $signed {

			#[ inline (always) ]
			fn safe_add (self, arg: $signed) -> $signed {
				$signed::checked_add (self, arg).unwrap ()
			}

			#[ inline (always) ]
			fn safe_sub (self, arg: $signed) -> $signed {
				$signed::checked_sub (self, arg).unwrap ()
			}

		}

		impl IntOpsSafe for $unsigned {

			#[ inline (always) ]
			fn safe_add (self, arg: $unsigned) -> $unsigned {
				$unsigned::checked_add (self, arg).unwrap ()
			}

			#[ inline (always) ]
			fn safe_sub (self, arg: $unsigned) -> $unsigned {
				$unsigned::checked_sub (self, arg).unwrap ()
			}

		}

		impl IntSigned for $signed {
			const NEG_ONE: $signed = Self::ZERO - Self::ONE;
		}

		impl IntUnsigned for $unsigned {}

		impl IntSized <$bits> for $signed {}
		impl IntSized <$bits> for $unsigned {}

	};

}

int_impl! (i8, u8, 8);
int_impl! (i16, u16, 16);
int_impl! (i32, u32, 32);
int_impl! (i64, u64, 64);
int_impl! (i128, u128, 128);
int_impl! (isize, usize, 128);

pub trait IntSigned: Int + Neg <Output = Self> {
	const NEG_ONE: Self::Signed;
}

pub trait IntUnsigned: Int {}

pub trait IntSized <const BITS: usize>: Int {}