priority queue add RemoveNode method

This commit is contained in:
eson 2019-12-23 01:14:30 +08:00
parent 71ba5d9ee0
commit aa15fab86e
4 changed files with 62 additions and 10 deletions

View File

@ -50,20 +50,20 @@ func (pq *PriorityQueue) IndexNode(idx int) (*Node, bool) {
return n, n != nil
}
func (pq *PriorityQueue) Get(key interface{}) (interface{}, bool) {
return pq.queue.Get(key)
func (pq *PriorityQueue) Get(value interface{}) (interface{}, bool) {
return pq.queue.Get(value)
}
func (pq *PriorityQueue) GetNode(key interface{}) (*Node, bool) {
return pq.queue.GetNode(key)
func (pq *PriorityQueue) GetNode(value interface{}) (*Node, bool) {
return pq.queue.GetNode(value)
}
func (pq *PriorityQueue) GetAround(key interface{}) [3]interface{} {
return pq.queue.GetAround(key)
func (pq *PriorityQueue) GetAround(value interface{}) [3]interface{} {
return pq.queue.GetAround(value)
}
func (pq *PriorityQueue) GetAroundNode(key interface{}) [3]*Node {
return pq.queue.getArounNode(key)
func (pq *PriorityQueue) GetAroundNode(value interface{}) [3]*Node {
return pq.queue.getArounNode(value)
}
func (pq *PriorityQueue) GetRange(k1, k2 interface{}) []interface{} {
@ -74,8 +74,12 @@ func (pq *PriorityQueue) RemoveIndex(idx int) (interface{}, bool) {
return pq.queue.RemoveIndex(idx)
}
func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) {
return pq.queue.Remove(key)
func (pq *PriorityQueue) Remove(vlaue interface{}) (interface{}, bool) {
return pq.queue.Remove(vlaue)
}
func (pq *PriorityQueue) RemoveNode(node *Node) {
pq.queue.removeNode(node)
}
func (pq *PriorityQueue) Values() []interface{} {

View File

@ -170,6 +170,28 @@ func TestQueueRemove(t *testing.T) {
}
}
func TestQueueRemoveNode(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}
for _, v := range l {
pq.Push(v)
}
content := ""
for _, v := range l {
if n, ok := pq.GetNode(v); ok {
pq.RemoveNode(n)
content += spew.Sprint(pq.Values())
} else {
t.Error("can not get Node: ", v)
}
}
if content != "[90 78 53 10 4 1][90 78 53 4 1][90 78 4 1][90 4 1][4 1][4][]" {
t.Error(content)
}
}
func TestQueueRemoveIndex(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}

View File

@ -78,6 +78,10 @@ func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) {
return pq.queue.Remove(key)
}
func (pq *PriorityQueue) RemoveNode(node *Node) {
pq.queue.removeNode(node)
}
func (pq *PriorityQueue) Values() []interface{} {
return pq.queue.Values()
}

View File

@ -170,6 +170,28 @@ func TestQueueRemove(t *testing.T) {
}
}
func TestQueueRemoveNode(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}
for _, v := range l {
pq.Push(v, v)
}
content := ""
for _, v := range l {
if n, ok := pq.GetNode(v); ok {
pq.RemoveNode(n)
content += spew.Sprint(pq.Values())
} else {
t.Error("can not get Node: ", v)
}
}
if content != "[90 78 53 10 4 1][90 78 53 4 1][90 78 4 1][90 4 1][4 1][4][]" {
t.Error(content)
}
}
func TestQueueRemoveIndex(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}