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

revset: detach index from internal evaluation tree #3273

Merged
merged 7 commits into from
Mar 12, 2024
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
155 changes: 154 additions & 1 deletion lib/src/default_index/rev_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::cmp::{max, Reverse};
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::iter::FusedIterator;
use std::iter::{Fuse, FusedIterator};
use std::ops::Range;

use smallvec::SmallVec;
Expand All @@ -39,6 +39,29 @@ pub(super) trait RevWalk<I: ?Sized> {
// The following methods are provided for convenience. They are not supposed
// to be reimplemented.

/// Wraps in adapter that will filter items by the given `predicate`.
fn filter<P>(self, predicate: P) -> FilterRevWalk<Self, P>
where
Self: Sized,
P: FnMut(&I, &Self::Item) -> bool,
{
FilterRevWalk {
walk: self,
predicate,
}
}

/// Wraps in adapter that can peek one more item without consuming.
fn peekable(self) -> PeekableRevWalk<I, Self>
where
Self: Sized,
{
PeekableRevWalk {
walk: self,
peeked: None,
}
}

/// Reattaches the underlying `index`.
fn attach(self, index: &I) -> RevWalkBorrowedIndexIter<'_, I, Self>
where
Expand All @@ -48,6 +71,101 @@ pub(super) trait RevWalk<I: ?Sized> {
}
}

impl<I: ?Sized, W: RevWalk<I> + ?Sized> RevWalk<I> for Box<W> {
type Item = W::Item;

fn next(&mut self, index: &I) -> Option<Self::Item> {
<W as RevWalk<I>>::next(self, index)
}
}

/// Adapter that turns `Iterator` into `RevWalk` by dropping index argument.
///
/// As the name suggests, the source object is usually a slice or `Vec`.
#[derive(Clone, Debug)]
pub(super) struct EagerRevWalk<T> {
iter: Fuse<T>,
}

impl<T: Iterator> EagerRevWalk<T> {
pub fn new(iter: T) -> Self {
EagerRevWalk { iter: iter.fuse() }
}
}

impl<I: ?Sized, T: Iterator> RevWalk<I> for EagerRevWalk<T> {
type Item = T::Item;

fn next(&mut self, _index: &I) -> Option<Self::Item> {
self.iter.next()
}
}

#[derive(Clone, Debug)]
#[must_use]
pub(super) struct FilterRevWalk<W, P> {
walk: W,
predicate: P,
}

impl<I, W, P> RevWalk<I> for FilterRevWalk<W, P>
where
I: ?Sized,
W: RevWalk<I>,
P: FnMut(&I, &W::Item) -> bool,
{
type Item = W::Item;

fn next(&mut self, index: &I) -> Option<Self::Item> {
while let Some(item) = self.walk.next(index) {
if (self.predicate)(index, &item) {
return Some(item);
}
}
None
}
}

#[derive(Clone, Debug)]
#[must_use]
pub(super) struct PeekableRevWalk<I: ?Sized, W: RevWalk<I>> {
walk: W,
// Since RevWalk is fused, we don't need a nested Option<Option<_>>.
peeked: Option<W::Item>,
}

impl<I: ?Sized, W: RevWalk<I>> PeekableRevWalk<I, W> {
pub fn peek(&mut self, index: &I) -> Option<&W::Item> {
if self.peeked.is_none() {
self.peeked = self.walk.next(index);
}
self.peeked.as_ref()
}

pub fn next_if(
&mut self,
index: &I,
predicate: impl FnOnce(&W::Item) -> bool,
) -> Option<W::Item> {
match self.next(index) {
Some(item) if predicate(&item) => Some(item),
other => {
assert!(self.peeked.is_none());
self.peeked = other;
None
}
}
}
}

impl<I: ?Sized, W: RevWalk<I>> RevWalk<I> for PeekableRevWalk<I, W> {
type Item = W::Item;

fn next(&mut self, index: &I) -> Option<Self::Item> {
self.peeked.take().or_else(|| self.walk.next(index))
}
}

/// Adapter that turns `RevWalk` into `Iterator` by attaching borrowed `index`.
#[derive(Clone, Debug)]
#[must_use]
Expand Down Expand Up @@ -678,6 +796,41 @@ mod tests {
.collect()
}

#[test]
fn test_peekable_rev_walk() {
let source = EagerRevWalk::new(vec![0, 1, 2, 3].into_iter());
let mut peekable = source.peekable();
assert_eq!(peekable.peek(&()), Some(&0));
assert_eq!(peekable.peek(&()), Some(&0));
assert_eq!(peekable.next(&()), Some(0));
assert_eq!(peekable.peeked, None);
assert_eq!(peekable.next_if(&(), |&v| v == 2), None);
assert_eq!(peekable.next(&()), Some(1));
assert_eq!(peekable.next_if(&(), |&v| v == 2), Some(2));
assert_eq!(peekable.peeked, None);
assert_eq!(peekable.peek(&()), Some(&3));
assert_eq!(peekable.next_if(&(), |&v| v == 3), Some(3));
assert_eq!(peekable.peeked, None);
assert_eq!(peekable.next(&()), None);
assert_eq!(peekable.next(&()), None);

let source = EagerRevWalk::new((vec![] as Vec<i32>).into_iter());
let mut peekable = source.peekable();
assert_eq!(peekable.peek(&()), None);
assert_eq!(peekable.next(&()), None);
}

#[test]
fn test_filter_rev_walk() {
let source = EagerRevWalk::new(vec![0, 1, 2, 3, 4].into_iter());
let mut filtered = source.filter(|_, &v| v & 1 == 0);
assert_eq!(filtered.next(&()), Some(0));
assert_eq!(filtered.next(&()), Some(2));
assert_eq!(filtered.next(&()), Some(4));
assert_eq!(filtered.next(&()), None);
assert_eq!(filtered.next(&()), None);
}

#[test]
fn test_walk_ancestors() {
let mut new_change_id = change_id_generator();
Expand Down
Loading