mywheel_rs/array_like.rs
1/// The `RepeatArray` struct represents an array that contains a single value repeated a specified
2/// number of times.
3///
4/// Properties:
5///
6/// * `value`: The `value` property is a generic type `T` that represents the value that will be repeated in the array.
7/// * `size`: The `size` property represents the number of elements in the array.
8pub struct RepeatArray<T> {
9 value: T,
10 size: usize,
11}
12
13impl<T: Copy> RepeatArray<T> {
14 /// The function creates a new RepeatArray with a given value and size.
15 ///
16 /// Arguments:
17 ///
18 /// * `value`: The value parameter is the value that will be repeated in the array.
19 /// * `size`: The `size` parameter represents the desired size of the `RepeatArray`. It specifies
20 /// the number of elements that the `RepeatArray` should contain.
21 ///
22 /// Returns:
23 ///
24 /// The `new` function is returning an instance of the `RepeatArray<T>` struct.
25 ///
26 /// Examples:
27 ///
28 /// ```rust
29 /// use mywheel_rs::array_like::RepeatArray;
30 /// let array = RepeatArray::new(1, 5);
31 /// assert_eq!(array.len(), 5);
32 /// assert_eq!(array[0], 1);
33 /// assert_eq!(array[1], 1);
34 /// ```
35 pub fn new(value: T, size: usize) -> RepeatArray<T> {
36 RepeatArray { value, size }
37 }
38
39 pub fn get(&self, _index: usize) -> T {
40 self.value
41 }
42
43 pub fn iter(&self) -> RepeatArrayIterator<T> {
44 RepeatArrayIterator {
45 value: self.value,
46 size: self.size,
47 }
48 }
49
50 pub fn len(&self) -> usize {
51 self.size
52 }
53}
54
55pub struct RepeatArrayIterator<T> {
56 value: T,
57 size: usize,
58}
59
60impl<T: Copy> Iterator for RepeatArrayIterator<T> {
61 type Item = T;
62
63 fn next(&mut self) -> Option<Self::Item> {
64 if self.size > 0 {
65 self.size -= 1;
66 Some(self.value)
67 } else {
68 None
69 }
70 }
71}
72
73impl<T: Copy> ExactSizeIterator for RepeatArrayIterator<T> {
74 fn len(&self) -> usize {
75 self.size
76 }
77}
78
79impl<T: Copy> std::ops::Index<usize> for RepeatArray<T> {
80 type Output = T;
81
82 /// The `index` function returns a reference to the value at the specified index.
83 ///
84 /// Arguments:
85 ///
86 /// * `_index`: The `_index` parameter is of type `usize`, which represents an index value used to
87 /// access elements in a collection or array.
88 ///
89 /// Returns:
90 ///
91 /// The method is returning a reference to the value stored in the `self` object.
92 ///
93 /// Examples:
94 ///
95 /// ```rust
96 /// use mywheel_rs::array_like::RepeatArray;
97 /// let array = RepeatArray::new(1, 5);
98 /// assert_eq!(array[0], 1);
99 /// assert_eq!(array[1], 1);
100 /// ```
101 fn index(&self, _index: usize) -> &Self::Output {
102 &self.value
103 }
104}
105
106/// The ShiftArray type represents an array that can be shifted to the left or right without copying or
107/// moving its elements.
108///
109/// Properties:
110///
111/// * `start`: The `start` property represents the index of the first element in the `ShiftArray`. It
112/// indicates the starting point from which elements are accessed or shifted.
113/// * `lst`: The `lst` property is a vector that holds the elements of the `ShiftArray`. It is of type
114/// `Vec<T>`, where `T` is a generic type parameter that can be replaced with any type.
115#[derive(Debug, Clone, PartialEq, Eq)]
116pub struct ShiftArray<T> {
117 pub start: usize,
118 pub lst: Vec<T>,
119}
120
121impl<T> ShiftArray<T> {
122 /// The function "new" initializes a new instance of a struct with a starting index of 0 and a given
123 /// list.
124 ///
125 /// Arguments:
126 ///
127 /// * `lst`: The `lst` parameter is a `Vec<T>`, which is a vector of elements of type `T`.
128 ///
129 /// Returns:
130 ///
131 /// The `new` function is returning an instance of the struct that it is defined in.
132 ///
133 /// Examples:
134 ///
135 /// ```
136 /// use mywheel_rs::array_like::ShiftArray;
137 ///
138 /// let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
139 /// assert_eq!(shift_array.start, 0);
140 /// assert_eq!(shift_array.lst, vec![1, 2, 3]);
141 /// assert_eq!(shift_array.len(), 3);
142 /// assert_eq!(shift_array[0], 1);
143 /// ```
144 pub fn new(lst: Vec<T>) -> Self {
145 Self { start: 0, lst }
146 }
147
148 /// The function sets the start value of a variable.
149 ///
150 /// Arguments:
151 ///
152 /// * `start`: The `start` parameter is of type `usize`, which represents an unsigned integer that
153 /// can hold the size of any object in memory. It is used to set the value of the `start` field in
154 /// the struct or object that this method belongs to.
155 ///
156 /// # Examples
157 ///
158 /// ```
159 /// use mywheel_rs::array_like::ShiftArray;
160 ///
161 /// let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
162 /// shift_array.set_start(1);
163 /// assert_eq!(shift_array.start, 1);
164 /// assert_eq!(shift_array.len(), 3);
165 /// ```
166 pub fn set_start(&mut self, start: usize) {
167 self.start = start;
168 }
169
170 /// The `items` function returns an iterator that yields the index and reference to each element in
171 /// the `lst` vector, with the index adjusted by the `start` value.
172 ///
173 /// # Examples
174 ///
175 /// ```rust
176 /// use mywheel_rs::array_like::ShiftArray;
177 /// let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
178 /// shift_array.set_start(1);
179 /// for (i, v) in shift_array.items() {
180 /// assert_eq!(i, *v as usize);
181 /// }
182 /// ```
183 pub fn items(&self) -> impl Iterator<Item = (usize, &T)> {
184 self.lst
185 .iter()
186 .enumerate()
187 .map(move |(i, v)| (i + self.start, v))
188 }
189
190 pub fn iter(&self) -> ShiftArrayIterator<'_, T> {
191 ShiftArrayIterator {
192 array: self,
193 current: self.start,
194 }
195 }
196
197 pub fn len(&self) -> usize {
198 self.lst.len()
199 }
200}
201
202pub struct ShiftArrayIterator<'a, T> {
203 array: &'a ShiftArray<T>,
204 current: usize,
205}
206
207impl<'a, T: Clone> Iterator for ShiftArrayIterator<'a, T> {
208 type Item = T;
209
210 fn next(&mut self) -> Option<Self::Item> {
211 if self.current < self.array.lst.len() {
212 let value = self.array.lst[self.current].clone();
213 self.current += 1;
214 Some(value)
215 } else {
216 None
217 }
218 }
219}
220
221impl<T> std::ops::Index<usize> for ShiftArray<T> {
222 type Output = T;
223
224 /// The `index` function returns a reference to an element in a list based on a given key.
225 ///
226 /// Arguments:
227 ///
228 /// * `key`: The `key` parameter is of type `usize` and represents the index of the element to be
229 /// accessed in the `lst` field.
230 ///
231 /// Returns:
232 ///
233 /// The method `index` returns a reference to an element of `self.lst` at the specified index `key -
234 /// self.start`.
235 ///
236 /// # Examples
237 ///
238 /// ```
239 /// use mywheel_rs::array_like::ShiftArray;
240 /// let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
241 /// assert_eq!(shift_array[2], 3);
242 /// shift_array.set_start(1);
243 /// assert_eq!(shift_array[2], 2);
244 /// ```
245 fn index(&self, key: usize) -> &Self::Output {
246 &self.lst[key - self.start]
247 }
248}
249
250impl<T> std::ops::IndexMut<usize> for ShiftArray<T> {
251 /// The function `index_mut` returns a mutable reference to an element in a list based on a given
252 /// key.
253 ///
254 /// Arguments:
255 ///
256 /// * `key`: The `key` parameter is of type `usize` and represents the index of the element to be
257 /// accessed in the `lst` vector.
258 ///
259 /// Returns:
260 ///
261 /// A mutable reference to an element in the `lst` vector, located at the index `key - self.start`.
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// use mywheel_rs::array_like::ShiftArray;
267 /// let mut shift_array = ShiftArray::new(vec![1, 2, 3]);
268 /// assert_eq!(shift_array[2], 3);
269 /// shift_array.set_start(1);
270 /// assert_eq!(shift_array[2], 2);
271 /// shift_array[2] = 4;
272 /// assert_eq!(shift_array[2], 4);
273 /// ```
274 fn index_mut(&mut self, key: usize) -> &mut Self::Output {
275 &mut self.lst[key - self.start]
276 }
277}
278
279#[cfg(test)]
280mod tests {
281 use super::*;
282
283 #[test]
284 fn test_repeat_array() {
285 let arr: RepeatArray<i32> = RepeatArray::new(1, 10);
286 assert_eq!(arr.len(), 10);
287 assert_eq!(arr[4], 1);
288 for i in arr.iter() {
289 assert_eq!(i, 1);
290 }
291 }
292
293 #[test]
294 fn test_shift_array() {
295 let mut a = ShiftArray::new(vec![2, 3, 5, 7, 11]);
296 a.set_start(5);
297 assert_eq!(a[6], 3);
298 assert_eq!(a.len(), 5);
299 a[6] = 13;
300 assert_eq!(a[6], 13);
301 let mut cnt = 5;
302 for v in a.iter() {
303 assert_eq!(v, a[cnt]);
304 cnt += 1;
305 }
306 for (i, v) in a.items() {
307 println!("{}: {}", i, v);
308 }
309 }
310
311 #[test]
312 fn test_repeat_array2() {
313 let repeat_array: RepeatArray<i32> = RepeatArray::new(1, 5);
314 assert_eq!(repeat_array.value, 1);
315 assert_eq!(repeat_array.size, 5);
316 assert_eq!(repeat_array[0], 1);
317 assert_eq!(repeat_array[1], 1);
318 assert_eq!(repeat_array[2], 1);
319 assert_eq!(repeat_array[3], 1);
320 assert_eq!(repeat_array[4], 1);
321 assert_eq!(repeat_array.get(0), 1);
322 assert_eq!(repeat_array.get(1), 1);
323 assert_eq!(repeat_array.get(2), 1);
324 assert_eq!(repeat_array.get(3), 1);
325 assert_eq!(repeat_array.get(4), 1);
326 for i in repeat_array.iter() {
327 assert_eq!(i, 1);
328 }
329 }
330
331 #[test]
332 fn test_shift_array2() {
333 let mut shift_array: ShiftArray<i32> = ShiftArray::new(vec![1, 2, 3, 4, 5]);
334 shift_array.set_start(3);
335 assert_eq!(shift_array[6], 4);
336 assert_eq!(shift_array[7], 5);
337 shift_array[6] = 8;
338 assert_eq!(shift_array[6], 8);
339 for (i, v) in shift_array.items() {
340 assert_eq!(v, &shift_array[i]);
341 }
342 }
343}