author | unC0Rr |
Wed, 18 Sep 2024 13:42:26 +0200 | |
branch | transitional_engine |
changeset 16035 | 0caa3dfb3ba2 |
parent 15915 | 8f093b1b18bc |
permissions | -rw-r--r-- |
15913 | 1 |
use integral_geometry::Size; |
14207 | 2 |
use std::{ |
3 |
ops::{Index, IndexMut}, |
|
15913 | 4 |
slice::SliceIndex, |
14207 | 5 |
}; |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
6 |
|
15912 | 7 |
#[derive(Debug)] |
13916 | 8 |
pub struct Vec2D<T> { |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
9 |
data: Vec<T>, |
14032 | 10 |
size: Size, |
13911
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 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
13 |
impl<T> Index<usize> for Vec2D<T> { |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
14 |
type Output = [T]; |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
15 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
16 |
#[inline] |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
17 |
fn index(&self, row: usize) -> &[T] { |
14032 | 18 |
debug_assert!(row < self.height()); |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
19 |
|
14032 | 20 |
let pos = row * self.width(); |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
21 |
|
14032 | 22 |
&self.data[pos..pos + self.width()] |
13911
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 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
26 |
impl<T> IndexMut<usize> for Vec2D<T> { |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
27 |
#[inline] |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
28 |
fn index_mut(&mut self, row: usize) -> &mut [T] { |
14032 | 29 |
debug_assert!(row < self.height()); |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
30 |
|
14032 | 31 |
let pos = row * self.width(); |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
32 |
|
14032 | 33 |
&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
|
34 |
} |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
35 |
} |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
36 |
|
15913 | 37 |
impl<T> Vec2D<T> { |
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
38 |
#[inline] |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
39 |
pub fn width(&self) -> usize { |
14032 | 40 |
self.size.width |
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 height(&self) -> usize { |
14032 | 45 |
self.size.height |
46 |
} |
|
47 |
||
48 |
#[inline] |
|
49 |
pub fn size(&self) -> Size { |
|
50 |
self.size |
|
51 |
} |
|
52 |
} |
|
53 |
||
54 |
impl<T: Copy> Vec2D<T> { |
|
15913 | 55 |
pub fn new(size: &Size, value: T) -> Self { |
56 |
Self { |
|
57 |
size: *size, |
|
58 |
data: vec![value; size.area()], |
|
59 |
} |
|
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
60 |
} |
13916 | 61 |
|
14160 | 62 |
#[inline] |
63 |
pub fn as_slice(&self) -> &[T] { |
|
64 |
self.data.as_slice() |
|
65 |
} |
|
66 |
||
67 |
#[inline] |
|
68 |
pub fn as_mut_slice(&mut self) -> &mut [T] { |
|
69 |
self.data.as_mut_slice() |
|
14121 | 70 |
} |
71 |
||
13916 | 72 |
#[inline] |
13930 | 73 |
pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> { |
15913 | 74 |
if row < self.height() && column < self.width() { |
75 |
Some(unsafe { self.data.get_unchecked(row * self.width() + column) }) |
|
76 |
} else { |
|
77 |
None |
|
78 |
} |
|
13930 | 79 |
} |
80 |
||
81 |
#[inline] |
|
15913 | 82 |
pub fn get_mut( |
83 |
&mut self, |
|
84 |
row: usize, |
|
85 |
column: usize, |
|
86 |
) -> Option<&mut <usize as SliceIndex<[T]>>::Output> { |
|
87 |
if row < self.height() && column < self.width() { |
|
88 |
Some(unsafe { self.data.get_unchecked_mut(row * self.size.width + column) }) |
|
89 |
} else { |
|
90 |
None |
|
91 |
} |
|
13916 | 92 |
} |
13924 | 93 |
|
94 |
#[inline] |
|
15913 | 95 |
pub unsafe fn get_unchecked( |
96 |
&self, |
|
97 |
row: usize, |
|
98 |
column: usize, |
|
99 |
) -> &<usize as SliceIndex<[T]>>::Output { |
|
14032 | 100 |
self.data.get_unchecked(row * self.width() + column) |
13930 | 101 |
} |
102 |
||
103 |
#[inline] |
|
15913 | 104 |
pub unsafe fn get_unchecked_mut( |
105 |
&mut self, |
|
106 |
row: usize, |
|
107 |
column: usize, |
|
108 |
) -> &mut <usize as SliceIndex<[T]>>::Output { |
|
14032 | 109 |
self.data.get_unchecked_mut(row * self.size.width + column) |
13924 | 110 |
} |
14030 | 111 |
|
112 |
#[inline] |
|
14170 | 113 |
pub fn rows(&self) -> impl DoubleEndedIterator<Item = &[T]> { |
14350 | 114 |
self.data.chunks_exact(self.width()) |
14030 | 115 |
} |
14160 | 116 |
|
117 |
#[inline] |
|
14170 | 118 |
pub fn rows_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut [T]> { |
14160 | 119 |
let width = self.width(); |
14350 | 120 |
self.data.chunks_exact_mut(width) |
14160 | 121 |
} |
14702 | 122 |
|
123 |
#[inline] |
|
124 |
pub unsafe fn as_bytes(&self) -> &[u8] { |
|
15913 | 125 |
use std::{mem, slice}; |
15901
f39f0f614dbf
Use LandPixels array allocate in hwengine-future library
unC0Rr
parents:
14702
diff
changeset
|
126 |
|
14702 | 127 |
slice::from_raw_parts( |
128 |
self.data.as_ptr() as *const u8, |
|
129 |
self.data.len() * mem::size_of::<T>(), |
|
130 |
) |
|
131 |
} |
|
14160 | 132 |
} |
133 |
||
134 |
impl<T: Copy> AsRef<[T]> for Vec2D<T> { |
|
135 |
fn as_ref(&self) -> &[T] { |
|
136 |
self.as_slice() |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
impl<T: Copy> AsMut<[T]> for Vec2D<T> { |
|
141 |
fn as_mut(&mut self) -> &mut [T] { |
|
142 |
self.as_mut_slice() |
|
143 |
} |
|
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
144 |
} |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
145 |
|
15915 | 146 |
impl<T: Clone> Vec2D<T> { |
147 |
pub fn from_iter<I: IntoIterator<Item = T>>(iter: I, size: &Size) -> Option<Vec2D<T>> { |
|
148 |
let data: Vec<T> = iter.into_iter().collect(); |
|
149 |
if size.width * size.height == data.len() { |
|
150 |
Some(Vec2D { data, size: *size }) |
|
151 |
} else { |
|
152 |
None |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
157 |
#[cfg(test)] |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
158 |
mod tests { |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
159 |
use super::*; |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
160 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
161 |
#[test] |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
162 |
fn basics() { |
14032 | 163 |
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
|
164 |
|
14032 | 165 |
assert_eq!(v.width(), 2); |
166 |
assert_eq!(v.height(), 3); |
|
13911
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
167 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
168 |
assert_eq!(v[0][0], 0xff); |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
169 |
assert_eq!(v[2][1], 0xff); |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
170 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
171 |
v[2][1] = 0; |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
172 |
|
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
173 |
assert_eq!(v[2][0], 0xff); |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
174 |
assert_eq!(v[2][1], 0); |
13916 | 175 |
|
176 |
v.get_mut(2, 1).map(|v| *v = 1); |
|
177 |
assert_eq!(v[2][1], 1); |
|
178 |
||
179 |
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
|
180 |
} |
fa9f93393e9c
Implement vec2d library to use for land arrays in the future
unc0rr
parents:
diff
changeset
|
181 |
} |