-
Notifications
You must be signed in to change notification settings - Fork 375
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
WIP: refactor AVL #450
WIP: refactor AVL #450
Conversation
I'm wondering if we can do better than having Iterate() methods take *avl.Node as the parameter type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
I'll try to find better/alternative solutions for the iterate, but right now it looks great to me.
Thinking about these ways: for _, item := tree.Items() {
_ = item.Key()
_ = item.Value()
} for key := tree.Keys() {
item := tree.ByKey(key)
} cur := tree.Root()
for item := tree.Root(); item != nil; {
_ = item.Key()
_ = item.Value()
} |
regarding Another option is to replace But I think best would be to implement the interface we already have: pkgs/db/types.go ("tendermint") both use this iterator type. type Iterator interface {
// The start & end (exclusive) limits to iterate over.
// If end < start, then the Iterator goes in reverse order.
//
// A domain of ([]byte{12, 13}, []byte{12, 14}) will iterate
// over anything with the prefix []byte{12, 13}.
//
// The smallest key is the empty byte array []byte{} - see BeginningKey().
// The largest key is the nil byte array []byte(nil) - see EndingKey().
// CONTRACT: start, end readonly []byte
Domain() (start []byte, end []byte)
// Valid returns whether the current position is valid.
// Once invalid, an Iterator is forever invalid.
Valid() bool
// Next moves the iterator to the next sequential key in the database, as
// defined by order of iteration.
//
// If Valid returns false, this method will panic.
Next()
// Key returns the key of the cursor.
// If Valid returns false, this method will panic.
// CONTRACT: key readonly []byte
Key() (key []byte)
// Value returns the value of the cursor.
// If Valid returns false, this method will panic.
// CONTRACT: value readonly []byte
Value() (value []byte)
// Close releases the Iterator.
Close()
} Usage is like: itr := ...
defer itr.Close()
for ; itr.Valid(); itr.Next() { ... } -- Recently there had been discussions about a standard iterator interface: golang/go#54245 ; but the above requires generics so it is yet out of scope. Random tangent on generics: It would be nice if we could keep the Iterate(...,cb func(*Node) bool) implementation as is, and be able to assign var x NewInterface = (*Node)(xx); where NewInterface lists |
This PR makes AVL more intuitive to use.