165 lines
2.7 KiB
Go
165 lines
2.7 KiB
Go
package plist
|
|
|
|
import (
|
|
"github.com/davecgh/go-spew/spew"
|
|
"github.com/emirpasic/gods/utils"
|
|
)
|
|
|
|
// INode 比较的必须继承的接口
|
|
type INode interface {
|
|
Compare(v INode) bool
|
|
|
|
// GetValue 获取值
|
|
GetValue() interface{}
|
|
// SetValue 设置值
|
|
SetValue(v interface{})
|
|
|
|
GetPrev() INode
|
|
SetPrev(INode)
|
|
GetNext() INode
|
|
SetNext(INode)
|
|
}
|
|
|
|
// Node 优先链表的节点
|
|
type Node struct {
|
|
next, prev *Node
|
|
// isrelease bool
|
|
Value interface{}
|
|
}
|
|
|
|
// PriorityList 优先列表
|
|
type PriorityList struct {
|
|
head *Node
|
|
tail *Node
|
|
|
|
compartor utils.Comparator
|
|
size int
|
|
}
|
|
|
|
// Size 长度
|
|
func (pl *PriorityList) Size() int {
|
|
return pl.size
|
|
}
|
|
|
|
// Clear 清空链表, 如果外部有引用其中一个节点 要把节点Prev Next值为nil, 三色标记
|
|
func (pl *PriorityList) Clear() {
|
|
pl.head = nil
|
|
pl.size = 0
|
|
}
|
|
|
|
// // ForEach 顺序遍历
|
|
// func (pl *PriorityList) ForEach(eachfunc func(*Node) *Node) {
|
|
// cur := pl.head
|
|
// }
|
|
|
|
// Get 获取索引长度
|
|
func (pl *PriorityList) Get(idx int) interface{} {
|
|
node := pl.GetNode(idx)
|
|
if node != nil {
|
|
return node.Value
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetNode 获取索引长度
|
|
func (pl *PriorityList) GetNode(idx int) *Node {
|
|
if idx >= 0 {
|
|
cur := pl.head.next
|
|
for i := 0; cur != pl.tail; i++ {
|
|
if i == idx {
|
|
return cur
|
|
}
|
|
cur = cur.next
|
|
}
|
|
} else {
|
|
cur := pl.tail.prev
|
|
for i := -1; cur != pl.head; i-- {
|
|
if i == idx {
|
|
return cur
|
|
}
|
|
cur = cur.prev
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Remove 删除节点
|
|
func (pl *PriorityList) Remove(idx int) {
|
|
pl.RemoveNode(pl.GetNode(idx))
|
|
}
|
|
|
|
// RemoveNode 删除节点
|
|
func (pl *PriorityList) RemoveNode(node *Node) {
|
|
if node == nil {
|
|
return
|
|
}
|
|
|
|
prev := node.prev
|
|
next := node.next
|
|
|
|
prev.next = next
|
|
next.prev = prev
|
|
|
|
pl.size--
|
|
}
|
|
|
|
func (pl *PriorityList) String() string {
|
|
content := "["
|
|
|
|
cur := pl.head.next
|
|
for cur != pl.tail {
|
|
content += spew.Sprint(cur.Value) + ","
|
|
cur = cur.next
|
|
}
|
|
|
|
if content[len(content)-1:] == "," {
|
|
content = content[:len(content)-1]
|
|
}
|
|
|
|
content += "]"
|
|
return content
|
|
}
|
|
|
|
func (pl *PriorityList) Iterator() *Iterator {
|
|
return &Iterator{pl: pl, cur: pl.head}
|
|
}
|
|
|
|
// Insert 插入节点
|
|
func (pl *PriorityList) Insert(value interface{}) {
|
|
pl.size++
|
|
inode := &Node{Value: value}
|
|
|
|
cur := pl.head.next
|
|
for i := 0; cur != pl.tail; i++ {
|
|
if pl.compartor(value, cur.Value) > 0 {
|
|
prev := cur.prev
|
|
prev.next = inode
|
|
cur.prev = inode
|
|
inode.prev = prev
|
|
inode.next = cur
|
|
return
|
|
}
|
|
cur = cur.next
|
|
}
|
|
|
|
prev := cur.prev
|
|
prev.next = inode
|
|
cur.prev = inode
|
|
inode.prev = prev
|
|
inode.next = cur
|
|
}
|
|
|
|
// New 创建 PriorityList
|
|
func New(compartor utils.Comparator) *PriorityList {
|
|
pl := new(PriorityList)
|
|
pl.compartor = compartor
|
|
pl.head = new(Node)
|
|
pl.tail = new(Node)
|
|
|
|
pl.head.next = pl.tail
|
|
pl.tail.prev = pl.head
|
|
|
|
return pl
|
|
}
|