Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iterator versions of BTreeSet operations #556

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
398 changes: 234 additions & 164 deletions src/adaptors/mod.rs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/combinations_with_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
// If this is the first iteration, return early
if self.first {
// In empty edge cases, stop iterating immediately
return if self.indices.len() != 0 && !self.pool.get_next() {
return if !self.indices.is_empty() && !self.pool.get_next() {
None
// Otherwise, yield the initial state
} else {
Expand All @@ -80,7 +80,7 @@ where
// Work out where we need to update our indices
let mut increment: Option<(usize, usize)> = None;
for (i, indices_int) in self.indices.iter().enumerate().rev() {
if *indices_int < self.pool.len()-1 {
if *indices_int < self.pool.len() - 1 {
increment = Some((i, indices_int + 1));
break;
}
Expand All @@ -106,4 +106,5 @@ impl<I> FusedIterator for CombinationsWithReplacement<I>
where
I: Iterator,
I::Item: Clone,
{}
{
}
16 changes: 5 additions & 11 deletions src/either_or_both.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@ impl<A, B> EitherOrBoth<A, B> {
/// If Left, return true otherwise, return false.
/// Exclusive version of [`has_left`](EitherOrBoth::has_left).
pub fn is_left(&self) -> bool {
match *self {
Left(_) => true,
_ => false,
}
matches!(*self, Left(_))
}

/// If Right, return true otherwise, return false.
/// Exclusive version of [`has_right`](EitherOrBoth::has_right).
pub fn is_right(&self) -> bool {
match *self {
Right(_) => true,
_ => false,
}
matches!(*self, Right(_))
}

/// If Right, return true otherwise, return false.
Expand Down Expand Up @@ -194,9 +188,9 @@ impl<T> EitherOrBoth<T, T> {
}
}

impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
fn into(self) -> Option<Either<A, B>> {
match self {
impl<A, B> From<EitherOrBoth<A, B>> for Option<Either<A, B>> {
fn from(val: EitherOrBoth<A, B>) -> Self {
match val {
EitherOrBoth::Left(l) => Some(Either::Left(l)),
EitherOrBoth::Right(r) => Some(Either::Right(r)),
_ => None,
Expand Down
84 changes: 46 additions & 38 deletions src/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,25 @@ use std::iter::{self, Zip};
type VecIntoIter<T> = alloc::vec::IntoIter<T>;

#[cfg(feature = "use_alloc")]
use alloc::{
string::String,
};
use alloc::string::String;

#[cfg(feature = "use_alloc")]
use crate::Itertools;

pub use crate::adaptors::{
interleave,
merge,
put_back,
};
pub use crate::adaptors::{interleave, merge, put_back};
#[cfg(feature = "use_alloc")]
pub use crate::put_back_n_impl::put_back_n;
pub use crate::kmerge_impl::kmerge;
pub use crate::merge_join::merge_join_by;
#[cfg(feature = "use_alloc")]
pub use crate::multipeek_impl::multipeek;
#[cfg(feature = "use_alloc")]
pub use crate::peek_nth::peek_nth;
#[cfg(feature = "use_alloc")]
pub use crate::kmerge_impl::kmerge;
pub use crate::zip_eq_impl::zip_eq;
pub use crate::merge_join::merge_join_by;
pub use crate::put_back_n_impl::put_back_n;
#[cfg(feature = "use_alloc")]
pub use crate::rciter_impl::rciter;
pub use crate::set_ops::{difference, intersection, symmetric_difference, union_ref};
pub use crate::zip_eq_impl::zip_eq;

/// Iterate `iterable` with a running index.
///
Expand All @@ -47,7 +42,8 @@ pub use crate::rciter_impl::rciter;
/// }
/// ```
pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
where I: IntoIterator
where
I: IntoIterator,
{
iterable.into_iter().enumerate()
}
Expand All @@ -64,8 +60,9 @@ pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
/// }
/// ```
pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
where I: IntoIterator,
I::IntoIter: DoubleEndedIterator
where
I: IntoIterator,
I::IntoIter: DoubleEndedIterator,
{
iterable.into_iter().rev()
}
Expand All @@ -83,8 +80,9 @@ pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
/// }
/// ```
pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
where I: IntoIterator,
J: IntoIterator
where
I: IntoIterator,
J: IntoIterator,
{
i.into_iter().zip(j)
}
Expand All @@ -100,9 +98,13 @@ pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
/// /* loop body */
/// }
/// ```
pub fn chain<I, J>(i: I, j: J) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
where I: IntoIterator,
J: IntoIterator<Item = I::Item>
pub fn chain<I, J>(
i: I,
j: J,
) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
where
I: IntoIterator,
J: IntoIterator<Item = I::Item>,
{
i.into_iter().chain(j)
}
Expand All @@ -117,8 +119,9 @@ pub fn chain<I, J>(i: I, j: J) -> iter::Chain<<I as IntoIterator>::IntoIter, <J
/// assert_eq!(cloned(b"abc").next(), Some(b'a'));
/// ```
pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned<I::IntoIter>
where I: IntoIterator<Item=&'a T>,
T: Clone,
where
I: IntoIterator<Item = &'a T>,
T: Clone,
{
iterable.into_iter().cloned()
}
Expand All @@ -133,8 +136,9 @@ pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned<I::IntoIter>
/// assert_eq!(fold(&[1., 2., 3.], 0., |a, &b| f32::max(a, b)), 3.);
/// ```
pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
where I: IntoIterator,
F: FnMut(B, I::Item) -> B
where
I: IntoIterator,
F: FnMut(B, I::Item) -> B,
{
iterable.into_iter().fold(init, f)
}
Expand All @@ -149,8 +153,9 @@ pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
/// assert!(all(&[1, 2, 3], |elt| *elt > 0));
/// ```
pub fn all<I, F>(iterable: I, f: F) -> bool
where I: IntoIterator,
F: FnMut(I::Item) -> bool
where
I: IntoIterator,
F: FnMut(I::Item) -> bool,
{
iterable.into_iter().all(f)
}
Expand All @@ -165,8 +170,9 @@ pub fn all<I, F>(iterable: I, f: F) -> bool
/// assert!(any(&[0, -1, 2], |elt| *elt > 0));
/// ```
pub fn any<I, F>(iterable: I, f: F) -> bool
where I: IntoIterator,
F: FnMut(I::Item) -> bool
where
I: IntoIterator,
F: FnMut(I::Item) -> bool,
{
iterable.into_iter().any(f)
}
Expand All @@ -181,8 +187,9 @@ pub fn any<I, F>(iterable: I, f: F) -> bool
/// assert_eq!(max(0..10), Some(9));
/// ```
pub fn max<I>(iterable: I) -> Option<I::Item>
where I: IntoIterator,
I::Item: Ord
where
I: IntoIterator,
I::Item: Ord,
{
iterable.into_iter().max()
}
Expand All @@ -197,13 +204,13 @@ pub fn max<I>(iterable: I) -> Option<I::Item>
/// assert_eq!(min(0..10), Some(0));
/// ```
pub fn min<I>(iterable: I) -> Option<I::Item>
where I: IntoIterator,
I::Item: Ord
where
I: IntoIterator,
I::Item: Ord,
{
iterable.into_iter().min()
}


/// Combine all iterator elements into one String, seperated by `sep`.
///
/// [`IntoIterator`] enabled version of [`Itertools::join`].
Expand All @@ -215,8 +222,9 @@ pub fn min<I>(iterable: I) -> Option<I::Item>
/// ```
#[cfg(feature = "use_alloc")]
pub fn join<I>(iterable: I, sep: &str) -> String
where I: IntoIterator,
I::Item: Display
where
I: IntoIterator,
I::Item: Display,
{
iterable.into_iter().join(sep)
}
Expand All @@ -233,9 +241,9 @@ pub fn join<I>(iterable: I, sep: &str) -> String
/// ```
#[cfg(feature = "use_alloc")]
pub fn sorted<I>(iterable: I) -> VecIntoIter<I::Item>
where I: IntoIterator,
I::Item: Ord
where
I: IntoIterator,
I::Item: Ord,
{
iterable.into_iter().sorted()
}