rust/vec2d/src/lib.rs
author Wuzzy <Wuzzy2@mail.ru>
Sat, 08 Jun 2019 11:25:39 +0200
changeset 15136 a77b0e339929
parent 14707 29dbe9ce8b7d
permissions -rw-r--r--
Lua API: Add SetAmmoSlot
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14212
bb2f301d4fe0 2018ize everything
alfadur
parents: 14175
diff changeset
     1
use std::{
bb2f301d4fe0 2018ize everything
alfadur
parents: 14175
diff changeset
     2
    ops::{Index, IndexMut},
bb2f301d4fe0 2018ize everything
alfadur
parents: 14175
diff changeset
     3
    slice::SliceIndex
bb2f301d4fe0 2018ize everything
alfadur
parents: 14175
diff changeset
     4
};
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
     5
use integral_geometry::Size;
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     6
13921
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
     7
pub struct Vec2D<T> {
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
     8
    data: Vec<T>,
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
     9
    size: Size,
13916
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] {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    17
        debug_assert!(row < self.height());
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    18
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    19
        let pos = row * self.width();
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    20
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    21
        &self.data[pos..pos + self.width()]
13916
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] {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    28
        debug_assert!(row < self.height());
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    29
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    30
        let pos = row * self.width();
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    31
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    32
        &mut self.data[pos..pos + self.size.width]
13916
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
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    36
impl <T> Vec2D<T> {
13916
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 {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    39
        self.size.width
13916
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 {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    44
        self.size.height
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    45
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    46
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    47
    #[inline]
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    48
    pub fn size(&self) -> Size {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    49
        self.size
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    50
    }
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    51
}
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    52
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    53
impl<T: Copy> Vec2D<T> {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    54
    pub fn new(size: Size, value: T) -> Self {
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    55
        Self { size, data: vec![value; size.area()] }
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
    56
    }
13921
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
    57
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    58
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    59
    pub fn as_slice(&self) -> &[T] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    60
        self.data.as_slice()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    61
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    62
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    63
    #[inline]
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    64
    pub fn as_mut_slice(&mut self) -> &mut [T] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    65
        self.data.as_mut_slice()
14126
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14037
diff changeset
    66
    }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14037
diff changeset
    67
13921
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
    68
    #[inline]
13935
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    69
    pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    70
        self.data.get(row * self.width() + column)
13935
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    71
    }
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    72
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    73
    #[inline]
13921
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
    74
    pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut <usize as SliceIndex<[T]>>::Output> {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    75
        self.data.get_mut(row * self.size.width + column)
13921
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
    76
    }
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13921
diff changeset
    77
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13921
diff changeset
    78
    #[inline]
13935
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    79
    pub unsafe fn get_unchecked(&self, row: usize, column: usize) -> &<usize as SliceIndex<[T]>>::Output {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    80
        self.data.get_unchecked(row * self.width() + column)
13935
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    81
    }
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    82
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    83
    #[inline]
5c9d963492bf Implement get_unchecked* functions for Vec2D
unc0rr
parents: 13929
diff changeset
    84
    pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
    85
        self.data.get_unchecked_mut(row * self.size.width + column)
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13921
diff changeset
    86
    }
14035
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13935
diff changeset
    87
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13935
diff changeset
    88
    #[inline]
14175
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14165
diff changeset
    89
    pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[T]> {
14355
31717e1436cd recruit some newly stabilized functions
alfadur
parents: 14212
diff changeset
    90
        self.data.chunks_exact(self.width())
14035
2ebd505e62c1 make theme editor render some random map lines
alfadur
parents: 13935
diff changeset
    91
    }
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    92
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    93
    #[inline]
14175
a4c1a2d0ac24 implement basic land bordering
alfadur
parents: 14165
diff changeset
    94
    pub fn rows_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut [T]> {
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    95
        let width = self.width();
14355
31717e1436cd recruit some newly stabilized functions
alfadur
parents: 14212
diff changeset
    96
        self.data.chunks_exact_mut(width)
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
    97
    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
    98
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
    99
    #[inline]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   100
    pub unsafe fn as_bytes(&self) -> &[u8] {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   101
        use std::{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   102
            slice,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   103
            mem
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   104
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   105
        
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   106
        slice::from_raw_parts(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   107
            self.data.as_ptr() as *const u8,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   108
            self.data.len() * mem::size_of::<T>(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   109
        )
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents: 14355
diff changeset
   110
    }
14165
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   111
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   112
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   113
impl<T: Copy> AsRef<[T]> for Vec2D<T> {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   114
    fn as_ref(&self) -> &[T] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   115
        self.as_slice()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   116
    }
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   117
}
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   118
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   119
impl<T: Copy> AsMut<[T]> for Vec2D<T> {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   120
    fn as_mut(&mut self) -> &mut [T] {
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   121
        self.as_mut_slice()
c24a76f131d6 implement basic land texturing
alfadur
parents: 14126
diff changeset
   122
    }
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   123
}
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   124
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   125
#[cfg(test)]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   126
mod tests {
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   127
    use super::*;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   128
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   129
    #[test]
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   130
    fn basics() {
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
   131
        let mut v: Vec2D<u8> = Vec2D::new(Size::new(2, 3), 0xff);
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   132
14037
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
   133
        assert_eq!(v.width(), 2);
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14035
diff changeset
   134
        assert_eq!(v.height(), 3);
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   135
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   136
        assert_eq!(v[0][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   137
        assert_eq!(v[2][1], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   138
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   139
        v[2][1] = 0;
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   140
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   141
        assert_eq!(v[2][0], 0xff);
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   142
        assert_eq!(v[2][1], 0);
13921
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
   143
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
   144
        v.get_mut(2, 1).map(|v| *v = 1);
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
   145
        assert_eq!(v[2][1], 1);
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
   146
cd437d76978a Add get_mut() to Vec2D
unc0rr
parents: 13920
diff changeset
   147
        assert_eq!(v.get_mut(2, 2), None);
13916
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   148
    }
fa9f93393e9c Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff changeset
   149
}