Skip to content

Commit

Permalink
Implement drain
Browse files Browse the repository at this point in the history
Losen unnecessary trait bounds
  • Loading branch information
Gianmarco Garrisi committed Aug 9, 2024
1 parent 4eecbd7 commit de363e4
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 282 deletions.
83 changes: 62 additions & 21 deletions src/core_iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,97 @@
//!
//! Usually you don't need to explicitly `use` any of the types declared here.
use core::iter::FusedIterator;

#[cfg(not(feature = "std"))]
pub(crate) mod std {
pub use ::alloc::vec;
pub use core::*;
}

use std::hash::Hash;
/// A draining iterator in arbitrary order over the couples
/// `(item, priority)` in the queue.
///
/// It can be obtained calling the `drain` method.
pub struct Drain<'a, I: 'a, P: 'a> {
pub(crate) iter: ::indexmap::map::Drain<'a, I, P>,
}

impl<'a, I: 'a, P: 'a> Iterator for Drain<'a, I, P> {
type Item = (I, P);
fn next(&mut self) -> Option<(I, P)> {
self.iter.next()
}
}

impl<I, P> DoubleEndedIterator for Drain<'_, I, P> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}

impl<I, P> ExactSizeIterator for Drain<'_, I, P> {
fn len(&self) -> usize {
self.iter.len()
}
}

impl<I, P> FusedIterator for Drain<'_, I, P> {}

/// An iterator in arbitrary order over the couples
/// `(item, priority)` in the queue.
///
/// It can be obtained calling the `iter` method.
pub struct Iter<'a, I: 'a, P: 'a>
where
I: Hash + Eq,
P: Ord,
{
pub struct Iter<'a, I: 'a, P: 'a> {
pub(crate) iter: ::indexmap::map::Iter<'a, I, P>,
}

impl<'a, I: 'a, P: 'a> Iterator for Iter<'a, I, P>
where
I: Hash + Eq,
P: Ord,
{
impl<'a, I: 'a, P: 'a> Iterator for Iter<'a, I, P> {
type Item = (&'a I, &'a P);
fn next(&mut self) -> Option<(&'a I, &'a P)> {
self.iter.next()
}
}

impl<I, P> DoubleEndedIterator for Iter<'_, I, P> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}

impl<I, P> ExactSizeIterator for Iter<'_, I, P> {
fn len(&self) -> usize {
self.iter.len()
}
}

impl<I, P> FusedIterator for Iter<'_, I, P> {}

/// An iterator in arbitrary order over the couples
/// `(item, priority)` that consumes the queue.
///
/// It can be obtained calling the `into_iter` method from the `IntoIterator` trait.
pub struct IntoIter<I, P>
where
I: Hash + Eq,
P: Ord,
{
pub struct IntoIter<I, P> {
pub(crate) iter: ::indexmap::map::IntoIter<I, P>,
}

impl<I, P> Iterator for IntoIter<I, P>
where
I: Hash + Eq,
P: Ord,
{
impl<I, P> Iterator for IntoIter<I, P> {
type Item = (I, P);
fn next(&mut self) -> Option<(I, P)> {
self.iter.next()
}
}

impl<I, P> DoubleEndedIterator for IntoIter<I, P> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}

impl<I, P> ExactSizeIterator for IntoIter<I, P> {
fn len(&self) -> usize {
self.iter.len()
}
}

impl<I, P> FusedIterator for IntoIter<I, P> {}
59 changes: 48 additions & 11 deletions src/double_priority_queue/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ pub(crate) mod std {
}

use core::hash::BuildHasher;
use std::cmp::{Eq, Ord};
use std::cmp::Ord;
#[cfg(feature = "std")]
use std::collections::hash_map::RandomState;
use std::hash::Hash;
use std::iter::*;

use crate::DoublePriorityQueue;
Expand All @@ -58,7 +57,6 @@ use crate::DoublePriorityQueue;
#[cfg(feature = "std")]
pub struct IterMut<'a, I: 'a, P: 'a, H: 'a = RandomState>
where
I: Hash + Eq,
P: Ord,
{
pq: &'a mut DoublePriorityQueue<I, P, H>,
Expand All @@ -68,7 +66,6 @@ where
#[cfg(not(feature = "std"))]
pub struct IterMut<'a, I: 'a, P: 'a, H: 'a>
where
I: Hash + Eq,
P: Ord,
{
pq: &'a mut DoublePriorityQueue<I, P, H>,
Expand All @@ -77,7 +74,6 @@ where

impl<'a, I: 'a, P: 'a, H: 'a> IterMut<'a, I, P, H>
where
I: Hash + Eq,
P: Ord,
{
pub(crate) fn new(pq: &'a mut DoublePriorityQueue<I, P, H>) -> Self {
Expand All @@ -87,7 +83,6 @@ where

impl<'a, 'b: 'a, I: 'a, P: 'a, H: 'a> Iterator for IterMut<'a, I, P, H>
where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
{
Expand All @@ -106,9 +101,44 @@ where
}
}

impl<'a, 'b: 'a, I: 'a, P: 'a, H: 'a> DoubleEndedIterator for IterMut<'a, I, P, H>
where
P: Ord,
H: BuildHasher,
{
fn next_back(&mut self) -> Option<Self::Item> {
use indexmap::map::MutableKeys;
let r: Option<(&'a mut I, &'a mut P)> = self
.pq
.store
.map
.get_index_mut2(self.pos)
.map(|(i, p)| (i as *mut I, p as *mut P))
.map(|(i, p)| unsafe { (i.as_mut().unwrap(), p.as_mut().unwrap()) });
self.pos -= 1;
r
}
}

impl<'a, I, P, H> ExactSizeIterator for IterMut<'_, I, P, H>
where
P: Ord,
H: BuildHasher,
{
fn len(&self) -> usize {
self.pq.len()
}
}

impl<'a, I, P, H> FusedIterator for IterMut<'_, I, P, H>
where
P: Ord,
H: BuildHasher,
{
}

impl<'a, I: 'a, P: 'a, H: 'a> Drop for IterMut<'a, I, P, H>
where
I: Hash + Eq,
P: Ord,
{
fn drop(&mut self) {
Expand All @@ -127,7 +157,6 @@ where
#[cfg(feature = "std")]
pub struct IntoSortedIter<I, P, H = RandomState>
where
I: Hash + Eq,
P: Ord,
{
pub(crate) pq: DoublePriorityQueue<I, P, H>,
Expand All @@ -136,15 +165,13 @@ where
#[cfg(not(feature = "std"))]
pub struct IntoSortedIter<I, P, H>
where
I: Hash + Eq,
P: Ord,
{
pub(crate) pq: DoublePriorityQueue<I, P, H>,
}

impl<I, P, H> Iterator for IntoSortedIter<I, P, H>
where
I: Hash + Eq,
P: Ord,
{
type Item = (I, P);
Expand All @@ -155,10 +182,20 @@ where

impl<I, P, H> DoubleEndedIterator for IntoSortedIter<I, P, H>
where
I: Hash + Eq,
P: Ord,
{
fn next_back(&mut self) -> Option<(I, P)> {
self.pq.pop_max()
}
}

impl<I, P, H> ExactSizeIterator for IntoSortedIter<I, P, H>
where
P: Ord,
{
fn len(&self) -> usize {
self.pq.len()
}
}

impl<I, P, H> FusedIterator for IntoSortedIter<I, P, H> where P: Ord {}
Loading

0 comments on commit de363e4

Please sign in to comment.