#![warn(missing_docs)]
#![cfg_attr(feature = "unstable", feature(core, zero_one))]
#![crate_name="itertools"]
use std::fmt::Write;
use std::cmp::Ordering;
pub use adaptors::{
Interleave,
Product,
PutBack,
FnMap,
Dedup,
Batching,
GroupBy,
Step,
Merge,
MultiPeek,
};
#[cfg(feature = "unstable")]
pub use adaptors::EnumerateFrom;
pub use intersperse::Intersperse;
pub use islice::{ISlice};
pub use repeatn::RepeatN;
pub use rciter::RcIter;
pub use stride::Stride;
pub use stride::StrideMut;
pub use tee::Tee;
pub use times::Times;
pub use times::times;
pub use linspace::{linspace, Linspace};
pub use zip::{ZipLongest, EitherOrBoth};
pub use ziptuple::{Zip};
#[cfg(feature = "unstable")]
pub use ziptrusted::{ZipTrusted, TrustedIterator};
mod adaptors;
mod intersperse;
mod islice;
mod linspace;
pub mod misc;
mod rciter;
mod repeatn;
mod stride;
mod tee;
mod times;
mod zip;
mod ziptuple;
#[cfg(feature = "unstable")]
mod ziptrusted;
#[macro_export]
macro_rules! iproduct {
($I:expr) => (
($I)
);
($I:expr, $J:expr) => (
{
let it = $crate::Product::new($I, $J);
it
}
);
($I:expr, $J:expr, $($K:expr),+) => (
{
let it = $crate::Product::new($I, $J);
$(
let it = $crate::misc::FlatTuples::new($crate::Product::new(it, $K));
)*
it
}
);
}
#[macro_export]
macro_rules! izip {
($I:expr) => (
($I)
);
(($I:expr),*) => (
{
$crate::Zip::new(($I),*)
}
);
}
#[macro_export]
macro_rules! icompr {
($r:expr, $x:pat, $J:expr, $pred:expr) => (
($J).filter_map(|$x| if $pred { Some($r) } else { None })
);
($r:expr, $x:pat, $J:expr) => (
($J).filter_map(|$x| Some($r))
);
}
pub trait Itertools : Iterator {
fn fn_map<B>(self, map: fn(Self::Item) -> B) -> FnMap<B, Self> where
Self: Sized
{
FnMap::new(self, map)
}
fn interleave<J>(self, other: J) -> Interleave<Self, J> where
J: Iterator<Item=Self::Item>,
Self: Sized
{
Interleave::new(self, other)
}
fn intersperse(self, element: Self::Item) -> Intersperse<Self> where
Self: Sized,
Self::Item: Clone
{
Intersperse::new(self, element)
}
#[inline]
fn zip_longest<U>(self, other: U) -> ZipLongest<Self, U> where
U: Iterator,
Self: Sized,
{
ZipLongest::new(self, other)
}
fn dedup(self) -> Dedup<Self> where
Self: Sized,
{
Dedup::new(self)
}
fn batching<B, F: FnMut(&mut Self) -> Option<B>>(self, f: F) -> Batching<Self, F> where
Self: Sized,
{
Batching::new(self, f)
}
fn group_by<K, F: FnMut(&Self::Item) -> K>(self, key: F) -> GroupBy<K, Self, F> where
Self: Sized,
{
GroupBy::new(self, key)
}
fn tee(self) -> (Tee<Self>, Tee<Self>) where
Self: Sized,
Self::Item: Clone
{
tee::new(self)
}
fn slice<R>(self, range: R) -> ISlice<Self> where
R: misc::GenericRange,
Self: Sized,
{
ISlice::new(self, range)
}
fn into_rc(self) -> RcIter<Self> where
Self: Sized,
{
RcIter::new(self)
}
fn step(self, n: usize) -> Step<Self> where
Self: Sized,
{
Step::new(self, n)
}
fn merge<J>(self, other: J)
-> Merge<Self, J, fn(&Self::Item, &Self::Item) -> Ordering> where
Self: Sized,
Self::Item: PartialOrd,
J: Iterator<Item=Self::Item>,
{
fn wrapper<A: PartialOrd>(a: &A, b: &A) -> Ordering {
a.partial_cmp(b).unwrap_or(Ordering::Less)
};
self.merge_by(other, wrapper)
}
fn merge_by<J, F>(self, other: J, cmp: F) -> Merge<Self, J, F> where
Self: Sized,
J: Iterator<Item=Self::Item>,
F: FnMut(&Self::Item, &Self::Item) -> Ordering
{
Merge::new(self, other, cmp)
}
fn cartesian_product<J>(self, other: J) -> Product<Self, J> where
Self: Sized,
Self::Item: Clone,
J: Clone + Iterator,
{
Product::new(self, other)
}
#[cfg(feature = "unstable")]
fn enumerate_from<K>(self, start: K) -> EnumerateFrom<Self, K> where
Self: Sized,
{
EnumerateFrom::new(self, start)
}
fn multipeek(self) -> MultiPeek<Self> where
Self: Sized
{
MultiPeek::new(self)
}
fn find_position<P>(&mut self, mut pred: P) -> Option<(usize, Self::Item)> where
P: FnMut(&Self::Item) -> bool,
{
let mut index = 0usize;
for elt in self {
if pred(&elt) {
return Some((index, elt))
}
index += 1;
}
None
}
fn dropn(&mut self, mut n: usize) -> usize {
let start = n;
while n > 0 {
match self.next() {
Some(..) => n -= 1,
None => break
}
}
start - n
}
fn dropping(mut self, n: usize) -> Self where
Self: Sized,
{
self.dropn(n);
self
}
fn drain(&mut self)
{
for _ in self { }
}
fn apply<F>(&mut self, f: F) where
F: FnMut(Self::Item),
{
self.foreach(f)
}
fn foreach<F>(&mut self, mut f: F) where
F: FnMut(Self::Item),
{
for elt in self { f(elt) }
}
fn collect_vec(self) -> Vec<Self::Item> where
Self: Sized,
{
self.collect()
}
#[inline]
fn set_from<'a, A: 'a, J>(&mut self, from: J) -> usize where
Self: Iterator<Item=&'a mut A>,
J: Iterator<Item=A>,
{
let mut count = 0;
for elt in from {
match self.next() {
None => break,
Some(ptr) => *ptr = elt
}
count += 1;
}
count
}
fn to_string_join(&mut self, sep: &str) -> String where
Self::Item: ToString,
{
self.map(|elt| elt.to_string()).join(sep)
}
fn join(&mut self, sep: &str) -> String where
Self::Item: std::fmt::Display,
{
match self.next() {
None => String::new(),
Some(first_elt) => {
let (lower, _) = self.size_hint();
let mut result = String::with_capacity(sep.len() * lower);
write!(&mut result, "{}", first_elt).unwrap();
for elt in self {
result.push_str(sep);
write!(&mut result, "{}", elt).unwrap();
}
result
}
}
}
fn fold_results<A, E, B, F>(&mut self, mut start: B, mut f: F) -> Result<B, E> where
Self: Iterator<Item=Result<A, E>>,
F: FnMut(B, A) -> B,
{
for elt in self {
match elt {
Ok(v) => start = f(start, v),
Err(u) => return Err(u),
}
}
Ok(start)
}
}
impl<T: ?Sized> Itertools for T where T: Iterator { }
#[inline]
pub fn write<'a, A: 'a, I, J>(mut to: I, from: J) -> usize where
I: Iterator<Item=&'a mut A>,
J: Iterator<Item=A>
{
let mut count = 0;
for elt in from {
match to.next() {
None => break,
Some(ptr) => *ptr = elt
}
count += 1;
}
count
}