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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::{fmt, marker};

#[derive(Clone, PartialEq, Eq)]
pub struct VecVec<T> {
    inner: Vec<T>,
    width: usize,
    height: usize,
}

impl<T> VecVec<T> {
    pub fn new(width: usize, height: usize, value: T) -> Self
        where T: Clone
    {
        VecVec {
            inner: vec![value; width * height],
            width: width,
            height: height,
        }
    }

    pub fn width(&self) -> usize {
        self.width
    }

    pub fn height(&self) -> usize {
        self.height
    }

    pub fn get(&self, x: usize, y: usize) -> Option<&T> {
        if x < self.width && y < self.height {
            Some(&self.inner[y * self.width + x])
        } else {
            None
        }
    }

    pub fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut T> {
        if x < self.width && y < self.height {
            Some(&mut self.inner[y * self.width + x])
        } else {
            None
        }
    }

    pub fn slice(&self,
                 x: usize,
                 y: usize,
                 width: usize,
                 height: usize)
                 -> Option<ImmutableSlice<T>> {
        self.as_slice().slice(x, y, width, height)
    }

    pub fn slice_mut(&mut self,
                     x: usize,
                     y: usize,
                     width: usize,
                     height: usize)
                     -> Option<MutableSlice<T>> {
        self.as_mut_slice().slice_mut(x, y, width, height)
    }

    pub fn hsplit_at(&self, y: usize) -> Option<(ImmutableSlice<T>, ImmutableSlice<T>)> {
        self.as_slice().hsplit_at(y)
    }

    pub fn vsplit_at(&self, x: usize) -> Option<(ImmutableSlice<T>, ImmutableSlice<T>)> {
        self.as_slice().vsplit_at(x)
    }

    pub fn hsplit_at_mut(&mut self, y: usize) -> Option<(MutableSlice<T>, MutableSlice<T>)> {
        self.as_mut_slice().hsplit_at_mut(y)
    }

    pub fn vsplit_at_mut(&mut self, x: usize) -> Option<(MutableSlice<T>, MutableSlice<T>)> {
        self.as_mut_slice().vsplit_at_mut(x)
    }

    pub fn as_slice(&self) -> ImmutableSlice<T> {
        Slice {
            ptr: self as *const _,
            x: 0,
            y: 0,
            width: self.width,
            height: self.height,
            _mutability: Immutable { marker: marker::PhantomData },
        }
    }

    pub fn as_mut_slice(&self) -> MutableSlice<T> {
        Slice {
            ptr: self as *const _,
            x: 0,
            y: 0,
            width: self.width,
            height: self.height,
            _mutability: Mutable { marker: marker::PhantomData },
        }
    }
}

impl<T: fmt::Debug> fmt::Debug for VecVec<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.slice(0, 0, self.width, self.height).unwrap().fmt(f)
    }
}

pub struct Immutable<'a, T: 'a> {
    marker: marker::PhantomData<&'a VecVec<T>>,
}

pub struct Mutable<'a, T: 'a> {
    marker: marker::PhantomData<&'a mut VecVec<T>>,
}

pub struct Slice<T, Mutability> {
    ptr: *const VecVec<T>,
    x: usize,
    y: usize,
    width: usize,
    height: usize,
    _mutability: Mutability,
}

pub type ImmutableSlice<'a, T> = Slice<T, Immutable<'a, T>>;

pub type MutableSlice<'a, T> = Slice<T, Mutable<'a, T>>;

impl<'a, T, Mutability> Slice<T, Mutability> {
    pub fn x(&self) -> usize {
        self.x
    }

    pub fn y(&self) -> usize {
        self.y
    }

    pub fn width(&self) -> usize {
        self.width
    }

    pub fn height(&self) -> usize {
        self.height
    }

    pub fn get(&self, x: usize, y: usize) -> Option<&T> {
        if x < self.width && y < self.height {
            unsafe { (*self.ptr).get(self.x + x, self.y + y) }
        } else {
            None
        }
    }

    pub fn slice(&self,
                 x: usize,
                 y: usize,
                 width: usize,
                 height: usize)
                 -> Option<ImmutableSlice<'a, T>> {
        if x + width <= self.width && y + height <= self.height {
            Some(Slice {
                ptr: self.ptr,
                x: self.x + x,
                y: self.y + y,
                width: width,
                height: height,
                _mutability: Immutable { marker: marker::PhantomData },
            })
        } else {
            None
        }
    }

    pub fn hsplit_at(&self, y: usize) -> Option<(ImmutableSlice<'a, T>, ImmutableSlice<'a, T>)> {
        if y <= self.height {
            Some((self.slice(0, 0, self.width, y).unwrap(),
                  self.slice(0, y, self.width, self.height - y).unwrap()))
        } else {
            None
        }
    }

    pub fn vsplit_at(&self, x: usize) -> Option<(ImmutableSlice<'a, T>, ImmutableSlice<'a, T>)> {
        if x <= self.width {
            Some((self.slice(0, 0, x, self.height).unwrap(),
                  self.slice(x, 0, self.width - x, self.height).unwrap()))
        } else {
            None
        }
    }
}

