rust/vec2d/src/lib.rs
author unc0rr
Sun, 14 Oct 2018 21:13:32 +0200
changeset 13916 fa9f93393e9c
child 13920 f64790b2a725
permissions -rw-r--r--
Implement vec2d library to use for land arrays in the future
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     1
use std::iter;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     2
use std::ops::{Index, IndexMut};
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     3
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     4
struct Vec2D<T> {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     5
    data: Vec<T>,
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     6
    width: usize,
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     7
    height: usize,
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     8
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     9
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    10
impl<T> Index<usize> for Vec2D<T> {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    11
    type Output = [T];
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    12
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    13
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    14
    fn index(&self, row: usize) -> &[T] {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    15
        debug_assert!(row < self.height);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    16
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    17
        let pos = row * self.width;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    18
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    19
        &self.data[pos..pos + self.width]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    20
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    21
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    22
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    23
impl<T> IndexMut<usize> for Vec2D<T> {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    24
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    25
    fn index_mut(&mut self, row: usize) -> &mut [T] {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    26
        debug_assert!(row < self.height);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    27
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    28
        let pos = row * self.width;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    29
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    30
        &mut self.data[pos..pos + self.width]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    31
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    32
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    33
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    34
impl<T: Copy> Vec2D<T> {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    35
    pub fn new(width: usize, height: usize, value: T) -> Self {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    36
        let mut vec = Self {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    37
            data: Vec::new(),
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    38
            width,
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    39
            height,
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    40
        };
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    41
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    42
        vec.data.extend(iter::repeat(value).take(width * height));
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    43
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    44
        vec
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    45
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    46
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    47
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    48
    pub fn width(&self) -> usize {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    49
        self.width
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    50
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    51
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    52
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    53
    pub fn height(&self) -> usize {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    54
        self.height
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    55
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    56
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    57
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    58
#[cfg(test)]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    59
mod tests {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    60
    use super::*;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    61
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    62
    #[test]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    63
    fn basics() {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    64
        let mut v: Vec2D<u8> = Vec2D::new(2, 3, 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    65
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    66
        assert_eq!(v.width, 2);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    67
        assert_eq!(v.height, 3);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    68
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    69
        assert_eq!(v[0][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    70
        assert_eq!(v[2][1], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    71
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    72
        v[2][1] = 0;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    73
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    74
        assert_eq!(v[2][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    75
        assert_eq!(v[2][1], 0);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    76
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    77
}