Compare commits
20 Commits
v0.4.0
...
feature/to
| Author | SHA1 | Date | |
|---|---|---|---|
| 044a9342f0 | |||
| f70017374b | |||
| 76cea0eb35 | |||
| c2dd82b426 | |||
| 7fc9a84fbf | |||
|
|
71d9d5026f | ||
|
|
f6cab7cc19 | ||
|
|
b6fe42de8f | ||
|
|
f63759645e | ||
|
|
c0ad187222 | ||
|
|
ade0b78c6f | ||
|
|
211ab2661c | ||
|
|
411281d865 | ||
| 5ef163bca7 | |||
| e5f53aec78 | |||
| 3ade1566c2 | |||
|
|
4a58bd283e | ||
|
|
6110d7c487 | ||
|
|
6516b424ee | ||
|
|
fe15076461 |
@@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/474420502/focus/list"
|
"github.com/474420502/focus/list"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ArrayList struct {
|
type ArrayList struct {
|
||||||
@@ -34,6 +35,7 @@ func New() *ArrayList {
|
|||||||
l.data = make([]interface{}, initCap, initCap)
|
l.data = make([]interface{}, initCap, initCap)
|
||||||
l.tidx = initCap / 2
|
l.tidx = initCap / 2
|
||||||
l.hidx = l.tidx - 1
|
l.hidx = l.tidx - 1
|
||||||
|
// l.shrinkSize = listMinLimit
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,15 +69,19 @@ func (l *ArrayList) shrink() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if l.size <= l.shrinkSize {
|
if l.size <= l.shrinkSize {
|
||||||
nSize := l.shrinkSize - l.shrinkSize>>1
|
lcap := uint(len(l.data))
|
||||||
|
nSize := lcap - lcap>>2
|
||||||
temp := make([]interface{}, nSize, nSize)
|
temp := make([]interface{}, nSize, nSize)
|
||||||
|
|
||||||
ghidx := l.size / 2
|
ghidx := l.size >> 2
|
||||||
gtidx := ghidx + l.size + 1
|
gtidx := ghidx + l.size + 1
|
||||||
copy(temp[ghidx+1:], l.data[l.hidx+1:l.tidx])
|
copy(temp[ghidx+1:], l.data[l.hidx+1:l.tidx])
|
||||||
l.data = temp
|
l.data = temp
|
||||||
l.hidx = ghidx
|
l.hidx = ghidx
|
||||||
l.tidx = gtidx
|
l.tidx = gtidx
|
||||||
|
|
||||||
|
// l.shrinkSize = l.shrinkSize - lcap>>2
|
||||||
|
l.shrinkSize = l.size - l.size>>2
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -98,6 +104,7 @@ func (l *ArrayList) growth() {
|
|||||||
l.hidx = ghidx
|
l.hidx = ghidx
|
||||||
l.tidx = gtidx
|
l.tidx = gtidx
|
||||||
|
|
||||||
|
l.shrinkSize = l.size - l.size>>2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Push(value interface{}) {
|
func (l *ArrayList) Push(value interface{}) {
|
||||||
@@ -140,6 +147,7 @@ func (l *ArrayList) PopFront() (result interface{}, found bool) {
|
|||||||
if l.size != 0 {
|
if l.size != 0 {
|
||||||
l.size--
|
l.size--
|
||||||
l.hidx++
|
l.hidx++
|
||||||
|
l.shrink()
|
||||||
return l.data[l.hidx], true
|
return l.data[l.hidx], true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
@@ -149,25 +157,32 @@ func (l *ArrayList) PopBack() (result interface{}, found bool) {
|
|||||||
if l.size != 0 {
|
if l.size != 0 {
|
||||||
l.size--
|
l.size--
|
||||||
l.tidx--
|
l.tidx--
|
||||||
|
l.shrink()
|
||||||
return l.data[l.tidx], true
|
return l.data[l.tidx], true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Index(idx uint) (interface{}, bool) {
|
func (l *ArrayList) Index(idx int) (interface{}, bool) {
|
||||||
if idx < l.size {
|
var uidx uint = (uint)(idx)
|
||||||
return l.data[idx+l.hidx+1], true
|
if uidx < l.size {
|
||||||
|
return l.data[uidx+l.hidx+1], true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Remove(idx uint) (result interface{}, isfound bool) {
|
func (l *ArrayList) Remove(idx int) (result interface{}, isfound bool) {
|
||||||
|
|
||||||
if idx >= l.size {
|
if idx < 0 {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
offset := l.hidx + 1 + idx
|
var uidx = (uint)(idx)
|
||||||
|
if uidx >= l.size {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := l.hidx + 1 + uidx
|
||||||
|
|
||||||
isfound = true
|
isfound = true
|
||||||
result = l.data[offset]
|
result = l.data[offset]
|
||||||
@@ -210,6 +225,10 @@ func (l *ArrayList) Values() []interface{} {
|
|||||||
return newElements
|
return newElements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *ArrayList) String() string {
|
||||||
|
return spew.Sprint(l.Values())
|
||||||
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Traversal(every func(interface{}) bool) {
|
func (l *ArrayList) Traversal(every func(interface{}) bool) {
|
||||||
for i := uint(0); i < l.size; i++ {
|
for i := uint(0); i < l.size; i++ {
|
||||||
if !every(l.data[i+l.hidx+1]) {
|
if !every(l.data[i+l.hidx+1]) {
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package arraylist
|
package arraylist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/gob"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
@@ -178,8 +174,8 @@ func TestRemove(t *testing.T) {
|
|||||||
var result string
|
var result string
|
||||||
|
|
||||||
for _, selval := range []uint{4, 3} {
|
for _, selval := range []uint{4, 3} {
|
||||||
last, _ := l.Index(selval)
|
last, _ := l.Index((int)(selval))
|
||||||
if v, isfound := l.Remove(selval); isfound {
|
if v, isfound := l.Remove((int)(selval)); isfound {
|
||||||
if v != last {
|
if v != last {
|
||||||
t.Error(v, " != ", last)
|
t.Error(v, " != ", last)
|
||||||
}
|
}
|
||||||
@@ -244,24 +240,56 @@ func TestTraversal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadTestData() []int {
|
func TestRemain(t *testing.T) {
|
||||||
data, err := ioutil.ReadFile("../../l.log")
|
l := New()
|
||||||
if err != nil {
|
for i := 0; i < 10; i++ {
|
||||||
log.Println(err)
|
l.Push(i)
|
||||||
|
if !l.Contains(i) {
|
||||||
|
t.Error("Contains", i)
|
||||||
}
|
}
|
||||||
var l []int
|
|
||||||
decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
||||||
decoder.Decode(&l)
|
|
||||||
return l
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPush(b *testing.B) {
|
if l.String() != "[0 1 2 3 4 5 6 7 8 9]" {
|
||||||
l := loadTestData()
|
t.Error(l.String())
|
||||||
b.N = len(l)
|
}
|
||||||
|
|
||||||
arr := New()
|
for i := 10; i < 100; i++ {
|
||||||
|
l.Push(i)
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for !l.Empty() {
|
||||||
arr.PushBack(l[i])
|
l.PopBack()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 10; i < 100; i++ {
|
||||||
|
l.Push(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Clear()
|
||||||
|
|
||||||
|
if l.Size() != 0 {
|
||||||
|
t.Error("Size != 0")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func loadTestData() []int {
|
||||||
|
// data, err := ioutil.ReadFile("../../l.log")
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println(err)
|
||||||
|
// }
|
||||||
|
// var l []int
|
||||||
|
// decoder := gob.NewDecoder(bytes.NewReader(data))
|
||||||
|
// decoder.Decode(&l)
|
||||||
|
// return l
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func BenchmarkPush(b *testing.B) {
|
||||||
|
// l := loadTestData()
|
||||||
|
// b.N = len(l)
|
||||||
|
|
||||||
|
// arr := New()
|
||||||
|
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// arr.PushBack(l[i])
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ type Iterator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) Value() interface{} {
|
func (iter *Iterator) Value() interface{} {
|
||||||
v, _ := iter.al.Index(iter.cur)
|
v, _ := iter.al.Index((int)(iter.cur))
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ type CircularIterator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *CircularIterator) Value() interface{} {
|
func (iter *CircularIterator) Value() interface{} {
|
||||||
v, _ := iter.al.Index(iter.cur)
|
v, _ := iter.al.Index((int)(iter.cur))
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -191,28 +191,34 @@ func (l *LinkedList) FindMany(every func(idx uint, value interface{}) int) (resu
|
|||||||
return result, isfound
|
return result, isfound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LinkedList) Index(idx uint) (interface{}, bool) {
|
func (l *LinkedList) Index(idx int) (interface{}, bool) {
|
||||||
if idx >= l.size {
|
|
||||||
|
if idx < 0 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
var uidx = (uint)(idx)
|
||||||
|
|
||||||
|
if uidx >= l.size || idx < 0 {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if idx > l.size/2 {
|
if uidx > l.size/2 {
|
||||||
idx = l.size - 1 - idx
|
uidx = l.size - 1 - uidx
|
||||||
// 尾部
|
// 尾部
|
||||||
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 头部
|
// 头部
|
||||||
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,33 +377,39 @@ func remove(cur *Node) {
|
|||||||
cur.next = nil
|
cur.next = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LinkedList) Remove(idx uint) (interface{}, bool) {
|
func (l *LinkedList) Remove(idx int) (interface{}, bool) {
|
||||||
if l.size <= idx {
|
|
||||||
|
if idx < 0 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
var uidx uint = (uint)(idx)
|
||||||
|
if l.size <= uidx {
|
||||||
// log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx)
|
// log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx)
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
l.size--
|
l.size--
|
||||||
if idx > l.size/2 {
|
if uidx > l.size/2 {
|
||||||
idx = l.size - idx // l.size - 1 - idx, 先减size
|
uidx = l.size - uidx // l.size - 1 - idx, 先减size
|
||||||
// 尾部
|
// 尾部
|
||||||
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
remove(cur)
|
remove(cur)
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 头部
|
// 头部
|
||||||
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
remove(cur)
|
remove(cur)
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
|
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ package list
|
|||||||
type IList interface {
|
type IList interface {
|
||||||
Push(value interface{})
|
Push(value interface{})
|
||||||
Contains(values ...interface{}) bool
|
Contains(values ...interface{}) bool
|
||||||
Index(idx uint) (interface{}, bool)
|
Index(idx int) (interface{}, bool)
|
||||||
Remove(idx uint) (result interface{}, isfound bool)
|
Remove(idx int) (result interface{}, isfound bool)
|
||||||
Values() []interface{}
|
Values() []interface{}
|
||||||
Traversal(every func(interface{}) bool)
|
Traversal(every func(interface{}) bool)
|
||||||
|
String() string
|
||||||
|
|
||||||
Clear()
|
Clear()
|
||||||
Empty() bool
|
Empty() bool
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/474420502/focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/list"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ type PriorityList struct {
|
|||||||
|
|
||||||
// 优先队列的Index 正负都有作用要定义一种新的接口类型
|
// 优先队列的Index 正负都有作用要定义一种新的接口类型
|
||||||
func assertImplementation() {
|
func assertImplementation() {
|
||||||
// var _ list.IList = (*PriorityList)(nil)
|
var _ list.IList = (*PriorityList)(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *PriorityList {
|
func New(Compare compare.Compare) *PriorityList {
|
||||||
@@ -173,11 +174,19 @@ func (pl *PriorityList) GetNode(idx int) (*Node, bool) {
|
|||||||
|
|
||||||
func (pl *PriorityList) RemoveWithIndex(idx int) {
|
func (pl *PriorityList) RemoveWithIndex(idx int) {
|
||||||
if n, ok := pl.GetNode(idx); ok {
|
if n, ok := pl.GetNode(idx); ok {
|
||||||
pl.Remove(n)
|
pl.RemoveNode(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pl *PriorityList) Remove(node *Node) {
|
func (pl *PriorityList) Remove(idx int) (result interface{}, isfound bool) {
|
||||||
|
if n, ok := pl.GetNode(idx); ok {
|
||||||
|
pl.RemoveNode(n)
|
||||||
|
return n.value, true
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pl *PriorityList) RemoveNode(node *Node) {
|
||||||
|
|
||||||
prev := node.prev
|
prev := node.prev
|
||||||
next := node.next
|
next := node.next
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func (lhmap *LinkedHashmap) Remove(key interface{}) (interface{}, bool) {
|
|||||||
func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) {
|
func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) {
|
||||||
if lhmap.list.Size() >= idx {
|
if lhmap.list.Size() >= idx {
|
||||||
// log.Printf("warn: out of list range, size is %d, idx is %d\n", lhmap.list.Size(), idx)
|
// log.Printf("warn: out of list range, size is %d, idx is %d\n", lhmap.list.Size(), idx)
|
||||||
if key, ok := lhmap.list.Remove(idx); ok {
|
if key, ok := lhmap.list.Remove((int)(idx)); ok {
|
||||||
result := lhmap.hmap[key]
|
result := lhmap.hmap[key]
|
||||||
delete(lhmap.hmap, key)
|
delete(lhmap.hmap, key)
|
||||||
return result, true
|
return result, true
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package pqueue
|
package pqueue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@@ -100,9 +101,9 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
|
|||||||
|
|
||||||
if idx1^idx2 < 0 {
|
if idx1^idx2 < 0 {
|
||||||
if idx1 < 0 {
|
if idx1 < 0 {
|
||||||
idx1 = tree.root.size + idx1 - 1
|
idx1 = tree.root.size + idx1
|
||||||
} else {
|
} else {
|
||||||
idx2 = tree.root.size + idx2 - 1
|
idx2 = tree.root.size + idx2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package pqueuekey
|
package pqueuekey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/compare"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@@ -101,9 +102,9 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
|
|||||||
|
|
||||||
if idx1^idx2 < 0 {
|
if idx1^idx2 < 0 {
|
||||||
if idx1 < 0 {
|
if idx1 < 0 {
|
||||||
idx1 = tree.root.size + idx1 - 1
|
idx1 = tree.root.size + idx1
|
||||||
} else {
|
} else {
|
||||||
idx2 = tree.root.size + idx2 - 1
|
idx2 = tree.root.size + idx2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
@@ -110,40 +110,28 @@ func (h *Heap) Pop() (interface{}, bool) {
|
|||||||
|
|
||||||
downvalue := h.elements[h.size]
|
downvalue := h.elements[h.size]
|
||||||
var cidx, c1, c2 int
|
var cidx, c1, c2 int
|
||||||
var cvalue1, cvalue2, cvalue interface{}
|
|
||||||
// down
|
// down
|
||||||
for {
|
for {
|
||||||
cidx = curidx << 1
|
cidx = curidx << 1
|
||||||
|
|
||||||
c2 = cidx + 2
|
|
||||||
if c2 < h.size {
|
|
||||||
cvalue2 = h.elements[c2]
|
|
||||||
|
|
||||||
c1 = cidx + 1
|
c1 = cidx + 1
|
||||||
cvalue1 = h.elements[c1]
|
c2 = cidx + 2
|
||||||
|
|
||||||
if h.Compare(cvalue1, cvalue2) >= 0 {
|
if c2 < h.size {
|
||||||
|
if h.Compare(h.elements[c1], h.elements[c2]) >= 0 {
|
||||||
cidx = c1
|
cidx = c1
|
||||||
cvalue = cvalue1
|
|
||||||
} else {
|
} else {
|
||||||
cidx = c2
|
cidx = c2
|
||||||
cvalue = cvalue2
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
c1 = cidx + 1
|
|
||||||
if c1 < h.size {
|
|
||||||
cvalue1 = h.elements[c1]
|
|
||||||
cidx = c1
|
cidx = c1
|
||||||
cvalue = cvalue1
|
if c1 >= h.size {
|
||||||
} else {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.Compare(cvalue, downvalue) > 0 {
|
if h.Compare(h.elements[cidx], downvalue) > 0 {
|
||||||
h.elements[curidx] = cvalue
|
h.elements[curidx] = h.elements[cidx]
|
||||||
curidx = cidx
|
curidx = cidx
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -1,17 +1,70 @@
|
|||||||
package heap
|
package heap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/474420502/focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/Pallinder/go-randomdata"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestHeapGrowSlimming(t *testing.T) {
|
||||||
|
|
||||||
|
for ii := 0; ii < 1000; ii++ {
|
||||||
|
|
||||||
|
h := New(compare.Int)
|
||||||
|
var results []int
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
v := randomdata.Number(0, 100)
|
||||||
|
results = append(results, v)
|
||||||
|
h.Put(v)
|
||||||
|
}
|
||||||
|
sort.Slice(results, func(i, j int) bool {
|
||||||
|
if results[i] > results[j] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if h.Size() != 100 || h.Empty() {
|
||||||
|
t.Error("size != 100")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; !h.Empty(); i++ {
|
||||||
|
v, _ := h.Pop()
|
||||||
|
if results[i] != v {
|
||||||
|
t.Error("heap is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if h.Size() != 0 {
|
||||||
|
t.Error("size != 0")
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Put(1)
|
||||||
|
h.Put(5)
|
||||||
|
h.Put(2)
|
||||||
|
|
||||||
|
if h.Values()[0] != 5 {
|
||||||
|
t.Error("top is not equal to 5")
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Clear()
|
||||||
|
h.Reborn()
|
||||||
|
|
||||||
|
if !h.Empty() {
|
||||||
|
t.Error("clear reborn is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestHeapPushTopPop(t *testing.T) {
|
func TestHeapPushTopPop(t *testing.T) {
|
||||||
h := New(compare.Int)
|
h := New(compare.Int)
|
||||||
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 {
|
||||||
@@ -31,8 +84,44 @@ func TestHeapPushTopPop(t *testing.T) {
|
|||||||
if h.Size() != 0 {
|
if h.Size() != 0 {
|
||||||
t.Error("heap size is not equals to zero")
|
t.Error("heap size is not equals to zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.Clear()
|
||||||
|
|
||||||
|
l = []int{3, 5, 2, 7, 1}
|
||||||
|
|
||||||
|
for _, v := range l {
|
||||||
|
h.Put(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Slice(l, func(i, j int) bool {
|
||||||
|
if l[i] > l[j] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 0; !h.Empty(); i++ {
|
||||||
|
v, _ := h.Pop()
|
||||||
|
if l[i] != v {
|
||||||
|
t.Error("heap is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// func BenchmarkPush(b *testing.B) {
|
||||||
|
// h := New(compare.Int)
|
||||||
|
// b.N = 40000000
|
||||||
|
// var results []int
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// results = append(results, randomdata.Number(0, 1000000000))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// b.ResetTimer()
|
||||||
|
// for _, v := range results {
|
||||||
|
// h.Put(v)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// func Int(k1, k2 interface{}) int {
|
// func Int(k1, k2 interface{}) int {
|
||||||
// c1 := k1.(int)
|
// c1 := k1.(int)
|
||||||
// c2 := k2.(int)
|
// c2 := k2.(int)
|
||||||
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)}
|
||||||
}
|
}
|
||||||
@@ -98,9 +104,9 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
|||||||
|
|
||||||
if idx1^idx2 < 0 {
|
if idx1^idx2 < 0 {
|
||||||
if idx1 < 0 {
|
if idx1 < 0 {
|
||||||
idx1 = tree.root.size + idx1 - 1
|
idx1 = tree.root.size + idx1
|
||||||
} else {
|
} else {
|
||||||
idx2 = tree.root.size + idx2 - 1
|
idx2 = tree.root.size + idx2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/474420502/focus/compare"
|
"github.com/Pallinder/go-randomdata"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
@@ -23,6 +25,98 @@ func loadTestData() []int {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLargePushRemove(t *testing.T) {
|
||||||
|
for n := 0; n < 10; n++ {
|
||||||
|
tree := New(compare.Int)
|
||||||
|
var results []int
|
||||||
|
for i := 0; i < 50000; i++ {
|
||||||
|
v := randomdata.Number(0, 100000000)
|
||||||
|
tree.Put(v)
|
||||||
|
results = append(results, v)
|
||||||
|
}
|
||||||
|
if tree.Size() != 50000 {
|
||||||
|
t.Error("Szie error")
|
||||||
|
}
|
||||||
|
for i := 0; i < 49990; i++ {
|
||||||
|
tree.Remove(results[i])
|
||||||
|
}
|
||||||
|
results = results[49990:]
|
||||||
|
if tree.Size() != 10 {
|
||||||
|
t.Error("Szie error")
|
||||||
|
}
|
||||||
|
sort.Slice(results, func(i, j int) bool {
|
||||||
|
if results[i] < results[j] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
if spew.Sprint(results) != spew.Sprint(tree.Values()) {
|
||||||
|
t.Error("tree is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
v1 := results[i]
|
||||||
|
v2, ok := tree.Index(i)
|
||||||
|
if !ok {
|
||||||
|
t.Error("not ok")
|
||||||
|
}
|
||||||
|
if v1 != v2 {
|
||||||
|
t.Error("v1(", v1, ") != v2(", v2, ")??")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.Clear()
|
||||||
|
if tree.String() != "AVLTree\nnil" {
|
||||||
|
t.Error("tree String is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveIndex(t *testing.T) {
|
||||||
|
tree := New(compare.Int)
|
||||||
|
l := []int{7, 14, 14, 14, 16, 1, 40, 15}
|
||||||
|
for _, v := range l {
|
||||||
|
tree.Put(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// [1 7 14 14 14 15 16 40]
|
||||||
|
var result string
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[1 7 14 14 14 15 16 40]" {
|
||||||
|
t.Error("result = ", result, " should be [1 7 14 14 14 15 16 40]")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.RemoveIndex(3)
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[1 7 14 14 15 16 40]" {
|
||||||
|
t.Error("result is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.RemoveIndex(-1)
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[1 7 14 14 15 16]" {
|
||||||
|
t.Error("result is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.RemoveIndex(0)
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[7 14 14 15 16]" {
|
||||||
|
t.Error("result is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Size() != 5 {
|
||||||
|
t.Error("size is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
for tree.Size() != 0 {
|
||||||
|
tree.RemoveIndex(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.root != nil {
|
||||||
|
t.Error("tree roor is not error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexRange(t *testing.T) {
|
func TestIndexRange(t *testing.T) {
|
||||||
tree := New(compare.Int)
|
tree := New(compare.Int)
|
||||||
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
||||||
@@ -82,6 +176,22 @@ func TestIndexRange(t *testing.T) {
|
|||||||
if result != "[40 40 40 40 50] true" {
|
if result != "[40 40 40 40 50] true" {
|
||||||
t.Error(result)
|
t.Error(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// [3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50]
|
||||||
|
result = spew.Sprint(tree.IndexRange(-5, 10)) //
|
||||||
|
if result != "[40 30] true" {
|
||||||
|
t.Error(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = spew.Sprint(tree.IndexRange(10, -5)) //
|
||||||
|
if result != "[30 40] true" {
|
||||||
|
t.Error(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = spew.Sprint(tree.IndexRange(-1, 8)) //
|
||||||
|
if result != "[50 40 40 40 40 30 21 20] true" {
|
||||||
|
t.Error(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAround(t *testing.T) {
|
func TestGetAround(t *testing.T) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package vbtkey
|
package vbtkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/stack/listarraystack"
|
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
@@ -148,7 +148,7 @@ func (iter *Iterator) Prev() (result bool) {
|
|||||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
||||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||||
iter.tstack.Clear()
|
iter.tstack.Clear()
|
||||||
iter.curPushPrevStack(iter.cur) // 把当前cur计算的逆向回朔
|
iter.curPushPrevStack(iter.cur) // 把当前cur计算的回朔
|
||||||
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
|
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
|
||||||
}
|
}
|
||||||
iter.dir = 1
|
iter.dir = 1
|
||||||
|
|||||||
@@ -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)}
|
||||||
}
|
}
|
||||||
@@ -99,9 +105,9 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
|||||||
|
|
||||||
if idx1^idx2 < 0 {
|
if idx1^idx2 < 0 {
|
||||||
if idx1 < 0 {
|
if idx1 < 0 {
|
||||||
idx1 = tree.root.size + idx1 - 1
|
idx1 = tree.root.size + idx1
|
||||||
} else {
|
} else {
|
||||||
idx2 = tree.root.size + idx2 - 1
|
idx2 = tree.root.size + idx2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/474420502/focus/compare"
|
"github.com/Pallinder/go-randomdata"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
"github.com/474420502/focus/compare"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestData() []int {
|
func loadTestData() []int {
|
||||||
@@ -23,6 +25,98 @@ func loadTestData() []int {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLargePushRemove(t *testing.T) {
|
||||||
|
for n := 0; n < 10; n++ {
|
||||||
|
tree := New(compare.Int)
|
||||||
|
var results []int
|
||||||
|
for i := 0; i < 50000; i++ {
|
||||||
|
v := randomdata.Number(0, 100000000)
|
||||||
|
tree.Put(v, v)
|
||||||
|
results = append(results, v)
|
||||||
|
}
|
||||||
|
if tree.Size() != 50000 {
|
||||||
|
t.Error("Szie error")
|
||||||
|
}
|
||||||
|
for i := 0; i < 49990; i++ {
|
||||||
|
tree.Remove(results[i])
|
||||||
|
}
|
||||||
|
results = results[49990:]
|
||||||
|
if tree.Size() != 10 {
|
||||||
|
t.Error("Szie error")
|
||||||
|
}
|
||||||
|
sort.Slice(results, func(i, j int) bool {
|
||||||
|
if results[i] < results[j] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
if spew.Sprint(results) != spew.Sprint(tree.Values()) {
|
||||||
|
t.Error("tree is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
v1 := results[i]
|
||||||
|
v2, ok := tree.Index(i)
|
||||||
|
if !ok {
|
||||||
|
t.Error("not ok")
|
||||||
|
}
|
||||||
|
if v1 != v2 {
|
||||||
|
t.Error("v1(", v1, ") != v2(", v2, ")??")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.Clear()
|
||||||
|
if tree.String() != "AVLTree\nnil" {
|
||||||
|
t.Error("tree String is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveIndex(t *testing.T) {
|
||||||
|
tree := New(compare.Int)
|
||||||
|
l := []int{7, 14, 14, 14, 16, 1, 40, 15}
|
||||||
|
for _, v := range l {
|
||||||
|
tree.Put(v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// [1 7 14 14 14 15 16 40]
|
||||||
|
var result string
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[1 7 14 14 14 15 16 40]" {
|
||||||
|
t.Error("result = ", result, " should be [1 7 14 14 14 15 16 40]")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.RemoveIndex(3)
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[1 7 14 14 15 16 40]" {
|
||||||
|
t.Error("result is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.RemoveIndex(-1)
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[1 7 14 14 15 16]" {
|
||||||
|
t.Error("result is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.RemoveIndex(0)
|
||||||
|
result = spew.Sprint(tree.Values())
|
||||||
|
if result != "[7 14 14 15 16]" {
|
||||||
|
t.Error("result is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.Size() != 5 {
|
||||||
|
t.Error("size is error")
|
||||||
|
}
|
||||||
|
|
||||||
|
for tree.Size() != 0 {
|
||||||
|
tree.RemoveIndex(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.root != nil {
|
||||||
|
t.Error("tree roor is not error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexRange(t *testing.T) {
|
func TestIndexRange(t *testing.T) {
|
||||||
tree := New(compare.Int)
|
tree := New(compare.Int)
|
||||||
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
||||||
@@ -82,6 +176,23 @@ func TestIndexRange(t *testing.T) {
|
|||||||
if result != "[40 40 40 40 50] true" {
|
if result != "[40 40 40 40 50] true" {
|
||||||
t.Error(result)
|
t.Error(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// [3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50]
|
||||||
|
result = spew.Sprint(tree.IndexRange(-5, 10)) //
|
||||||
|
if result != "[40 30] true" {
|
||||||
|
t.Error(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = spew.Sprint(tree.IndexRange(10, -5)) //
|
||||||
|
if result != "[30 40] true" {
|
||||||
|
t.Error(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = spew.Sprint(tree.IndexRange(-1, 8)) //
|
||||||
|
if result != "[50 40 40 40 40 30 21 20] true" {
|
||||||
|
t.Error(result)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAround(t *testing.T) {
|
func TestGetAround(t *testing.T) {
|
||||||
@@ -265,8 +376,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
|
||||||
|
|||||||
Reference in New Issue
Block a user