Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuse the iterator in LazyBuffer #727

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/combinations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use std::iter::FusedIterator;
use std::iter::{Fuse, FusedIterator};

use super::lazy_buffer::LazyBuffer;
use alloc::vec::Vec;
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<I: Iterator> Combinations<I> {

/// Returns a reference to the source iterator.
#[inline]
pub(crate) fn src(&self) -> &I { &self.pool.it }
pub(crate) fn src(&self) -> &Fuse<I> { &self.pool.it }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only used in Powerset::size_hint, where we ask for src().size_hint - I think that using the size_hint of the fused iterator is actually what we want there. Can you confirm this?

I guess that Powerset::size_hint should - in the future - also redirect to LazyBuffer::size_hint, no? Then we could get rid of this src exposing the innards of LazyBuffer entirely

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes entirely, I confirm that Powerset::size_hint wants the not yet written LazyBuffer::size_hint from the wrapped Combinations.
fn src might become fn pool(&self) -> &LazyBuffer<I> later in order for Powerset::size_hint to let src_total = self.combs.pool().size_hint(); instead of doing the job of LazyBuffer::size_hint.


/// Resets this `Combinations` back to an initial state for combinations of length
/// `k` over the same pool data source. If `k` is larger than the current length
Expand Down
16 changes: 4 additions & 12 deletions src/lazy_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::iter::Fuse;
use std::ops::Index;
use alloc::vec::Vec;

#[derive(Debug, Clone)]
pub struct LazyBuffer<I: Iterator> {
pub it: I,
done: bool,
pub it: Fuse<I>,
buffer: Vec<I::Item>,
}

Expand All @@ -14,8 +14,7 @@ where
{
pub fn new(it: I) -> LazyBuffer<I> {
LazyBuffer {
it,
done: false,
it: it.fuse(),
buffer: Vec::new(),
}
}
Expand All @@ -25,26 +24,19 @@ where
}

pub fn get_next(&mut self) -> bool {
if self.done {
return false;
}
if let Some(x) = self.it.next() {
self.buffer.push(x);
true
} else {
self.done = true;
false
}
}

pub fn prefill(&mut self, len: usize) {
let buffer_len = self.buffer.len();

if !self.done && len > buffer_len {
if len > buffer_len {
let delta = len - buffer_len;

self.buffer.extend(self.it.by_ref().take(delta));
self.done = self.buffer.len() < len;
}
}
}
Expand Down