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
//! Sub-view of a Grid3.

use crate::{
    range::Range0To,
    grid3::*,
};
use mint::Vector3;
use std::{
    ops::{RangeBounds, Bound},
    fmt::Debug,
};


/// Sub-view of a Grid3.
///
/// The valid coordinates in this grid are a subset
/// of the valid coordinates in the inner grid.
pub struct Grid3Slice<G, X, Y, Z> 
where
    G: Grid3,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    inner: G,
    x_bound: X,
    y_bound: Y,
    z_bound: Z,
}

/// Verify that a is more strict than b.
fn more_strict(a: impl RangeBounds<i32>, b: impl RangeBounds<i32>) -> bool {
    
    fn lower_inclusive(bound: Bound<&i32>) -> Option<i32> {
        match bound {
            Bound::Included(&i) => Some(i),
            Bound::Excluded(&i) => Some(i + 1),
            Bound::Unbounded   => None,
        }
    }
    
    fn upper_inclusive(bound: Bound<&i32>) -> Option<i32> {
        match bound {
            Bound::Included(&i) => Some(i),
            Bound::Excluded(&i) => Some(i - 1),
            Bound::Unbounded   => None,
        }
    }
    
    let lower_ok = match (
        lower_inclusive(a.start_bound()), 
        lower_inclusive(b.start_bound()),
    ) {
        (Some(i1), Some(i2)) => i1 >= i2,
        (Some(_), None)      => true,
        (None, Some(_))      => false,
        (None, None)         => true,
    };
    if !lower_ok { return false; }
    
    let upper_ok = match (
        upper_inclusive(a.end_bound()), 
        upper_inclusive(b.end_bound()),
    ) {
        (Some(i1), Some(i2)) => i1 <= i2,
        (Some(_), None)      => true,
        (None, Some(_))      => false,
        (None, None)         => true,
    };
    if !upper_ok { return false; }
    
    true
}

impl<G, X, Y, Z> Grid3Slice<G, X, Y, Z>
where
    G: Grid3,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    /// Fails if the new bounds are not a subset of the old ones.
    pub fn try_new(inner: G, new_x: X, new_y: Y, new_z: Z) -> Result<Self, G>
    {
        if more_strict(new_x.clone(), inner.x_bound()) 
            && more_strict(new_y.clone(), inner.y_bound())
            && more_strict(new_z.clone(), inner.z_bound())
        {
            Ok(Grid3Slice {
                inner,
                x_bound: new_x,
                y_bound: new_y,
                z_bound: new_z,
            })
        } else {
            Err(inner)
        }
    }
    
    /// Panics if the new bounds are not a subset of the old ones.
    pub fn new(inner: G, new_x: X, new_y: Y, new_z: Z) -> Self
    where
        X: Debug,
        Y: Debug,
        Z: Debug,
        <G as Grid3>::XBound: Debug,
        <G as Grid3>::YBound: Debug,
        <G as Grid3>::ZBound: Debug,
    {
        if more_strict(new_x.clone(), inner.x_bound()) 
            && more_strict(new_y.clone(), inner.y_bound())
            && more_strict(new_z.clone(), inner.z_bound())
        {
            Grid3Slice {
                inner,
                x_bound: new_x,
                y_bound: new_y,
                z_bound: new_z,
            }
        } else {
            panic!("new bounds are not a subset of old bounds, new={:?}, old={:?}",
                (new_x, new_y, new_z),
                (inner.x_bound(), inner.y_bound(), inner.z_bound()));
        }
    }
}

impl<G, X, Y, Z> Grid3 for Grid3Slice<G, X, Y, Z>
where
    G: Grid3,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    type Item = <G as Grid3>::Item;
    type XBound = X;
    type YBound = Y;
    type ZBound = Z;
    
    fn x_bound(&self) -> Self::XBound { self.x_bound.clone() }
    fn y_bound(&self) -> Self::YBound { self.y_bound.clone() }
    fn z_bound(&self) -> Self::ZBound { self.z_bound.clone() }
}

impl<G> Grid3Len for Grid3Slice<G, Range0To, Range0To, Range0To> 
where
    G: Grid3
{}

impl<G, X, Y, Z> Grid3Get for Grid3Slice<G, X, Y, Z>
where
    G: Grid3 + Grid3Get,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    fn get<I>(&self, coord: I) -> Self::Item
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        match self.try_get(coord) {
            Some(item) => item,
            None => panic!("invalid index {:?}", coord),
        }
    }
        
    fn try_get<I>(&self, coord: I) -> Option<Self::Item>
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            Some(self.inner.get(coord))
        } else {
            None
        }
    }
}

impl<G, X, Y, Z> Grid3Set for Grid3Slice<G, X, Y, Z>
where
    G: Grid3 + Grid3Set,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    fn set<I>(&mut self, coord: I, elem: Self::Item)
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        match self.try_set(coord, elem) {
            Ok(_) => {},
            Err(_) => panic!("invalid index {:?}", coord),
        }
    }
        
    fn try_set<I>(&mut self, coord: I, elem: Self::Item) -> Result<(), Self::Item> 
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            self.inner.set(coord, elem);
            Ok({})
        } else {
            Err(elem)
        }
    }
}

impl<G, X, Y, Z> Grid3Ref for Grid3Slice<G, X, Y, Z>
where
    G: Grid3 + Grid3Ref,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    fn idx<I>(&self, coord: I) -> &Self::Item
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        match self.try_idx(coord) {
            Some(item) => item,
            None => panic!("invalid index {:?}", coord),
        }
    }
    
    fn try_idx<I>(&self, coord: I) -> Option<&Self::Item>
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            Some(self.inner.idx(coord))
        } else {
            None
        }
    }
}

impl<G, X, Y, Z> Grid3Mut for Grid3Slice<G, X, Y, Z>
where
    G: Grid3 + Grid3Mut,
    X: RangeBounds<i32> + Clone,
    Y: RangeBounds<i32> + Clone,
    Z: RangeBounds<i32> + Clone,
{
    fn midx<I>(&mut self, coord: I) -> &mut Self::Item
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        match self.try_midx(coord) {
            Some(item) => item,
            None => panic!("invalid index {:?}", coord),
        }
    }
    
    fn try_midx<I>(&mut self, coord: I) -> Option<&mut Self::Item>
    where
        I: Into<Vector3<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            Some(self.inner.midx(coord))
        } else {
            None
        }
    }
}