mindustry logic execution, map- and schematic- parsing and rendering
Diffstat (limited to 'src/utils/array.rs')
-rw-r--r--src/utils/array.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/utils/array.rs b/src/utils/array.rs
new file mode 100644
index 0000000..6685baa
--- /dev/null
+++ b/src/utils/array.rs
@@ -0,0 +1,63 @@
+use std::{
+ fmt::{Debug, Write},
+ ops::Deref,
+};
+#[derive(Clone, PartialEq, Eq)]
+pub struct Array2D<T: Clone> {
+ width: usize,
+ height: usize,
+ /// column
+ data: Box<[T]>,
+}
+
+impl<T: Debug + Clone> Debug for Array2D<Option<T>> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("Arr[\n")?;
+ for y in (0..self.height).rev() {
+ for x in 0..self.width {
+ let t = &self[x][y];
+ if let Some(t) = t {
+ t.fmt(f)?;
+ } else {
+ f.write_char('_')?;
+ }
+ f.write_str(", ")?;
+ }
+ f.write_char('\n')?;
+ }
+ f.write_char(']')?;
+ Ok(())
+ }
+}
+
+impl<T: Clone> Array2D<T> {
+ pub fn new(fill: T, width: usize, height: usize) -> Array2D<T> {
+ Array2D {
+ width,
+ height,
+ data: vec![fill; width * height].into_boxed_slice(),
+ }
+ }
+}
+
+impl<T: Clone> Deref for Array2D<T> {
+ type Target = Box<[T]>;
+ /// a sin it is
+ fn deref(&self) -> &Self::Target {
+ &self.data
+ }
+}
+
+impl<T: Clone> std::ops::Index<usize> for Array2D<T> {
+ type Output = [T];
+
+ fn index(&self, x: usize) -> &Self::Output {
+ &self.data[self.height * x..self.height * (x + 1)]
+ }
+}
+
+impl<T: Clone> std::ops::IndexMut<usize> for Array2D<T> {
+ fn index_mut(&mut self, x: usize) -> &mut Self::Output {
+ &mut self.data[self.height * x..self.height * (x + 1)]
+ }
+}