Skip to main content

duble_vec/
lib.rs

1/// The `Position` is a structure composed of variables of type `usize` named `x` and `y`
2#[derive(Debug, Default, Clone)]
3pub struct Index {
4    pub x: usize,
5    pub y: usize,
6}
7/// The `Size` is a structure composed of variables of type `usize` named `w` and `h`
8#[derive(Debug, Default, Clone)]
9pub struct Size {
10    pub w: usize,
11    pub h: usize,
12}
13
14#[derive(Debug, Default, Clone)]
15pub struct DubleVec<T> {
16    /// 2D size of `Size`
17    pub scale: Size,
18    vector: Vec<T>,
19}
20
21impl<T: Default + Clone> DubleVec<T> {
22    /// Creates a new `DubleVec`
23    pub fn new(scale: Size) -> Self {
24        let vector = vec![T::default(); scale.w * scale.h];
25        Self { scale, vector }
26    }
27
28    fn map(&self, index: Index) -> usize {
29        index.y * self.scale.w + index.x
30    }
31    /// Push new item`T` on `index`
32    pub fn push(&mut self, item: T, index: Index) {
33        let idx = self.map(index);
34        if idx < self.vector.len() {
35            self.vector[idx] = item;
36        }
37    }
38    /// Remove item`T` on `index`
39    /// Set item`T` on `index` to default value
40    pub fn remove(&mut self, index: Index) {
41        let idx = self.map(index);
42        if idx < self.vector.len() {
43            self.vector[idx] = T::default();
44        }
45    }
46    /// Flip the vector
47    pub fn reverse(&mut self) {
48        self.vector.reverse();
49    }
50    /// Get item`T` on `index` as `Option<&T>`
51    pub fn get(&self, index: Index) -> Option<&T> {
52        let idx = self.map(index);
53        self.vector.get(idx)
54    }
55    /// Get clone of raw vector`Vec<T>`
56    pub fn get_vec(&self) -> Vec<T> {
57        self.vector.clone()
58    }
59    /// Get mutable item`T` on `index` as `Option<&mut T>`
60    pub fn get_mut(&mut self, index: Index) -> Option<&mut T> {
61        let idx = self.map(index);
62        self.vector.get_mut(idx)
63    }
64    /// Is vector empty?
65    pub fn is_empty(&self) -> bool {
66        self.vector.is_empty()
67    }
68    /// Return size of `vector`
69    pub fn size(&self) -> usize {
70        self.vector.len()
71    }
72}
73
74impl<T> IntoIterator for DubleVec<T> {
75    type Item = T;
76    type IntoIter = std::vec::IntoIter<T>;
77
78    fn into_iter(self) -> Self::IntoIter {
79        self.vector.into_iter()
80    }
81}
82
83impl<'a, T> IntoIterator for &'a DubleVec<T> {
84    type Item = &'a T;
85    type IntoIter = std::slice::Iter<'a, T>;
86
87    fn into_iter(self) -> Self::IntoIter {
88        self.vector.iter()
89    }
90}
91
92impl<'a, T> IntoIterator for &'a mut DubleVec<T> {
93    type Item = &'a mut T;
94    type IntoIter = std::slice::IterMut<'a, T>;
95
96    fn into_iter(self) -> Self::IntoIter {
97        self.vector.iter_mut()
98    }
99}
100
101impl<T: std::fmt::Display> DubleVec<T> {
102    /// Print a formatted vector
103    pub fn print(&self) {
104        for y in 0..self.scale.h {
105            for x in 0..self.scale.w {
106                let index = Index { y, x };
107                let idx = index.y * self.scale.w + index.x;
108                print!("{} ", self.vector[idx]);
109            }
110            println!();
111        }
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn it_works() {
121        let mut vec: DubleVec<i32> = DubleVec::new(Size { w: 6, h: 5 });
122        vec.push(5, Index { x: 1, y: 1 });
123        if let Some(value) = vec.get(Index { x: 1, y: 1 }) {
124            println!("Value: {}", value);
125        } else {
126            println!("No value at this index");
127        }
128
129        println!("Size: {}", vec.size());
130        vec.print();
131    }
132}