Compare commits
14 Commits
v0.6.0
...
feature/to
| Author | SHA1 | Date | |
|---|---|---|---|
| 044a9342f0 | |||
| f70017374b | |||
| 76cea0eb35 | |||
| c2dd82b426 | |||
| 7fc9a84fbf | |||
|
|
71d9d5026f | ||
|
|
f6cab7cc19 | ||
|
|
b6fe42de8f | ||
|
|
f63759645e | ||
|
|
c0ad187222 | ||
|
|
ade0b78c6f | ||
|
|
211ab2661c | ||
|
|
411281d865 | ||
| 5ef163bca7 |
@@ -1,8 +1,9 @@
|
||||
package pqueue
|
||||
|
||||
import (
|
||||
"github.com/474420502/focus/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
@@ -100,9 +101,9 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
|
||||
|
||||
if idx1^idx2 < 0 {
|
||||
if idx1 < 0 {
|
||||
idx1 = tree.root.size + idx1 - 1
|
||||
idx1 = tree.root.size + idx1
|
||||
} else {
|
||||
idx2 = tree.root.size + idx2 - 1
|
||||
idx2 = tree.root.size + idx2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package pqueuekey
|
||||
|
||||
import (
|
||||
"github.com/474420502/focus/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
@@ -101,9 +102,9 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
|
||||
|
||||
if idx1^idx2 < 0 {
|
||||
if idx1 < 0 {
|
||||
idx1 = tree.root.size + idx1 - 1
|
||||
idx1 = tree.root.size + idx1
|
||||
} else {
|
||||
idx2 = tree.root.size + idx2 - 1
|
||||
idx2 = tree.root.size + idx2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -110,40 +110,28 @@ func (h *Heap) Pop() (interface{}, bool) {
|
||||
|
||||
downvalue := h.elements[h.size]
|
||||
var cidx, c1, c2 int
|
||||
var cvalue1, cvalue2, cvalue interface{}
|
||||
// down
|
||||
for {
|
||||
cidx = curidx << 1
|
||||
|
||||
c1 = cidx + 1
|
||||
c2 = cidx + 2
|
||||
|
||||
if c2 < h.size {
|
||||
cvalue2 = h.elements[c2]
|
||||
|
||||
c1 = cidx + 1
|
||||
cvalue1 = h.elements[c1]
|
||||
|
||||
if h.Compare(cvalue1, cvalue2) >= 0 {
|
||||
if h.Compare(h.elements[c1], h.elements[c2]) >= 0 {
|
||||
cidx = c1
|
||||
cvalue = cvalue1
|
||||
} else {
|
||||
cidx = c2
|
||||
cvalue = cvalue2
|
||||
}
|
||||
} else {
|
||||
|
||||
c1 = cidx + 1
|
||||
if c1 < h.size {
|
||||
cvalue1 = h.elements[c1]
|
||||
cidx = c1
|
||||
cvalue = cvalue1
|
||||
} else {
|
||||
cidx = c1
|
||||
if c1 >= h.size {
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if h.Compare(cvalue, downvalue) > 0 {
|
||||
h.elements[curidx] = cvalue
|
||||
if h.Compare(h.elements[cidx], downvalue) > 0 {
|
||||
h.elements[curidx] = h.elements[cidx]
|
||||
curidx = cidx
|
||||
} else {
|
||||
break
|
||||
|
||||
@@ -1,11 +1,64 @@
|
||||
package heap
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"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) {
|
||||
h := New(compare.Int)
|
||||
l := []int{9, 5, 15, 2, 3}
|
||||
@@ -31,8 +84,44 @@ func TestHeapPushTopPop(t *testing.T) {
|
||||
if h.Size() != 0 {
|
||||
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 {
|
||||
// c1 := k1.(int)
|
||||
// c2 := k2.(int)
|
||||
|
||||
@@ -104,9 +104,9 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
||||
|
||||
if idx1^idx2 < 0 {
|
||||
if idx1 < 0 {
|
||||
idx1 = tree.root.size + idx1 - 1
|
||||
idx1 = tree.root.size + idx1
|
||||
} else {
|
||||
idx2 = tree.root.size + idx2 - 1
|
||||
idx2 = tree.root.size + idx2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,13 @@ import (
|
||||
"encoding/gob"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
)
|
||||
|
||||
func loadTestData() []int {
|
||||
@@ -23,6 +25,98 @@ func loadTestData() []int {
|
||||
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) {
|
||||
tree := New(compare.Int)
|
||||
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" {
|
||||
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) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package vbtkey
|
||||
|
||||
import (
|
||||
"github.com/474420502/focus/stack/listarraystack"
|
||||
lastack "github.com/474420502/focus/stack/listarraystack"
|
||||
)
|
||||
|
||||
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 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
iter.tstack.Clear()
|
||||
iter.curPushPrevStack(iter.cur) // 把当前cur计算的逆向回朔
|
||||
iter.curPushPrevStack(iter.cur) // 把当前cur计算的回朔
|
||||
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
|
||||
}
|
||||
iter.dir = 1
|
||||
|
||||
@@ -105,9 +105,9 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
||||
|
||||
if idx1^idx2 < 0 {
|
||||
if idx1 < 0 {
|
||||
idx1 = tree.root.size + idx1 - 1
|
||||
idx1 = tree.root.size + idx1
|
||||
} else {
|
||||
idx2 = tree.root.size + idx2 - 1
|
||||
idx2 = tree.root.size + idx2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
"encoding/gob"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
@@ -23,6 +25,98 @@ func loadTestData() []int {
|
||||
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) {
|
||||
tree := New(compare.Int)
|
||||
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" {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user