diff --git a/priority_queue/priority_queue.go b/priority_queue/priority_queue.go index ce52fe6..fff8ec5 100644 --- a/priority_queue/priority_queue.go +++ b/priority_queue/priority_queue.go @@ -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{} { diff --git a/priority_queue/priority_queue_test.go b/priority_queue/priority_queue_test.go index d096bdf..2375e59 100644 --- a/priority_queue/priority_queue_test.go +++ b/priority_queue/priority_queue_test.go @@ -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} diff --git a/priority_queuekey/priority_queuekey.go b/priority_queuekey/priority_queuekey.go index b3d3779..41851f2 100644 --- a/priority_queuekey/priority_queuekey.go +++ b/priority_queuekey/priority_queuekey.go @@ -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() } diff --git a/priority_queuekey/priority_queuekey_test.go b/priority_queuekey/priority_queuekey_test.go index 5b9b538..ce96b71 100644 --- a/priority_queuekey/priority_queuekey_test.go +++ b/priority_queuekey/priority_queuekey_test.go @@ -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}