File size: 2,585 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::cmp::{max, min};
use std::hash::{Hash, Hasher};

use common::types::{PointOffsetType, ScoreType};
use seahash::SeaHasher;

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct PointPair {
    a: PointOffsetType,
    b: PointOffsetType,
}

impl PointPair {
    pub fn new(a: PointOffsetType, b: PointOffsetType) -> Self {
        PointPair {
            a: min(a, b),
            b: max(a, b),
        }
    }
}

#[derive(Clone, Debug)]
struct CacheObj {
    points: PointPair,
    value: ScoreType,
}

#[allow(dead_code)]
#[derive(Debug)]
pub struct DistanceCache {
    cache: Vec<Option<CacheObj>>,
    pub hits: usize,
    pub misses: usize,
}

#[allow(dead_code)]
impl DistanceCache {
    fn hasher() -> impl Hasher {
        SeaHasher::new()
    }

    pub fn new(size: usize) -> Self {
        let mut cache = Vec::with_capacity(size);
        cache.resize(size, None);
        DistanceCache {
            cache,
            hits: 0,
            misses: 0,
        }
    }

    pub fn get(&self, point_a: PointOffsetType, point_b: PointOffsetType) -> Option<ScoreType> {
        let points = PointPair::new(point_a, point_b);
        let mut s = DistanceCache::hasher();
        points.hash(&mut s);
        let idx = s.finish() as usize % self.cache.len();

        self.cache[idx].as_ref().and_then(|x| {
            if x.points == points {
                Some(x.value)
            } else {
                None
            }
        })
    }

    pub fn put(&mut self, point_a: PointOffsetType, point_b: PointOffsetType, value: ScoreType) {
        let points = PointPair::new(point_a, point_b);
        let mut s = DistanceCache::hasher();
        points.hash(&mut s);
        let idx = s.finish() as usize % self.cache.len();
        self.cache[idx] = Some(CacheObj { points, value });
    }
}

impl Default for DistanceCache {
    fn default() -> Self {
        DistanceCache::new(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cache() {
        let mut cache = DistanceCache::new(1000);

        cache.put(100, 10, 0.8);
        cache.put(10, 101, 0.7);
        cache.put(10, 110, 0.1);

        assert_eq!(cache.get(12, 99), None);
        assert_eq!(cache.get(10, 100), Some(0.8));
        assert_eq!(cache.get(10, 101), Some(0.7));
    }

    #[test]
    fn test_collision() {
        let mut cache = DistanceCache::new(1);

        cache.put(1, 2, 0.8);
        cache.put(3, 4, 0.7);

        assert_eq!(cache.get(1, 2), None);
        assert_eq!(cache.get(2, 1), None);
        assert_eq!(cache.get(4, 3), Some(0.7));
    }
}