rust/vec2d/src/lib.rs
author alfadur
Tue, 30 Oct 2018 02:14:00 +0300
changeset 14030 2ebd505e62c1
parent 13930 5c9d963492bf
child 14032 2869c2ccb1b8
permissions -rw-r--r--
make theme editor render some random map lines
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     1
use std::ops::{Index, IndexMut};
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
     2
use std::slice::SliceIndex;
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     3
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
     4
pub struct Vec2D<T> {
13911
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 {
13915
f64790b2a725 Simplify Vec2D::new() a bit
unc0rr
parents: 13911
diff changeset
    36
        Self {
f64790b2a725 Simplify Vec2D::new() a bit
unc0rr
parents: 13911
diff changeset
    37
            data: vec![value; width * height],
13911
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,
13915
f64790b2a725 Simplify Vec2D::new() a bit
unc0rr
parents: 13911
diff changeset
    40
        }
13911
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
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    43
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    44
    pub fn width(&self) -> usize {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    45
        self.width
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
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    48
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    49
    pub fn height(&self) -> usize {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    50
        self.height
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    51
    }
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    52
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    53
    #[inline]
13930
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    54
    pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    55
        self.data.get(row * self.width + column)
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    56
    }
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    57
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    58
    #[inline]
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    59
    pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut <usize as SliceIndex<[T]>>::Output> {
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    60
        self.data.get_mut(row * self.width + column)
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    61
    }
13924
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13916
diff changeset
    62
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13916
diff changeset
    63
    #[inline]
13930
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    64
    pub unsafe fn get_unchecked(&self, row: usize, column: usize) -> &<usize as SliceIndex<[T]>>::Output {
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    65
        self.data.get_unchecked(row * self.width + column)
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    66
    }
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    67
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    68
    #[inline]
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    69
    pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output {
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    70
        self.data.get_unchecked_mut(row * self.width + column)
13924
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13916
diff changeset
    71
    }
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    72
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    73
    #[inline]
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    74
    pub fn rows(&self) -> impl Iterator<Item = &[T]> {
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    75
        self.data.chunks(self.width)
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    76
    }
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    77
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    78
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    79
#[cfg(test)]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    80
mod tests {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    81
    use super::*;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    82
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    83
    #[test]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    84
    fn basics() {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    85
        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
    86
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    87
        assert_eq!(v.width, 2);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    88
        assert_eq!(v.height, 3);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    89
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    90
        assert_eq!(v[0][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    91
        assert_eq!(v[2][1], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    92
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    93
        v[2][1] = 0;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    94
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    95
        assert_eq!(v[2][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    96
        assert_eq!(v[2][1], 0);
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    97
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    98
        v.get_mut(2, 1).map(|v| *v = 1);
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    99
        assert_eq!(v[2][1], 1);
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   100
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   101
        assert_eq!(v.get_mut(2, 2), None);
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   102
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   103
}