Documentation ¶
Overview ¶
Example (Usage) ¶
ExampleUsage Shows how this package can be used
package main import ( "fmt" ) func NewUserStore() *UserStore { return &UserStore{tree: &Tree{}} } type UserVisit func(*User) bool type User struct { ID uint64 Age uint8 } func (u *User) Less(k Key) bool { return u.ID < k.(*User).ID } func (u *User) Equals(k Key) bool { return u.ID == k.(*User).ID } type UserStore struct { tree *Tree } func (us *UserStore) Get(id uint64) *User { v := us.tree.Get(&User{ID: id}) if v == nil { return nil } return v.(*User) } func (us *UserStore) Size() int { return us.tree.Size() } func (us *UserStore) Clear() { us.tree.Clear() } func (us *UserStore) Has(id uint64) bool { return us.tree.Has(&User{ID: id}) } func (us *UserStore) Del(id uint64) { us.tree.Del(&User{ID: id}) } func (us *UserStore) Put(u *User) { us.tree.Put(u) } func (us *UserStore) VisitAscending(v UserVisit) { us.tree.VisitAscending(func(k Key) bool { v(k.(*User)) return true }) } func (us *UserStore) VisitDescending(v UserVisit) { us.tree.VisitDescending(func(k Key) bool { v(k.(*User)) return true }) } // ExampleUsage Shows how this package can be used func main() { us := NewUserStore() for i := 1; i <= 256; i++ { us.Put(&User{ID: uint64(i)}) } if us.Size() != 256 { panic("Size should be 256") } user := us.Get(128) if user == nil { panic("Should have user with ID 128") } fmt.Printf("%+v\n", user) if !us.Has(128) { panic("Should have user ID 128") } us.Del(128) if us.Has(128) { panic("Should not have user ID 128") } us.Clear() }
Output: &{ID:128 Age:0}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree An AVL tree to insert, search, and delete in O(log n)
func (*Tree) Get ¶
Get Searches for node with the Key k and returns the key. It returns nil if the Key cannot be found in the tree.
func (*Tree) Put ¶
Put Inserts a node with Key k into the tree. The node is not inserted if a node with the Key k alredy exist.
func (*Tree) VisitAscending ¶
VisitAscending Traverse the tree from the smallest to the largest Key by calling the Visit function v.
func (*Tree) VisitDescending ¶
VisitDescending Traverse the tree from the largest to the smalles Key by calling the Visit function v.
Click to show internal directories.
Click to hide internal directories.