Heap.st
changeset 3711 2f73ab944d25
parent 3017 ab364a8619f9
child 4285 d0db4e125e7a
--- a/Heap.st	Fri Jan 22 13:21:46 2016 +0100
+++ b/Heap.st	Fri Jan 22 13:24:41 2016 +0100
@@ -1,5 +1,7 @@
 "{ Package: 'stx:libbasic2' }"
 
+"{ NameSpace: Smalltalk }"
+
 SequenceableCollection subclass:#Heap
 	instanceVariableNames:'array tally sortBlock'
 	classVariableNames:''
@@ -239,16 +241,20 @@
 !
 
 privateRemoveAt: index
-	"Remove the element at the given index and make sure the sorting order is okay"
-	| removed |
-	removed := array at: index.
-	array at: index put: (array at: tally).
-	array at: tally put: nil.
-	tally := tally - 1.
-	index > tally ifFalse:[
-		"Use #downHeapSingle: since only one element has been removed"
-		self downHeapSingle: index].
-	^removed
+    "Remove the element at the given index and make sure the sorting order is okay.
+     Return the removed object"
+
+    | removed |
+
+    removed := array at: index.
+    array at: index put: (array at: tally).
+    array at: tally put: nil.
+    tally := tally - 1.
+    index > tally ifFalse:[
+        "Use #downHeapSingle: since only one element has been removed"
+        self downHeapSingle: index
+    ].
+    ^removed
 !
 
 setCollection: aCollection
@@ -327,13 +333,16 @@
 !Heap methodsFor:'removing'!
 
 remove: oldObject ifAbsent: aBlock
-	"Remove oldObject as one of the receiver's elements. If several of the 
-	elements are equal to oldObject, only one is removed. If no element is 
-	equal to oldObject, answer the result of evaluating anExceptionBlock. 
-	Otherwise, answer the argument, oldObject."
-	1 to: tally do:[:i| 
-		(array at: i) = oldObject ifTrue:[^self privateRemoveAt: i]].
-	^aBlock value
+    "Remove the first occurrence of oldObject as one of the receiver's elements. 
+     If several of the elements are equal to oldObject, only one is removed. 
+     If no element is equal to oldObject, answer the result of evaluating anExceptionBlock. 
+     Otherwise, answer the removed object
+     (which may be non-identical, but equal to oldObject)"
+     
+    1 to: tally do:[:i| 
+        (array at: i) = oldObject ifTrue:[^self privateRemoveAt: i]
+    ].
+    ^aBlock value
 !
 
 removeAt: index
@@ -347,6 +356,18 @@
 removeFirst
 	"Remove the first element from the receiver"
 	^self removeAt: 1
+!
+
+removeIdentical: oldObject ifAbsent: aBlock
+    "Remove the first occurrence of oldObject as one of the receiver's elements. 
+     If oldObject is present multiple times, only the first occurrence is removed. 
+     If oldObject is not present, answer the result of evaluating anExceptionBlock. 
+     Otherwise, answer the argument, oldObject."
+     
+    1 to: tally do:[:i| 
+        (array at: i) == oldObject ifTrue:[^self privateRemoveAt: i]
+    ].
+    ^aBlock value
 ! !
 
 !Heap methodsFor:'testing'!
@@ -378,10 +399,10 @@
 !Heap class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Heap.st,v 1.6 2013-06-25 11:23:43 cg Exp $'
+    ^ '$Header$'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/Heap.st,v 1.6 2013-06-25 11:23:43 cg Exp $'
+    ^ '$Header$'
 ! !