Skip to content

Commit

Permalink
revert: "fix: simplify API of Tape and Band"
Browse files Browse the repository at this point in the history
This reverts commit da5f13c.
  • Loading branch information
nfejzic committed Oct 3, 2023
1 parent 1528304 commit ad005dd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 48 deletions.
52 changes: 25 additions & 27 deletions src/band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ use crate::{ribbon, Ribbon};
///
/// [`Ribbon`]: crate::Ribbon
#[derive(Debug)]
pub struct Band<const LEN: usize, I>
where
I: Iterator,
{
pub struct Band<const LEN: usize, I, T> {
iter: I,
tape: [Option<I::Item>; LEN],
tape: [Option<T>; LEN],
head: usize,
len: usize,
}

impl<const LEN: usize, I> Band<LEN, I>
where
I: Iterator,
{
impl<const LEN: usize, I, T> Band<LEN, I, T> {
/// Creates a new `Tape` from the given iterator.
pub fn new(iter: I) -> Band<LEN, I>
pub fn new(iter: I) -> Band<LEN, I, T>
where
I: Iterator,
I: Iterator<Item = T>,
T: Sized,
{
let tape = [0; LEN].map(|_| None);

Expand All @@ -37,7 +32,7 @@ where
}

/// Shifts all items by 1, returning the head of the `Band`.
fn slide(&mut self) -> Option<I::Item> {
fn slide(&mut self) -> Option<T> {
let first = self.tape[self.head].take()?;

self.incr_head();
Expand All @@ -46,7 +41,10 @@ where
}

/// Checks if the `Band` is at full capacity.
fn is_full(&self) -> bool {
fn is_full(&self) -> bool
where
I: Iterator<Item = T>,
{
self.len() == LEN
}

Expand All @@ -59,11 +57,11 @@ where
}
}

impl<const LEN: usize, I> ribbon::Ribbon<I::Item> for Band<LEN, I>
impl<const LEN: usize, I, T> ribbon::Ribbon<T> for Band<LEN, I, T>
where
I: Iterator,
I: Iterator<Item = T>,
{
fn progress(&mut self) -> Option<I::Item> {
fn progress(&mut self) -> Option<T> {
let next = self.iter.next()?; // do nothing if iterator does not produce

let head = self.slide();
Expand All @@ -83,26 +81,26 @@ where
}
}

fn pop_front(&mut self) -> Option<I::Item> {
fn pop_front(&mut self) -> Option<T> {
self.slide()
}

fn peek_front(&self) -> Option<&I::Item> {
fn peek_front(&self) -> Option<&T> {
self.peek_at(0)
}

fn pop_back(&mut self) -> Option<I::Item> {
fn pop_back(&mut self) -> Option<T> {
let back = self.tape[self.tail()].take()?;
self.len -= 1;
Some(back)
}

fn peek_back(&self) -> Option<&I::Item> {
fn peek_back(&self) -> Option<&T> {
let idx = self.len().saturating_sub(1);
self.peek_at(idx)
}

fn peek_at(&self, index: usize) -> Option<&I::Item> {
fn peek_at(&self, index: usize) -> Option<&T> {
if index >= LEN {
return None;
}
Expand All @@ -123,7 +121,7 @@ mod tests {

#[test]
fn expands() {
let mut band: Band<5, _> = Band::new(0u32..10u32);
let mut band: Band<5, _, _> = Band::new(0u32..10u32);

assert_eq!(band.peek_front(), None);
assert_eq!(band.peek_back(), None);
Expand All @@ -143,7 +141,7 @@ mod tests {

#[test]
fn pops_front() {
let mut band: Band<5, _> = Band::new(0u32..10u32);
let mut band: Band<5, _, _> = Band::new(0u32..10u32);
band.expand_n(5);

assert_eq!(band.pop_front(), Some(0));
Expand All @@ -156,7 +154,7 @@ mod tests {

#[test]
fn pops_back() {
let mut band: Band<5, _> = Band::new(0u32..10u32);
let mut band: Band<5, _, _> = Band::new(0u32..10u32);
dbg!(&band);
band.expand_n(5);
dbg!(&band);
Expand All @@ -172,7 +170,7 @@ mod tests {

#[test]
fn peeks_at() {
let mut band: Band<5, _> = Band::new(0u32..10u32);
let mut band: Band<5, _, _> = Band::new(0u32..10u32);
band.expand_n(5);

assert_eq!(band.peek_at(0), Some(&0));
Expand All @@ -185,7 +183,7 @@ mod tests {

#[test]
fn len_correct() {
let mut band: Band<5, _> = Band::new(0u32..10u32);
let mut band: Band<5, _, _> = Band::new(0u32..10u32);
band.expand_n(5);

assert_eq!(band.len(), 5);
Expand All @@ -208,7 +206,7 @@ mod tests {

#[test]
fn makes_progress() {
let mut band: Band<5, _> = Band::new(0u32..5u32);
let mut band: Band<5, _, _> = Band::new(0u32..5u32);

// band was empty, first progress has nothing to return
assert_eq!(band.progress(), None);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! use ribbon::Ribbon;
//!
//! // Band with capacity for 5 items
//! let mut band: Band<3, _> = Band::new(0..4);
//! let mut band: Band<3, _, _> = Band::new(0..4);
//! band.expand_n(2); // consume 0, 1 from iterator
//!
//! assert_eq!(band.len(), 2);
Expand Down
32 changes: 13 additions & 19 deletions src/tape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ use std::collections::VecDeque;
/// [`VecDeque`]: std::collections::VecDeque
/// [`Ribbon`]: crate::Ribbon
#[derive(Debug)]
pub struct Tape<I>
where
I: Iterator,
{
pub struct Tape<I, T> {
iter: I,
tape: VecDeque<I::Item>,
tape: VecDeque<T>,
}

impl<I> Tape<I>
where
I: Iterator,
{
impl<I, T> Tape<I, T> {
/// Creates a new `Tape` from the given iterator.
pub fn new(iter: I) -> Tape<I>
pub fn new(iter: I) -> Tape<I, T>
where
I: Iterator,
I: Iterator<Item = T>,
{
Tape {
iter,
Expand All @@ -35,11 +29,11 @@ where
}
}

impl<I> super::ribbon::Ribbon<I::Item> for Tape<I>
impl<I, T> super::ribbon::Ribbon<T> for Tape<I, T>
where
I: Iterator,
I: Iterator<Item = T>,
{
fn progress(&mut self) -> Option<I::Item> {
fn progress(&mut self) -> Option<T> {
let next = self.iter.next()?;

let head = self.pop_front();
Expand All @@ -54,23 +48,23 @@ where
}
}

fn pop_front(&mut self) -> Option<I::Item> {
fn pop_front(&mut self) -> Option<T> {
self.tape.pop_front()
}

fn peek_front(&self) -> Option<&I::Item> {
fn peek_front(&self) -> Option<&T> {
self.tape.front()
}

fn pop_back(&mut self) -> Option<I::Item> {
fn pop_back(&mut self) -> Option<T> {
self.tape.pop_back()
}

fn peek_back(&self) -> Option<&I::Item> {
fn peek_back(&self) -> Option<&T> {
self.tape.back()
}

fn peek_at(&self, index: usize) -> Option<&I::Item> {
fn peek_at(&self, index: usize) -> Option<&T> {
self.tape.get(index)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_band() {
use ribbon::Band;

// Band with capacity for 5 items
let mut band: Band<3, _> = Band::new(0..4);
let mut band: Band<3, _, _> = Band::new(0..4);
band.expand_n(2); // consume 0, 1 from iterator

assert_eq!(band.len(), 2);
Expand Down

0 comments on commit ad005dd

Please sign in to comment.