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
//! Kolmogorov by-refernce encoding of Grid3.

use crate::grid3::*;
use mint::Vector3;
use std::{
    ops::RangeFull,
    marker::PhantomData,
};

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

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

impl<'a, F, I, T> Grid3 for KolmoRefGrid3<'a, F, I, T>
where
    F: Fn(I) -> &'a T,
    T: 'a,
    I: From<Vector3<i32>>
{
    type Item = T;
    type XBound = RangeFull;
    type YBound = RangeFull;
    type ZBound = RangeFull;
    
    fn x_bound(&self) -> RangeFull { RangeFull }
    fn y_bound(&self) -> RangeFull { RangeFull }
    fn z_bound(&self) -> RangeFull { RangeFull }
}

impl<'a, F, I, T> Grid3Ref for KolmoRefGrid3<'a, F, I, T>
where
    F: Fn(I) -> &'a T,
    T: 'a,
    I: From<Vector3<i32>>
{
    fn idx<C>(&self, coord: C) -> &Self::Item
    where
        C: Into<Vector3<i32>>
    {
        (self.func)(I::from(coord.into()))
    }
}

impl<'a, F, I, T> Grid3Get for KolmoRefGrid3<'a, F, I, T>
where
    F: Fn(I) -> &'a T,
    T: Clone + 'a,
    I: From<Vector3<i32>>
{
    fn get<C: Into<Vector3<i32>>>(&self, coord: C) -> Self::Item 
    { self.idx(coord).clone() }
}