148 lines
2.2 KiB
Go
148 lines
2.2 KiB
Go
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 {
|
|
node *Node
|
|
prev *LevelNode
|
|
next *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)
|
|
p.level.lsize.size = 1
|
|
|
|
lnode := new(LevelNode)
|
|
lnode.lsize = new(LevelSize)
|
|
p.level.sub = lnode
|
|
|
|
node := new(Node)
|
|
lnode.sub = node
|
|
|
|
return p
|
|
}
|
|
|
|
// String 展示需要的
|
|
func (pl *PriorityList) String() string {
|
|
|
|
content := ""
|
|
|
|
return content
|
|
}
|
|
|
|
// InsertValues 插入值
|
|
func (pl *PriorityList) InsertValues(values ...interface{}) {
|
|
|
|
for _, value := range values {
|
|
node := new(Node)
|
|
node.IValue = value
|
|
|
|
ll := pl.level
|
|
// 寻找 适合的level
|
|
for ll.level != 0 {
|
|
|
|
if pl.compare(node, ll.node) > 0 {
|
|
temp := ll.prev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 找到数据后
|
|
if ll.level == 0 {
|
|
|
|
if ll.sub == nil {
|
|
ll.sub = node
|
|
continue
|
|
}
|
|
|
|
cur := ll.sub.(*Node)
|
|
|
|
isTail := false
|
|
|
|
for i := 0; i < ll.lsize.size; i++ {
|
|
if pl.compare(node, cur) > 0 {
|
|
|
|
temp := cur.prev
|
|
cur.prev = node
|
|
if temp != nil {
|
|
node.prev = temp
|
|
temp.next = node
|
|
}
|
|
ll.lsize.size++
|
|
pl.size++
|
|
|
|
isTail = true
|
|
}
|
|
cur = cur.next
|
|
}
|
|
|
|
if isTail {
|
|
temp := cur.next
|
|
cur.next = node
|
|
if temp != nil {
|
|
node.next = temp
|
|
temp.prev = node
|
|
}
|
|
ll.lsize.size++
|
|
pl.size++
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|