修改ArrayList的一些bug
This commit is contained in:
parent
d8142b0685
commit
ad3cec7de0
|
@ -70,10 +70,11 @@ func (l *ArrayList) growth() {
|
||||||
log.Panic("list size is over listMaxLimit", listMaxLimit)
|
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)
|
temp := make([]interface{}, nSize, nSize)
|
||||||
|
|
||||||
ghidx := l.size / 2
|
ghidx := lcap / 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
|
||||||
|
@ -98,7 +99,7 @@ func (l *ArrayList) PushFront(values ...interface{}) {
|
||||||
|
|
||||||
func (l *ArrayList) PushBack(values ...interface{}) {
|
func (l *ArrayList) PushBack(values ...interface{}) {
|
||||||
psize := uint(len(values))
|
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()
|
l.growth()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
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"
|
||||||
|
@ -8,6 +12,7 @@ import (
|
||||||
|
|
||||||
func TestPush(t *testing.T) {
|
func TestPush(t *testing.T) {
|
||||||
l := New()
|
l := New()
|
||||||
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
l.PushFront(1)
|
l.PushFront(1)
|
||||||
}
|
}
|
||||||
|
@ -24,6 +29,7 @@ func TestPush(t *testing.T) {
|
||||||
if result != "[1 1 2 2]" {
|
if result != "[1 1 2 2]" {
|
||||||
t.Error(result)
|
t.Error(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGrowth(t *testing.T) {
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user