保存数据
This commit is contained in:
107
list/arraylist/arraylist.go
Normal file
107
list/arraylist/arraylist.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package list
|
||||
package arraylist
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -73,7 +73,7 @@ func TestRemove(t *testing.T) {
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if n, ok := l.Remove(0); ok {
|
||||
if n.Value() != 4-i {
|
||||
if n != 4-i {
|
||||
t.Error(n)
|
||||
}
|
||||
} else {
|
||||
108
list/list.go
108
list/list.go
@@ -1,108 +0,0 @@
|
||||
package list
|
||||
|
||||
type Node struct {
|
||||
next *Node
|
||||
value interface{}
|
||||
}
|
||||
|
||||
func (node *Node) Value() interface{} {
|
||||
return node.value
|
||||
}
|
||||
|
||||
type List struct {
|
||||
head *Node
|
||||
size uint
|
||||
}
|
||||
|
||||
func New() *List {
|
||||
return &List{}
|
||||
}
|
||||
|
||||
func (l *List) Size() uint {
|
||||
return l.size
|
||||
}
|
||||
|
||||
func (l *List) Push(v interface{}) {
|
||||
l.size++
|
||||
if l.head == nil {
|
||||
l.head = &Node{value: v}
|
||||
return
|
||||
}
|
||||
l.head = &Node{value: v, next: l.head}
|
||||
}
|
||||
|
||||
func (l *List) PushNode(n *Node) {
|
||||
l.size++
|
||||
if l.head == nil {
|
||||
l.head = n
|
||||
return
|
||||
}
|
||||
|
||||
n.next = l.head
|
||||
l.head = n
|
||||
}
|
||||
|
||||
func (l *List) Pop() (result interface{}, found bool) {
|
||||
if n, ok := l.PopNode(); ok {
|
||||
return n.value, ok
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (l *List) PopNode() (result *Node, found bool) {
|
||||
if l.head == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
result = l.head
|
||||
found = true
|
||||
l.head = result.next
|
||||
result.next = nil
|
||||
l.size--
|
||||
return
|
||||
}
|
||||
|
||||
func (l *List) Remove(idx uint) (result *Node, found bool) {
|
||||
if l.size == 0 {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if idx == 0 {
|
||||
result = l.head
|
||||
found = true
|
||||
l.head = result.next
|
||||
result.next = nil
|
||||
l.size--
|
||||
return
|
||||
}
|
||||
|
||||
for cur := l.head; cur.next != nil; cur = cur.next {
|
||||
if idx == 1 {
|
||||
l.size--
|
||||
result = cur.next
|
||||
found = true
|
||||
cur.next = result.next
|
||||
result.next = nil
|
||||
return
|
||||
}
|
||||
idx--
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (l *List) Values() (result []interface{}) {
|
||||
l.Traversal(func(cur *Node) bool {
|
||||
result = append(result, cur.value)
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (l *List) Traversal(every func(*Node) bool) {
|
||||
for cur := l.head; cur != nil; cur = cur.next {
|
||||
if !every(cur) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user