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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! Two-dimensional data grid.

/// Combinators.
pub mod combinate;

/// Implementations.
pub mod backends;

use crate::{
    range::{
        Range0To,
        RangeBoundsTimes,
        RangeBoundsPlus,
        BoundRange,
    },
};
use mint::Vector2;
use std::{
    ops::RangeBounds,
    fmt::Debug,
};

/// Allocate a grid on the heap.
pub fn alloc<I, T>(x_len: i32, y_len: i32, startval: T) -> backends::heap::ArrayGrid2<T>
where
    T: Clone
{
    backends::heap::ArrayGrid2::broadcast(x_len, y_len, startval)
}

/// Allocate a grid on the heap, populate with a function.
pub fn alloc_gen<I, T, F>(x_len: i32, y_len: i32, generator: F) -> backends::heap::ArrayGrid2<T>
where
    I: From<Vector2<i32>>,
    F: FnMut(I) -> T,
{
    backends::heap::ArrayGrid2::new(x_len, y_len, generator)
}

/// Inline 3x3 array grid.
pub fn array3x3<I, T>(startval: T) -> backends::inline3x3::Inline3x3Grid<T>
where
    T: Clone
{
    backends::inline3x3::Inline3x3Grid::broadcast(startval)
}

/// Inline 3x3 array grid, populate with a function.
pub fn array3x3_gen<I, T, F>(generator: F) -> backends::inline3x3::Inline3x3Grid<T>
where
    I: From<Vector2<i32>>,
    F: FnMut(I) -> T,
{
    backends::inline3x3::Inline3x3Grid::new(generator)
}

/// Represent a coord → Item function as a grid.
pub fn value_fn<I, T, F>(f: F) -> backends::kolmo::KolmoGrid2<F, I, T>
where
    I: From<Vector2<i32>>,
    F: Fn(I) -> T,
{
    backends::kolmo::KolmoGrid2::new(f)
}

/// Represent a coord → &Item function as a grid.
pub fn ref_fn<'a, I, T, F>(f: F) -> backends::kolmoref::KolmoRefGrid2<'a, F, I, T>
where
    I: From<Vector2<i32>>,
    T: 'a,
    F: Fn(I) -> &'a T,
{
    backends::kolmoref::KolmoRefGrid2::new(f)
}

/// Represent a coord → &mut Item function as a grid.
pub fn mut_fn<'a, I, T, F>(f: F) -> backends::kolmomut::KolmoMutGrid2<'a, F, I, T>
where
    I: From<Vector2<i32>>,
    T: 'a,
    F: Fn(I) -> &'a mut T,
{
    backends::kolmomut::KolmoMutGrid2::new(f)
}

/// Read/write through closures.
///
/// This is a powerful type, which acts like a combination
/// of `ref_fn` and `mut_fn`. This grid owns a *referent* 
/// value, and contains a *reader* and *writer* function
/// which immutable and mutable (respectively) borrow the
/// elements from the referent.
pub fn reader_writer<I, R, T, Fr, Fw>(referent: R, reader: Fr, writer: Fw) -> backends::kolmorw::KolmoRwGrid2<I, R, T, Fr, Fw>
where
    I: From<Vector2<i32>>,
    Fr: Fn(I, &R) -> &T,
    Fw: Fn(I, &mut R) -> &mut T,
{
    backends::kolmorw::KolmoRwGrid2::new(referent, reader, writer)
}

/// Top-level trait for 2D grids.
pub trait Grid2 {
    type Item;
    type XBound: RangeBounds<i32>;
    type YBound: RangeBounds<i32>;
    
    fn x_bound(&self) -> Self::XBound;
    fn y_bound(&self) -> Self::YBound;
    
    fn in_bounds<I>(&self, coord: I) -> bool 
    where
        I: Into<Vector2<i32>>
    {
        let Vector2 { x, y } = coord.into();
        
        self.x_bound().contains(&x)
        && self.y_bound().contains(&y)
    }
    
    /// Element by-value mapping.
    fn map<F, T>(self, func: F) -> combinate::map::Grid2Map<Self, F, T>
    where
        Self: Sized,
        F: Fn(Self::Item) -> T,
    {
        combinate::map::Grid2Map::new(self, func)
    }
    
    /// Element by-value+coord mapping.
    fn enumap<I, F, T>(self, func: F) -> combinate::enumap::Grid2EnuMap<Self, F, T, I>
    where
        Self: Sized,
        I: From<Vector2<i32>>,
        F: Fn(I, Self::Item) -> T,
    {
        combinate::enumap::Grid2EnuMap::new(self, func)
    }
    
    /// Flattening a grid of grids with a regular stride.
    fn flatten<I>(self, stride: I) -> combinate::flatten::Grid2Flat<Self>
    where
        Self: Sized,
        Self::Item: Grid2,
        Self::XBound: Clone + RangeBoundsTimes,
        Self::YBound: Clone + RangeBoundsTimes,
        I: Into<Vector2<i32>>,
    {
        combinate::flatten::Grid2Flat::new(self, stride)
    }
    
    /// <0, 0> in this grid becomes new_origin in resultant grid.
    fn new_origin<I>(self, new_origin: I) -> combinate::neworigin::Grid2NewOrigin<Self>
    where
        Self: Sized,
        Self::XBound: RangeBoundsPlus,
        Self::YBound: RangeBoundsPlus,
        I: Into<Vector2<i32>>,
    {
        combinate::neworigin::Grid2NewOrigin::new(self, new_origin)
    }
    
