修改ArrayList的一些bug

This commit is contained in:
eson 2019-07-05 01:10:23 +08:00
parent d8142b0685
commit ad3cec7de0
2 changed files with 32 additions and 3 deletions

View File

@ -70,10 +70,11 @@ func (l *ArrayList) growth() {
log.Panic("list size is over listMaxLimit", listMaxLimit)
}
nSize := l.size << 1
lcap := uint(len(l.data))
nSize := lcap << 1
temp := make([]interface{}, nSize, nSize)
ghidx := l.size / 2
ghidx := lcap / 2
gtidx := ghidx + l.size + 1
copy(temp[ghidx+1:], l.data[l.hidx+1:l.tidx])
l.data = temp
@ -98,7 +99,7 @@ func (l *ArrayList) PushFront(values ...interface{}) {
func (l *ArrayList) PushBack(values ...interface{}) {
psize := uint(len(values))
for l.tidx+psize > uint(len(l.data)) {
for l.tidx+psize > uint(len(l.data)) { // [0 1 2 3 4 5 6]
l.growth()
}

View File

@ -1,6 +1,10 @@
package arraylist
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"testing"
"github.com/davecgh/go-spew/spew"
@ -8,6 +12,7 @@ import (
func TestPush(t *testing.T) {
l := New()
for i := 0; i < 2; i++ {
l.PushFront(1)
}
@ -24,6 +29,7 @@ func TestPush(t *testing.T) {
if result != "[1 1 2 2]" {
t.Error(result)
}
}
func TestGrowth(t *testing.T) {
@ -141,3 +147,25 @@ func TestRemove(t *testing.T) {
}
}
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])
}
}