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
use std::marker::PhantomData;
use std::ops::Range;
use num::One;
use crate::idx::Idx;
use crate::qty::{MocQty, Hpx, Time};
use crate::elem::{
cell::{Cell, MocCell},
cellrange::CellRange,
cellcellrange::{CellOrCellRange, MocCellOrCellRange}
};
pub type HpxRange<T> = MocRange<T, Hpx<T>>;
pub type TimeRange<T> = MocRange<T, Time<T>>;
#[repr(transparent)] #[derive(Debug)]
pub struct MocRange<T: Idx, Q: MocQty<T>>(pub Range<T>, PhantomData<Q>);
impl<T: Idx, Q: MocQty<T>> Clone for MocRange<T, Q> {
fn clone(&self) -> MocRange<T, Q> {
MocRange(self.0.clone(), PhantomData)
}
}
impl<T: Idx, Q: MocQty<T>> MocRange<T, Q> {
pub fn new(start: T, end: T) -> MocRange<T, Q> {
MocRange(Range { start, end }, PhantomData)
}
pub fn next_cell_with_knowledge(&mut self, depth_max: u8, shift_dd: usize, range_len_min: T, mask: T) -> Option<MocCell<T, Q>> {
if self.0.end <= self.0.start {
None
} else {
let len = self.0.end - self.0.start;
if len == range_len_min || self.0.start & mask != T::zero() {
let c = Cell::new(depth_max, self.0.start >> shift_dd).into();
self.0.start += range_len_min;
Some(c)
} else {
let dd_max_from_len = Q::delta_depth_max_from_n_bits_unchecked(T::N_BITS - 1 - len.leading_zeros() as u8);
let dd_max_from_low = Q::delta_depth_max_from_n_bits_unchecked(self.0.start.trailing_zeros() as u8);
let delta_depth = dd_max_from_len.min(dd_max_from_low).min(Q::MAX_DEPTH);
let delta_depth_shift = Q::shift(delta_depth) as usize;
let c = Cell::new(Q::MAX_DEPTH - delta_depth, self.0.start >> delta_depth_shift).into();
self.0.start += T::one() << delta_depth_shift;
Some(c)
}
}
}
}
impl<T: Idx, Q: MocQty<T>> From<Range<T>> for MocRange<T, Q> {
fn from(range: Range<T>) -> Self {
MocRange(range, PhantomData)
}
}
impl<T: Idx, Q: MocQty<T>> From<&Range<T>> for MocRange<T, Q> {
fn from(range: &Range<T>) -> Self {
MocRange(range.clone(), PhantomData)
}
}
impl<T: Idx, Q: MocQty<T>> From<(u8, T)> for MocRange<T, Q> {
fn from((depth, ipix): (u8, T)) -> Self {
let tdd = Q::shift_from_depth_max(depth) as u32;
let range = Range {
start: ipix.unsigned_shl(tdd),
end: (ipix + One::one()).unsigned_shl(tdd),
};
Self::from(range)
}
}
impl<T: Idx, Q: MocQty<T>> From<Cell<T>> for MocRange<T, Q> {
fn from(cell: Cell<T>) -> Self {
Self::from((cell.depth, cell.idx))
}
}
impl<T: Idx, Q: MocQty<T>> From<&Cell<T>> for MocRange<T, Q> {
fn from(cell: &Cell<T>) -> Self {
Self::from((cell.depth, cell.idx))
}
}
impl<T: Idx, Q: MocQty<T>> From<MocCell<T, Q>> for MocRange<T, Q> {
fn from(cell: MocCell<T, Q>) -> Self {
Self::from(cell.0)
}
}
impl<T: Idx, Q: MocQty<T>> From<&MocCell<T, Q>> for MocRange<T, Q> {
fn from(cell: &MocCell<T, Q>) -> Self {
Self::from(&cell.0)
}
}
impl<T: Idx, Q: MocQty<T>> From<CellRange<T>> for MocRange<T, Q> {
fn from(cellrange: CellRange<T>) -> Self {
let tdd = Q::shift_from_depth_max(cellrange.depth) as u32;
let range = Range {
start: cellrange.range.start.unsigned_shl(tdd),
end: cellrange.range.end.unsigned_shl(tdd),
};
Self::from(range)
}
}
impl<T: Idx, Q: MocQty<T>> From<&CellRange<T>> for MocRange<T, Q> {
fn from(cellrange: &CellRange<T>) -> Self {
let tdd = Q::shift_from_depth_max(cellrange.depth) as u32;
let range = Range {
start: cellrange.range.start.unsigned_shl(tdd),
end: cellrange.range.end.unsigned_shl(tdd),
};
Self::from(range)
}
}
impl<T: Idx, Q: MocQty<T>> From<CellOrCellRange<T>> for MocRange<T, Q> {
fn from(cellcellrange: CellOrCellRange<T>) -> Self {
match cellcellrange {
CellOrCellRange::Cell(cell) => Self::from(cell),
CellOrCellRange::CellRange(cellrange) => Self::from(cellrange),
}
}
}
impl<T: Idx, Q: MocQty<T>> From<&CellOrCellRange<T>> for MocRange<T, Q> {
fn from(cellcellrange: &CellOrCellRange<T>) -> Self {
match cellcellrange {
CellOrCellRange::Cell(cell) => Self::from(cell),
CellOrCellRange::CellRange(cellrange) => Self::from(cellrange),
}
}
}
impl<T: Idx, Q: MocQty<T>> From<MocCellOrCellRange<T, Q>> for MocRange<T, Q> {
fn from(moccellcellrange: MocCellOrCellRange<T, Q>) -> Self {
match moccellcellrange {
MocCellOrCellRange::MocCell(mocell) => Self::from(mocell.0),
MocCellOrCellRange::MocCellRange(mocellrange) => Self::from(mocellrange.0),
}
}
}
impl<T: Idx, Q: MocQty<T>> Iterator for MocRange<T, Q> {
type Item = MocCell<T, Q>;
fn next(&mut self) -> Option<Self::Item> {
if self.0.end <= self.0.start {
None
} else {
let len = self.0.end - self.0.start;
let dd_max_from_len = Q::delta_depth_max_from_n_bits_unchecked(T::N_BITS - 1 - len.leading_zeros() as u8);
let dd_max_from_low = Q::delta_depth_max_from_n_bits_unchecked(self.0.start.trailing_zeros() as u8);
let delta_depth = dd_max_from_len.min(dd_max_from_low).min(Q::MAX_DEPTH);
let delta_depth_shift = Q::shift(delta_depth) as usize;
let c = Cell::new(Q::MAX_DEPTH - delta_depth, self.0.start >> delta_depth_shift).into();
self.0.start += T::one() << delta_depth_shift;
Some(c)
}
}
}