Giter VIP home page Giter VIP logo

Comments (7)

orium avatar orium commented on July 23, 2024 1

Not currently, but it is something we want to implement.

from rpds.

FallingSnow avatar FallingSnow commented on July 23, 2024

Are you open to pull requests?

from rpds.

orium avatar orium commented on July 23, 2024

Sure, PRs are welcomed :)

from rpds.

FallingSnow avatar FallingSnow commented on July 23, 2024

So I've been looking into how best to implement the range function. Would it be worth it to add a parent reference to each node? That way we can create a range iterator that will be very optimized.

For example: suppose a tree of the following. If I range from 3..9 (all inclusive) I start an inorder traversal, arrive at 3 and give back an iterator. Now all I need to do on each .next() is continue the inorder traversal until I reach 9.

     5
    /  \
  3      8
 / \    / \
2   4  7   9

That said, this all requires that we add a parent reference to each node. This would offer big savings in very large trees.

from rpds.

orium avatar orium commented on July 23, 2024

I don't think you need the a parent field to do this efficiently. Take a look at how the current iterator works: it maintains a stack that will let you go to the parent node.

Try to modify the current iterator (and the normal iterator just calls the "range" iterator with the range (Unbounded, Unbounded)).

You need to change dig():

fn dig(stack: &mut Vec<&Node<K, V>>, backwards: bool) {
let child = stack.last().and_then(|node| {
let c = if backwards {
&node.right
} else {
&node.left
};
Node::borrow(c)
});
if let Some(c) = child {
stack.push(c);
IterArc::dig(stack, backwards);
}
}

to not go left unless left is within the range.

from rpds.

jneem avatar jneem commented on July 23, 2024

I've taken a look at this, and it seems there's a choice to be made: if I try to reuse the existing ArcIter implementation then I need to get rid of size_hint, since there's no efficient way to initialize left_index and right_index unless we're iterating over the whole tree. An alternative would be to introduce a new struct (RangeIter say). That would cause some code duplication, though.

Do you have any thoughts?

from rpds.

orium avatar orium commented on July 23, 2024

Good point: it will not be a implement ExactSizeIterator. I think we can have the best of both worlds: we can have a RangeIter that does not override size_hint() and does not implement ExactSizeIterator.

Then we can make Iter just a wrapper around a RangeIter (with range (Unbounded, Unbounded)). Iter will still implement ExactSizeIterator, but it only needs to have logic to maintain {right,left}_index

from rpds.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.