impl<'a, T: 'a> Slice<T, Mutable<'a, T>> {
    pub fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut T> {
        if x < self.width && y < self.height {
            unsafe { (*(self.ptr as *mut VecVec<T>)).get_mut(self.x + x, self.y + y) }
        } else {
            None
        }
    }

    pub fn slice_mut(&mut self,
                     x: usize,
                     y: usize,
                     width: usize,
                     height: usize)
                     -> Option<MutableSlice<'a, T>> {
        unsafe { self.slice_mut_unsafe(x, y, width, height) }
    }

    pub fn hsplit_at_mut(&mut self,
                         y: usize)
                         -> Option<(MutableSlice<'a, T>, MutableSlice<'a, T>)> {
        if y <= self.height {
            let width = self.width;
            let height = self.height;
            unsafe {
                Some((self.slice_mut_unsafe(0, 0, width, y).unwrap(),
                      self.slice_mut_unsafe(0, y, width, height - y).unwrap()))
            }
        } else {
            None
        }
    }

    pub fn vsplit_at_mut(&mut self,
                         x: usize)
                         -> Option<(MutableSlice<'a, T>, MutableSlice<'a, T>)> {
        if x <= self.width {
            let width = self.width;
            let height = self.height;
            unsafe {
                Some((self.slice_mut_unsafe(0, 0, x, height).unwrap(),
                      self.slice_mut_unsafe(x, 0, width - x, height).unwrap()))
            }
        } else {
            None
        }
    }

    // Unsafe because the lifetime attached to the return value is chosen by the
    // caller. The caller must ensure the chosen lifetime does not cause memory
    // unsafety.
    unsafe fn slice_mut_unsafe<'arbitrary>(&mut self,
                                           x: usize,
                                           y: usize,
                                           width: usize,
                                           height: usize)
                                           -> Option<MutableSlice<'arbitrary, T>> {
        if x + width <= self.width && y + height <= self.height {
            Some(Slice {
                ptr: self.ptr,
                x: self.x + x,
                y: self.y + y,
                width: width,
                height: height,
                _mutability: Mutable { marker: marker::PhantomData },
            })
        } else {
            None
        }
    }
}

impl<T: PartialEq, M1, M2> PartialEq<Slice<T, M2>> for Slice<T, M1> {
    fn eq(&self, rhs: &Slice<T, M2>) -> bool {
        if self.width() == rhs.width() && self.height() == rhs.height() {
            for y in 0..self.height() {
                for x in 0..self.width() {
                    if self.get(x, y).unwrap() != rhs.get(x, y).unwrap() {
                        return false;
                    }
                }
            }
            true
        } else {
            false
        }
    }
}

impl<T: fmt::Debug, Mutability> fmt::Debug for Slice<T, Mutability> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        struct Row<'a, T: fmt::Debug + 'a, Mutability: 'a>(&'a Slice<T, Mutability>, usize);

        impl<'a, T: fmt::Debug, Mutability> fmt::Debug for Row<'a, T, Mutability> {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.debug_list()
                    .entries((0..self.0.width).map(|x| self.0.get(x, self.1).unwrap()))
                    .finish()
            }
        }

        f.debug_list()
            .entries((0..self.height).map(|y| Row(self, y)))
            .finish()
    }
}

impl<'a, T: PartialEq, Mutability> PartialEq<&'a [&'a [T]]> for Slice<T, Mutability> {
    fn eq(&self, rhs: &&[&[T]]) -> bool {
        if rhs.len() == self.height() && rhs.iter().all(|row| row.len() == self.width()) {
            rhs.iter().enumerate().all(|(y, row)| {
                row.iter().enumerate().all(|(x, cell)| self.get(x, y).unwrap() == cell)
            })
        } else {
            false
        }
    }
}