2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 22:51:57 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
//! # Iterator adaptors
|
|
|
|
//!
|
|
|
|
//! Iterator adaptors needed by Bitcoin but not provided by the Rust
|
|
|
|
//! standard library.
|
|
|
|
|
|
|
|
/// An iterator that returns pairs of elements
|
2015-04-06 00:10:37 +00:00
|
|
|
pub struct Pair<I>
|
2015-04-07 22:51:57 +00:00
|
|
|
where I: Iterator
|
2015-04-06 00:10:37 +00:00
|
|
|
{
|
2015-04-07 22:51:57 +00:00
|
|
|
iter: I,
|
|
|
|
last_elem: Option<I::Item>
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<I: Iterator> Iterator for Pair<I> {
|
2015-04-07 22:51:57 +00:00
|
|
|
type Item = (I::Item, I::Item);
|
2015-04-06 00:10:37 +00:00
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<(I::Item, I::Item)> {
|
|
|
|
let elem1 = self.iter.next();
|
|
|
|
if elem1.is_none() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let elem2 = self.iter.next();
|
|
|
|
if elem2.is_none() {
|
|
|
|
self.last_elem = elem1;
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((elem1.unwrap(), elem2.unwrap()))
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
match self.iter.size_hint() {
|
|
|
|
(n, None) => (n/2, None),
|
|
|
|
(n, Some(m)) => (n/2, Some(m/2))
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<I: Iterator> Pair<I> {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Returns the last element of the iterator if there were an odd
|
|
|
|
/// number of elements remaining before it was Pair-ified.
|
|
|
|
#[inline]
|
|
|
|
pub fn remainder(self) -> Option<I::Item> {
|
|
|
|
self.last_elem
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that returns elements of the original iterator 2 at a time
|
2015-09-20 21:44:05 +00:00
|
|
|
pub trait Pairable : Sized + Iterator {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Returns an iterator that returns elements of the original iterator 2 at a time
|
|
|
|
fn pair(self) -> Pair<Self>;
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<I: Iterator> Pairable for I {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Creates an iterator that yields pairs ef elements from the underlying
|
|
|
|
/// iterator, yielding `None` when there are fewer than two elements to
|
|
|
|
/// return.
|
|
|
|
#[inline]
|
|
|
|
fn pair(self) -> Pair<I> {
|
|
|
|
Pair {iter: self, last_elem: None }
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|