Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6110d7c487 | ||
|
|
6516b424ee | ||
|
|
fe15076461 | ||
|
|
04ccac58fe | ||
|
|
06f4366e51 | ||
|
|
cc239d175c |
@@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/474420502/focus/list"
|
"github.com/474420502/focus/list"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ArrayList struct {
|
type ArrayList struct {
|
||||||
@@ -34,6 +35,7 @@ func New() *ArrayList {
|
|||||||
l.data = make([]interface{}, initCap, initCap)
|
l.data = make([]interface{}, initCap, initCap)
|
||||||
l.tidx = initCap / 2
|
l.tidx = initCap / 2
|
||||||
l.hidx = l.tidx - 1
|
l.hidx = l.tidx - 1
|
||||||
|
// l.shrinkSize = listMinLimit
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,15 +69,19 @@ func (l *ArrayList) shrink() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if l.size <= l.shrinkSize {
|
if l.size <= l.shrinkSize {
|
||||||
nSize := l.shrinkSize - l.shrinkSize>>1
|
lcap := uint(len(l.data))
|
||||||
|
nSize := lcap - lcap>>2
|
||||||
temp := make([]interface{}, nSize, nSize)
|
temp := make([]interface{}, nSize, nSize)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
// l.shrinkSize = l.shrinkSize - lcap>>2
|
||||||
|
l.shrinkSize = l.size - l.size>>2
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -98,6 +104,7 @@ func (l *ArrayList) growth() {
|
|||||||
l.hidx = ghidx
|
l.hidx = ghidx
|
||||||
l.tidx = gtidx
|
l.tidx = gtidx
|
||||||
|
|
||||||
|
l.shrinkSize = l.size - l.size>>2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Push(value interface{}) {
|
func (l *ArrayList) Push(value interface{}) {
|
||||||
@@ -140,6 +147,7 @@ func (l *ArrayList) PopFront() (result interface{}, found bool) {
|
|||||||
if l.size != 0 {
|
if l.size != 0 {
|
||||||
l.size--
|
l.size--
|
||||||
l.hidx++
|
l.hidx++
|
||||||
|
l.shrink()
|
||||||
return l.data[l.hidx], true
|
return l.data[l.hidx], true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
@@ -149,25 +157,32 @@ func (l *ArrayList) PopBack() (result interface{}, found bool) {
|
|||||||
if l.size != 0 {
|
if l.size != 0 {
|
||||||
l.size--
|
l.size--
|
||||||
l.tidx--
|
l.tidx--
|
||||||
|
l.shrink()
|
||||||
return l.data[l.tidx], true
|
return l.data[l.tidx], true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Index(idx uint) (interface{}, bool) {
|
func (l *ArrayList) Index(idx int) (interface{}, bool) {
|
||||||
if idx < l.size {
|
var uidx uint = (uint)(idx)
|
||||||
return l.data[idx+l.hidx+1], true
|
if uidx < l.size {
|
||||||
|
return l.data[uidx+l.hidx+1], true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Remove(idx uint) (result interface{}, isfound bool) {
|
func (l *ArrayList) Remove(idx int) (result interface{}, isfound bool) {
|
||||||
|
|
||||||
if idx >= l.size {
|
if idx < 0 {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
offset := l.hidx + 1 + idx
|
var uidx = (uint)(idx)
|
||||||
|
if uidx >= l.size {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := l.hidx + 1 + uidx
|
||||||
|
|
||||||
isfound = true
|
isfound = true
|
||||||
result = l.data[offset]
|
result = l.data[offset]
|
||||||
@@ -210,6 +225,10 @@ func (l *ArrayList) Values() []interface{} {
|
|||||||
return newElements
|
return newElements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *ArrayList) String() string {
|
||||||
|
return spew.Sprint(l.Values())
|
||||||
|
}
|
||||||
|
|
||||||
func (l *ArrayList) Traversal(every func(interface{}) bool) {
|
func (l *ArrayList) Traversal(every func(interface{}) bool) {
|
||||||
for i := uint(0); i < l.size; i++ {
|
for i := uint(0); i < l.size; i++ {
|
||||||
if !every(l.data[i+l.hidx+1]) {
|
if !every(l.data[i+l.hidx+1]) {
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package arraylist
|
package arraylist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/gob"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
@@ -178,8 +174,8 @@ func TestRemove(t *testing.T) {
|
|||||||
var result string
|
var result string
|
||||||
|
|
||||||
for _, selval := range []uint{4, 3} {
|
for _, selval := range []uint{4, 3} {
|
||||||
last, _ := l.Index(selval)
|
last, _ := l.Index((int)(selval))
|
||||||
if v, isfound := l.Remove(selval); isfound {
|
if v, isfound := l.Remove((int)(selval)); isfound {
|
||||||
if v != last {
|
if v != last {
|
||||||
t.Error(v, " != ", last)
|
t.Error(v, " != ", last)
|
||||||
}
|
}
|
||||||
@@ -244,24 +240,56 @@ func TestTraversal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadTestData() []int {
|
func TestRemain(t *testing.T) {
|
||||||
data, err := ioutil.ReadFile("../../l.log")
|
l := New()
|
||||||
if err != nil {
|
for i := 0; i < 10; i++ {
|
||||||
log.Println(err)
|
l.Push(i)
|
||||||
|
if !l.Contains(i) {
|
||||||
|
t.Error("Contains", i)
|
||||||
}
|
}
|
||||||
var l []int
|
|
||||||
decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
||||||
decoder.Decode(&l)
|
|
||||||
return l
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPush(b *testing.B) {
|
if l.String() != "[0 1 2 3 4 5 6 7 8 9]" {
|
||||||
l := loadTestData()
|
t.Error(l.String())
|
||||||
b.N = len(l)
|
}
|
||||||
|
|
||||||
arr := New()
|
for i := 10; i < 100; i++ {
|
||||||
|
l.Push(i)
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for !l.Empty() {
|
||||||
arr.PushBack(l[i])
|
l.PopBack()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 10; i < 100; i++ {
|
||||||
|
l.Push(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Clear()
|
||||||
|
|
||||||
|
if l.Size() != 0 {
|
||||||
|
t.Error("Size != 0")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func loadTestData() []int {
|
||||||
|
// data, err := ioutil.ReadFile("../../l.log")
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println(err)
|
||||||
|
// }
|
||||||
|
// var l []int
|
||||||
|
// decoder := gob.NewDecoder(bytes.NewReader(data))
|
||||||
|
// decoder.Decode(&l)
|
||||||
|
// return l
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func BenchmarkPush(b *testing.B) {
|
||||||
|
// l := loadTestData()
|
||||||
|
// b.N = len(l)
|
||||||
|
|
||||||
|
// arr := New()
|
||||||
|
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// arr.PushBack(l[i])
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ type Iterator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) Value() interface{} {
|
func (iter *Iterator) Value() interface{} {
|
||||||
v, _ := iter.al.Index(iter.cur)
|
v, _ := iter.al.Index((int)(iter.cur))
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ type CircularIterator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *CircularIterator) Value() interface{} {
|
func (iter *CircularIterator) Value() interface{} {
|
||||||
v, _ := iter.al.Index(iter.cur)
|
v, _ := iter.al.Index((int)(iter.cur))
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -191,28 +191,34 @@ func (l *LinkedList) FindMany(every func(idx uint, value interface{}) int) (resu
|
|||||||
return result, isfound
|
return result, isfound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LinkedList) Index(idx uint) (interface{}, bool) {
|
func (l *LinkedList) Index(idx int) (interface{}, bool) {
|
||||||
if idx >= l.size {
|
|
||||||
|
if idx < 0 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
var uidx = (uint)(idx)
|
||||||
|
|
||||||
|
if uidx >= l.size || idx < 0 {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if idx > l.size/2 {
|
if uidx > l.size/2 {
|
||||||
idx = l.size - 1 - idx
|
uidx = l.size - 1 - uidx
|
||||||
// 尾部
|
// 尾部
|
||||||
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 头部
|
// 头部
|
||||||
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,33 +377,39 @@ func remove(cur *Node) {
|
|||||||
cur.next = nil
|
cur.next = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LinkedList) Remove(idx uint) (interface{}, bool) {
|
func (l *LinkedList) Remove(idx int) (interface{}, bool) {
|
||||||
if l.size <= idx {
|
|
||||||
|
if idx < 0 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
var uidx uint = (uint)(idx)
|
||||||
|
if l.size <= uidx {
|
||||||
// log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx)
|
// log.Printf("out of list range, size is %d, idx is %d\n", l.size, idx)
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
l.size--
|
l.size--
|
||||||
if idx > l.size/2 {
|
if uidx > l.size/2 {
|
||||||
idx = l.size - idx // l.size - 1 - idx, 先减size
|
uidx = l.size - uidx // l.size - 1 - idx, 先减size
|
||||||
// 尾部
|
// 尾部
|
||||||
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
for cur := l.tail.prev; cur != l.head; cur = cur.prev {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
remove(cur)
|
remove(cur)
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 头部
|
// 头部
|
||||||
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
||||||
if idx == 0 {
|
if uidx == 0 {
|
||||||
remove(cur)
|
remove(cur)
|
||||||
return cur.value, true
|
return cur.value, true
|
||||||
|
|
||||||
}
|
}
|
||||||
idx--
|
uidx--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ package list
|
|||||||
type IList interface {
|
type IList interface {
|
||||||
Push(value interface{})
|
Push(value interface{})
|
||||||
Contains(values ...interface{}) bool
|
Contains(values ...interface{}) bool
|
||||||
Index(idx uint) (interface{}, bool)
|
Index(idx int) (interface{}, bool)
|
||||||
Remove(idx uint) (result interface{}, isfound bool)
|
Remove(idx int) (result interface{}, isfound bool)
|
||||||
Values() []interface{}
|
Values() []interface{}
|
||||||
Traversal(every func(interface{}) bool)
|
Traversal(every func(interface{}) bool)
|
||||||
|
String() string
|
||||||
|
|
||||||
Clear()
|
Clear()
|
||||||
Empty() bool
|
Empty() bool
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/474420502/focus/compare"
|
"github.com/474420502/focus/compare"
|
||||||
|
"github.com/474420502/focus/list"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ type PriorityList struct {
|
|||||||
|
|
||||||
// 优先队列的Index 正负都有作用要定义一种新的接口类型
|
// 优先队列的Index 正负都有作用要定义一种新的接口类型
|
||||||
func assertImplementation() {
|
func assertImplementation() {
|
||||||
// var _ list.IList = (*PriorityList)(nil)
|
var _ list.IList = (*PriorityList)(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(Compare compare.Compare) *PriorityList {
|
func New(Compare compare.Compare) *PriorityList {
|
||||||
@@ -173,11 +174,19 @@ func (pl *PriorityList) GetNode(idx int) (*Node, bool) {
|
|||||||
|
|
||||||
func (pl *PriorityList) RemoveWithIndex(idx int) {
|
func (pl *PriorityList) RemoveWithIndex(idx int) {
|
||||||
if n, ok := pl.GetNode(idx); ok {
|
if n, ok := pl.GetNode(idx); ok {
|
||||||
pl.Remove(n)
|
pl.RemoveNode(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pl *PriorityList) Remove(node *Node) {
|
func (pl *PriorityList) Remove(idx int) (result interface{}, isfound bool) {
|
||||||
|
if n, ok := pl.GetNode(idx); ok {
|
||||||
|
pl.RemoveNode(n)
|
||||||
|
return n.value, true
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pl *PriorityList) RemoveNode(node *Node) {
|
||||||
|
|
||||||
prev := node.prev
|
prev := node.prev
|
||||||
next := node.next
|
next := node.next
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func (lhmap *LinkedHashmap) Remove(key interface{}) (interface{}, bool) {
|
|||||||
func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) {
|
func (lhmap *LinkedHashmap) RemoveIndex(idx uint) (interface{}, bool) {
|
||||||
if lhmap.list.Size() >= idx {
|
if lhmap.list.Size() >= idx {
|
||||||
// log.Printf("warn: out of list range, size is %d, idx is %d\n", lhmap.list.Size(), idx)
|
// log.Printf("warn: out of list range, size is %d, idx is %d\n", lhmap.list.Size(), idx)
|
||||||
if key, ok := lhmap.list.Remove(idx); ok {
|
if key, ok := lhmap.list.Remove((int)(idx)); ok {
|
||||||
result := lhmap.hmap[key]
|
result := lhmap.hmap[key]
|
||||||
delete(lhmap.hmap, key)
|
delete(lhmap.hmap, key)
|
||||||
return result, true
|
return result, true
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package pqueue
|
package pqueue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package pqueuekey
|
package pqueuekey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package lastack
|
package lastack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/474420502/focus/stack"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,7 +14,11 @@ type Node struct {
|
|||||||
type Stack struct {
|
type Stack struct {
|
||||||
top *Node
|
top *Node
|
||||||
cache *Node
|
cache *Node
|
||||||
size int
|
size uint
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ stack.IStack = (*Stack)(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *Stack) grow() bool {
|
func (as *Stack) grow() bool {
|
||||||
@@ -25,7 +30,7 @@ func (as *Stack) grow() bool {
|
|||||||
grownode.cur = -1
|
grownode.cur = -1
|
||||||
as.cache = nil
|
as.cache = nil
|
||||||
} else {
|
} else {
|
||||||
var growsize int
|
var growsize uint
|
||||||
if as.size <= 256 {
|
if as.size <= 256 {
|
||||||
growsize = as.size << 1
|
growsize = as.size << 1
|
||||||
} else {
|
} else {
|
||||||
@@ -78,10 +83,11 @@ func (as *Stack) Empty() bool {
|
|||||||
return as.size == 0
|
return as.size == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *Stack) Size() int {
|
func (as *Stack) Size() uint {
|
||||||
return as.size
|
return as.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String 左为Top
|
||||||
func (as *Stack) String() string {
|
func (as *Stack) String() string {
|
||||||
content := ""
|
content := ""
|
||||||
cur := as.top
|
cur := as.top
|
||||||
@@ -119,7 +125,7 @@ func (as *Stack) Values() []interface{} {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *Stack) Get(idx int) (interface{}, bool) {
|
func (as *Stack) Index(idx int) (interface{}, bool) {
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func TestPush(t *testing.T) {
|
func TestPush(t *testing.T) {
|
||||||
var result string
|
var result string
|
||||||
s := New()
|
s := NewWithCap(10)
|
||||||
|
|
||||||
result = spew.Sprint(s.Values())
|
result = spew.Sprint(s.Values())
|
||||||
if result != "[]" {
|
if result != "[]" {
|
||||||
@@ -67,9 +67,24 @@ func TestPush(t *testing.T) {
|
|||||||
t.Error(result)
|
t.Error(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
s.Push(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
if v, _ := s.Index(50); v != 49 {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 50; i++ {
|
||||||
|
s.Pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, _ := s.Peek(); v != 49 {
|
||||||
|
t.Error(s.Peek())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBase(t *testing.T) {
|
||||||
s := New()
|
s := New()
|
||||||
|
|
||||||
l := []int{10, 7, 3, 4, 5, 15}
|
l := []int{10, 7, 3, 4, 5, 15}
|
||||||
@@ -77,7 +92,34 @@ func TestGet(t *testing.T) {
|
|||||||
s.Push(v)
|
s.Push(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, isfound := s.Get(0); isfound {
|
if _, ok := s.Index(-1); ok {
|
||||||
|
t.Error("not ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.String() != "15 5 4 3 7 10" {
|
||||||
|
t.Error(s.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Size() != 6 {
|
||||||
|
t.Error("Size error, is", s.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Clear()
|
||||||
|
if !s.Empty() {
|
||||||
|
t.Error("Size should be Empty, is Clean.")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndex(t *testing.T) {
|
||||||
|
s := New()
|
||||||
|
|
||||||
|
l := []int{10, 7, 3, 4, 5, 15}
|
||||||
|
for _, v := range l {
|
||||||
|
s.Push(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, isfound := s.Index(0); isfound {
|
||||||
if v != 15 {
|
if v != 15 {
|
||||||
t.Error("15 is not equal to 15")
|
t.Error("15 is not equal to 15")
|
||||||
}
|
}
|
||||||
@@ -86,7 +128,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, tv := range l {
|
for i, tv := range l {
|
||||||
if v, isfound := s.Get(len(l) - 1 - i); isfound {
|
if v, isfound := s.Index(len(l) - 1 - i); isfound {
|
||||||
if v != tv {
|
if v != tv {
|
||||||
t.Error(v, "is not equal to", tv)
|
t.Error(v, "is not equal to", tv)
|
||||||
}
|
}
|
||||||
@@ -100,7 +142,7 @@ func TestGet(t *testing.T) {
|
|||||||
l = l[0 : len(l)-1]
|
l = l[0 : len(l)-1]
|
||||||
for i, tv := range l {
|
for i, tv := range l {
|
||||||
index := len(l) - 1 - i
|
index := len(l) - 1 - i
|
||||||
if v, isfound := s.Get(index); isfound {
|
if v, isfound := s.Index(index); isfound {
|
||||||
if v != tv {
|
if v != tv {
|
||||||
t.Error(v, "is not equal to", tv)
|
t.Error(v, "is not equal to", tv)
|
||||||
}
|
}
|
||||||
@@ -110,7 +152,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func BenchmarkGet(b *testing.B) {
|
// func BenchmarkIndex(b *testing.B) {
|
||||||
// s := New()
|
// s := New()
|
||||||
// b.N = 20000000
|
// b.N = 20000000
|
||||||
|
|
||||||
@@ -123,7 +165,7 @@ func TestGet(t *testing.T) {
|
|||||||
// b.StartTimer()
|
// b.StartTimer()
|
||||||
|
|
||||||
// for i := 0; i < b.N; i++ {
|
// for i := 0; i < b.N; i++ {
|
||||||
// s.Get(i)
|
// s.Index(i)
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
102
stack/liststack/stack.go
Normal file
102
stack/liststack/stack.go
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package lastack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/474420502/focus/stack"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
value interface{}
|
||||||
|
down *Node
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stack struct {
|
||||||
|
top *Node
|
||||||
|
size uint
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertImplementation() {
|
||||||
|
var _ stack.IStack = (*Stack)(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func New() *Stack {
|
||||||
|
s := &Stack{}
|
||||||
|
s.size = 0
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Clear() {
|
||||||
|
as.size = 0
|
||||||
|
as.top = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Empty() bool {
|
||||||
|
return as.size == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Size() uint {
|
||||||
|
return as.size
|
||||||
|
}
|
||||||
|
|
||||||
|
// String 从左到右 左边第一个表示Top 如链表 a(top)->b->c
|
||||||
|
func (as *Stack) String() string {
|
||||||
|
content := ""
|
||||||
|
cur := as.top
|
||||||
|
for ; cur != nil; cur = cur.down {
|
||||||
|
content += spew.Sprint(cur.value) + " "
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(content) > 0 {
|
||||||
|
content = content[0 : len(content)-1]
|
||||||
|
} else {
|
||||||
|
content = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Values() []interface{} {
|
||||||
|
|
||||||
|
if as.size == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]interface{}, as.size, as.size)
|
||||||
|
|
||||||
|
cur := as.top
|
||||||
|
n := 0
|
||||||
|
for ; cur != nil; cur = cur.down {
|
||||||
|
result[n] = cur.value
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Push(v interface{}) {
|
||||||
|
nv := &Node{value: v}
|
||||||
|
nv.down = as.top
|
||||||
|
as.top = nv
|
||||||
|
as.size++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Pop() (interface{}, bool) {
|
||||||
|
if as.size == 0 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
as.size--
|
||||||
|
|
||||||
|
result := as.top
|
||||||
|
as.top = as.top.down
|
||||||
|
result.down = nil
|
||||||
|
return result.value, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *Stack) Peek() (interface{}, bool) {
|
||||||
|
if as.size == 0 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
return as.top.value, true
|
||||||
|
}
|
||||||
104
stack/stack.go
104
stack/stack.go
@@ -1,96 +1,20 @@
|
|||||||
package lastack
|
package stack
|
||||||
|
|
||||||
import (
|
type IStack interface {
|
||||||
"github.com/davecgh/go-spew/spew"
|
Clear()
|
||||||
)
|
|
||||||
|
|
||||||
type Node struct {
|
Empty() bool
|
||||||
value interface{}
|
|
||||||
down *Node
|
|
||||||
}
|
|
||||||
|
|
||||||
type Stack struct {
|
Size() uint
|
||||||
top *Node
|
|
||||||
size int
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() *Stack {
|
|
||||||
s := &Stack{}
|
|
||||||
s.size = 0
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Clear() {
|
|
||||||
as.size = 0
|
|
||||||
as.top = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Empty() bool {
|
|
||||||
return as.size == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Size() int {
|
|
||||||
return as.size
|
|
||||||
}
|
|
||||||
|
|
||||||
// String 从左到右 左边第一个表示Top 如链表 a(top)->b->c
|
// String 从左到右 左边第一个表示Top 如链表 a(top)->b->c
|
||||||
func (as *Stack) String() string {
|
String() string
|
||||||
content := ""
|
|
||||||
cur := as.top
|
Values() []interface{}
|
||||||
for ; cur != nil; cur = cur.down {
|
|
||||||
content += spew.Sprint(cur.value) + " "
|
Push(v interface{})
|
||||||
}
|
|
||||||
|
Pop() (interface{}, bool)
|
||||||
if len(content) > 0 {
|
|
||||||
content = content[0 : len(content)-1]
|
Peek() (interface{}, bool)
|
||||||
} else {
|
|
||||||
content = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return content
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Values() []interface{} {
|
|
||||||
|
|
||||||
if as.size == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make([]interface{}, as.size, as.size)
|
|
||||||
|
|
||||||
cur := as.top
|
|
||||||
n := 0
|
|
||||||
for ; cur != nil; cur = cur.down {
|
|
||||||
result[n] = cur.value
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Push(v interface{}) {
|
|
||||||
nv := &Node{value: v}
|
|
||||||
nv.down = as.top
|
|
||||||
as.top = nv
|
|
||||||
as.size++
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Pop() (interface{}, bool) {
|
|
||||||
if as.size <= 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
as.size--
|
|
||||||
|
|
||||||
result := as.top
|
|
||||||
as.top = as.top.down
|
|
||||||
result.down = nil
|
|
||||||
return result.value, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (as *Stack) Peek() (interface{}, bool) {
|
|
||||||
if as.size <= 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
return as.top.value, true
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package avl
|
package avl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package avldup
|
package avldup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package avlkey
|
package avlkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package avlkeydup
|
package avlkeydup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package vbt
|
package vbt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package vbtkey
|
package vbtkey
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/474420502/focus/lastack"
|
"github.com/474420502/focus/stack/listarraystack"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user