Merge branch 'release/0.6.0'
This commit is contained in:
commit
e5f53aec78
|
@ -1,8 +1,10 @@
|
||||||
package avl
|
package avl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
@ -31,6 +33,10 @@ type Tree struct {
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ tree.IBSTree = (*Tree)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *Tree {
|
func New(Compare compare.Compare) *Tree {
|
||||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package avldup
|
package avldup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
@ -32,6 +34,10 @@ type Tree struct {
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ tree.IBSTree = (*Tree)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *Tree {
|
func New(Compare compare.Compare) *Tree {
|
||||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package avlkey
|
package avlkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
@ -35,6 +37,10 @@ func New(Compare compare.Compare) *Tree {
|
||||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ tree.IBSTreeKey = (*Tree)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func (tree *Tree) String() string {
|
func (tree *Tree) String() string {
|
||||||
if tree.size == 0 {
|
if tree.size == 0 {
|
||||||
return ""
|
return ""
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package avlkeydup
|
package avlkeydup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
@ -31,6 +33,10 @@ type Tree struct {
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ tree.IBSTreeKey = (*Tree)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *Tree {
|
func New(Compare compare.Compare) *Tree {
|
||||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ func (h *Heap) Top() (interface{}, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Heap) Push(v interface{}) {
|
func (h *Heap) Put(v interface{}) {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
|
@ -11,7 +11,7 @@ func TestHeapPushTopPop(t *testing.T) {
|
||||||
l := []int{9, 5, 15, 2, 3}
|
l := []int{9, 5, 15, 2, 3}
|
||||||
ol := []int{15, 9, 5, 3, 2}
|
ol := []int{15, 9, 5, 3, 2}
|
||||||
for _, v := range l {
|
for _, v := range l {
|
||||||
h.Push(v)
|
h.Put(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tv := range ol {
|
for _, tv := range ol {
|
27
tree/tree.go
Normal file
27
tree/tree.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package tree
|
||||||
|
|
||||||
|
// IBSTreeKey Compare函数可以自定义所以key不一定value, 可以是value结构体中一个属性
|
||||||
|
type IBSTreeKey interface {
|
||||||
|
String() string
|
||||||
|
Size() int
|
||||||
|
Remove(key interface{}) (interface{}, bool)
|
||||||
|
Clear()
|
||||||
|
// Values 返回先序遍历的值
|
||||||
|
Values() []interface{}
|
||||||
|
Get(key interface{}) (interface{}, bool)
|
||||||
|
Put(key, value interface{})
|
||||||
|
Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// IBSTree
|
||||||
|
type IBSTree interface {
|
||||||
|
String() string
|
||||||
|
Size() int
|
||||||
|
Remove(key interface{}) (interface{}, bool)
|
||||||
|
Clear()
|
||||||
|
// Values 返回先序遍历的值
|
||||||
|
Values() []interface{}
|
||||||
|
Get(key interface{}) (interface{}, bool)
|
||||||
|
Put(value interface{})
|
||||||
|
Traversal(every func(v interface{}) bool, traversalMethod ...interface{})
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
package vbt
|
package vbt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
@ -31,6 +33,10 @@ type Tree struct {
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ tree.IBSTree = (*Tree)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *Tree {
|
func New(Compare compare.Compare) *Tree {
|
||||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||||
}
|
}
|
||||||
|
@ -231,6 +237,11 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tree *Tree) Clear() {
|
||||||
|
tree.root = nil
|
||||||
|
tree.iter = NewIteratorWithCap(nil, 16)
|
||||||
|
}
|
||||||
|
|
||||||
// Values 返回先序遍历的值
|
// Values 返回先序遍历的值
|
||||||
func (tree *Tree) Values() []interface{} {
|
func (tree *Tree) Values() []interface{} {
|
||||||
mszie := 0
|
mszie := 0
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package vbtkey
|
package vbtkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/tree"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
@ -32,6 +34,10 @@ type Tree struct {
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ tree.IBSTreeKey = (*Tree)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *Tree {
|
func New(Compare compare.Compare) *Tree {
|
||||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||||
}
|
}
|
||||||
|
@ -232,6 +238,11 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tree *Tree) Clear() {
|
||||||
|
tree.root = nil
|
||||||
|
tree.iter = NewIteratorWithCap(nil, 16)
|
||||||
|
}
|
||||||
|
|
||||||
// Values 返回先序遍历的值
|
// Values 返回先序遍历的值
|
||||||
func (tree *Tree) Values() []interface{} {
|
func (tree *Tree) Values() []interface{} {
|
||||||
mszie := 0
|
mszie := 0
|
||||||
|
@ -239,7 +250,7 @@ func (tree *Tree) Values() []interface{} {
|
||||||
mszie = tree.root.size
|
mszie = tree.root.size
|
||||||
}
|
}
|
||||||
result := make([]interface{}, 0, mszie)
|
result := make([]interface{}, 0, mszie)
|
||||||
tree.Traversal(func(v interface{}) bool {
|
tree.Traversal(func(k, v interface{}) bool {
|
||||||
result = append(result, v)
|
result = append(result, v)
|
||||||
return true
|
return true
|
||||||
}, LDR)
|
}, LDR)
|
||||||
|
@ -500,8 +511,8 @@ const (
|
||||||
RLD
|
RLD
|
||||||
)
|
)
|
||||||
|
|
||||||
// Traversal 遍历的方法 默认是LDR 从小到大 comparator 为 l < r
|
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
|
||||||
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
|
func (tree *Tree) Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{}) {
|
||||||
if tree.root == nil {
|
if tree.root == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -518,7 +529,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
|
||||||
if cur == nil {
|
if cur == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if !every(cur.value) {
|
if !every(cur.key, cur.value) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !traverasl(cur.children[0]) {
|
if !traverasl(cur.children[0]) {
|
||||||
|
@ -539,7 +550,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
|
||||||
if !traverasl(cur.children[0]) {
|
if !traverasl(cur.children[0]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !every(cur.value) {
|
if !every(cur.key, cur.value) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !traverasl(cur.children[1]) {
|
if !traverasl(cur.children[1]) {
|
||||||
|
@ -560,7 +571,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
|
||||||
if !traverasl(cur.children[1]) {
|
if !traverasl(cur.children[1]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !every(cur.value) {
|
if !every(cur.key, cur.value) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -572,7 +583,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
|
||||||
if cur == nil {
|
if cur == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if !every(cur.value) {
|
if !every(cur.key, cur.value) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !traverasl(cur.children[0]) {
|
if !traverasl(cur.children[0]) {
|
||||||
|
@ -593,7 +604,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
|
||||||
if !traverasl(cur.children[1]) {
|
if !traverasl(cur.children[1]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !every(cur.value) {
|
if !every(cur.key, cur.value) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !traverasl(cur.children[0]) {
|
if !traverasl(cur.children[0]) {
|
||||||
|
@ -614,7 +625,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
|
||||||
if !traverasl(cur.children[0]) {
|
if !traverasl(cur.children[0]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !every(cur.value) {
|
if !every(cur.key, cur.value) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -7,9 +7,9 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
|
@ -265,8 +265,8 @@ func TestTravalsal(t *testing.T) {
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
var result []interface{}
|
var result []interface{}
|
||||||
tree.Traversal(func(v interface{}) bool {
|
tree.Traversal(func(k, v interface{}) bool {
|
||||||
result = append(result, v)
|
result = append(result, k)
|
||||||
i++
|
i++
|
||||||
if i >= 10 {
|
if i >= 10 {
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in New Issue
Block a user