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
//! Kolmogorov by-mutable-reference encoding of Grid2.

use crate::grid2::*;
use mint::Vector2;
use std::{
    ops::RangeFull,
    marker::PhantomData,
};

/// Kolmogorov by-mutable-reference encoding of Grid2.
/// 
/// This is a Grid2 implementation which only stores a
/// function from coordinate to value. It is subsequently
/// unbounded.
pub struct KolmoMutGrid2<'a, F, I, T> 
where
    F: Fn(I) -> &'a mut T,
    T: 'a,
    I: From<Vector2<i32>>
{
    func: F,
    p: PhantomData<fn(T, I)>,
}

impl<'a, F, I, T> KolmoMutGrid2<'a, F, I, T>
where
    F: Fn(I) -> &'a mut T,
    T: 'a,
    I: From<Vector2<i32>>
{
    pub fn new(func: F) -> Self {
        KolmoMutGrid2 {
            func,
            p: PhantomData,
        }
    }
}

impl<'a, F, I, T> Grid2 for KolmoMutGrid2<'a, F, I, T>
where
    F: Fn(I) -> &'a mut T,
    T: 'a,
    I: From<Vector2<i32>>
{
    type Item = T;
    type XBound = RangeFull;
    type YBound = RangeFull;
    
    fn x_bound(&self) -> RangeFull { RangeFull }
    fn y_bound(&self) -> RangeFull { RangeFull }
}



impl<'a, F, I, T> Grid2Mut for KolmoMutGrid2<'a, F, I, T>
where
    F: Fn(I) -> &'a mut T,
    T: 'a,
    I: From<Vector2<i32>>
{
    fn midx<C>(&mut self, coord: C) -> &mut Self::Item
    where
        C: Into<Vector2<i32>>
    {
        (self.func)(I::from(coord.into()))
    }
}

impl<'a, F, I, T> Grid2Set for KolmoMutGrid2<'a, F, I, T> 
where
    F: Fn(I) -> &'a mut T,
    T: 'a,
    I: From<Vector2<i32>>
{
    fn set<C: Into<Vector2<i32>>>(&mut self, coord: C, elem: Self::Item) 
    { *self.midx(coord) = elem; }
}