保存数据

This commit is contained in:
2019-05-09 18:29:09 +08:00
parent 5b7311ec94
commit 80f32247f6
7 changed files with 111 additions and 624 deletions

107
list/arraylist/arraylist.go Normal file
View File

@@ -0,0 +1,107 @@
package arraylist
type ArrayList struct {
data []interface{}
headIndex uint
nextIndex uint
size uint
reserveHead uint
reserveLimit uint
growSize uint
shrinkSize uint
}
func New() *ArrayList {
al := &ArrayList{}
al.reserveHead = 2
al.reserveLimit = 256
al.headIndex = al.reserveHead
al.nextIndex = al.headIndex
al.data = make([]interface{}, 8, 8)
return al
}
func (l *ArrayList) Size() uint {
return l.size
}
func (l *ArrayList) grow() {
newsize := uint(len(l.data)) << 1
l.reserveHead = al.reserveHead << 1
l.headIndex = al.reserveHead
l.nextIndex = al.headIndex
l.data = make([]interface{}, 8, 8)
}
// Add add value to the tail of list
func (l *ArrayList) Add(v interface{}) {
if l.nextIndex >= uint(len(l.data)) {
l.grow()
}
l.size++
l.data[l.nextIndex] = v
// grow
}
// Push push is equal to add
func (l *ArrayList) Push(v interface{}) {
l.data = append(l.data, v)
}
func (l *ArrayList) Set(idx uint, value interface{}) {
l.data[idx] = value
}
func (l *ArrayList) Get(idx uint) (result interface{}, isfound bool) {
if idx >= l.Size() {
return nil, false
}
return l.data[idx], true
}
func (l *ArrayList) Pop() (result interface{}, found bool) {
if l.Size() == 0 {
return nil, false
}
rindex := len(l.data) - 1
result = l.data[rindex]
l.data = l.data[0:rindex]
return result, true
}
func (l *ArrayList) Remove(idx uint) (rvalue interface{}, isfound bool) {
if idx >= l.Size() {
return nil, false
}
rvalue = l.data[idx]
l.data = append(l.data[0:idx], l.data[idx+1:])
return rvalue, true
}
func (l *ArrayList) Values() (result []interface{}) {
values := make([]interface{}, l.Size(), l.Size())
copy(values, l.data)
return values
}
func (l *ArrayList) Traversal(every func(index int, cur interface{}) bool) {
for i, cur := range l.data {
if !every(i, cur) {
return
}
}
}

View File

@@ -0,0 +1,87 @@
package arraylist
import (
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestPush(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.Push(i)
}
var result string
result = spew.Sprint(l.Values())
if result != "[4 3 2 1 0]" {
t.Error(result)
}
l.Push(0)
result = spew.Sprint(l.Values())
if result != "[0 4 3 2 1 0]" {
t.Error(result)
}
}
func TestPop(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.Push(i)
}
if v, ok := l.Pop(); ok {
if v != 4 {
t.Error(v)
}
} else {
t.Error("Pop should ok, but is not ok")
}
var result string
result = spew.Sprint(l.Values())
if result != "[3 2 1 0]" {
t.Error(result)
}
for i := 3; l.Size() != 0; i-- {
if v, ok := l.Pop(); ok {
if v != i {
t.Error(i, v, "is not equals")
}
} else {
t.Error("Pop should ok, but is not ok", i)
}
}
l.Push(0)
result = spew.Sprint(l.Values())
if result != "[0]" {
t.Error(result)
}
if l.Size() != 1 {
t.Error("l.Size() == 1, but is error, size = ", l.Size())
}
}
func TestRemove(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.Push(i)
}
for i := 0; i < 5; i++ {
if n, ok := l.Remove(0); ok {
if n != 4-i {
t.Error(n)
}
} else {
t.Error("Pop should ok, but is not ok", i)
}
}
if l.Size() != 0 {
t.Error("l.Size() == 0, but is error, size = ", l.Size())
}
}