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 + PartialEq> 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) -> Option<usize> {
29        if index.x < self.scale.w && index.y < self.scale.h {
30            Some(index.y * self.scale.w + index.x)
31        } else {
32            None
33        }
34    }
35    /// Push new item`T` on `index`
36    pub fn push(&mut self, item: T, index: Index) {
37        if let Some(idx) = self.map(index) {
38            if self.vector[idx] == T::default() {
39                self.vector[idx] = item;
40            }
41        }
42    }
43    /// Force push new item`T` on `index`
44    pub fn fush(&mut self, item: T, index: Index) {
45        if let Some(idx) = self.map(index) {
46            self.vector[idx] = item;
47        }
48    }
49    /// Remove item`T` on `index`
50    /// Set item`T` on `index` to default value
51    pub fn remove(&mut self, index: Index) {
52        if let Some(idx) = self.map(index) {
53            self.vector[idx] = T::default();
54        }
55    }
56    /// Flip the vector
57    pub fn reverse(&mut self) {
58        self.vector.reverse();
59    }
60    /// Get item`T` on `index` as `Option<&T>`
61    pub fn get(&self, index: Index) -> Option<&T> {
62        if let Some(idx) = self.map(index) {
63            self.vector.get(idx)
64        } else {
65            None
66        }
67    }
68    /// Get clone of raw vector`Vec<T>`
69    pub fn get_vec(&self) -> Vec<T> {
70        self.vector.clone()
71    }
72    /// Get mutable item`T` on `index` as `Option<&mut T>`
73    pub fn get_mut(&mut self, index: Index) -> Option<&mut T> {
74        if let Some(idx) = self.map(index) {
75            self.vector.get_mut(idx)
76        } else {
77            None
78        }
79    }
80    /// Is vector empty?
81    pub fn is_empty(&self) -> bool {
82        self.vector.is_empty()
83    }
84    /// Return size of `vector`
85    pub fn size(&self) -> usize {
86        self.vector.len()
87    }
88}
89
90impl<T> IntoIterator for DubleVec<T> {
91    type Item = T;
92    type IntoIter = std::vec::IntoIter<T>;
93
94    fn into_iter(self) -> Self::IntoIter {
95        self.vector.into_iter()
96    }
97}
98
99impl<'a, T> IntoIterator for &'a DubleVec<T> {
100    type Item = &'a T;
101    type IntoIter = std::slice::Iter<'a, T>;
102
103    fn into_iter(self) -> Self::IntoIter {
104        self.vector.iter()
105    }
106}
107
108impl<'a, T> IntoIterator for &'a mut DubleVec<T> {
109    type Item = &'a mut T;
110    type IntoIter = std::slice::IterMut<'a, T>;
111
112    fn into_iter(self) -> Self::IntoIter {
113        self.vector.iter_mut()
114    }
115}
116
117impl<T: std::fmt::Display> DubleVec<T> {
118    /// Print a formatted vector
119    pub fn print(&self) {
120        for y in 0..self.scale.h {
121            for x in 0..self.scale.w {
122                let index = Index { y, x };
123                let idx = index.y * self.scale.w + index.x;
124                print!("{}", self.vector[idx]);
125            }
126            println!();
127        }
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::*;
134
135    #[test]
136    fn it_works() {
137        let mut vec: DubleVec<i32> = DubleVec::new(Size { w: 6, h: 5 });
138        vec.push(5, Index { x: 1, y: 1 }); // 5
139        vec.fush(2, Index { x: 1, y: 1 }); // 2
140        vec.push(4, Index { x: 1, y: 1 }); // 2
141        if let Some(value) = vec.get(Index { x: 1, y: 1 }) {
142            println!("Value: {}", value);
143        } else {
144            println!("No value at this index");
145        }
146
147        println!("Size: {}", vec.size());
148        vec.print();
149    }
150}