structure_old/priority_queue/priority_list.go

148 lines
2.2 KiB
Go
Raw Normal View History

2019-01-26 10:45:30 +00:00
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 {
2019-01-27 10:42:08 +00:00
node *Node
prev *LevelNode
next *LevelNode
2019-01-26 10:45:30 +00:00
sub interface{}
lsize *LevelSize
level int
}
// New a node
func New() *PriorityList {
p := new(PriorityList)
p.level = new(LevelNode)
p.level.lsize = new(LevelSize)
2019-01-27 10:42:08 +00:00
p.level.lsize.size = 1
lnode := new(LevelNode)
lnode.lsize = new(LevelSize)
p.level.sub = lnode
2019-01-26 10:45:30 +00:00
node := new(Node)
2019-01-27 10:42:08 +00:00
lnode.sub = node
2019-01-26 10:45:30 +00:00
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
2019-01-27 10:42:08 +00:00
ll := pl.level
// 寻找 适合的level
for ll.level != 0 {
if pl.compare(node, ll.node) > 0 {
temp := ll.prev
}
}
// 找到数据后
2019-01-26 10:45:30 +00:00
if ll.level == 0 {
if ll.sub == nil {
ll.sub = node
continue
}
cur := ll.sub.(*Node)
2019-01-27 10:42:08 +00:00
isTail := false
2019-01-26 10:45:30 +00:00
2019-01-27 10:42:08 +00:00
for i := 0; i < ll.lsize.size; i++ {
if pl.compare(node, cur) > 0 {
2019-01-26 10:45:30 +00:00
2019-01-27 10:42:08 +00:00
temp := cur.prev
cur.prev = node
2019-01-26 10:45:30 +00:00
if temp != nil {
2019-01-27 10:42:08 +00:00
node.prev = temp
temp.next = node
2019-01-26 10:45:30 +00:00
}
2019-01-27 10:42:08 +00:00
ll.lsize.size++
2019-01-26 10:45:30 +00:00
pl.size++
2019-01-27 10:42:08 +00:00
isTail = true
}
cur = cur.next
}
2019-01-26 10:45:30 +00:00
2019-01-27 10:42:08 +00:00
if isTail {
temp := cur.next
cur.next = node
if temp != nil {
node.next = temp
temp.prev = node
}
ll.lsize.size++
pl.size++
2019-01-26 10:45:30 +00:00
}
}
}
}
// 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
}