Skip to main content

duble_vec/
lib.rs

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