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
112
113
114
115
116
117
118
119
120
121
//! Different [storage layouts][Layout] for DBMs.

use super::*;

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

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

    /// Sets the bound for the clock difference `left - right`.
    ///
    /// # Safety
    /// Assumes that the indices of each clock are strictly less than `num_variables`.
    /// Causes undefined behavior otherwise.
    unsafe fn set_unchecked(&mut self, left: impl AnyClock, right: impl AnyClock, bound: B);

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

    /// Retrieves the bound for the clock difference `left - right`.
    ///
    /// # Safety
    /// Assumes that the indices of each clock are strictly less than `num_variables`.
    /// Causes undefined behavior otherwise.
    unsafe fn get_unchecked(&self, left: impl AnyClock, right: impl AnyClock) -> &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: impl AnyClock, right: impl AnyClock) -> usize {
        left.into_index() * self.dimension + right.into_index()
    }
}

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

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

    #[inline(always)]
    unsafe fn set_unchecked(&mut self, left: impl AnyClock, right: impl AnyClock, bound: B) {
        let index = self.index_of(left, right);
        *self.bounds.get_unchecked_mut(index) = bound;
    }

    #[inline(always)]
    fn get(&self, left: impl AnyClock, right: impl AnyClock) -> &B {
        &self.bounds[self.index_of(left, right)]
    }

    #[inline(always)]
    unsafe fn get_unchecked(&self, left: impl AnyClock, right: impl AnyClock) -> &B {
        &self.bounds.get_unchecked(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_variables: usize, default: B) -> Self {
        let dimension = num_variables + 1;
        MatrixLayout {
            matrix: vec![vec![default; dimension].into(); dimension].into(),
        }
    }

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

    #[inline(always)]
    unsafe fn set_unchecked(&mut self, left: impl AnyClock, right: impl AnyClock, bound: B) {
        *self
            .matrix
            .get_unchecked_mut(left.into_index())
            .get_unchecked_mut(right.into_index()) = bound;
    }

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

    #[inline(always)]
    unsafe fn get_unchecked(&self, left: impl AnyClock, right: impl AnyClock) -> &B {
        &self
            .matrix
            .get_unchecked(left.into_index())
            .get_unchecked(right.into_index())
    }
}