rust_3d/
is_index_container.rs

1/*
2Copyright 2020 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! IsIndexContainer trait for containers holding indices
24
25/// IsIndexContainer trait for containers holding indices
26
27pub trait IsIndexContainer: Clone + Default {
28    /// Should reserve space for n more elements
29    fn reserve(&mut self, n: usize);
30
31    /// Should ensure that given number can be supported
32    fn ensure_supported(&mut self, x: usize);
33
34    /// Should return the number of elements
35    fn len(&self) -> usize;
36
37    /// Should return the element at index
38    fn get(&self, index: usize) -> usize;
39
40    /// Should overwrite the element at index with value
41    fn set(&mut self, index: usize, value: usize);
42
43    /// Should push value to the end of the container
44    fn push(&mut self, value: usize);
45
46    /// Should return an iterator over the values
47    fn iter(&self) -> IsIndexContainerIterator<Self>;
48
49    /// Creates a new object with the given capacity
50    fn with_capacity(n: usize) -> Self {
51        let mut result = Self::default();
52        result.reserve(n);
53        result
54    }
55
56    /// Creates a new object that can support the given number
57    fn with_support_for(x: usize) -> Self {
58        let mut result = Self::default();
59        result.ensure_supported(x);
60        result
61    }
62
63    /// Creates a new object with the given capacity and support for the given number
64    fn with_capacity_and_support_for(n: usize, x: usize) -> Self {
65        let mut result = Self::default();
66        result.ensure_supported(x);
67        result.reserve(n);
68        result
69    }
70}
71
72//------------------------------------------------------------------------------
73
74/// Iterator for IsIndexContainer
75pub struct IsIndexContainerIterator<'a, IC>
76where
77    IC: IsIndexContainer,
78{
79    parent: &'a IC,
80    max: usize,
81    index: usize,
82}
83
84impl<'a, IC> IsIndexContainerIterator<'a, IC>
85where
86    IC: IsIndexContainer,
87{
88    pub fn new(parent: &'a IC) -> Self {
89        Self {
90            parent,
91            max: parent.len(),
92            index: 0,
93        }
94    }
95}
96
97impl<'a, IC> Iterator for IsIndexContainerIterator<'a, IC>
98where
99    IC: IsIndexContainer,
100{
101    type Item = usize;
102
103    fn next(&mut self) -> Option<usize> {
104        let result = if self.index < self.max {
105            Some(self.parent.get(self.index))
106        } else {
107            None
108        };
109
110        self.index += 1;
111
112        result
113    }
114}