GoLLRB
GoLLRB copied to clipboard
IterRange can never include the last (Max) element
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
< @upperto<= @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?
I'll look into this and update.
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.
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())
}