Newer
Older
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! The SlotRange struct which succinctly handles the ten values that
//! represent all sub ranges between 0 and 3 inclusive.
use rstd::{result, ops::Add, convert::{TryFrom, TryInto}};
use sr_primitives::traits::CheckedSub;
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
/// Total number of possible sub ranges of slots.
pub const SLOT_RANGE_COUNT: usize = 10;
/// A compactly represented sub-range from the series (0, 1, 2, 3).
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
#[repr(u8)]
pub enum SlotRange {
/// Sub range from index 0 to index 0 inclusive.
ZeroZero = 0,
/// Sub range from index 0 to index 1 inclusive.
ZeroOne = 1,
/// Sub range from index 0 to index 2 inclusive.
ZeroTwo = 2,
/// Sub range from index 0 to index 3 inclusive.
ZeroThree = 3,
/// Sub range from index 1 to index 1 inclusive.
OneOne = 4,
/// Sub range from index 1 to index 2 inclusive.
OneTwo = 5,
/// Sub range from index 1 to index 3 inclusive.
OneThree = 6,
/// Sub range from index 2 to index 2 inclusive.
TwoTwo = 7,
/// Sub range from index 2 to index 3 inclusive.
TwoThree = 8,
/// Sub range from index 3 to index 3 inclusive.
ThreeThree = 9, // == SLOT_RANGE_COUNT - 1
}
#[cfg(feature = "std")]
impl std::fmt::Debug for SlotRange {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
let p = self.as_pair();
write!(fmt, "[{}..{}]", p.0, p.1)
}
}
impl SlotRange {
pub fn new_bounded<
Index: Add<Output=Index> + CheckedSub + Copy + Ord + From<u32> + TryInto<u32>
>(
initial: Index,
first: Index,
last: Index
) -> result::Result<Self, &'static str> {
if first > last || first < initial || last > initial + 3.into() {
return Err("Invalid range for this auction")
}
let count: u32 = last.checked_sub(&first)
.ok_or("range ends before it begins")?
.try_into()
.map_err(|_| "range too big")?;
let first: u32 = first.checked_sub(&initial)
.ok_or("range begins too early")?
.try_into()
.map_err(|_| "start too far")?;
match first {
0 => match count {
0 => Some(SlotRange::ZeroZero),
1 => Some(SlotRange::ZeroOne),
2 => Some(SlotRange::ZeroTwo),
3 => Some(SlotRange::ZeroThree),
_ => None,
},
1 => match count {
0 => Some(SlotRange::OneOne),
1 => Some(SlotRange::OneTwo),
2 => Some(SlotRange::OneThree),
_ => None
},
2 => match count { 0 => Some(SlotRange::TwoTwo), 1 => Some(SlotRange::TwoThree), _ => None },
3 => match count { 0 => Some(SlotRange::ThreeThree), _ => None },
_ => return Err("range begins too late"),
}.ok_or("range ends too late")
}
pub fn as_pair(&self) -> (u8, u8) {
match self {
SlotRange::ZeroZero => (0, 0),
SlotRange::ZeroOne => (0, 1),
SlotRange::ZeroTwo => (0, 2),
SlotRange::ZeroThree => (0, 3),
SlotRange::OneOne => (1, 1),
SlotRange::OneTwo => (1, 2),
SlotRange::OneThree => (1, 3),
SlotRange::TwoTwo => (2, 2),
SlotRange::TwoThree => (2, 3),
SlotRange::ThreeThree => (3, 3),
}
}
pub fn intersects(&self, other: SlotRange) -> bool {
let a = self.as_pair();
let b = other.as_pair();
b.0 <= a.1 && a.0 <= b.1
// == !(b.0 > a.1 || a.0 > b.1)
}
pub fn len(&self) -> usize {
match self {
SlotRange::ZeroZero => 1,
SlotRange::ZeroOne => 2,
SlotRange::ZeroTwo => 3,
SlotRange::ZeroThree => 4,
SlotRange::OneOne => 1,
SlotRange::OneTwo => 2,
SlotRange::OneThree => 3,
SlotRange::TwoTwo => 1,
SlotRange::TwoThree => 2,
SlotRange::ThreeThree => 1,
}
}
}
impl TryFrom<usize> for SlotRange {
type Error = ();
fn try_from(x: usize) -> Result<SlotRange, ()> {
Ok(match x {
0 => SlotRange::ZeroZero,
1 => SlotRange::ZeroOne,
2 => SlotRange::ZeroTwo,
3 => SlotRange::ZeroThree,
4 => SlotRange::OneOne,
5 => SlotRange::OneTwo,
6 => SlotRange::OneThree,
7 => SlotRange::TwoTwo,
8 => SlotRange::TwoThree,
9 => SlotRange::ThreeThree,
_ => return Err(()),
})
}
}