2019-05-08 02:42:51 +00:00
|
|
|
package treeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-05-08 03:04:03 +00:00
|
|
|
"github.com/474420502/focus/compare"
|
2019-07-20 20:10:08 +00:00
|
|
|
"github.com/474420502/focus/tree/avldup"
|
2019-05-08 02:42:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TreeSet
|
|
|
|
type TreeSet struct {
|
|
|
|
tree *avldup.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
// New
|
|
|
|
func New(Compare compare.Compare) *TreeSet {
|
|
|
|
return &TreeSet{tree: avldup.New(Compare)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add
|
|
|
|
func (set *TreeSet) Add(items ...interface{}) {
|
|
|
|
for _, item := range items {
|
|
|
|
set.tree.Put(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove
|
|
|
|
func (set *TreeSet) Remove(items ...interface{}) {
|
|
|
|
for _, item := range items {
|
|
|
|
set.tree.Remove(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values
|
|
|
|
func (set *TreeSet) Values() []interface{} {
|
|
|
|
return set.tree.Values()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains
|
|
|
|
func (set *TreeSet) Contains(item interface{}) bool {
|
|
|
|
if _, ok := set.tree.Get(item); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains the result is [r1,r2], not [r1, r2)
|
|
|
|
func (set *TreeSet) GetRange(r1, r2 interface{}) (result []interface{}) {
|
|
|
|
return set.tree.GetRange(r1, r2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains the result is [r1,item,r2] r1->item->r2 are close-knit
|
|
|
|
func (set *TreeSet) GetAround(item interface{}) (result [3]interface{}) {
|
|
|
|
return set.tree.GetAround(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty
|
|
|
|
func (set *TreeSet) Empty() bool {
|
|
|
|
return set.Size() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear
|
|
|
|
func (set *TreeSet) Clear() {
|
|
|
|
set.tree.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size
|
|
|
|
func (set *TreeSet) Size() int {
|
|
|
|
return set.tree.Size()
|
|
|
|
}
|
|
|
|
|
2019-07-20 20:10:08 +00:00
|
|
|
// Iterator avl Iterator
|
|
|
|
func (set *TreeSet) Iterator() *avldup.Iterator {
|
|
|
|
return set.tree.Iterator()
|
|
|
|
}
|
|
|
|
|
2019-05-08 02:42:51 +00:00
|
|
|
// String
|
|
|
|
func (set *TreeSet) String() string {
|
2019-07-20 20:10:08 +00:00
|
|
|
// content := "HashSet\n"
|
|
|
|
var content = ""
|
2019-05-08 02:42:51 +00:00
|
|
|
items := []string{}
|
|
|
|
|
|
|
|
set.tree.Traversal(func(k interface{}) bool {
|
|
|
|
items = append(items, fmt.Sprintf("%v", k))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2019-07-20 20:10:08 +00:00
|
|
|
content += "(" + strings.Join(items, ", ") + ")"
|
2019-05-08 02:42:51 +00:00
|
|
|
return content
|
|
|
|
}
|