summaryrefslogtreecommitdiffstats
path: root/vendor/itertools/src/groupbylazy.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/itertools/src/groupbylazy.rs')
-rw-r--r--vendor/itertools/src/groupbylazy.rs195
1 files changed, 110 insertions, 85 deletions
diff --git a/vendor/itertools/src/groupbylazy.rs b/vendor/itertools/src/groupbylazy.rs
index 80c6f09f3..6cf33838c 100644
--- a/vendor/itertools/src/groupbylazy.rs
+++ b/vendor/itertools/src/groupbylazy.rs
@@ -1,5 +1,5 @@
-use std::cell::{Cell, RefCell};
use alloc::vec::{self, Vec};
+use std::cell::{Cell, RefCell};
/// A trait to unify `FnMut` for `GroupBy` with the chunk key in `IntoChunks`
trait KeyFunction<A> {
@@ -8,7 +8,8 @@ trait KeyFunction<A> {
}
impl<A, K, F: ?Sized> KeyFunction<A> for F
- where F: FnMut(A) -> K
+where
+ F: FnMut(A) -> K,
{
type Key = K;
#[inline]
@@ -17,7 +18,6 @@ impl<A, K, F: ?Sized> KeyFunction<A> for F
}
}
-
/// `ChunkIndex` acts like the grouping key function for `IntoChunks`
#[derive(Debug, Clone)]
struct ChunkIndex {
@@ -52,7 +52,8 @@ impl<A> KeyFunction<A> for ChunkIndex {
#[derive(Clone)]
struct GroupInner<K, I, F>
- where I: Iterator
+where
+ I: Iterator,
{
key: F,
iter: I,
@@ -75,9 +76,10 @@ struct GroupInner<K, I, F>
}
impl<K, I, F> GroupInner<K, I, F>
- where I: Iterator,
- F: for<'a> KeyFunction<&'a I::Item, Key=K>,
- K: PartialEq,
+where
+ I: Iterator,
+ F: for<'a> KeyFunction<&'a I::Item, Key = K>,
+ K: PartialEq,
{
/// `client`: Index of group that requests next element
#[inline(always)]
@@ -90,9 +92,8 @@ impl<K, I, F> GroupInner<K, I, F>
*/
if client < self.oldest_buffered_group {
None
- } else if client < self.top_group ||
- (client == self.top_group &&
- self.buffer.len() > self.top_group - self.bottom_group)
+ } else if client < self.top_group
+ || (client == self.top_group && self.buffer.len() > self.top_group - self.bottom_group)
{
self.lookup_buffer(client)
} else if self.done {
@@ -118,8 +119,10 @@ impl<K, I, F> GroupInner<K, I, F>
// `bottom_group..oldest_buffered_group` is unused, and if it's large enough, erase it.
self.oldest_buffered_group += 1;
// skip forward further empty queues too
- while self.buffer.get(self.oldest_buffered_group - self.bottom_group)
- .map_or(false, |buf| buf.len() == 0)
+ while self
+ .buffer
+ .get(self.oldest_buffered_group - self.bottom_group)
+ .map_or(false, |buf| buf.len() == 0)
{
self.oldest_buffered_group += 1;
}
@@ -144,12 +147,14 @@ impl<K, I, F> GroupInner<K, I, F>
fn next_element(&mut self) -> Option<I::Item> {
debug_assert!(!self.done);
match self.iter.next() {
- None => { self.done = true; None }
+ None => {
+ self.done = true;
+ None
+ }
otherwise => otherwise,
}
}
-
#[inline(never)]
fn step_buffering(&mut self, client: usize) -> Option<I::Item> {
// requested a later group -- walk through the current group up to
@@ -171,11 +176,13 @@ impl<K, I, F> GroupInner<K, I, F>
let key = self.key.call_mut(&elt);
match self.current_key.take() {
None => {}
- Some(old_key) => if old_key != key {
- self.current_key = Some(key);
- first_elt = Some(elt);
- break;
- },
+ Some(old_key) => {
+ if old_key != key {
+ self.current_key = Some(key);
+ first_elt = Some(elt);
+ break;
+ }
+ }
}
self.current_key = Some(key);
if self.top_group != self.dropped_group {
@@ -220,12 +227,14 @@ impl<K, I, F> GroupInner<K, I, F>
let key = self.key.call_mut(&elt);
match self.current_key.take() {
None => {}
- Some(old_key) => if old_key != key {
- self.current_key = Some(key);
- self.current_elt = Some(elt);
- self.top_group += 1;
- return None;
- },
+ Some(old_key) => {
+ if old_key != key {
+ self.current_key = Some(key);
+ self.current_elt = Some(elt);
+ self.top_group += 1;
+ return None;
+ }
+ }
}
self.current_key = Some(key);
Some(elt)
@@ -261,7 +270,8 @@ impl<K, I, F> GroupInner<K, I, F>
}
impl<K, I, F> GroupInner<K, I, F>
- where I: Iterator,
+where
+ I: Iterator,
{
/// Called when a group is dropped
fn drop_group(&mut self, client: usize) {
@@ -287,7 +297,8 @@ impl<K, I, F> GroupInner<K, I, F>
/// See [`.group_by()`](crate::Itertools::group_by) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct GroupBy<K, I, F>
- where I: Iterator,
+where
+ I: Iterator,
{
inner: RefCell<GroupInner<K, I, F>>,
// the group iterator's current index. Keep this in the main value
@@ -297,8 +308,9 @@ pub struct GroupBy<K, I, F>
/// Create a new
pub fn new<K, J, F>(iter: J, f: F) -> GroupBy<K, J::IntoIter, F>
- where J: IntoIterator,
- F: FnMut(&J::Item) -> K,
+where
+ J: IntoIterator,
+ F: FnMut(&J::Item) -> K,
{
GroupBy {
inner: RefCell::new(GroupInner {
@@ -318,12 +330,14 @@ pub fn new<K, J, F>(iter: J, f: F) -> GroupBy<K, J::IntoIter, F>
}
impl<K, I, F> GroupBy<K, I, F>
- where I: Iterator,
+where
+ I: Iterator,
{
/// `client`: Index of group that requests next element
fn step(&self, client: usize) -> Option<I::Item>
- where F: FnMut(&I::Item) -> K,
- K: PartialEq,
+ where
+ F: FnMut(&I::Item) -> K,
+ K: PartialEq,
{
self.inner.borrow_mut().step(client)
}
@@ -335,10 +349,11 @@ impl<K, I, F> GroupBy<K, I, F>
}
impl<'a, K, I, F> IntoIterator for &'a GroupBy<K, I, F>
- where I: Iterator,
- I::Item: 'a,
- F: FnMut(&I::Item) -> K,
- K: PartialEq
+where
+ I: Iterator,
+ I::Item: 'a,
+ F: FnMut(&I::Item) -> K,
+ K: PartialEq,
{
type Item = (K, Group<'a, K, I, F>);
type IntoIter = Groups<'a, K, I, F>;
@@ -348,7 +363,6 @@ impl<'a, K, I, F> IntoIterator for &'a GroupBy<K, I, F>
}
}
-
/// An iterator that yields the Group iterators.
///
/// Iterator element type is `(K, Group)`:
@@ -357,17 +371,19 @@ impl<'a, K, I, F> IntoIterator for &'a GroupBy<K, I, F>
/// See [`.group_by()`](crate::Itertools::group_by) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Groups<'a, K: 'a, I: 'a, F: 'a>
- where I: Iterator,
- I::Item: 'a
+where
+ I: Iterator,
+ I::Item: 'a,
{
parent: &'a GroupBy<K, I, F>,
}
impl<'a, K, I, F> Iterator for Groups<'a, K, I, F>
- where I: Iterator,
- I::Item: 'a,
- F: FnMut(&I::Item) -> K,
- K: PartialEq
+where
+ I: Iterator,
+ I::Item: 'a,
+ F: FnMut(&I::Item) -> K,
+ K: PartialEq,
{
type Item = (K, Group<'a, K, I, F>);
@@ -378,11 +394,14 @@ impl<'a, K, I, F> Iterator for Groups<'a, K, I, F>
let inner = &mut *self.parent.inner.borrow_mut();
inner.step(index).map(|elt| {
let key = inner.group_key(index);
- (key, Group {
- parent: self.parent,
- index,
- first: Some(elt),
- })
+ (
+ key,
+ Group {
+ parent: self.parent,
+ index,
+ first: Some(elt),
+ },
+ )
})
}
}
@@ -391,8 +410,9 @@ impl<'a, K, I, F> Iterator for Groups<'a, K, I, F>
///
/// Iterator element type is `I::Item`.
pub struct Group<'a, K: 'a, I: 'a, F: 'a>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
parent: &'a GroupBy<K, I, F>,
index: usize,
@@ -400,8 +420,9 @@ pub struct Group<'a, K: 'a, I: 'a, F: 'a>
}
impl<'a, K, I, F> Drop for Group<'a, K, I, F>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
fn drop(&mut self) {
self.parent.drop_group(self.index);
@@ -409,10 +430,11 @@ impl<'a, K, I, F> Drop for Group<'a, K, I, F>
}
impl<'a, K, I, F> Iterator for Group<'a, K, I, F>
- where I: Iterator,
- I::Item: 'a,
- F: FnMut(&I::Item) -> K,
- K: PartialEq,
+where
+ I: Iterator,
+ I::Item: 'a,
+ F: FnMut(&I::Item) -> K,
+ K: PartialEq,
{
type Item = I::Item;
#[inline]
@@ -428,7 +450,8 @@ impl<'a, K, I, F> Iterator for Group<'a, K, I, F>
/// Create a new
pub fn new_chunks<J>(iter: J, size: usize) -> IntoChunks<J::IntoIter>
- where J: IntoIterator,
+where
+ J: IntoIterator,
{
IntoChunks {
inner: RefCell::new(GroupInner {
@@ -447,7 +470,6 @@ pub fn new_chunks<J>(iter: J, size: usize) -> IntoChunks<J::IntoIter>
}
}
-
/// `ChunkLazy` is the storage for a lazy chunking operation.
///
/// `IntoChunks` behaves just like `GroupBy`: it is iterable, and
@@ -463,7 +485,8 @@ pub fn new_chunks<J>(iter: J, size: usize) -> IntoChunks<J::IntoIter>
/// See [`.chunks()`](crate::Itertools::chunks) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct IntoChunks<I>
- where I: Iterator,
+where
+ I: Iterator,
{
inner: RefCell<GroupInner<usize, I, ChunkIndex>>,
// the chunk iterator's current index. Keep this in the main value
@@ -472,15 +495,16 @@ pub struct IntoChunks<I>
}
impl<I> Clone for IntoChunks<I>
- where I: Clone + Iterator,
- I::Item: Clone,
+where
+ I: Clone + Iterator,
+ I::Item: Clone,
{
clone_fields!(inner, index);
}
-
impl<I> IntoChunks<I>
- where I: Iterator,
+where
+ I: Iterator,
{
/// `client`: Index of chunk that requests next element
fn step(&self, client: usize) -> Option<I::Item> {
@@ -494,20 +518,18 @@ impl<I> IntoChunks<I>
}
impl<'a, I> IntoIterator for &'a IntoChunks<I>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
type Item = Chunk<'a, I>;
type IntoIter = Chunks<'a, I>;
fn into_iter(self) -> Self::IntoIter {
- Chunks {
- parent: self,
- }
+ Chunks { parent: self }
}
}
-
/// An iterator that yields the Chunk iterators.
///
/// Iterator element type is `Chunk`.
@@ -516,15 +538,17 @@ impl<'a, I> IntoIterator for &'a IntoChunks<I>
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct Chunks<'a, I: 'a>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
parent: &'a IntoChunks<I>,
}
impl<'a, I> Iterator for Chunks<'a, I>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
type Item = Chunk<'a, I>;
@@ -533,12 +557,10 @@ impl<'a, I> Iterator for Chunks<'a, I>
let index = self.parent.index.get();
self.parent.index.set(index + 1);
let inner = &mut *self.parent.inner.borrow_mut();
- inner.step(index).map(|elt| {
- Chunk {
- parent: self.parent,
- index,
- first: Some(elt),
- }
+ inner.step(index).map(|elt| Chunk {
+ parent: self.parent,
+ index,
+ first: Some(elt),
})
}
}
@@ -547,8 +569,9 @@ impl<'a, I> Iterator for Chunks<'a, I>
///
/// Iterator element type is `I::Item`.
pub struct Chunk<'a, I: 'a>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
parent: &'a IntoChunks<I>,
index: usize,
@@ -556,8 +579,9 @@ pub struct Chunk<'a, I: 'a>
}
impl<'a, I> Drop for Chunk<'a, I>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
fn drop(&mut self) {
self.parent.drop_group(self.index);
@@ -565,8 +589,9 @@ impl<'a, I> Drop for Chunk<'a, I>
}
impl<'a, I> Iterator for Chunk<'a, I>
- where I: Iterator,
- I::Item: 'a,
+where
+ I: Iterator,
+ I::Item: 'a,
{
type Item = I::Item;
#[inline]