GoLLRB icon indicating copy to clipboard operation
GoLLRB copied to clipboard

IterRange can never include the last (Max) element

Open peterbourgon opened this issue 14 years ago • 3 comments

I want to iterate from a given key to the end of the tree inclusively. But there seems to be no way to get the last (tree.Max()) value as part of that iteration. I don't know what the most elegant solution would be...

  • a bool param to IterRange that would change < @upper to <= @upper?
  • a new IterRangeInclusive that goes @lower <= E <= @upper?
  • telling me I should simply tack on tree.Max() when my IterRange chan returns a nil and I expect another element?

peterbourgon avatar Apr 21 '11 17:04 peterbourgon

I'll look into this and update.

petar avatar Apr 24 '11 11:04 petar

I'm not yet entirely sure how to design the interface that controls inclusion/exclusion. One tempting possibility is to give a method to iterates from a given node (not item) to a given other node, inclusive. This is most general. To use it, you'd first "find" the nodes you need and then call the iterator. This will require some exposing the Node type, which is not terrible. I will think about this a bit. In the meantime, I've added IterRangeInclusive.

petar avatar Apr 25 '11 16:04 petar

Now that I think on it a bit, this specific case devolves to a more general case: what I could really use is

func (t *Tree) IterAscendFrom(start Item) <-chan Item

and the complementary IterDescendFrom... working exactly like the existing IterAscend but allowing you to specify a start item which is included in the iteration. Looking forward, IterAscend would then become:

func (t *Tree) IterAscend() <-chan Item {
    return t.IterAscendFrom(t.Min())
}

peterbourgon avatar Apr 26 '11 12:04 peterbourgon