1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::cmp::Ordering;

use crate::IdxSize;

pub mod enumerate_idx;

/// Utility extension trait of iterator methods.
pub trait Itertools: Iterator {
    /// Equivalent to `.collect::<Vec<_>>()`.
    fn collect_vec(self) -> Vec<Self::Item>
    where
        Self: Sized,
    {
        self.collect()
    }

    /// Equivalent to `.collect::<Result<_, _>>()`.
    fn try_collect<T, U, E>(self) -> Result<U, E>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        Result<U, E>: FromIterator<Result<T, E>>,
    {
        self.collect()
    }

    /// Equivalent to `.collect::<Result<Vec<_>, _>>()`.
    fn try_collect_vec<T, U, E>(self) -> Result<Vec<U>, E>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        Result<Vec<U>, E>: FromIterator<Result<T, E>>,
    {
        self.collect()
    }

    fn enumerate_idx(self) -> enumerate_idx::EnumerateIdx<Self, IdxSize>
    where
        Self: Sized,
    {
        enumerate_idx::EnumerateIdx::new(self)
    }

    fn enumerate_u32(self) -> enumerate_idx::EnumerateIdx<Self, u32>
    where
        Self: Sized,
    {
        enumerate_idx::EnumerateIdx::new(self)
    }

    fn all_equal(mut self) -> bool
    where
        Self: Sized,
        Self::Item: PartialEq,
    {
        match self.next() {
            None => true,
            Some(a) => self.all(|x| a == x),
        }
    }

    // Stable copy of the unstable eq_by from the stdlib.
    fn eq_by_<I, F>(mut self, other: I, mut eq: F) -> bool
    where
        Self: Sized,
        I: IntoIterator,
        F: FnMut(Self::Item, I::Item) -> bool,
    {
        let mut other = other.into_iter();
        loop {
            match (self.next(), other.next()) {
                (None, None) => return true,
                (None, Some(_)) => return false,
                (Some(_), None) => return false,
                (Some(l), Some(r)) => {
                    if eq(l, r) {
                        continue;
                    } else {
                        return false;
                    }
                },
            }
        }
    }

    // Stable copy of the unstable partial_cmp_by from the stdlib.
    fn partial_cmp_by_<I, F>(mut self, other: I, mut partial_cmp: F) -> Option<Ordering>
    where
        Self: Sized,
        I: IntoIterator,
        F: FnMut(Self::Item, I::Item) -> Option<Ordering>,
    {
        let mut other = other.into_iter();
        loop {
            match (self.next(), other.next()) {
                (None, None) => return Some(Ordering::Equal),
                (None, Some(_)) => return Some(Ordering::Less),
                (Some(_), None) => return Some(Ordering::Greater),
                (Some(l), Some(r)) => match partial_cmp(l, r) {
                    Some(Ordering::Equal) => continue,
                    ord => return ord,
                },
            }
        }
    }
}

impl<T: Iterator + ?Sized> Itertools for T {}