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
use crate::noise_fns::NoiseFn;
use std::cell::{Cell, RefCell};

/// Noise function that caches the last output value generated by the source
/// function.
///
/// If the input coordinates passed to `Cache::get` are equal to the previous
/// call, the function returns the cached result of the previous call to
/// `Source::get`. Otherwise, `Source::get` is called with the new coordinates,
/// overwriting the cache with the result, and returning the result to the
/// caller.
///
/// Caching a noise function is useful if it is used as a source function for
/// multiple noise functions. If a source function is not cached, the source
/// function will redundantly calculate the same output value once for each
/// noise function in which it is included.
#[derive(Clone, Debug)]
pub struct Cache<Source> {
    /// Outputs the value to be cached.
    pub source: Source,

    value: Cell<Option<f64>>,

    point: RefCell<Vec<f64>>,
}

impl<Source> Cache<Source> {
    pub fn new(source: Source) -> Self {
        Cache {
            source,
            value: Cell::new(None),
            point: RefCell::new(Vec::new()),
        }
    }
}

impl<Source> NoiseFn<[f64; 2]> for Cache<Source>
where
    Source: NoiseFn<[f64; 2]>,
{
    fn get(&self, point: [f64; 2]) -> f64 {
        match self.value.get() {
            Some(value) if quick_eq(&*self.point.borrow(), &point) => value,
            Some(_) | None => {
                let value = self.source.get(point);
                self.value.set(Some(value));

                let mut cached_point = self.point.borrow_mut();
                cached_point.clear();
                cached_point.extend_from_slice(&point);

                value
            }
        }
    }
}

impl<Source> NoiseFn<[f64; 3]> for Cache<Source>
where
    Source: NoiseFn<[f64; 3]>,
{
    fn get(&self, point: [f64; 3]) -> f64 {
        match self.value.get() {
            Some(value) if quick_eq(&*self.point.borrow(), &point) => value,
            Some(_) | None => {
                let value = self.source.get(point);
                self.value.set(Some(value));

                let mut cached_point = self.point.borrow_mut();
                cached_point.clear();
                cached_point.extend_from_slice(&point);

                value
            }
        }
    }
}

impl<Source> NoiseFn<[f64; 4]> for Cache<Source>
where
    Source: NoiseFn<[f64; 4]>,
{
    fn get(&self, point: [f64; 4]) -> f64 {
        match self.value.get() {
            Some(value) if quick_eq(&*self.point.borrow(), &point) => value,
            Some(_) | None => {
                let value = self.source.get(point);
                self.value.set(Some(value));

                let mut cached_point = self.point.borrow_mut();
                cached_point.clear();
                cached_point.extend_from_slice(&point);

                value
            }
        }
    }
}

fn quick_eq(a: &[f64], b: &[f64]) -> bool {
    assert_eq!(a.len(), b.len());

    a.iter().eq(b)
}