6 Commits

Author SHA1 Message Date
e5f53aec78 Merge branch 'release/0.6.0' 2019-07-29 02:22:18 +08:00
3ade1566c2 添加了树的接口 2019-07-29 02:18:22 +08:00
huangsimin
4a58bd283e Merge tag 'v0.5.0' into develop
list stack interface 相对完善
2019-07-25 17:07:57 +08:00
huangsimin
6110d7c487 Merge branch 'release/0.5.0' 2019-07-25 17:07:42 +08:00
huangsimin
6516b424ee 完成ArrayList shrink与list接口检测功能 2019-07-25 17:05:29 +08:00
huangsimin
fe15076461 Merge tag 'v0.4.0' into develop
stack IStack添加
2019-07-25 14:31:47 +08:00
17 changed files with 216 additions and 74 deletions

View File

@@ -4,6 +4,7 @@ import (
"log"
"github.com/474420502/focus/list"
"github.com/davecgh/go-spew/spew"
)
type ArrayList struct {
@@ -34,6 +35,7 @@ func New() *ArrayList {
l.data = make([]interface{}, initCap, initCap)
l.tidx = initCap / 2
l.hidx = l.tidx - 1
// l.shrinkSize = listMinLimit
return l
}
@@ -67,15 +69,19 @@ func (l *ArrayList) shrink() {
}
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)
ghidx := l.size / 2
ghidx := l.size >> 2
gtidx := ghidx + l.size + 1
copy(temp[ghidx+1:], l.data[l.hidx+1:l.tidx])
l.data = temp
l.hidx = ghidx
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.tidx = gtidx
l.shrinkSize = l.size - l.size>>2
}
func (l *ArrayList) Push(value interface{}) {
@@ -140,6 +147,7 @@ func (l *ArrayList) PopFront() (result interface{}, found bool) {
if l.size != 0 {
l.size--
l.hidx++
l.shrink()
return l.data[l.hidx], true
}
return nil, false
@@ -149,25 +157,32 @@ func (l *ArrayList) PopBack() (result interface{}, found bool) {
if l.size != 0 {
l.size--
l.tidx--
l.shrink()
return l.data[l.tidx], true
}
return nil, false
}
func (l *ArrayList) Index(idx uint) (interface{}, bool) {
if idx < l.size {
return l.data[idx+l.hidx+1], true
func (l *ArrayList) Index(idx int) (interface{}, bool) {
var uidx uint = (uint)(idx)
if uidx < l.size {
return l.data[uidx+l.hidx+1], true
}
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
}
offset := l.hidx + 1 + idx
var uidx = (uint)(idx)
if uidx >= l.size {
return nil, false
}
offset := l.hidx + 1 + uidx
isfound = true
result = l.data[offset]
@@ -210,6 +225,10 @@ func (l *ArrayList) Values() []interface{} {
return newElements
}
func (l *ArrayList) String() string {
return spew.Sprint(l.Values())
}
func (l *ArrayList) Traversal(every func(interface{}) bool) {
for i := uint(0); i < l.size; i++ {
if !every(l.data[i+l.hidx+1]) {

View File

@@ -1,10 +1,6 @@
package arraylist
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"testing"
"github.com/davecgh/go-spew/spew"
@@ -178,8 +174,8 @@ func TestRemove(t *testing.T) {
var result string
for _, selval := range []uint{4, 3} {
last, _ := l.Index(selval)
if v, isfound := l.Remove(selval); isfound {
last, _ := l.Index((int)(selval))
if v, isfound := l.Remove((int)(selval)); isfound {
if v != last {
t.Error(v, " != ", last)
}
@@ -244,24 +240,56 @@ func TestTraversal(t *testing.T) {
}
}
func loadTestData() []int {
data, err := ioutil.ReadFile("../../l.log")
if err != nil {
log.Println(err)
func TestRemain(t *testing.T) {
l := New()
for i := 0; i < 10; i++ {
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) {
l := loadTestData()
b.N = len(l)
if l.String() != "[0 1 2 3 4 5 6 7 8 9]" {
t.Error(l.String())
}
arr := New()
for i := 10; i < 100; i++ {
l.Push(i)
}
for i := 0; i < b.N; i++ {
arr.PushBack(l[i])
for !l.Empty() {
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])
// }
// }

View File

@@ -7,7 +7,7 @@ type Iterator struct {
}
func (iter *Iterator) Value() interface{} {
v, _ := iter.al.Index(iter.cur)
v, _ := iter.al.Index((int)(iter.cur))
return v
}
@@ -64,7 +64,7 @@ type CircularIterator struct {
}
func (iter *CircularIterator) Value() interface{} {
v, _ := iter.al.Index(iter.cur)
v, _ := iter.al.Index((int)(iter.cur))
return v
}

View File

@@ -191,28 +191,34 @@ func (l *LinkedList) FindMany(every func(idx uint, value interface{}) int) (resu
return result, isfound
}
func (l *LinkedList) Index(idx uint) (interface{}, bool) {
if idx >= l.size {
func (l *LinkedList) Index(idx int) (interface{}, bool) {
if idx < 0 {
return nil, false
}
var uidx = (uint)(idx)
if uidx >= l.size || idx < 0 {
return nil, false
}
if idx > l.size/2 {
idx = l.size - 1 - idx
if uidx > l.size/2 {
uidx = l.size - 1 - uidx
// 尾部
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
if idx == 0 {
if uidx == 0 {
return cur.value, true
}
idx--
uidx--
}
} else {
// 头部
for cur := l.head.next; cur != l.tail; cur = cur.next {
if idx == 0 {
if uidx == 0 {
return cur.value, true
}
idx--
uidx--
}
}
@@ -371,33 +377,39 @@ func remove(cur *Node) {
cur.next = nil
}
func (l *LinkedList) Remove(idx uint) (interface{}, bool) {
if l.size <= idx {
func (l *LinkedList) Remove(idx int) (interface{}, bool) {
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)
return nil, false
}
l.size--
if idx > l.size/2 {
idx = l.size - idx // l.size - 1 - idx, 先减size
if uidx > l.size/2 {
uidx = l.size - uidx // l.size - 1 - idx, 先减size
// 尾部
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
if idx == 0 {
if uidx == 0 {
remove(cur)
return cur.value, true
}
idx--
uidx--
}
} else {
// 头部
for cur := l.head.next; cur != l.tail; cur = cur.next {
if idx == 0 {
if uidx == 0 {
remove(cur)
return cur.value, true
}
idx--
uidx--
}
}

View File

@@ -4,10 +4,11 @@ package list
type IList interface {
Push(value interface{})
Contains(values ...interface{}) bool
Index(idx uint) (interface{}, bool)
Remove(idx uint) (result interface{}, isfound bool)
Index(idx int) (interface{}, bool)
Remove(idx int) (result interface{}, isfound bool)
Values() []interface{}
Traversal(every func(interface{}) bool)
String() string
Clear()
Empty() bool

View File

@@ -4,6 +4,7 @@ import (
"strings"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/list"
"github.com/davecgh/go-spew/spew"
)
@@ -20,7 +21,7 @@ type PriorityList struct {
// 优先队列的Index 正负都有作用要定义一种新的接口类型
func assertImplementation() {
// var _ list.IList = (*PriorityList)(nil)
var _ list.IList = (*PriorityList)(nil)
}
func New(Compare compare.Compare) *PriorityList {
@@ -173,11 +174,19 @@ func (pl *PriorityList) GetNode(idx int) (*Node, bool) {
func (pl *PriorityList) RemoveWithIndex(idx int) {
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
next := node.next

View File

@@ -82,7 +82,7 @@ func (lhmap *LinkedHashmap) Remove(key interface{}) (interface{}, bool) {
func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) {
if 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]
delete(lhmap.hmap, key)
return result, true

View File

@@ -1,8 +1,10 @@
package avl
import (
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree"
)
type Node struct {
@@ -31,6 +33,10 @@ type Tree struct {
iter *Iterator
}
func assertImplementation() {
var _ tree.IBSTree = (*Tree)(nil)
}
func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}

View File

@@ -1,8 +1,10 @@
package avldup
import (
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree"
)
type Node struct {
@@ -32,6 +34,10 @@ type Tree struct {
iter *Iterator
}
func assertImplementation() {
var _ tree.IBSTree = (*Tree)(nil)
}
func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}

View File

@@ -1,8 +1,10 @@
package avlkey
import (
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree"
)
type Node struct {
@@ -35,6 +37,10 @@ func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}
func assertImplementation() {
var _ tree.IBSTreeKey = (*Tree)(nil)
}
func (tree *Tree) String() string {
if tree.size == 0 {
return ""

View File

@@ -1,8 +1,10 @@
package avlkeydup
import (
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree"
)
type Node struct {
@@ -31,6 +33,10 @@ type Tree struct {
iter *Iterator
}
func assertImplementation() {
var _ tree.IBSTreeKey = (*Tree)(nil)
}
func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}

View File

@@ -54,7 +54,7 @@ func (h *Heap) Top() (interface{}, bool) {
return nil, false
}
func (h *Heap) Push(v interface{}) {
func (h *Heap) Put(v interface{}) {
if v == nil {
return
}

View File

@@ -11,7 +11,7 @@ func TestHeapPushTopPop(t *testing.T) {
l := []int{9, 5, 15, 2, 3}
ol := []int{15, 9, 5, 3, 2}
for _, v := range l {
h.Push(v)
h.Put(v)
}
for _, tv := range ol {

27
tree/tree.go Normal file
View 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{})
}

View File

@@ -1,8 +1,10 @@
package vbt
import (
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree"
)
type Node struct {
@@ -31,6 +33,10 @@ type Tree struct {
iter *Iterator
}
func assertImplementation() {
var _ tree.IBSTree = (*Tree)(nil)
}
func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}
@@ -231,6 +237,11 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
return nil, false
}
func (tree *Tree) Clear() {
tree.root = nil
tree.iter = NewIteratorWithCap(nil, 16)
}
// Values 返回先序遍历的值
func (tree *Tree) Values() []interface{} {
mszie := 0

View File

@@ -1,8 +1,10 @@
package vbtkey
import (
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree"
)
type Node struct {
@@ -32,6 +34,10 @@ type Tree struct {
iter *Iterator
}
func assertImplementation() {
var _ tree.IBSTreeKey = (*Tree)(nil)
}
func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}
@@ -232,6 +238,11 @@ func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
return nil, false
}
func (tree *Tree) Clear() {
tree.root = nil
tree.iter = NewIteratorWithCap(nil, 16)
}
// Values 返回先序遍历的值
func (tree *Tree) Values() []interface{} {
mszie := 0
@@ -239,7 +250,7 @@ func (tree *Tree) Values() []interface{} {
mszie = tree.root.size
}
result := make([]interface{}, 0, mszie)
tree.Traversal(func(v interface{}) bool {
tree.Traversal(func(k, v interface{}) bool {
result = append(result, v)
return true
}, LDR)
@@ -500,8 +511,8 @@ const (
RLD
)
// Traversal 遍历的方法 默认是LDR 从小到大 comparator 为 l < r
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
func (tree *Tree) Traversal(every func(k, v interface{}) bool, traversalMethod ...interface{}) {
if tree.root == nil {
return
}
@@ -518,7 +529,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if cur == nil {
return true
}
if !every(cur.value) {
if !every(cur.key, cur.value) {
return false
}
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]) {
return false
}
if !every(cur.value) {
if !every(cur.key, cur.value) {
return false
}
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]) {
return false
}
if !every(cur.value) {
if !every(cur.key, cur.value) {
return false
}
return true
@@ -572,7 +583,7 @@ func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...i
if cur == nil {
return true
}
if !every(cur.value) {
if !every(cur.key, cur.value) {
return false
}
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]) {
return false
}
if !every(cur.value) {
if !every(cur.key, cur.value) {
return false
}
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]) {
return false
}
if !every(cur.value) {
if !every(cur.key, cur.value) {
return false
}
return true

View File

@@ -7,9 +7,9 @@ import (
"log"
"testing"
"github.com/474420502/focus/compare"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
)
func loadTestData() []int {
@@ -265,8 +265,8 @@ func TestTravalsal(t *testing.T) {
i := 0
var result []interface{}
tree.Traversal(func(v interface{}) bool {
result = append(result, v)
tree.Traversal(func(k, v interface{}) bool {
result = append(result, k)
i++
if i >= 10 {
return false