Queue.st
changeset 1494 8f617564556b
parent 1245 99e070f7c351
child 1497 5db4d4288c2c
--- a/Queue.st	Thu Nov 18 13:03:24 2004 +0100
+++ b/Queue.st	Fri Nov 19 11:04:55 2004 +0100
@@ -243,6 +243,72 @@
     contentsArray atAllPut:nil          "/ to help the garbage collector
 !
 
+removeIdentical:anElement ifAbsent:exceptionalValue
+    |rPos "{ Class: SmallInteger }"
+     endPos "{ Class: SmallInteger }" 
+     wPos "{ Class: SmallInteger }"
+     countRemoved
+     el sz|
+
+    (tally == 0) ifTrue:[^ exceptionalValue value].
+    sz := contentsArray size.
+
+    rPos := wPos := readPosition.
+    endPos := writePosition.
+
+    countRemoved := 0.
+    tally timesRepeat:[
+        el := contentsArray at:rPos.
+        el == anElement ifTrue:[
+            countRemoved := countRemoved + 1.
+            rPos == readPosition ifTrue:[
+                readPosition == sz ifTrue:[
+                    readPosition := 1.
+                ] ifFalse:[
+                    readPosition := readPosition + 1.
+                ].
+            ].
+        ] ifFalse:[
+            rPos ~~ wPos ifTrue:[
+                contentsArray at:wPos put:el.
+            ].
+            wPos == sz ifTrue:[
+                wPos := 1.
+            ] ifFalse:[
+                wPos := wPos + 1.
+            ].
+        ].    
+        rPos == sz ifTrue:[
+            rPos := 1.
+        ] ifFalse:[
+            rPos := rPos + 1.
+        ].
+    ].
+    countRemoved == 0 ifTrue:[
+        ^ exceptionalValue value
+    ].
+
+    wPos := wPos - 1.
+    wPos == 0 ifTrue:[ wPos := sz].
+    writePosition := wPos.
+    tally := tally - countRemoved.
+    ^ anElement
+
+    "
+     |q|
+
+     q := Queue new:10.
+     q nextPut:1; nextPut:2; nextPutAll:(3 to:10).
+     q next.
+     q nextPut:11.
+     q next.
+     q nextPut:12.
+     q next.
+     q removeIdentical:5.     
+     q       
+    "
+!
+
 removeLast
     "return the last value in the queue; 
      Return nil, if the queue is empty"
@@ -349,5 +415,5 @@
 !Queue class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.28 2003-06-11 08:37:18 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.29 2004-11-19 10:04:51 penk Exp $'
 ! !