2019-03-12 02:54:52 +00:00
|
|
|
package plist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2019-03-24 17:40:12 +00:00
|
|
|
"474420502.top/eson/structure/compare"
|
2019-03-12 02:54:52 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
prev, next *Node
|
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PriorityList struct {
|
|
|
|
head, tail *Node
|
|
|
|
size int
|
2019-03-24 17:40:12 +00:00
|
|
|
Compare compare.Compare
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 17:40:12 +00:00
|
|
|
func New(Compare compare.Compare) *PriorityList {
|
|
|
|
pl := &PriorityList{head: &Node{}, tail: &Node{}, size: 0, Compare: Compare}
|
2019-03-12 02:54:52 +00:00
|
|
|
pl.head.next = pl.tail
|
|
|
|
pl.tail.prev = pl.head
|
|
|
|
return pl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) String() string {
|
|
|
|
content := ""
|
|
|
|
|
|
|
|
cur := pl.head.next
|
|
|
|
|
|
|
|
for ; cur != pl.tail; cur = cur.next {
|
|
|
|
content += spew.Sprint(cur.value) + " "
|
|
|
|
}
|
|
|
|
content = strings.TrimRight(content, " ")
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) RString() string {
|
|
|
|
content := ""
|
|
|
|
|
|
|
|
cur := pl.tail.prev
|
|
|
|
|
|
|
|
for ; cur != pl.head; cur = cur.prev {
|
|
|
|
content += spew.Sprint(cur.value) + " "
|
|
|
|
}
|
|
|
|
content = strings.TrimRight(content, " ")
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) Iterator() *Iterator {
|
|
|
|
return &Iterator{pl: pl, cur: pl.head}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) CircularIterator() *CircularIterator {
|
|
|
|
return &CircularIterator{pl: pl, cur: pl.head}
|
|
|
|
}
|
|
|
|
|
2019-03-24 17:40:12 +00:00
|
|
|
func (pl *PriorityList) Size() int {
|
|
|
|
return pl.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) Push(value interface{}) {
|
2019-03-12 02:54:52 +00:00
|
|
|
pl.size++
|
2019-03-24 17:40:12 +00:00
|
|
|
pnode := &Node{value: value}
|
2019-03-12 02:54:52 +00:00
|
|
|
if pl.size == 1 {
|
|
|
|
pl.head.next = pnode
|
|
|
|
pl.tail.prev = pnode
|
|
|
|
pnode.prev = pl.head
|
|
|
|
pnode.next = pl.tail
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cur := pl.head
|
|
|
|
for ; cur.next != pl.tail; cur = cur.next {
|
2019-03-24 17:40:12 +00:00
|
|
|
if pl.Compare(value, cur.next.value) > 0 {
|
2019-03-12 02:54:52 +00:00
|
|
|
cnext := cur.next
|
|
|
|
|
|
|
|
cur.next = pnode
|
|
|
|
cnext.prev = pnode
|
|
|
|
pnode.prev = cur
|
|
|
|
pnode.next = cnext
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.next = pnode
|
|
|
|
pnode.prev = cur
|
|
|
|
pnode.next = pl.tail
|
|
|
|
pl.tail.prev = pnode
|
|
|
|
}
|
|
|
|
|
2019-03-24 17:40:12 +00:00
|
|
|
func (pl *PriorityList) Top() (result interface{}, ok bool) {
|
|
|
|
if pl.size > 0 {
|
|
|
|
return pl.head.next.value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) Pop() (result interface{}, ok bool) {
|
|
|
|
if pl.size > 0 {
|
|
|
|
pl.size--
|
|
|
|
temp := pl.head.next
|
|
|
|
temp.next.prev = pl.head
|
|
|
|
pl.head.next = temp.next
|
|
|
|
return temp.value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 17:40:12 +00:00
|
|
|
func (pl *PriorityList) Get(idx int) (interface{}, bool) {
|
|
|
|
if n, ok := pl.GetNode(idx); ok {
|
|
|
|
return n.value, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) GetNode(idx int) (*Node, bool) {
|
2019-03-12 02:54:52 +00:00
|
|
|
if idx >= 0 {
|
|
|
|
cur := pl.head.next
|
|
|
|
for i := 0; cur != pl.tail; i++ {
|
|
|
|
if i == idx {
|
2019-03-24 17:40:12 +00:00
|
|
|
return cur, true
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
cur = cur.next
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cur := pl.tail.prev
|
|
|
|
for i := -1; cur != pl.head; i-- {
|
|
|
|
if i == idx {
|
2019-03-24 17:40:12 +00:00
|
|
|
return cur, true
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
cur = cur.prev
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 17:40:12 +00:00
|
|
|
return nil, false
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) RemoveWithIndex(idx int) {
|
2019-03-24 17:40:12 +00:00
|
|
|
if n, ok := pl.GetNode(idx); ok {
|
|
|
|
pl.Remove(n)
|
|
|
|
}
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PriorityList) Remove(node *Node) {
|
|
|
|
|
|
|
|
prev := node.prev
|
|
|
|
next := node.next
|
|
|
|
prev.next = next
|
|
|
|
next.prev = prev
|
|
|
|
|
|
|
|
node.prev = nil
|
|
|
|
node.next = nil
|
|
|
|
|
|
|
|
pl.size--
|
|
|
|
}
|
|
|
|
func (pl *PriorityList) Values() []interface{} {
|
|
|
|
values := make([]interface{}, pl.size, pl.size)
|
|
|
|
for i, cur := 0, pl.head.next; cur != pl.tail; i, cur = i+1, cur.next {
|
|
|
|
values[i] = cur.value
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|