test remove
This commit is contained in:
parent
60089c905a
commit
fcf81f45a0
|
@ -3,15 +3,18 @@ package arraylist
|
||||||
import "log"
|
import "log"
|
||||||
|
|
||||||
type ArrayList struct {
|
type ArrayList struct {
|
||||||
data []interface{}
|
data []interface{}
|
||||||
hidx uint
|
hidx uint // [ nil(hdix) 1 nil(tidx) ]
|
||||||
tidx uint
|
tidx uint
|
||||||
size uint
|
size uint
|
||||||
growSizePoint uint
|
|
||||||
|
growthSize uint
|
||||||
|
shrinkSize uint
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
listLimit = uint(1) << 63
|
listMaxLimit = uint(1) << 63
|
||||||
|
listMinLimit = uint(8)
|
||||||
initCap = uint(8)
|
initCap = uint(8)
|
||||||
growthFactor = float32(2.0) // growth by 100%
|
growthFactor = float32(2.0) // growth by 100%
|
||||||
shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
|
shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
|
||||||
|
@ -43,53 +46,47 @@ func (l *ArrayList) Size() uint {
|
||||||
|
|
||||||
func (l *ArrayList) shrink() {
|
func (l *ArrayList) shrink() {
|
||||||
|
|
||||||
|
if l.size <= listMinLimit {
|
||||||
|
log.Panic("list size is over listMaxLimit", listMinLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l.size <= l.shrinkSize {
|
||||||
|
nSize := l.shrinkSize - l.shrinkSize>>1
|
||||||
|
temp := make([]interface{}, nSize, nSize)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 后续需要优化 growth 策略
|
// 后续需要优化 growth 策略
|
||||||
func (l *ArrayList) growth(flag int) {
|
func (l *ArrayList) growth() {
|
||||||
|
|
||||||
if l.size >= listLimit {
|
if l.size >= listMaxLimit {
|
||||||
log.Panic("list size is over limit", listLimit)
|
log.Panic("list size is over listMaxLimit", listMaxLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch flag {
|
nSize := l.size << 1
|
||||||
case -1:
|
temp := make([]interface{}, nSize, nSize)
|
||||||
growthsize := l.size << 1
|
|
||||||
temp := make([]interface{}, growthsize, growthsize)
|
|
||||||
|
|
||||||
ghidx := l.size / 2
|
ghidx := l.size / 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
|
||||||
l.hidx = ghidx
|
l.hidx = ghidx
|
||||||
l.tidx = gtidx
|
l.tidx = gtidx
|
||||||
case 1:
|
|
||||||
growthsize := l.size << 1
|
|
||||||
temp := make([]interface{}, growthsize, growthsize)
|
|
||||||
|
|
||||||
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
|
|
||||||
case 0:
|
|
||||||
growthsize := l.size << 1
|
|
||||||
temp := make([]interface{}, growthsize, growthsize)
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) PushFront(values ...interface{}) {
|
func (l *ArrayList) PushFront(values ...interface{}) {
|
||||||
psize := uint(len(values))
|
psize := uint(len(values))
|
||||||
if l.hidx+1-psize > listLimit {
|
for l.hidx+1-psize > listMaxLimit {
|
||||||
l.growth(-1)
|
l.growth()
|
||||||
// panic("growth -1")
|
// panic("growth -1")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,8 +99,8 @@ 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))
|
||||||
if l.tidx+psize >= uint(len(l.data)) {
|
for l.tidx+psize > uint(len(l.data)) {
|
||||||
l.growth(1)
|
l.growth()
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
|
|
|
@ -34,7 +34,7 @@ func TestGrowth(t *testing.T) {
|
||||||
|
|
||||||
var result string
|
var result string
|
||||||
result = spew.Sprint(l.Values())
|
result = spew.Sprint(l.Values())
|
||||||
if result != "[1 1]" {
|
if result != "[1 1 1 1 1]" {
|
||||||
t.Error(result)
|
t.Error(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func TestGrowth(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
result = spew.Sprint(l.Values())
|
result = spew.Sprint(l.Values())
|
||||||
if result != "[1 1]" {
|
if result != "[1 1 1 1 1 1 1]" {
|
||||||
t.Error(result)
|
t.Error(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user