allocated_btree/compressed/
iters.rs

1use core::ops::Add;
2use core::ops::Mul;
3
4use allocator_api2::alloc::Allocator;
5use generic_array::ArrayLength;
6use typenum::{Prod, Sum, U1, U2};
7
8use super::node::{IntoIter as NodeIntoIter, Iter as NodeIter, MutNodeRef, NodeRef};
9
10/// An iterator over the key-value pairs of a `CompressedBTreeMap`, in sorted order by key.
11///
12/// This struct is created by the [`iter`](super::AllocatedBTreeMap::iter) method on
13/// [`AllocatedBTreeMap`](super::AllocatedBTreeMap). See its documentation for more.
14pub struct Iter<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength>
15where
16    U2: Mul<B>,
17    Prod<U2, B>: ArrayLength,
18    U1: Add<Prod<U2, B>>,
19    Sum<U1, Prod<U2, B>>: ArrayLength,
20{
21    pub(super) inner: NodeIter<'a, K, V, B, NodeRef<'a, K, V, B>>,
22}
23
24impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength> Iterator
25    for Iter<'a, K, V, B>
26where
27    U2: Mul<B>,
28    Prod<U2, B>: ArrayLength,
29    U1: Add<Prod<U2, B>>,
30    Sum<U1, Prod<U2, B>>: ArrayLength,
31{
32    type Item = (&'a K, &'a V);
33
34    fn next(&mut self) -> Option<Self::Item> {
35        self.inner.next()
36    }
37}
38
39/// An iterator over the keys of a `CompressedBTreeMap`, in sorted order.
40///
41/// This struct is created by the [`keys`](super::AllocatedBTreeMap::keys) method on
42/// [`AllocatedBTreeMap`](super::AllocatedBTreeMap). See its documentation for more.
43pub struct Keys<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength>
44where
45    U2: Mul<B>,
46    Prod<U2, B>: ArrayLength,
47    U1: Add<Prod<U2, B>>,
48    Sum<U1, Prod<U2, B>>: ArrayLength,
49{
50    pub(super) inner: NodeIter<'a, K, V, B, NodeRef<'a, K, V, B>>,
51}
52
53impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength> Iterator
54    for Keys<'a, K, V, B>
55where
56    U2: Mul<B>,
57    Prod<U2, B>: ArrayLength,
58    U1: Add<Prod<U2, B>>,
59    Sum<U1, Prod<U2, B>>: ArrayLength,
60{
61    type Item = &'a K;
62
63    fn next(&mut self) -> Option<Self::Item> {
64        self.inner.next().map(|(k, _)| k)
65    }
66}
67
68/// An iterator over the values of a `CompressedBTreeMap`, in order by key.
69///
70/// This struct is created by the [`values`](super::AllocatedBTreeMap::values) method on
71/// [`AllocatedBTreeMap`](super::AllocatedBTreeMap). See its documentation for more.
72pub struct Values<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength>
73where
74    U2: Mul<B>,
75    Prod<U2, B>: ArrayLength,
76    U1: Add<Prod<U2, B>>,
77    Sum<U1, Prod<U2, B>>: ArrayLength,
78{
79    pub(super) inner: NodeIter<'a, K, V, B, NodeRef<'a, K, V, B>>,
80}
81
82impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength> Iterator
83    for Values<'a, K, V, B>
84where
85    U2: Mul<B>,
86    Prod<U2, B>: ArrayLength,
87    U1: Add<Prod<U2, B>>,
88    Sum<U1, Prod<U2, B>>: ArrayLength,
89{
90    type Item = &'a V;
91
92    fn next(&mut self) -> Option<Self::Item> {
93        self.inner.next().map(|(_, v)| v)
94    }
95}
96
97/// A mutable iterator over the values of a `CompressedBTreeMap`, in order by key.
98///
99/// This struct is created by the [`values_mut`](super::AllocatedBTreeMap::values_mut) method on
100/// [`AllocatedBTreeMap`](super::AllocatedBTreeMap). See its documentation for more.
101pub struct ValuesMut<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength>
102where
103    U2: Mul<B>,
104    Prod<U2, B>: ArrayLength,
105    U1: Add<Prod<U2, B>>,
106    Sum<U1, Prod<U2, B>>: ArrayLength,
107{
108    pub(super) inner: NodeIter<'a, K, V, B, MutNodeRef<'a, K, V, B>>,
109}
110
111impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength> Iterator
112    for ValuesMut<'a, K, V, B>
113where
114    U2: Mul<B>,
115    Prod<U2, B>: ArrayLength,
116    U1: Add<Prod<U2, B>>,
117    Sum<U1, Prod<U2, B>>: ArrayLength,
118{
119    type Item = &'a mut V;
120
121    fn next(&mut self) -> Option<Self::Item> {
122        self.inner.next().map(|(_, v)| v)
123    }
124}
125
126/// An owning iterator over the key-value pairs of a `CompressedBTreeMap`, in sorted order by key.
127///
128/// This struct is created by the [`into_iter`](std::iter::IntoIterator::into_iter) method on
129/// [`CompressedBTreeMap`](super::wrapper::CompressedBTreeMap) (provided by the [`IntoIterator`] trait).
130pub struct IntoIter<
131    'a,
132    K: core::cmp::PartialOrd + core::fmt::Debug,
133    V,
134    B: ArrayLength,
135    A: 'a + Allocator,
136> where
137    U2: Mul<B>,
138    Prod<U2, B>: ArrayLength,
139    U1: Add<Prod<U2, B>>,
140    Sum<U1, Prod<U2, B>>: ArrayLength,
141{
142    pub(super) inner: NodeIntoIter<'a, K, V, B, A>,
143}
144
145impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength, A: 'a + Allocator> Iterator
146    for IntoIter<'a, K, V, B, A>
147where
148    U2: Mul<B>,
149    Prod<U2, B>: ArrayLength,
150    U1: Add<Prod<U2, B>>,
151    Sum<U1, Prod<U2, B>>: ArrayLength,
152{
153    type Item = (K, V);
154
155    fn next(&mut self) -> Option<Self::Item> {
156        self.inner.next()
157    }
158}
159
160/// An owning iterator over the keys of a `CompressedBTreeMap`, in sorted order.
161///
162/// This struct is created internally when converting the map into an iterator over keys.
163pub struct IntoKeys<
164    'a,
165    K: core::cmp::PartialOrd + core::fmt::Debug,
166    V,
167    B: ArrayLength,
168    A: 'a + Allocator,
169> where
170    U2: Mul<B>,
171    Prod<U2, B>: ArrayLength,
172    U1: Add<Prod<U2, B>>,
173    Sum<U1, Prod<U2, B>>: ArrayLength,
174{
175    pub(super) inner: NodeIntoIter<'a, K, V, B, A>,
176}
177
178impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength, A: 'a + Allocator> Iterator
179    for IntoKeys<'a, K, V, B, A>
180where
181    U2: Mul<B>,
182    Prod<U2, B>: ArrayLength,
183    U1: Add<Prod<U2, B>>,
184    Sum<U1, Prod<U2, B>>: ArrayLength,
185{
186    type Item = K;
187
188    fn next(&mut self) -> Option<Self::Item> {
189        self.inner.next().map(|(k, _)| k)
190    }
191}
192
193/// An owning iterator over the values of a `CompressedBTreeMap`, in order by key.
194///
195/// This struct is created internally when converting the map into an iterator over values.
196pub struct IntoValues<
197    'a,
198    K: core::cmp::PartialOrd + core::fmt::Debug,
199    V,
200    B: ArrayLength,
201    A: 'a + Allocator,
202> where
203    U2: Mul<B>,
204    Prod<U2, B>: ArrayLength,
205    U1: Add<Prod<U2, B>>,
206    Sum<U1, Prod<U2, B>>: ArrayLength,
207{
208    pub(super) inner: NodeIntoIter<'a, K, V, B, A>,
209}
210
211impl<'a, K: core::cmp::PartialOrd + core::fmt::Debug, V, B: ArrayLength, A: 'a + Allocator> Iterator
212    for IntoValues<'a, K, V, B, A>
213where
214    U2: Mul<B>,
215    Prod<U2, B>: ArrayLength,
216    U1: Add<Prod<U2, B>>,
217    Sum<U1, Prod<U2, B>>: ArrayLength,
218{
219    type Item = V;
220
221    fn next(&mut self) -> Option<Self::Item> {
222        self.inner.next().map(|(_, v)| v)
223    }
224}