priority_queue/
core_iterators.rs

1/*
2 *  Copyright 2017 Gianmarco Garrisi
3 *
4 *
5 *  This program is free software: you can redistribute it and/or modify
6 *  it under the terms of the GNU Lesser General Public License as published by
7 *  the Free Software Foundation, either version 3 of the License, or
8 *  (at your option) any later version, or (at your option) under the terms
9 *  of the Mozilla Public License version 2.0.
10 *
11 *  ----
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public License
19 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 *
21 *  ----
22 *
23 *  This Source Code Form is subject to the terms of the Mozilla Public License,
24 *  v. 2.0. If a copy of the MPL was not distributed with this file, You can
25 *  obtain one at http://mozilla.org/MPL/2.0/.
26 *
27 */
28//! This module defines iterator types that are used with
29//! both the [`PriorityQueue`](super::PriorityQueue) and the [`DoublePriorityQueue`](super::DoublePriorityQueue)
30//!
31//! Usually you don't need to explicitly `use` any of the types declared here.
32
33use core::iter::FusedIterator;
34
35#[cfg(not(feature = "std"))]
36pub(crate) mod std {
37    pub use ::alloc::vec;
38    pub use core::*;
39}
40
41/// A draining iterator in arbitrary order over the couples
42/// `(item, priority)` in the queue.
43///
44/// It can be obtained calling the `drain` method.
45pub struct Drain<'a, I: 'a, P: 'a> {
46    pub(crate) iter: ::indexmap::map::Drain<'a, I, P>,
47}
48
49impl<'a, I: 'a, P: 'a> Iterator for Drain<'a, I, P> {
50    type Item = (I, P);
51    fn next(&mut self) -> Option<(I, P)> {
52        self.iter.next()
53    }
54}
55
56impl<I, P> DoubleEndedIterator for Drain<'_, I, P> {
57    fn next_back(&mut self) -> Option<Self::Item> {
58        self.iter.next_back()
59    }
60}
61
62impl<I, P> ExactSizeIterator for Drain<'_, I, P> {
63    fn len(&self) -> usize {
64        self.iter.len()
65    }
66}
67
68impl<I, P> FusedIterator for Drain<'_, I, P> {}
69
70/// An iterator in arbitrary order over the couples
71/// `(item, priority)` in the queue.
72///
73/// It can be obtained calling the `iter` method.
74pub struct Iter<'a, I: 'a, P: 'a> {
75    pub(crate) iter: ::indexmap::map::Iter<'a, I, P>,
76}
77
78impl<'a, I: 'a, P: 'a> Iterator for Iter<'a, I, P> {
79    type Item = (&'a I, &'a P);
80    fn next(&mut self) -> Option<(&'a I, &'a P)> {
81        self.iter.next()
82    }
83}
84
85impl<I, P> DoubleEndedIterator for Iter<'_, I, P> {
86    fn next_back(&mut self) -> Option<Self::Item> {
87        self.iter.next_back()
88    }
89}
90
91impl<I, P> ExactSizeIterator for Iter<'_, I, P> {
92    fn len(&self) -> usize {
93        self.iter.len()
94    }
95}
96
97impl<I, P> FusedIterator for Iter<'_, I, P> {}
98
99/// An iterator in arbitrary order over the couples
100/// `(item, priority)` that consumes the queue.
101///
102/// It can be obtained calling the `into_iter` method from the [`IntoIterator`] trait.
103pub struct IntoIter<I, P> {
104    pub(crate) iter: ::indexmap::map::IntoIter<I, P>,
105}
106
107impl<I, P> Iterator for IntoIter<I, P> {
108    type Item = (I, P);
109    fn next(&mut self) -> Option<(I, P)> {
110        self.iter.next()
111    }
112}
113
114impl<I, P> DoubleEndedIterator for IntoIter<I, P> {
115    fn next_back(&mut self) -> Option<Self::Item> {
116        self.iter.next_back()
117    }
118}
119
120impl<I, P> ExactSizeIterator for IntoIter<I, P> {
121    fn len(&self) -> usize {
122        self.iter.len()
123    }
124}
125
126impl<I, P> FusedIterator for IntoIter<I, P> {}