lzma_rs/util/
vec2d.rs

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::ops::{Index, IndexMut};

/// A 2 dimensional matrix in row-major order backed by a contiguous slice.
#[derive(Debug)]
pub struct Vec2D<T> {
    data: Box<[T]>,
    cols: usize,
}

impl<T> Vec2D<T> {
    /// Initialize a grid of size (`rows`, `cols`) with the given data element.
    pub fn init(data: T, size: (usize, usize)) -> Vec2D<T>
    where
        T: Clone,
    {
        let (rows, cols) = size;
        let len = rows
            .checked_mul(cols)
            .unwrap_or_else(|| panic!("{} rows by {} cols exceeds usize::MAX", rows, cols));
        Vec2D {
            data: vec![data; len].into_boxed_slice(),
            cols,
        }
    }

    /// Fills the grid with elements by cloning `value`.
    pub fn fill(&mut self, value: T)
    where
        T: Clone,
    {
        self.data.fill(value)
    }
}

impl<T> Index<usize> for Vec2D<T> {
    type Output = [T];

    #[inline]
    fn index(&self, row: usize) -> &Self::Output {
        let start_row = row
            .checked_mul(self.cols)
            .unwrap_or_else(|| panic!("{} row by {} cols exceeds usize::MAX", row, self.cols));
        &self.data[start_row..start_row + self.cols]
    }
}

impl<T> IndexMut<usize> for Vec2D<T> {
    #[inline]
    fn index_mut(&mut self, row: usize) -> &mut Self::Output {
        let start_row = row
            .checked_mul(self.cols)
            .unwrap_or_else(|| panic!("{} row by {} cols exceeds usize::MAX", row, self.cols));
        &mut self.data[start_row..start_row + self.cols]
    }
}

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

    #[test]
    fn init() {
        let vec2d = Vec2D::init(1, (2, 3));
        assert_eq!(vec2d[0], [1, 1, 1]);
        assert_eq!(vec2d[1], [1, 1, 1]);
    }

    #[test]
    #[should_panic]
    fn init_overflow() {
        Vec2D::init(1, (usize::MAX, usize::MAX));
    }

    #[test]
    fn fill() {
        let mut vec2d = Vec2D::init(0, (2, 3));
        vec2d.fill(7);
        assert_eq!(vec2d[0], [7, 7, 7]);
        assert_eq!(vec2d[1], [7, 7, 7]);
    }

    #[test]
    fn index() {
        let vec2d = Vec2D {
            data: vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice(),
            cols: 2,
        };
        assert_eq!(vec2d[0], [0, 1]);
        assert_eq!(vec2d[1], [2, 3]);
        assert_eq!(vec2d[2], [4, 5]);
        assert_eq!(vec2d[3], [6, 7]);
    }

    #[test]
    fn indexmut() {
        let mut vec2d = Vec2D {
            data: vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice(),
            cols: 2,
        };

        vec2d[1][1] = 9;
        assert_eq!(vec2d[0], [0, 1]);
        // (1, 1) should be 9.
        assert_eq!(vec2d[1], [2, 9]);
        assert_eq!(vec2d[2], [4, 5]);
        assert_eq!(vec2d[3], [6, 7]);
    }

    #[test]
    #[should_panic]
    fn index_out_of_bounds() {
        let vec2d = Vec2D::init(1, (2, 3));
        let _x = vec2d[2][3];
    }

    #[test]
    #[should_panic]
    fn index_out_of_bounds_vec_edge() {
        let vec2d = Vec2D::init(1, (2, 3));
        let _x = vec2d[1][3];
    }

    #[test]
    #[should_panic]
    fn index_column_out_of_bounds() {
        let vec2d = Vec2D::init(1, (2, 3));
        let _x = vec2d[0][3];
    }

    #[test]
    #[should_panic]
    fn index_row_out_of_bounds() {
        let vec2d = Vec2D::init(1, (2, 3));
        let _x = vec2d[2][0];
    }

    #[test]
    #[should_panic]
    fn index_mul_overflow() {
        // Matrix with 4 columns.
        let matrix = Vec2D::init(0, (3, 4));
        // 2^{usize.numbits() - 2}.
        let row = (usize::MAX / 4) + 1;
        // Returns the same as matrix[0] if overflow is not caught.
        let _ = matrix[row];
    }

    #[test]
    #[should_panic]
    fn index_add_overflow() {
        // Matrix with 5 columns.
        let matrix = Vec2D::init(0, (3, 5));
        // Somehow, as long as numbits(usize) is a multiple of 4, then 5 divides usize::MAX.
        // This is clear in hexadecimal: usize::MAX is 0xFFF...F and usize::MAX / 5 is 0x333...3.
        let row = usize::MAX / 5;
        // This will therefore try to index data[usize::MAX..4].
        let _ = matrix[row];
    }

    #[test]
    #[should_panic]
    fn indexmut_out_of_bounds() {
        let mut vec2d = Vec2D::init(1, (2, 3));
        vec2d[2][3] = 0;
    }

    #[test]
    #[should_panic]
    fn indexmut_out_of_bounds_vec_edge() {
        let mut vec2d = Vec2D::init(1, (2, 3));
        vec2d[1][3] = 0;
    }

    #[test]
    #[should_panic]
    fn indexmut_column_out_of_bounds() {
        let mut vec2d = Vec2D::init(1, (2, 3));
        vec2d[0][3] = 0;
    }

    #[test]
    #[should_panic]
    fn indexmut_row_out_of_bounds() {
        let mut vec2d = Vec2D::init(1, (2, 3));
        vec2d[2][0] = 0;
    }

    #[test]
    #[should_panic]
    fn indexmut_mul_overflow() {
        // Matrix with 4 columns.
        let mut matrix = Vec2D::init(0, (3, 4));
        // 2^{usize.numbits() - 2}.
        let row = (usize::MAX / 4) + 1;
        // Returns the same as matrix[0] if overflow is not caught.
        matrix[row][0] = 9;
    }

    #[test]
    #[should_panic]
    fn indexmut_add_overflow() {
        // Matrix with 5 columns.
        let mut matrix = Vec2D::init(0, (3, 5));
        // Somehow, as long as numbits(usize) is a multiple of 4, then 5 divides usize::MAX.
        // This is clear in hexadecimal: usize::MAX is 0xFFF...F and usize::MAX / 5 is 0x333...3.
        let row = usize::MAX / 5;
        // This will therefore try to index data[usize::MAX..4].
        matrix[row][0] = 9;
    }
}