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
use super::super::math::pre::*;

pub trait FasterIndex<T> {
	fn at<I>(&self, idx: I) -> &T
	where
		usize: Cast<I>;
	fn at_mut<I>(&mut self, idx: I) -> &mut T
	where
		usize: Cast<I>;
}
macro_rules! impl_faster_index {
	() => {
		fn at<I>(&self, idx: I) -> &T
		where
			usize: Cast<I>,
		{
			let i = usize(idx);
			#[cfg(debug_assertions)]
			{
				&self[i]
			}
			#[cfg(not(debug_assertions))]
			{
				unsafe { self.get_unchecked(i) }
			}
		}
		fn at_mut<I>(&mut self, idx: I) -> &mut T
		where
			usize: Cast<I>,
		{
			let i = usize(idx);
			#[cfg(debug_assertions)]
			{
				&mut self[i]
			}
			#[cfg(not(debug_assertions))]
			{
				unsafe { self.get_unchecked_mut(i) }
			}
		}
	};
}
impl<T> FasterIndex<T> for Vec<T> {
	impl_faster_index!();
}
impl<T, const L: usize> FasterIndex<T> for [T; L] {
	impl_faster_index!();
}
impl<T> FasterIndex<T> for [T] {
	impl_faster_index!();
}

pub trait LastIdx {
	fn last_idx(&self) -> usize;
}
impl LastIdx for &str {
	fn last_idx(&self) -> usize {
		self.len().max(1) - 1
	}
}
impl<T> LastIdx for &[T] {
	fn last_idx(&self) -> usize {
		self.len().max(1) - 1
	}
}
impl<T> LastIdx for [T] {
	fn last_idx(&self) -> usize {
		(&self).last_idx()
	}
}
impl<T> LastIdx for Vec<T> {
	fn last_idx(&self) -> usize {
		self.as_slice().last_idx()
	}
}