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
use super::*;

/// Represents a storage layout for [DBM].
pub trait Layout<B: Bound> {
    /// Initializes the storage for `num_clock` clocks using `init` as
    /// initial bound for all differences.
    fn new(num_clocks: usize, init: B) -> Self;

    /// Sets the bound for the clock difference `left - right`.
    fn set(&mut self, left: Clock, right: Clock, bound: B);

    /// Retrieves the bound for the clock difference `left - right`.
    fn get(&self, left: Clock, right: Clock) -> &B;
}

/// A [storage layout](Layout) using a one-dimensional array.
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub struct LinearLayout<B: Bound> {
    dimension: usize,
    bounds: Box<[B]>,
}

impl<B: Bound> LinearLayout<B> {
    /// Computes the index where the bound for `left - right` is stored.
    #[inline(always)]
    fn index_of(&self, left: Clock, right: Clock) -> usize {
        left * self.dimension + right
    }
}

impl<B: Bound> Layout<B> for LinearLayout<B> {
    #[inline(always)]
    fn new(num_clocks: usize, default: B) -> Self {
        let dimension = num_clocks + 1;
        LinearLayout {
            dimension,
            bounds: vec![default; dimension * dimension].into(),
        }
    }

    #[inline(always)]
    fn set(&mut self, left: Clock, right: Clock, bound: B) {
        let index = self.index_of(left, right);
        self.bounds[index] = bound;
    }
    #[inline(always)]
    fn get(&self, left: Clock, right: Clock) -> &B {
        &self.bounds[self.index_of(left, right)]
    }
}

/// A [storage layout](Layout) using a two-dimensional array.
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
#[repr(transparent)]
pub struct MatrixLayout<B: Bound> {
    matrix: Box<[Box<[B]>]>,
}

impl<B: Bound> Layout<B> for MatrixLayout<B> {
    #[inline(always)]
    fn new(num_clocks: usize, default: B) -> Self {
        let dimension = num_clocks + 1;
        MatrixLayout {
            matrix: vec![vec![default; dimension].into(); dimension].into(),
        }
    }

    #[inline(always)]
    fn set(&mut self, left: Clock, right: Clock, bound: B) {
        self.matrix[left][right] = bound;
    }

    #[inline(always)]
    fn get(&self, left: Clock, right: Clock) -> &B {
        &self.matrix[left][right]
    }
}