From: Russ Cox Date: Wed, 16 Sep 2009 17:43:49 +0000 (-0700) Subject: add heap.Remove X-Git-Tag: weekly.2009-11-06~565 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0ee18ca8169b6dcbc527627bb803b0ddcbee06f6;p=gostls13.git add heap.Remove R=gri DELTA=14 (14 added, 0 deleted, 0 changed) OCL=34636 CL=34687 --- diff --git a/src/pkg/container/heap/heap.go b/src/pkg/container/heap/heap.go index d35f4d1335..6e7eccb5f2 100644 --- a/src/pkg/container/heap/heap.go +++ b/src/pkg/container/heap/heap.go @@ -52,6 +52,20 @@ func Pop(h HeapInterface) interface{} { } +// Remove removes the element at index i from the heap. +// The complexity is O(log(n)) where n = h.Len(). +// +func Remove(h HeapInterface, i int) interface{} { + n := h.Len()-1; + if n != i { + h.Swap(n, i); + down(h, i, n); + up(h, i); + } + return h.Pop(); +} + + func up(h HeapInterface, j int) { for { i := (j-1)/2;