#![feature(core, std_misc)]
#![feature(unboxed_closures)]
#![crate_name="itertools"]
#![crate_type="dylib"]
pub use adaptors::{
Interleave,
Product,
PutBack,
FnMap,
Dedup,
Batching,
GroupBy,
Step,
Merge,
};
pub use intersperse::Intersperse;
pub use islice::{ISlice};
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, ZipTrusted, TrustedIterator};
mod adaptors;
mod intersperse;
mod islice;
mod linspace;
pub mod misc;
mod rciter;
mod stride;
mod tee;
mod times;
mod zip;
mod ziptuple;
#[macro_export]
pub 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]
pub macro_rules! izip {
($I:expr) => (
($I)
);
(($I:expr),*) => (
{
$crate::Zip::new(($I),*)
}
);
}
#[macro_export]
pub 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: Iterator<Item=Self::Item>>(self, other: J) -> Interleave<Self, J> where
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: Iterator>(self, other: U) -> ZipLongest<Self, U> where
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: misc::GenericRange>(self, range: R) -> ISlice<Self> where
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> where
Self: Sized,
Self::Item: PartialOrd,
J: Iterator<Item=Self::Item>,
{
Merge::new(self, other)
}
fn cartesian_product<J>(self, other: J) -> Product<Self, J> where
Self: Sized,
Self::Item: Clone,
J: Clone + Iterator,
{
Product::new(self, other)
}
fn find_position<P>(&mut self, mut pred: P) -> Option<(usize, Self::Item)> where
P: FnMut(&Self::Item) -> bool,
Self: Sized,
{
let mut index = 0us;
for elt in IteratorExt::by_ref(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) where
Self: Sized
{
for _ in self.by_ref() { }
}
fn apply<F: FnMut(Self::Item)>(&mut self, f: F) where
Self: Sized
{
self.foreach(f)
}
fn foreach<F: FnMut(Self::Item)>(&mut self, mut f: F) where
Self: Sized
{
for elt in self.by_ref() { f(elt) }
}
fn collect_vec(self) -> Vec<Self::Item> where
Self: Sized,
{
self.collect()
}
}
impl<T: ?Sized> Itertools for T where T: Iterator { }
#[inline]
pub fn write<'a, A: 'a, I: Iterator<Item=&'a mut A>, J: Iterator<Item=A>>
(mut to: I, from: J) -> usize
{
let mut count = 0;
for elt in from {
match to.next() {
None => break,
Some(ptr) => *ptr = elt
}
count += 1;
}
count
}