Queue.st
changeset 1497 5db4d4288c2c
parent 1494 8f617564556b
child 1918 38fa135c92ef
--- a/Queue.st	Tue Nov 23 22:20:55 2004 +0100
+++ b/Queue.st	Wed Nov 24 10:31:46 2004 +0100
@@ -197,7 +197,7 @@
     pos := pos + 1.
     pos > sz ifTrue:[pos := 1].
     writePosition := pos.
-    tally := tally + 1
+    tally := tally + 1.
 !
 
 nextPutAll:aCollection
@@ -245,29 +245,25 @@
 
 removeIdentical:anElement ifAbsent:exceptionalValue
     |rPos "{ Class: SmallInteger }"
-     endPos "{ Class: SmallInteger }" 
      wPos "{ Class: SmallInteger }"
      countRemoved
      el sz|
 
-    (tally == 0) ifTrue:[^ exceptionalValue value].
+    
+    (tally == 0) ifTrue:[
+        ^ exceptionalValue value
+    ].
     sz := contentsArray size.
 
     rPos := wPos := readPosition.
-    endPos := writePosition.
 
     countRemoved := 0.
-    tally timesRepeat:[
+
+    1 to:tally do:[:index|
         el := contentsArray at:rPos.
         el == anElement ifTrue:[
             countRemoved := countRemoved + 1.
-            rPos == readPosition ifTrue:[
-                readPosition == sz ifTrue:[
-                    readPosition := 1.
-                ] ifFalse:[
-                    readPosition := readPosition + 1.
-                ].
-            ].
+            contentsArray at:wPos put:nil.
         ] ifFalse:[
             rPos ~~ wPos ifTrue:[
                 contentsArray at:wPos put:el.
@@ -288,8 +284,9 @@
         ^ exceptionalValue value
     ].
 
-    wPos := wPos - 1.
-    wPos == 0 ifTrue:[ wPos := sz].
+    tally = countRemoved ifTrue:[
+        wPos := readPosition.
+    ].
     writePosition := wPos.
     tally := tally - countRemoved.
     ^ anElement
@@ -305,7 +302,50 @@
      q nextPut:12.
      q next.
      q removeIdentical:5.     
+     q
+
+     |q|
+
+     q := Queue new:10.
+     q nextPut:1; nextPut:2; nextPutAll:(3 to:8).
+     self assert:(q next == 1).
+     self assert:(q next == 2).
+     q removeIdentical:5.
+     self assert:(q next == 3).
+     self assert:(q next == 4).
+     self assert:(q next == 6).
+     self assert:(q next == 7).
+     self assert:(q next == 8).
+     self assert:(q isEmpty).
      q       
+
+     |q|
+
+     q := Queue new:10.
+     q nextPut:1; nextPut:2.
+     self assert:(q next == 1).
+     q removeIdentical:2.
+     self assert:(q isEmpty).
+     q nextPut:3.
+     self assert:(q isEmpty not).
+     self assert:(q next == 3).
+     self assert:(q isEmpty).
+
+     |q|
+
+     q := Queue new:10.
+     q nextPut:1; nextPut:2; nextPut:3.
+     self assert:(q next == 1).
+     q removeIdentical:3.
+     self assert:(q isEmpty not).
+     q nextPut:4.
+     q removeIdentical:4.
+     q nextPut:5.
+     self assert:(q isEmpty not).
+     self assert:(q next == 2).
+     self assert:(q next == 5).
+     self assert:(q isEmpty).
+
     "
 !
 
@@ -415,5 +455,5 @@
 !Queue class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.29 2004-11-19 10:04:51 penk Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.30 2004-11-24 09:31:46 penk Exp $'
 ! !