2019-05-09 10:29:09 +00:00
|
|
|
package arraylist
|
|
|
|
|
2019-07-24 18:30:55 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/474420502/focus/list"
|
2019-07-25 09:05:29 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-07-24 18:30:55 +00:00
|
|
|
)
|
2019-05-17 11:54:55 +00:00
|
|
|
|
2019-05-09 10:29:09 +00:00
|
|
|
type ArrayList struct {
|
2019-06-04 10:27:43 +00:00
|
|
|
data []interface{}
|
|
|
|
hidx uint // [ nil(hdix) 1 nil(tidx) ]
|
|
|
|
tidx uint
|
|
|
|
size uint
|
|
|
|
|
|
|
|
growthSize uint
|
|
|
|
shrinkSize uint
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-07-24 18:30:55 +00:00
|
|
|
func assertImplementation() {
|
|
|
|
var _ list.IList = (*ArrayList)(nil)
|
2019-07-25 03:40:50 +00:00
|
|
|
var _ list.ILinkedList = (*ArrayList)(nil)
|
2019-07-24 18:30:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 11:54:55 +00:00
|
|
|
const (
|
2019-06-04 10:27:43 +00:00
|
|
|
listMaxLimit = uint(1) << 63
|
|
|
|
listMinLimit = uint(8)
|
2019-05-22 09:52:50 +00:00
|
|
|
initCap = uint(8)
|
2019-06-07 20:26:13 +00:00
|
|
|
//growthFactor = float32(2.0) // growth by 100%
|
|
|
|
//shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
|
2019-05-17 11:54:55 +00:00
|
|
|
)
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func New() *ArrayList {
|
|
|
|
l := &ArrayList{}
|
|
|
|
l.data = make([]interface{}, initCap, initCap)
|
|
|
|
l.tidx = initCap / 2
|
|
|
|
l.hidx = l.tidx - 1
|
2019-07-25 09:05:29 +00:00
|
|
|
// l.shrinkSize = listMinLimit
|
2019-05-22 09:52:50 +00:00
|
|
|
return l
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-07-24 18:30:55 +00:00
|
|
|
func (l *ArrayList) Iterator() *Iterator {
|
|
|
|
return &Iterator{al: l, cur: 0, isInit: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *ArrayList) CircularIterator() *CircularIterator {
|
|
|
|
return &CircularIterator{al: l, cur: 0, isInit: false}
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) Clear() {
|
|
|
|
l.data = make([]interface{}, 8, 8)
|
|
|
|
l.tidx = initCap / 2
|
|
|
|
l.hidx = l.tidx - 1
|
|
|
|
l.size = 0
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) Empty() bool {
|
|
|
|
return l.size == 0
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) Size() uint {
|
|
|
|
return l.size
|
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) shrink() {
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-06-04 10:27:43 +00:00
|
|
|
if l.size <= listMinLimit {
|
2019-06-05 18:17:35 +00:00
|
|
|
return
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-06-04 10:27:43 +00:00
|
|
|
if l.size <= l.shrinkSize {
|
2019-07-25 09:05:29 +00:00
|
|
|
lcap := uint(len(l.data))
|
|
|
|
nSize := lcap - lcap>>2
|
2019-06-04 10:27:43 +00:00
|
|
|
temp := make([]interface{}, nSize, nSize)
|
2019-05-22 09:52:50 +00:00
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
ghidx := l.size >> 2
|
2019-05-22 09:52:50 +00:00
|
|
|
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
|
2019-07-25 09:05:29 +00:00
|
|
|
|
|
|
|
// l.shrinkSize = l.shrinkSize - lcap>>2
|
|
|
|
l.shrinkSize = l.size - l.size>>2
|
2019-06-04 10:27:43 +00:00
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
|
2019-06-04 10:27:43 +00:00
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
|
2019-06-04 10:27:43 +00:00
|
|
|
// 后续需要优化 growth 策略
|
|
|
|
func (l *ArrayList) growth() {
|
|
|
|
|
|
|
|
if l.size >= listMaxLimit {
|
|
|
|
log.Panic("list size is over listMaxLimit", listMaxLimit)
|
2019-05-22 09:52:50 +00:00
|
|
|
}
|
2019-06-04 10:27:43 +00:00
|
|
|
|
2019-07-04 17:10:23 +00:00
|
|
|
lcap := uint(len(l.data))
|
|
|
|
nSize := lcap << 1
|
2019-06-04 10:27:43 +00:00
|
|
|
temp := make([]interface{}, nSize, nSize)
|
|
|
|
|
2019-07-04 17:10:23 +00:00
|
|
|
ghidx := lcap / 2
|
2019-06-04 10:27:43 +00:00
|
|
|
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
|
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
l.shrinkSize = l.size - l.size>>2
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-07-24 18:30:55 +00:00
|
|
|
func (l *ArrayList) Push(value interface{}) {
|
|
|
|
for l.tidx+1 > uint(len(l.data)) {
|
|
|
|
l.growth()
|
|
|
|
}
|
|
|
|
l.data[l.tidx] = value
|
|
|
|
l.tidx++
|
|
|
|
l.size += 1
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) PushFront(values ...interface{}) {
|
|
|
|
psize := uint(len(values))
|
2019-06-04 10:27:43 +00:00
|
|
|
for l.hidx+1-psize > listMaxLimit {
|
|
|
|
l.growth()
|
2019-05-22 09:52:50 +00:00
|
|
|
// panic("growth -1")
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
for _, v := range values {
|
|
|
|
l.data[l.hidx] = v
|
|
|
|
l.hidx--
|
|
|
|
}
|
|
|
|
l.size += psize
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) PushBack(values ...interface{}) {
|
|
|
|
psize := uint(len(values))
|
2019-07-04 17:10:23 +00:00
|
|
|
for l.tidx+psize > uint(len(l.data)) { // [0 1 2 3 4 5 6]
|
2019-06-04 10:27:43 +00:00
|
|
|
l.growth()
|
2019-05-22 09:52:50 +00:00
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
for _, v := range values {
|
|
|
|
l.data[l.tidx] = v
|
|
|
|
l.tidx++
|
|
|
|
}
|
|
|
|
l.size += psize
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) PopFront() (result interface{}, found bool) {
|
|
|
|
if l.size != 0 {
|
|
|
|
l.size--
|
|
|
|
l.hidx++
|
2019-07-25 09:05:29 +00:00
|
|
|
l.shrink()
|
2019-05-22 09:52:50 +00:00
|
|
|
return l.data[l.hidx], true
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
return nil, false
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func (l *ArrayList) PopBack() (result interface{}, found bool) {
|
|
|
|
if l.size != 0 {
|
|
|
|
l.size--
|
|
|
|
l.tidx--
|
2019-07-25 09:05:29 +00:00
|
|
|
l.shrink()
|
2019-05-22 09:52:50 +00:00
|
|
|
return l.data[l.tidx], true
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
return nil, false
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
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
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
return nil, false
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
func (l *ArrayList) Remove(idx int) (result interface{}, isfound bool) {
|
|
|
|
|
|
|
|
if idx < 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
var uidx = (uint)(idx)
|
|
|
|
if uidx >= l.size {
|
2019-05-22 09:52:50 +00:00
|
|
|
return nil, false
|
2019-05-09 10:29:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
offset := l.hidx + 1 + uidx
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
isfound = true
|
|
|
|
result = l.data[offset]
|
2019-06-05 18:17:35 +00:00
|
|
|
// l.data[offset] = nil // cleanup reference
|
|
|
|
|
|
|
|
if l.size-l.tidx > l.hidx {
|
|
|
|
copy(l.data[offset:], l.data[offset+1:l.tidx]) // shift to the left by one (slow operation, need ways to optimize this)
|
|
|
|
l.tidx--
|
|
|
|
} else {
|
|
|
|
copy(l.data[l.hidx+2:], l.data[l.hidx+1:offset])
|
|
|
|
l.hidx++
|
|
|
|
}
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
l.size--
|
|
|
|
l.shrink()
|
2019-05-09 10:29:09 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
return
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:26:13 +00:00
|
|
|
func (l *ArrayList) Contains(values ...interface{}) bool {
|
|
|
|
|
|
|
|
for _, searchValue := range values {
|
|
|
|
found := false
|
|
|
|
for _, element := range l.data[l.hidx+1 : l.tidx] {
|
|
|
|
if element == searchValue {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-05-22 09:52:50 +00:00
|
|
|
|
|
|
|
func (l *ArrayList) Values() []interface{} {
|
|
|
|
newElements := make([]interface{}, l.size, l.size)
|
|
|
|
copy(newElements, l.data[l.hidx+1:l.tidx])
|
|
|
|
return newElements
|
2019-05-17 11:54:55 +00:00
|
|
|
}
|
2019-07-25 03:40:50 +00:00
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
func (l *ArrayList) String() string {
|
|
|
|
return spew.Sprint(l.Values())
|
|
|
|
}
|
|
|
|
|
2019-07-25 03:40:50 +00:00
|
|
|
func (l *ArrayList) Traversal(every func(interface{}) bool) {
|
|
|
|
for i := uint(0); i < l.size; i++ {
|
|
|
|
if !every(l.data[i+l.hidx+1]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|