Skip to content

Commit

Permalink
Drain
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkazik committed Jan 13, 2024
1 parent 9997a5e commit 4ecad7f
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/drain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Drain;
use core::iter::FusedIterator;

impl<'a, K, V> Drop for Drain<'a, K, V> {
fn drop(&mut self) {
for pair in &mut self.iter {
unsafe { pair.assume_init_drop() };
}
}
}

impl<'a, K: PartialEq, V> Iterator for Drain<'a, K, V> {
type Item = (K, V);

#[inline]
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|p| unsafe { p.assume_init_read() })
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.iter.len(), Some(self.iter.len()))
}
}

impl<'a, K: PartialEq, V> ExactSizeIterator for Drain<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}

impl<'a, K: PartialEq, V> FusedIterator for Drain<'a, K, V> {}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod clone;
mod ctors;
mod debug;
mod display;
mod drain;
mod entry;
mod eq;
mod from;
Expand All @@ -66,7 +67,7 @@ mod serialization;
mod set;
mod values;

pub use crate::set::{Set, SetIntoIter, SetIter};
pub use crate::set::{Set, SetDrain, SetIntoIter, SetIter};
use core::mem::MaybeUninit;

/// A faster alternative of [`std::collections::HashMap`].
Expand Down Expand Up @@ -175,3 +176,10 @@ pub struct VacantEntry<'a, K: 'a + PartialEq, V: 'a, const N: usize> {
key: K,
table: &'a mut Map<K, V, N>,
}

/// A draining iterator over the entries of a `Map`.
///
/// This struct is created by the drain method on `Map`. See its documentation for more.
pub struct Drain<'a, K: 'a, V: 'a> {
iter: core::slice::IterMut<'a, MaybeUninit<(K, V)>>,
}
13 changes: 12 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{Entry, Map, OccupiedEntry, VacantEntry};
use crate::{Drain, Entry, Map, OccupiedEntry, VacantEntry};
use core::borrow::Borrow;

mod internal {
Expand Down Expand Up @@ -105,6 +105,17 @@ impl<K: PartialEq, V, const N: usize> Map<K, V, N> {
self.len
}

/// Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
///
/// If the returned iterator is dropped before being fully consumed, it drops the remaining key-value pairs. The returned iterator keeps a mutable borrow on the map to optimize its implementation.
pub fn drain(&mut self) -> Drain<'_, K, V> {
let drain = Drain {
iter: self.pairs[0..self.len].iter_mut(),
};
self.len = 0;
drain
}

/// Does the map contain this key?
#[inline]
#[must_use]
Expand Down
46 changes: 46 additions & 0 deletions src/set/drain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::SetDrain;
use core::iter::FusedIterator;

impl<'a, K: PartialEq> Iterator for SetDrain<'a, K> {
type Item = K;

#[inline]
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(k, ())| k)
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.iter.len(), Some(self.iter.len()))
}
}

impl<'a, K: PartialEq> ExactSizeIterator for SetDrain<'a, K> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}

impl<'a, K: PartialEq> FusedIterator for SetDrain<'a, K> {}
11 changes: 10 additions & 1 deletion src/set/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Set;
use crate::{Set, SetDrain};
use core::borrow::Borrow;

impl<T: PartialEq, const N: usize> Set<T, N> {
Expand All @@ -43,6 +43,15 @@ impl<T: PartialEq, const N: usize> Set<T, N> {
self.map.len()
}

/// Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
///
/// If the returned iterator is dropped before being fully consumed, it drops the remaining elements. The returned iterator keeps a mutable borrow on the set to optimize its implementation.
pub fn drain(&mut self) -> SetDrain<'_, T> {
SetDrain {
iter: self.map.drain(),
}
}

/// Does the set contain this key?
#[inline]
#[must_use]
Expand Down
7 changes: 7 additions & 0 deletions src/set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod clone;
mod ctors;
mod debug;
mod display;
mod drain;
mod eq;
mod from;
mod functions;
Expand Down Expand Up @@ -54,3 +55,9 @@ pub struct SetIter<'a, T> {
pub struct SetIntoIter<T: PartialEq, const N: usize> {
iter: crate::IntoKeys<T, (), N>,
}

#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
pub struct SetDrain<'a, T: PartialEq> {
iter: crate::Drain<'a, T, ()>,
}

0 comments on commit 4ecad7f

Please sign in to comment.