退回最简单的优先链表
This commit is contained in:
parent
0403bebea4
commit
05bcac44ef
|
@ -72,84 +72,32 @@ func (pq *PriorityQueue) Push(v interface{}) {
|
||||||
|
|
||||||
node := new(Node)
|
node := new(Node)
|
||||||
node.value = v
|
node.value = v
|
||||||
|
pq.size++
|
||||||
|
|
||||||
if pq.node == nil {
|
if pq.node == nil {
|
||||||
//创建索引
|
//创建索引
|
||||||
index := new(Index)
|
|
||||||
index.nlen = 1
|
|
||||||
index.node = node
|
|
||||||
|
|
||||||
pq.index = index
|
|
||||||
pq.node = node
|
pq.node = node
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// find the node of index to start
|
|
||||||
idx := pq.index
|
|
||||||
|
|
||||||
for {
|
cur := pq.node
|
||||||
|
|
||||||
if idx.next == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if pq.comparator(v, idx.next.node.value) > 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
idx = idx.next
|
|
||||||
}
|
|
||||||
|
|
||||||
cur := idx.node
|
|
||||||
|
|
||||||
//cur := pq.node
|
//cur := pq.node
|
||||||
if pq.comparator(v, pq.node.value) > 0 {
|
if pq.comparator(v, pq.node.value) > 0 {
|
||||||
pq.node = node
|
pq.node = node
|
||||||
node.next = cur
|
node.next = cur
|
||||||
|
|
||||||
pq.index.node = pq.node
|
|
||||||
pq.index.nlen++
|
|
||||||
|
|
||||||
// cur.prev = node
|
// cur.prev = node
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; cur.next != nil; i++ {
|
for i := 0; cur.next != nil; i++ {
|
||||||
|
|
||||||
if i >= pq.indexlimit {
|
|
||||||
|
|
||||||
if idx.next != nil && idx.next.nlen < pq.indexlimit {
|
|
||||||
idx.next.nlen += idx.nlen - pq.indexlimit
|
|
||||||
idx.nlen = pq.indexlimit
|
|
||||||
idx.next.node = cur
|
|
||||||
} else {
|
|
||||||
index := new(Index)
|
|
||||||
index.node = cur
|
|
||||||
index.nlen = idx.nlen - pq.indexlimit
|
|
||||||
index.next = idx.next
|
|
||||||
|
|
||||||
idx.next = index
|
|
||||||
idx.nlen = pq.indexlimit
|
|
||||||
idx = index
|
|
||||||
i = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if pq.comparator(v, cur.next.value) > 0 {
|
if pq.comparator(v, cur.next.value) > 0 {
|
||||||
temp := cur.next
|
temp := cur.next
|
||||||
cur.next = node
|
cur.next = node
|
||||||
node.next = temp
|
node.next = temp
|
||||||
// node.prev = cur
|
// node.prev = cur
|
||||||
// temp.prev = node
|
// temp.prev = node
|
||||||
|
|
||||||
idx.nlen++
|
|
||||||
|
|
||||||
// if pq.index.nlen >= pq.indexlimit {
|
|
||||||
// // 分裂
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,8 +108,6 @@ func (pq *PriorityQueue) Push(v interface{}) {
|
||||||
cur.next = node
|
cur.next = node
|
||||||
|
|
||||||
// node.prev = cur
|
// node.prev = cur
|
||||||
pq.size++
|
|
||||||
idx.nlen++
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user