    /// Provide function to provide elments at out-of-bounds coordinates.
    ///
    /// This produces an unbounded grid.
    fn oob_handler<I, F>(self, handler: F) -> combinate::oobhandler::Grid2OobHandler<Self, I, F>
    where
        Self: Sized,
        I: From<Vector2<i32>>,
        F: Fn(I) -> Self::Item,
    {
        combinate::oobhandler::Grid2OobHandler::new(self, handler)
    }
    
    /// View a sub-rectangle of this grid.
    /// 
    /// If the new bounds are not a subset of the current bounds,
    /// this will panic.
    fn subview<X, Y>(self, new_x: X, new_y: Y) -> combinate::slice::Grid2Slice<Self, X, Y>
    where
        Self: Sized,
        Self::XBound: Debug,
        Self::YBound: Debug,
        X: RangeBounds<i32> + Clone + Debug,
        Y: RangeBounds<i32> + Clone + Debug,
    {
        combinate::slice::Grid2Slice::new(self, new_x, new_y)
    }
    
    /// View a sub-rectangle of this grid.
    /// 
    /// If the new bounds are not a subset of the current bounds,
    /// this will fail.
    fn try_subview<X, Y>(self, new_x: X, new_y: Y) -> Result<combinate::slice::Grid2Slice<Self, X, Y>, Self>
    where
        Self: Sized,
        Self::XBound: Debug,
        Self::YBound: Debug,
        X: RangeBounds<i32> + Clone + Debug,
        Y: RangeBounds<i32> + Clone + Debug,
    {
        combinate::slice::Grid2Slice::try_new(self, new_x, new_y)
    }
    
    /// View a sub-rectangle of this grid, beginning at origin.
    /// 
    /// If the new bounds are not a subset of the current bounds,
    /// this will panic.
    fn subview_0to(self, new_x_len: i32, new_y_len: i32) -> combinate::slice::Grid2Slice<Self, Range0To, Range0To>
    where
        Self: Sized,
        Self::XBound: Debug,
        Self::YBound: Debug,
    {
        combinate::slice::Grid2Slice::new(
            self, 
            Range0To { end: new_x_len },
            Range0To { end: new_y_len })
    }
    
    /// View a sub-rectangle of this grid, beginning at origin.
    /// 
    /// If the new bounds are not a subset of the current bounds,
    /// this will fail.
    fn try_subview_0to(self, new_x_len: i32, new_y_len: i32) -> Result<combinate::slice::Grid2Slice<Self, Range0To, Range0To>, Self>
    where
        Self: Sized,
        Self::XBound: Debug,
        Self::YBound: Debug,
    {
        combinate::slice::Grid2Slice::try_new(
            self, 
            Range0To { end: new_x_len },
            Range0To { end: new_y_len })
    }
    
    /// View of this grid which wraps around the edges.
    ///
    /// The input grid must be bounded in all directions, and the
    /// output grid is completely unbounded.
    fn wrapping(self) -> combinate::wrapping::Grid2Wrapping<Self>
    where
        Self: Sized,
        Self::XBound: BoundRange,
        Self::YBound: BoundRange,
    {
        combinate::wrapping::Grid2Wrapping::new(self)
    }
    
    /// Collect a grid's elements into a heap allocation.
    ///
    /// The grid must be bound from 0 to a finite limit.
    fn collect(&self) -> backends::heap::ArrayGrid2<Self::Item>
    where
        Self: Grid2Get,
        Self::XBound: Into<Range0To>,
        Self::YBound: Into<Range0To>,
    {
        let x_len = self.x_bound().into().end;
        let y_len = self.y_bound().into().end;
        backends::heap::ArrayGrid2::new(
            x_len, y_len,
            |coord: Vector2<i32>| self.get(coord))
    }
}

/// 2D grid bounded from 0 to a finite number.
pub trait Grid2Len: Grid2<XBound=Range0To, YBound=Range0To> {
    fn x_len(&self) -> i32 {
        self.x_bound().end
    }
    
    fn y_len(&self) -> i32 {
        self.y_bound().end
    }
}

/// 2D grid read by value.
pub trait Grid2Get: Grid2 {
    fn get<I>(&self, coord: I) -> Self::Item
    where
        I: Into<Vector2<i32>>;
        
    fn try_get<I>(&self, coord: I) -> Option<Self::Item>
    where
        I: Into<Vector2<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            Some(self.get(coord))
        } else {
            None
        }
    }
}

/// 2D grid write by value.
pub trait Grid2Set: Grid2 {
    fn set<I>(&mut self, coord: I, elem: Self::Item)
    where
        I: Into<Vector2<i32>>;
        
    fn try_set<I>(&mut self, coord: I, elem: Self::Item) -> Result<(), Self::Item> 
    where
        I: Into<Vector2<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            self.set(coord, elem);
            Ok({})
        } else {
            Err(elem)
        }
    }
}

/// 2D grid read by reference.
pub trait Grid2Ref: Grid2 {
    fn idx<I>(&self, coord: I) -> &Self::Item
    where
        I: Into<Vector2<i32>>;
    
    fn try_idx<I>(&self, coord: I) -> Option<&Self::Item>
    where
        I: Into<Vector2<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            Some(self.idx(coord))
        } else {
            None
        }
    }
}

/// 2D grid write by reference.
pub trait Grid2Mut: Grid2 {
    fn midx<I>(&mut self, coord: I) -> &mut Self::Item
    where
        I: Into<Vector2<i32>>;
    
    fn try_midx<I>(&mut self, coord: I) -> Option<&mut Self::Item>
    where
        I: Into<Vector2<i32>>
    {
        let coord = coord.into();
        if self.in_bounds(coord) {
            Some(self.midx(coord))
        } else {
            None
        }
    }
}