use super::{Bucket, Entries, IndexSet, Slice}; use alloc::vec::{self, Vec}; use core::fmt; use core::hash::{BuildHasher, Hash}; use core::iter::{Chain, FusedIterator}; use core::ops::RangeBounds; use core::slice::Iter as SliceIter; impl<'a, T, S> IntoIterator for &'a IndexSet { type Item = &'a T; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Self::IntoIter { self.iter() } } impl IntoIterator for IndexSet { type Item = T; type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { IntoIter::new(self.into_entries()) } } /// An iterator over the items of an [`IndexSet`]. /// /// This `struct` is created by the [`IndexSet::iter`] method. /// See its documentation for more. pub struct Iter<'a, T> { iter: SliceIter<'a, Bucket>, } impl<'a, T> Iter<'a, T> { pub(super) fn new(entries: &'a [Bucket]) -> Self { Self { iter: entries.iter(), } } /// Returns a slice of the remaining entries in the iterator. pub fn as_slice(&self) -> &'a Slice { Slice::from_slice(self.iter.as_slice()) } } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; iterator_methods!(Bucket::key_ref); } impl DoubleEndedIterator for Iter<'_, T> { double_ended_iterator_methods!(Bucket::key_ref); } impl ExactSizeIterator for Iter<'_, T> { fn len(&self) -> usize { self.iter.len() } } impl FusedIterator for Iter<'_, T> {} impl Clone for Iter<'_, T> { fn clone(&self) -> Self { Iter { iter: self.iter.clone(), } } } impl fmt::Debug for Iter<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } impl Default for Iter<'_, T> { fn default() -> Self { Self { iter: [].iter() } } } /// An owning iterator over the items of an [`IndexSet`]. /// /// This `struct` is created by the [`IndexSet::into_iter`] method /// (provided by the [`IntoIterator`] trait). See its documentation for more. pub struct IntoIter { iter: vec::IntoIter>, } impl IntoIter { pub(super) fn new(entries: Vec>) -> Self { Self { iter: entries.into_iter(), } } /// Returns a slice of the remaining entries in the iterator. pub fn as_slice(&self) -> &Slice { Slice::from_slice(self.iter.as_slice()) } } impl Iterator for IntoIter { type Item = T; iterator_methods!(Bucket::key); } impl DoubleEndedIterator for IntoIter { double_ended_iterator_methods!(Bucket::key); } impl ExactSizeIterator for IntoIter { fn len(&self) -> usize { self.iter.len() } } impl FusedIterator for IntoIter {} impl fmt::Debug for IntoIter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.iter.as_slice().iter().map(Bucket::key_ref); f.debug_list().entries(iter).finish() } } impl Default for IntoIter { fn default() -> Self { Self { iter: Vec::new().into_iter(), } } } /// A draining iterator over the items of an [`IndexSet`]. /// /// This `struct` is created by the [`IndexSet::drain`] method. /// See its documentation for more. pub struct Drain<'a, T> { iter: vec::Drain<'a, Bucket>, } impl<'a, T> Drain<'a, T> { pub(super) fn new(iter: vec::Drain<'a, Bucket>) -> Self { Self { iter } } /// Returns a slice of the remaining entries in the iterator. pub fn as_slice(&self) -> &Slice { Slice::from_slice(self.iter.as_slice()) } } impl Iterator for Drain<'_, T> { type Item = T; iterator_methods!(Bucket::key); } impl DoubleEndedIterator for Drain<'_, T> { double_ended_iterator_methods!(Bucket::key); } impl ExactSizeIterator for Drain<'_, T> { fn len(&self) -> usize { self.iter.len() } } impl FusedIterator for Drain<'_, T> {} impl fmt::Debug for Drain<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.iter.as_slice().iter().map(Bucket::key_ref); f.debug_list().entries(iter).finish() } } /// A lazy iterator producing elements in the difference of [`IndexSet`]s. /// /// This `struct` is created by the [`IndexSet::difference`] method. /// See its documentation for more. pub struct Difference<'a, T, S> { iter: Iter<'a, T>, other: &'a IndexSet, } impl<'a, T, S> Difference<'a, T, S> { pub(super) fn new(set: &'a IndexSet, other: &'a IndexSet) -> Self { Self { iter: set.iter(), other, } } } impl<'a, T, S> Iterator for Difference<'a, T, S> where T: Eq + Hash, S: BuildHasher, { type Item = &'a T; fn next(&mut self) -> Option { while let Some(item) = self.iter.next() { if !self.other.contains(item) { return Some(item); } } None } fn size_hint(&self) -> (usize, Option) { (0, self.iter.size_hint().1) } } impl DoubleEndedIterator for Difference<'_, T, S> where T: Eq + Hash, S: BuildHasher, { fn next_back(&mut self) -> Option { while let Some(item) = self.iter.next_back() { if !self.other.contains(item) { return Some(item); } } None } } impl FusedIterator for Difference<'_, T, S> where T: Eq + Hash, S: BuildHasher, { } impl Clone for Difference<'_, T, S> { fn clone(&self) -> Self { Difference { iter: self.iter.clone(), ..*self } } } impl fmt::Debug for Difference<'_, T, S> where T: fmt::Debug + Eq + Hash, S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } /// A lazy iterator producing elements in the intersection of [`IndexSet`]s. /// /// This `struct` is created by the [`IndexSet::intersection`] method. /// See its documentation for more. pub struct Intersection<'a, T, S> { iter: Iter<'a, T>, other: &'a IndexSet, } impl<'a, T, S> Intersection<'a, T, S> { pub(super) fn new(set: &'a IndexSet, other: &'a IndexSet) -> Self { Self { iter: set.iter(), other, } } } impl<'a, T, S> Iterator for Intersection<'a, T, S> where T: Eq + Hash, S: BuildHasher, { type Item = &'a T; fn next(&mut self) -> Option { while let Some(item) = self.iter.next() { if self.other.contains(item) { return Some(item); } } None } fn size_hint(&self) -> (usize, Option) { (0, self.iter.size_hint().1) } } impl DoubleEndedIterator for Intersection<'_, T, S> where T: Eq + Hash, S: BuildHasher, { fn next_back(&mut self) -> Option { while let Some(item) = self.iter.next_back() { if self.other.contains(item) { return Some(item); } } None } } impl FusedIterator for Intersection<'_, T, S> where T: Eq + Hash, S: BuildHasher, { } impl Clone for Intersection<'_, T, S> { fn clone(&self) -> Self { Intersection { iter: self.iter.clone(), ..*self } } } impl fmt::Debug for Intersection<'_, T, S> where T: fmt::Debug + Eq + Hash, S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } /// A lazy iterator producing elements in the symmetric difference of [`IndexSet`]s. /// /// This `struct` is created by the [`IndexSet::symmetric_difference`] method. /// See its documentation for more. pub struct SymmetricDifference<'a, T, S1, S2> { iter: Chain, Difference<'a, T, S1>>, } impl<'a, T, S1, S2> SymmetricDifference<'a, T, S1, S2> where T: Eq + Hash, S1: BuildHasher, S2: BuildHasher, { pub(super) fn new(set1: &'a IndexSet, set2: &'a IndexSet) -> Self { let diff1 = set1.difference(set2); let diff2 = set2.difference(set1); Self { iter: diff1.chain(diff2), } } } impl<'a, T, S1, S2> Iterator for SymmetricDifference<'a, T, S1, S2> where T: Eq + Hash, S1: BuildHasher, S2: BuildHasher, { type Item = &'a T; fn next(&mut self) -> Option { self.iter.next() } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } fn fold(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B, { self.iter.fold(init, f) } } impl DoubleEndedIterator for SymmetricDifference<'_, T, S1, S2> where T: Eq + Hash, S1: BuildHasher, S2: BuildHasher, { fn next_back(&mut self) -> Option { self.iter.next_back() } fn rfold(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B, { self.iter.rfold(init, f) } } impl FusedIterator for SymmetricDifference<'_, T, S1, S2> where T: Eq + Hash, S1: BuildHasher, S2: BuildHasher, { } impl Clone for SymmetricDifference<'_, T, S1, S2> { fn clone(&self) -> Self { SymmetricDifference { iter: self.iter.clone(), } } } impl fmt::Debug for SymmetricDifference<'_, T, S1, S2> where T: fmt::Debug + Eq + Hash, S1: BuildHasher, S2: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } /// A lazy iterator producing elements in the union of [`IndexSet`]s. /// /// This `struct` is created by the [`IndexSet::union`] method. /// See its documentation for more. pub struct Union<'a, T, S> { iter: Chain, Difference<'a, T, S>>, } impl<'a, T, S> Union<'a, T, S> where T: Eq + Hash, S: BuildHasher, { pub(super) fn new(set1: &'a IndexSet, set2: &'a IndexSet) -> Self where S2: BuildHasher, { Self { iter: set1.iter().chain(set2.difference(set1)), } } } impl<'a, T, S> Iterator for Union<'a, T, S> where T: Eq + Hash, S: BuildHasher, { type Item = &'a T; fn next(&mut self) -> Option { self.iter.next() } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } fn fold(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B, { self.iter.fold(init, f) } } impl DoubleEndedIterator for Union<'_, T, S> where T: Eq + Hash, S: BuildHasher, { fn next_back(&mut self) -> Option { self.iter.next_back() } fn rfold(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B, { self.iter.rfold(init, f) } } impl FusedIterator for Union<'_, T, S> where T: Eq + Hash, S: BuildHasher, { } impl Clone for Union<'_, T, S> { fn clone(&self) -> Self { Union { iter: self.iter.clone(), } } } impl fmt::Debug for Union<'_, T, S> where T: fmt::Debug + Eq + Hash, S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.clone()).finish() } } /// A splicing iterator for `IndexSet`. /// /// This `struct` is created by [`IndexSet::splice()`]. /// See its documentation for more. pub struct Splice<'a, I, T, S> where I: Iterator, T: Hash + Eq, S: BuildHasher, { iter: crate::map::Splice<'a, UnitValue, T, (), S>, } impl<'a, I, T, S> Splice<'a, I, T, S> where I: Iterator, T: Hash + Eq, S: BuildHasher, { pub(super) fn new(set: &'a mut IndexSet, range: R, replace_with: I) -> Self where R: RangeBounds, { Self { iter: set.map.splice(range, UnitValue(replace_with)), } } } impl Iterator for Splice<'_, I, T, S> where I: Iterator, T: Hash + Eq, S: BuildHasher, { type Item = T; fn next(&mut self) -> Option { Some(self.iter.next()?.0) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl DoubleEndedIterator for Splice<'_, I, T, S> where I: Iterator, T: Hash + Eq, S: BuildHasher, { fn next_back(&mut self) -> Option { Some(self.iter.next_back()?.0) } } impl ExactSizeIterator for Splice<'_, I, T, S> where I: Iterator, T: Hash + Eq, S: BuildHasher, { fn len(&self) -> usize { self.iter.len() } } impl FusedIterator for Splice<'_, I, T, S> where I: Iterator, T: Hash + Eq, S: BuildHasher, { } struct UnitValue(I); impl Iterator for UnitValue { type Item = (I::Item, ()); fn next(&mut self) -> Option { self.0.next().map(|x| (x, ())) } } impl<'a, I, T, S> fmt::Debug for Splice<'a, I, T, S> where I: fmt::Debug + Iterator, T: fmt::Debug + Hash + Eq, S: BuildHasher, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.iter, f) } } impl fmt::Debug for UnitValue { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.0, f) } }