TODO: assertImplementation, add interface InsertIf 组合等

This commit is contained in:
eson 2019-07-25 02:30:55 +08:00
parent 67ffc2e6c0
commit 7c4b358bd4
9 changed files with 278 additions and 6 deletions

View File

@ -1,4 +1,4 @@
package structure
package focus
import (
"bytes"

View File

@ -1 +1 @@
package structure
package focus

View File

@ -1,6 +1,10 @@
package arraylist
import "log"
import (
"log"
"github.com/474420502/focus/list"
)
type ArrayList struct {
data []interface{}
@ -12,6 +16,10 @@ type ArrayList struct {
shrinkSize uint
}
func assertImplementation() {
var _ list.IList = (*ArrayList)(nil)
}
const (
listMaxLimit = uint(1) << 63
listMinLimit = uint(8)
@ -28,6 +36,14 @@ func New() *ArrayList {
return l
}
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}
}
func (l *ArrayList) Clear() {
l.data = make([]interface{}, 8, 8)
l.tidx = initCap / 2
@ -83,6 +99,15 @@ func (l *ArrayList) growth() {
}
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
}
func (l *ArrayList) PushFront(values ...interface{}) {
psize := uint(len(values))
for l.hidx+1-psize > listMaxLimit {

View File

@ -10,6 +10,67 @@ import (
"github.com/davecgh/go-spew/spew"
)
func TestIterator(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
l.Push(i)
}
iter := l.Iterator()
var result []int
for iter.Next() {
result = append(result, iter.Value().(int))
}
if spew.Sprint(result) != "[0 1 2 3 4]" {
t.Error(result)
}
iter = l.Iterator()
result = nil
for iter.Prev() {
result = append(result, iter.Value().(int))
}
if spew.Sprint(result) != "[4 3 2 1 0]" {
t.Error(result)
}
citer := l.CircularIterator()
result = nil
for i := 0; i < 11; i++ {
if citer.Next() {
result = append(result, citer.Value().(int))
}
}
if len(result) != 11 {
t.Error("len(result) != 11, is ", len(result))
}
if spew.Sprint(result) != "[0 1 2 3 4 0 1 2 3 4 0]" {
t.Error(result)
}
citer = l.CircularIterator()
result = nil
for i := 0; i < 11; i++ {
if citer.Prev() {
result = append(result, citer.Value().(int))
}
}
if len(result) != 11 {
t.Error("len(result) != 11, is ", len(result))
}
if spew.Sprint(result) != "[4 3 2 1 0 4 3 2 1 0 4]" {
t.Error(result)
}
}
func TestPush(t *testing.T) {
l := New()
@ -30,6 +91,12 @@ func TestPush(t *testing.T) {
t.Error(result)
}
l.Push(3)
result = spew.Sprint(l.Values())
if result != "[1 1 2 2 3]" {
t.Error(result)
}
}
func TestGrowth(t *testing.T) {

125
list/array_list/iterator.go Normal file
View File

@ -0,0 +1,125 @@
package arraylist
type Iterator struct {
al *ArrayList
cur uint
isInit bool
}
func (iter *Iterator) Value() interface{} {
v, _ := iter.al.Index(iter.cur)
return v
}
func (iter *Iterator) Prev() bool {
if iter.isInit == false {
if iter.al.size != 0 {
iter.isInit = true
iter.cur = iter.al.size - 1
return true
}
return false
}
if iter.cur <= 0 {
return false
}
iter.cur--
return true
}
func (iter *Iterator) Next() bool {
if iter.isInit == false {
if iter.al.size != 0 {
iter.isInit = true
iter.cur = 0
return true
}
return false
}
if iter.cur >= iter.al.size-1 {
return false
}
iter.cur++
return true
}
func (iter *Iterator) ToHead() {
iter.isInit = true
iter.cur = 0
}
func (iter *Iterator) ToTail() {
iter.isInit = true
iter.cur = iter.al.size - 1
}
type CircularIterator struct {
al *ArrayList
cur uint
isInit bool
}
func (iter *CircularIterator) Value() interface{} {
v, _ := iter.al.Index(iter.cur)
return v
}
func (iter *CircularIterator) Prev() bool {
if iter.isInit == false {
if iter.al.size != 0 {
iter.isInit = true
iter.cur = iter.al.size - 1
return true
}
return false
}
if iter.al.size == 0 {
return false
}
if iter.cur <= 0 {
iter.cur = iter.al.size - 1
} else {
iter.cur--
}
return true
}
func (iter *CircularIterator) Next() bool {
if iter.isInit == false {
if iter.al.size != 0 {
iter.isInit = true
iter.cur = 0
return true
}
return false
}
if iter.al.size == 0 {
return false
}
if iter.cur >= iter.al.size-1 {
iter.cur = 0
} else {
iter.cur++
}
return true
}
func (iter *CircularIterator) ToHead() {
iter.isInit = true
iter.cur = 0
}
func (iter *CircularIterator) ToTail() {
iter.isInit = true
iter.cur = iter.al.size - 1
}

View File

@ -78,10 +78,10 @@ func (iter *CircularIterator) Next() bool {
return true
}
func (iter *CircularIterator) MoveToHead() {
func (iter *CircularIterator) ToHead() {
iter.cur = iter.pl.head
}
func (iter *CircularIterator) MoveToTail() {
func (iter *CircularIterator) ToTail() {
iter.cur = iter.pl.tail
}

View File

@ -57,6 +57,21 @@ func (l *LinkedList) Size() uint {
return l.size
}
func (l *LinkedList) Push(value interface{}) {
var node *Node
l.size++
node = &Node{}
node.value = value
tprev := l.tail.prev
tprev.next = node
node.prev = tprev
node.next = l.tail
l.tail.prev = node
}
func (l *LinkedList) PushFront(values ...interface{}) {
var node *Node

View File

@ -7,6 +7,24 @@ import (
"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 != "[0 1 2 3 4]" {
t.Error(result)
}
l.Push(0)
result = spew.Sprint(l.Values())
if result != "[0 1 2 3 4 0]" {
t.Error(result)
}
}
func TestPushFront(t *testing.T) {
l := New()
for i := 0; i < 5; i++ {
@ -602,7 +620,7 @@ func TestCircularIterator(t *testing.T) {
}
}
iter.MoveToTail()
iter.ToTail()
for i := 0; i != 10; i++ {
iter.Prev()
if iter.Value() != i {

22
list/list.go Normal file
View File

@ -0,0 +1,22 @@
package list
// IList 通用接口
type IList interface {
Push(value interface{})
Contains(values ...interface{}) bool
Index(idx uint) (interface{}, bool)
Remove(idx uint) (result interface{}, isfound bool)
Values() []interface{}
Clear()
Empty() bool
Size() uint
}
// ILinkedList 通用接口
type ILinkedList interface {
PushFront(values ...interface{})
PushBack(values ...interface{})
PopFront() (result interface{}, found bool)
PopBack() (result interface{}, found bool)
}