Unverified Commit a1eb27d6 authored by Michael Müller's avatar Michael Müller Committed by GitHub
Browse files

Remove iter_mut() for BinaryHeap (#569)

* [storage] Remove iter_mut() for BinaryHeap

* [storage] Remove unused import
parent eee8dd1d
Pipeline #113351 failed with stages
in 19 minutes and 15 seconds
......@@ -23,7 +23,6 @@ use super::{
StorageVec,
};
use crate::{
collections::extend_lifetime,
traits::{
KeyPtr,
PackedLayout,
......@@ -179,17 +178,6 @@ where
Iter::new(&self)
}
/// Returns an iterator yielding exclusive references to all elements of the heap.
///
/// # Note
///
/// Avoid unbounded iteration over big storage heaps.
/// Prefer using methods like `Iterator::take` in order to limit the number
/// of yielded elements.
pub fn iter_mut(&mut self) -> IterMut<T> {
IterMut::new(self)
}
/// Returns a shared reference to the first element if any.
pub fn first(&self) -> Option<&T> {
if self.is_empty() {
......@@ -383,87 +371,3 @@ where
self.elements.get_child(cur)?.child.as_ref()
}
}
/// An iterator over exclusive references to the elements of a heap.
#[derive(Debug)]
pub struct IterMut<'a, T>
where
T: PackedLayout + Ord,
{
/// The heap elements to iterate over.
elements: &'a mut ChildrenVec<T>,
/// The current begin of the iteration.
begin: u32,
/// The current end of the iteration.
end: u32,
}
impl<'a, T> IterMut<'a, T>
where
T: PackedLayout + Ord,
{
/// Creates a new iterator for the given heap elements.
pub fn new(elements: &'a mut ChildrenVec<T>) -> Self {
let end = elements.len();
Self {
elements,
begin: 0,
end,
}
}
/// Returns the amount of remaining elements to yield by the iterator.
fn remaining(&self) -> u32 {
self.end - self.begin
}
}
impl<'a, T> IterMut<'a, T>
where
T: PackedLayout + Ord,
{
fn get_mut<'b>(&'b mut self, at: u32) -> Option<&'a mut T> {
let child_mut = self.elements.get_child_mut(at)?.child.as_mut();
child_mut.map(|value| {
// SAFETY: We extend the lifetime of the reference here.
//
// This is safe because the iterator yields an exclusive
// reference to every element in the iterated heap
// just once and also there can be only one such iterator
// for the same heap at the same time which is
// guaranteed by the constructor of the iterator.
unsafe { extend_lifetime::<'b, 'a, T>(value) }
})
}
}
impl<'a, T> Iterator for IterMut<'a, T>
where
T: PackedLayout + Ord,
{
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
<Self as Iterator>::nth(self, 0)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.remaining() as usize;
(remaining, Some(remaining))
}
fn count(self) -> usize {
self.remaining() as usize
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
debug_assert!(self.begin <= self.end);
let n = n as u32;
if self.begin + n >= self.end {
return None
}
let cur = self.begin + n;
self.begin += 1 + n;
self.get_mut(cur).expect("access is out of bounds").into()
}
}
......@@ -32,10 +32,7 @@ use crate::{
traits::PackedLayout,
};
pub use children_vec::{
Iter,
IterMut,
};
pub use children_vec::Iter;
pub use reverse::Reverse;
/// A priority queue implemented with a binary heap.
......@@ -92,17 +89,6 @@ where
self.elements.iter()
}
/// Returns an iterator yielding exclusive references to all elements of the heap.
///
/// # Note
///
/// Avoid unbounded iteration over big heaps.
/// Prefer using methods like `Iterator::take` in order to limit the number
/// of yielded elements.
pub fn iter_mut(&mut self) -> IterMut<T> {
self.elements.iter_mut()
}
/// Returns a shared reference to the greatest element of the heap
///
/// Returns `None` if the heap is empty
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment