This repository was archived by the owner on Nov 24, 2020. It is now read-only.
forked from ancientlore/go-avltree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjecttree.go
47 lines (38 loc) · 1.4 KB
/
objecttree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package avltree
import ()
// ObjectTree is a specialization of Tree that hides the wrapping of Elements around objects.
// The object just needs to implement Interface
type ObjectTree struct {
Tree
}
// Implement Interface so that your object can be sorted in the tree
type Interface interface {
// Return -1 if this < b, 0 if this == b, and 1 if this > b
Compare(b Interface) int
}
func objectCompare(o1 interface{}, o2 interface{}) int {
return o1.(Interface).Compare(o2.(Interface))
}
// Initialize or reset an ObjectTree
func (t *ObjectTree) Init(flags byte) *ObjectTree {
t.Tree.Init(objectCompare, flags)
return t
}
// Return an initialized ObjectTree
func NewObjectTree(flags byte) *ObjectTree { return new(ObjectTree).Init(flags) }
// Find returns the element where the comparison function matches
// the node's value and the given key value
func (t *ObjectTree) Find(key Interface) interface{} {
return t.Tree.Find(key)
}
// Add adds an item to the tree, returning a pair indicating the added
// (or duplicate) item, and a flag indicating whether the item is the
// duplicate that was found. A duplicate will never be returned if the
// tree's AllowDuplicates flag is set.
func (t *ObjectTree) Add(o Interface) (val interface{}, isDupe bool) {
return t.Tree.Add(o)
}
// Remove removes the element matching the given value.
func (t *ObjectTree) Remove(ptr Interface) interface{} {
return t.Tree.Remove(ptr)
}