]> Cypherpunks repositories - gostls13.git/commitdiff
add heap.Remove
authorRuss Cox <rsc@golang.org>
Wed, 16 Sep 2009 17:43:49 +0000 (10:43 -0700)
committerRuss Cox <rsc@golang.org>
Wed, 16 Sep 2009 17:43:49 +0000 (10:43 -0700)
R=gri
DELTA=14  (14 added, 0 deleted, 0 changed)
OCL=34636
CL=34687

src/pkg/container/heap/heap.go

index d35f4d1335122a6d3b7bb3cc333e7098164eb66b..6e7eccb5f26d8c6c356998c5e36871d0ebf6500c 100644 (file)
@@ -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;