完成ArrayList shrink与list接口检测功能
This commit is contained in:
@@ -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]) {
|
||||
|
||||
@@ -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])
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user