TODO: Finish priority_list

This commit is contained in:
huangsimin 2019-01-26 18:45:30 +08:00
parent fd4d47445f
commit 741a685e16
4 changed files with 188 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package plist
import "testing"
import (
"testing"
)
func TestPriority(t *testing.T) {
pl := New()

View File

@ -0,0 +1,134 @@
package plist
import (
"474420502.top/eson/structure"
)
type NodeList struct {
head, tail *Node
}
// Node 节点结构
type Node struct {
prev, next *Node
structure.NValue
}
// PriorityList 跳表
type PriorityList struct {
size int
compare func(v1, v2 interface{}) int
level *LevelNode
}
type LevelSize struct {
size int
}
// LevelNode 层级列表
type LevelNode struct {
head *LevelNode
tail *LevelNode
sub interface{}
lsize *LevelSize
level int
}
// New a node
func New() *PriorityList {
p := new(PriorityList)
p.level = new(LevelNode)
p.level.lsize = new(LevelSize)
node := new(Node)
p.level.sub = node
return p
}
// String 展示需要的
func (pl *PriorityList) String() string {
content := ""
return content
}
// InsertValues 插入值
func (pl *PriorityList) InsertValues(values ...interface{}) {
ll := pl.level
for _, value := range values {
node := new(Node)
node.IValue = value
if ll.level == 0 {
if ll.sub == nil {
ll.sub = node
continue
}
cur := ll.sub.(*Node)
cur.
}
if level.tail == nil {
level.head.lsize
node.prev = level.head
level.tail = node
continue
}
cur := level.head
for cur != nil {
if pl.compare(cur, node) < 0 {
if cur.sub != nil {
cur = cur.sub
continue
} else {
temp := cur.next
cur.next = node
if temp != nil {
node.next = temp
temp.prev = node
}
pl.size++
break
}
} else {
}
cur = cur.next
}
}
}
// Get 获取索引长度
// func (pl *PriorityList) Get(idx int) INode {
// if idx >= pl.size {
// return nil
// }
// cur := pl.head
// for i := 0; i < idx; i++ {
// cur = cur.GetNext()
// }
// return cur
// }
// Size 长度
func (pl *PriorityList) Size() int {
return pl.size
}
// Clear 清空链表, 如果外部有引用其中一个节点 要把节点Prev Next值为nil, 三色标记
func (pl *PriorityList) Clear() {
pl.level = nil
pl.size = 0
}

View File

@ -0,0 +1,18 @@
package plist
import "testing"
func IntCompare(v1, v2 interface{}) int {
if v1.(*Node).ValueInt() > v2.(*Node).ValueInt() {
return 1
}
return -1
}
func TestPriorityList_InsertValues(t *testing.T) {
pl := New()
pl.compare = IntCompare
pl.InsertValues(1, 2, 5, 6, 2, 4, 3)
t.Error(pl.String())
}

View File

@ -1,3 +1,36 @@
package structure
// NValue Node节点的必备的结构
type NValue struct {
IValue interface{}
}
// ValueInt 返回 Int
func (node *NValue) ValueInt() int {
return node.IValue.(int)
}
// Value 返回 Value interface
func (node *NValue) Value() interface{} {
return node.IValue
}
// ValueInt8 返回 Int
func (node *NValue) ValueInt8() int8 {
return node.IValue.(int8)
}
// ValueInt16 返回 Int
func (node *NValue) ValueInt16() int16 {
return node.IValue.(int16)
}
// ValueInt32 返回 Int32
func (node *NValue) ValueInt32() int32 {
return node.IValue.(int32)
}
// ValueInt64 返回 Int64
func (node *NValue) ValueInt64() int64 {
return node.IValue.(int64)
}