-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.rs
167 lines (145 loc) · 3.81 KB
/
index.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use core::iter::FusedIterator;
use core::ops::{Add, AddAssign, Sub, SubAssign};
pub(crate) struct RawIndex<const N: usize>(usize);
impl<const N: usize> Clone for RawIndex<N> {
#[inline]
#[must_use]
fn clone(&self) -> Self {
self.0.into()
}
}
impl<const N: usize> Copy for RawIndex<N> {}
impl<const N: usize> RawIndex<N> {
#[inline]
#[must_use]
pub(crate) fn to_usize(self) -> usize {
self.0
}
/// Increments the index and returns a copy of the index /before/ incrementing.
#[inline]
#[must_use]
pub(crate) fn inc(&mut self) -> Self {
let old = *self;
self.0 = if self.0 == N - 1 { 0 } else { self.0 + 1 };
old
}
/// Decrements the index and returns a copy of the new value.
#[inline]
#[must_use]
pub(crate) fn dec(&mut self) -> Self {
self.0 = if self.0 == 0 { N - 1 } else { self.0 - 1 };
*self
}
}
impl<const N: usize> From<usize> for RawIndex<N> {
#[inline]
#[must_use]
fn from(index: usize) -> Self {
debug_assert!(index < N);
RawIndex(index)
}
}
impl<const N: usize> PartialEq for RawIndex<N> {
#[inline]
#[must_use]
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<const N: usize> Eq for RawIndex<N> {}
impl<const N: usize> Add for RawIndex<N> {
type Output = RawIndex<N>;
#[inline]
#[must_use]
fn add(self, other: Self) -> Self::Output {
self + other.0
}
}
impl<const N: usize> Add<usize> for RawIndex<N> {
type Output = RawIndex<N>;
#[inline]
#[must_use]
fn add(self, other: usize) -> Self::Output {
let mut result = self.0 + other;
while result >= N {
result -= N;
}
result.into()
}
}
impl<const N: usize> AddAssign<usize> for RawIndex<N> {
#[inline]
fn add_assign(&mut self, other: usize) {
self.0 += other;
while self.0 >= N {
self.0 -= N;
}
}
}
impl<const N: usize> Sub for RawIndex<N> {
type Output = RawIndex<N>;
#[inline]
#[must_use]
fn sub(self, other: Self) -> Self::Output {
self - other.0
}
}
impl<const N: usize> Sub<usize> for RawIndex<N> {
type Output = RawIndex<N>;
#[inline]
#[must_use]
fn sub(self, other: usize) -> Self::Output {
let mut start = self.0;
while other > start {
start += N;
}
(start - other).into()
}
}
impl<const N: usize> SubAssign<usize> for RawIndex<N> {
#[inline]
fn sub_assign(&mut self, other: usize) {
while other > self.0 {
self.0 += N;
}
self.0 -= other;
}
}
pub(crate) struct IndexIter<const N: usize> {
pub(crate) remaining: usize,
pub(crate) left_index: RawIndex<N>,
pub(crate) right_index: RawIndex<N>,
}
impl<const N: usize> Iterator for IndexIter<N> {
type Item = RawIndex<N>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.remaining > 0 {
self.remaining -= 1;
Some(self.left_index.inc())
} else {
None
}
}
#[inline]
#[must_use]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
impl<const N: usize> DoubleEndedIterator for IndexIter<N> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.remaining > 0 {
self.remaining -= 1;
Some(self.right_index.dec())
} else {
None
}
}
}
impl<const N: usize> ExactSizeIterator for IndexIter<N> {}
impl<const N: usize> FusedIterator for IndexIter<N> {}