author | alfadur |
Sat, 03 Aug 2019 02:51:19 +0300 | |
changeset 15309 | 0f734fa371e1 |
parent 14723 | 29dbe9ce8b7d |
child 15930 | f39f0f614dbf |
permissions | -rw-r--r-- |
14228 | 1 |
use std::{ |
2 |
ops::{Index, IndexMut}, |
|
3 |
slice::SliceIndex |
|
4 |
}; |
|
14053 | 5 |
use integral_geometry::Size; |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
6 |
|
13937 | 7 |
pub struct Vec2D<T> { |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
8 |
data: Vec<T>, |
14053 | 9 |
size: Size, |
13932
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] { |
14053 | 17 |
debug_assert!(row < self.height()); |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
18 |
|
14053 | 19 |
let pos = row * self.width(); |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
20 |
|
14053 | 21 |
&self.data[pos..pos + self.width()] |
13932
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] { |
14053 | 28 |
debug_assert!(row < self.height()); |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
29 |
|
14053 | 30 |
let pos = row * self.width(); |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
31 |
|
14053 | 32 |
&mut self.data[pos..pos + self.size.width] |
13932
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 |
|
14053 | 36 |
impl <T> Vec2D<T> { |
13932
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 { |
14053 | 39 |
self.size.width |
13932
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 { |
14053 | 44 |
self.size.height |
45 |
} |
|
46 |
||
47 |
#[inline] |
|
48 |
pub fn size(&self) -> Size { |
|
49 |
self.size |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
impl<T: Copy> Vec2D<T> { |
|
54 |
pub fn new(size: Size, value: T) -> Self { |
|
55 |
Self { size, data: vec![value; size.area()] } |
|
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
56 |
} |
13937 | 57 |
|
14181 | 58 |
#[inline] |
59 |
pub fn as_slice(&self) -> &[T] { |
|
60 |
self.data.as_slice() |
|
61 |
} |
|
62 |
||
63 |
#[inline] |
|
64 |
pub fn as_mut_slice(&mut self) -> &mut [T] { |
|
65 |
self.data.as_mut_slice() |
|
14142 | 66 |
} |
67 |
||
13937 | 68 |
#[inline] |
13951 | 69 |
pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> { |
14053 | 70 |
self.data.get(row * self.width() + column) |
13951 | 71 |
} |
72 |
||
73 |
#[inline] |
|
13937 | 74 |
pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut <usize as SliceIndex<[T]>>::Output> { |
14053 | 75 |
self.data.get_mut(row * self.size.width + column) |
13937 | 76 |
} |
13945 | 77 |
|
78 |
#[inline] |
|
13951 | 79 |
pub unsafe fn get_unchecked(&self, row: usize, column: usize) -> &<usize as SliceIndex<[T]>>::Output { |
14053 | 80 |
self.data.get_unchecked(row * self.width() + column) |
13951 | 81 |
} |
82 |
||
83 |
#[inline] |
|
84 |
pub unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut <usize as SliceIndex<[T]>>::Output { |
|
14053 | 85 |
self.data.get_unchecked_mut(row * self.size.width + column) |
13945 | 86 |
} |
14051 | 87 |
|
88 |
#[inline] |
|
14191 | 89 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[T]> { |
14371 | 90 |
self.data.chunks_exact(self.width()) |
14051 | 91 |
} |
14181 | 92 |
|
93 |
#[inline] |
|
14191 | 94 |
pub fn rows_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut [T]> { |
14181 | 95 |
let width = self.width(); |
14371 | 96 |
self.data.chunks_exact_mut(width) |
14181 | 97 |
} |
14723 | 98 |
|
99 |
#[inline] |
|
100 |
pub unsafe fn as_bytes(&self) -> &[u8] { |
|
101 |
use std::{ |
|
102 |
slice, |
|
103 |
mem |
|
104 |
}; |
|
105 |
||
106 |
slice::from_raw_parts( |
|
107 |
self.data.as_ptr() as *const u8, |
|
108 |
self.data.len() * mem::size_of::<T>(), |
|
109 |
) |
|
110 |
} |
|
14181 | 111 |
} |
112 |
||
113 |
impl<T: Copy> AsRef<[T]> for Vec2D<T> { |
|
114 |
fn as_ref(&self) -> &[T] { |
|
115 |
self.as_slice() |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
impl<T: Copy> AsMut<[T]> for Vec2D<T> { |
|
120 |
fn as_mut(&mut self) -> &mut [T] { |
|
121 |
self.as_mut_slice() |
|
122 |
} |
|
13932
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() { |
14053 | 131 |
let mut v: Vec2D<u8> = Vec2D::new(Size::new(2, 3), 0xff); |
13932
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
132 |
|
14053 | 133 |
assert_eq!(v.width(), 2); |
134 |
assert_eq!(v.height(), 3); |
|
13932
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); |
13937 | 143 |
|
144 |
v.get_mut(2, 1).map(|v| *v = 1); |
|
145 |
assert_eq!(v[2][1], 1); |
|
146 |
||
147 |
assert_eq!(v.get_mut(2, 2), None); |
|
13932
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 |
} |