Skip to main content

range_set_blaze/
keys.rs

1use alloc::collections::btree_map;
2use core::iter::FusedIterator;
3
4use crate::{
5    Integer, SortedDisjointMap,
6    iter_map::{IntoIterMap, IterMap},
7    map::{EndValue, ValueRef},
8};
9
10/// An iterator over the integer elements of a [`RangeMapBlaze`]. Double-ended.
11///
12/// This `struct` is created by the [`keys`] method on [`RangeMapBlaze`]. See its
13/// documentation for more.
14///
15/// [`keys`]: crate::RangeMapBlaze::keys
16/// [`RangeMapBlaze`]: crate::RangeMapBlaze
17#[must_use = "iterators are lazy and do nothing unless consumed"]
18#[derive(Clone, Debug)]
19pub struct Keys<T, VR, I> {
20    iter: IterMap<T, VR, I>,
21}
22
23impl<T, VR, I> Keys<T, VR, I>
24where
25    T: Integer,
26    VR: ValueRef,
27    I: SortedDisjointMap<T, VR>,
28{
29    pub(crate) const fn new(iter: I) -> Self {
30        Self {
31            iter: IterMap::new(iter),
32        }
33    }
34}
35
36impl<T, VR, I> FusedIterator for Keys<T, VR, I>
37where
38    T: Integer,
39    VR: ValueRef,
40    I: SortedDisjointMap<T, VR> + FusedIterator,
41{
42}
43
44impl<T, VR, I> Iterator for Keys<T, VR, I>
45where
46    T: Integer,
47    VR: ValueRef,
48    I: SortedDisjointMap<T, VR>,
49{
50    type Item = T;
51
52    fn next(&mut self) -> Option<Self::Item> {
53        self.iter.next().map(|(key, _value)| key)
54    }
55
56    fn size_hint(&self) -> (usize, Option<usize>) {
57        self.iter.size_hint()
58    }
59}
60
61impl<T, VR, I> DoubleEndedIterator for Keys<T, VR, I>
62where
63    T: Integer,
64    VR: ValueRef,
65    I: SortedDisjointMap<T, VR> + DoubleEndedIterator,
66{
67    fn next_back(&mut self) -> Option<Self::Item> {
68        self.iter.next_back().map(|(key, _value)| key)
69    }
70}
71
72/// An iterator over the integer elements of a [`RangeMapBlaze`]. Double-ended.
73///
74/// This `struct` is created by the [`into_keys`] method on [`RangeMapBlaze`]. See its
75/// documentation for more.
76///
77/// [`into_keys`]: crate::RangeMapBlaze::into_keys
78/// [`RangeMapBlaze`]: crate::RangeMapBlaze
79#[must_use = "iterators are lazy and do nothing unless consumed"]
80#[derive(Debug)]
81pub struct IntoKeys<T, V> {
82    into_iter: IntoIterMap<T, V>,
83}
84
85impl<T, V> IntoKeys<T, V>
86where
87    T: Integer,
88    V: Eq + Clone,
89{
90    pub(crate) const fn new(btree_map_into_iter: btree_map::IntoIter<T, EndValue<T, V>>) -> Self {
91        let into_iter = IntoIterMap::new(btree_map_into_iter);
92        Self { into_iter }
93    }
94}
95
96impl<T, V> Iterator for IntoKeys<T, V>
97where
98    T: Integer,
99    V: Eq + Clone,
100{
101    type Item = T;
102
103    fn next(&mut self) -> Option<Self::Item> {
104        self.into_iter.next().map(|(key, _value)| key)
105    }
106
107    fn size_hint(&self) -> (usize, Option<usize>) {
108        self.into_iter.size_hint()
109    }
110}
111
112impl<T, V> FusedIterator for IntoKeys<T, V>
113where
114    T: Integer,
115    V: Eq + Clone,
116{
117}
118
119impl<T, V> DoubleEndedIterator for IntoKeys<T, V>
120where
121    T: Integer,
122    V: Eq + Clone,
123{
124    fn next_back(&mut self) -> Option<Self::Item> {
125        self.into_iter.next_back().map(|(key, _value)| key)
126    }
127}