emap/
values.rs

1// Copyright (c) 2023 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::Map;
22use crate::{IntoValues, Values};
23use std::marker::PhantomData;
24
25impl<'a, V: Clone + 'a> Iterator for Values<'a, V> {
26    type Item = &'a V;
27
28    #[inline]
29    #[must_use]
30    fn next(&mut self) -> Option<Self::Item> {
31        while self.pos < self.max {
32            let opt = unsafe { &*self.head.add(self.pos) };
33            self.pos += 1;
34            if opt.is_some() {
35                return opt.as_ref();
36            }
37        }
38        None
39    }
40}
41
42impl<V: Copy> Iterator for IntoValues<V> {
43    type Item = V;
44
45    #[inline]
46    #[must_use]
47    fn next(&mut self) -> Option<Self::Item> {
48        while self.pos < self.max {
49            let opt = unsafe { &*self.head.add(self.pos) };
50            self.pos += 1;
51            if opt.is_some() {
52                return *opt;
53            }
54        }
55        None
56    }
57}
58
59impl<V: Clone> Map<V> {
60    /// Make an iterator over all values.
61    ///
62    /// # Panics
63    ///
64    /// It may panic in debug mode, if the [`Map`] is not initialized.
65    #[inline]
66    #[must_use]
67    pub const fn values(&self) -> Values<V> {
68        #[cfg(debug_assertions)]
69        assert!(self.initialized, "Can't values() non-initialized Map");
70        Values {
71            max: self.max,
72            pos: 0,
73            head: self.head,
74            _marker: PhantomData,
75        }
76    }
77
78    /// Make an into-iterator over all items.
79    ///
80    /// # Panics
81    ///
82    /// It may panic in debug mode, if the [`Map`] is not initialized.
83    #[inline]
84    #[must_use]
85    pub const fn into_values(&self) -> IntoValues<V> {
86        #[cfg(debug_assertions)]
87        assert!(self.initialized, "Can't into_values() non-initialized Map");
88        IntoValues {
89            max: self.max,
90            pos: 0,
91            head: self.head,
92        }
93    }
94}
95
96#[test]
97fn empty_values() {
98    let m: Map<u32> = Map::with_capacity_none(16);
99    assert!(m.values().next().is_none());
100}
101
102#[test]
103fn insert_and_jump_over_next() {
104    let mut m: Map<&str> = Map::with_capacity_none(16);
105    m.insert(0, "foo");
106    let mut values = m.into_values();
107    assert_eq!("foo", values.next().unwrap());
108    assert!(values.next().is_none());
109}
110
111#[test]
112fn count_them_all() {
113    let mut m: Map<&str> = Map::with_capacity_none(16);
114    m.insert(0, "one");
115    m.insert(1, "two");
116    m.insert(2, "three");
117    assert_eq!(3, m.into_values().count());
118}