rust/vec2d/src/lib.rs
author unc0rr
Sat, 03 Nov 2018 18:29:58 +0100
changeset 14121 69db1d2e4cec
parent 14032 2869c2ccb1b8
child 14160 c24a76f131d6
permissions -rw-r--r--
land_dump app for testing templated landgen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
     1
extern crate integral_geometry;
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
     2
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     3
use std::ops::{Index, IndexMut};
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
     4
use std::slice::SliceIndex;
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
     5
use integral_geometry::Size;
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     6
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
     7
pub struct Vec2D<T> {
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     8
    data: Vec<T>,
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
     9
    size: Size,
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    10
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    11
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    12
impl<T> Index<usize> for Vec2D<T> {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    13
    type Output = [T];
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    14
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    15
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    16
    fn index(&self, row: usize) -> &[T] {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    17
        debug_assert!(row < self.height());
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    18
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    19
        let pos = row * self.width();
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    20
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    21
        &self.data[pos..pos + self.width()]
13911
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
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    24
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    25
impl<T> IndexMut<usize> for Vec2D<T> {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    26
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    27
    fn index_mut(&mut self, row: usize) -> &mut [T] {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    28
        debug_assert!(row < self.height());
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    29
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    30
        let pos = row * self.width();
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    31
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    32
        &mut self.data[pos..pos + self.size.width]
13911
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
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    35
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    36
impl <T> Vec2D<T> {
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    37
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    38
    pub fn width(&self) -> usize {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    39
        self.size.width
13911
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
    #[inline]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    43
    pub fn height(&self) -> usize {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    44
        self.size.height
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    45
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    46
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    47
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    48
    pub fn size(&self) -> Size {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    49
        self.size
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    50
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    51
}
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    52
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    53
impl<T: Copy> Vec2D<T> {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    54
    pub fn new(size: Size, value: T) -> Self {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    55
        Self { size, data: vec![value; size.area()] }
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    56
    }
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    57
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14032
diff changeset
    58
    pub fn raw_data(&self) -> &[T] {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14032
diff changeset
    59
        &self.data
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14032
diff changeset
    60
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14032
diff changeset
    61
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    62
    #[inline]
13930
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    63
    pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    64
        self.data.get(row * self.width() + column)
13930
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    65
    }
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
    #[inline]
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    68
    pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut <usize as SliceIndex<[T]>>::Output> {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    69
        self.data.get_mut(row * self.size.width + column)
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
    70
    }
13924
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13916
diff changeset
    71
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13916
diff changeset
    72
    #[inline]
13930
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    73
    pub unsafe fn get_unchecked(&self, row: usize, column: usize) -> &<usize as SliceIndex<[T]>>::Output {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    74
        self.data.get_unchecked(row * self.width() + column)
13930
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    75
    }
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    76
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    77
    #[inline]
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13924
diff changeset
    78
    pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    79
        self.data.get_unchecked_mut(row * self.size.width + column)
13924
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13916
diff changeset
    80
    }
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    81
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    82
    #[inline]
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    83
    pub fn rows(&self) -> impl Iterator<Item = &[T]> {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    84
        self.data.chunks(self.width())
14030
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13930
diff changeset
    85
    }
13911
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
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    88
#[cfg(test)]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    89
mod tests {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    90
    use super::*;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    91
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    92
    #[test]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    93
    fn basics() {
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    94
        let mut v: Vec2D<u8> = Vec2D::new(Size::new(2, 3), 0xff);
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    95
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    96
        assert_eq!(v.width(), 2);
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14030
diff changeset
    97
        assert_eq!(v.height(), 3);
13911
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    98
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    99
        assert_eq!(v[0][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   100
        assert_eq!(v[2][1], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   101
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   102
        v[2][1] = 0;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   103
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   104
        assert_eq!(v[2][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   105
        assert_eq!(v[2][1], 0);
13916
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   106
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   107
        v.get_mut(2, 1).map(|v| *v = 1);
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   108
        assert_eq!(v[2][1], 1);
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   109
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13915
diff changeset
   110
        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
   111
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   112
}