Queue.st
changeset 1494 8f617564556b
parent 1245 99e070f7c351
child 1497 5db4d4288c2c
equal deleted inserted replaced
1493:b4195d4908da 1494:8f617564556b
   241     tally := 0.
   241     tally := 0.
   242     readPosition := writePosition := 1.
   242     readPosition := writePosition := 1.
   243     contentsArray atAllPut:nil          "/ to help the garbage collector
   243     contentsArray atAllPut:nil          "/ to help the garbage collector
   244 !
   244 !
   245 
   245 
       
   246 removeIdentical:anElement ifAbsent:exceptionalValue
       
   247     |rPos "{ Class: SmallInteger }"
       
   248      endPos "{ Class: SmallInteger }" 
       
   249      wPos "{ Class: SmallInteger }"
       
   250      countRemoved
       
   251      el sz|
       
   252 
       
   253     (tally == 0) ifTrue:[^ exceptionalValue value].
       
   254     sz := contentsArray size.
       
   255 
       
   256     rPos := wPos := readPosition.
       
   257     endPos := writePosition.
       
   258 
       
   259     countRemoved := 0.
       
   260     tally timesRepeat:[
       
   261         el := contentsArray at:rPos.
       
   262         el == anElement ifTrue:[
       
   263             countRemoved := countRemoved + 1.
       
   264             rPos == readPosition ifTrue:[
       
   265                 readPosition == sz ifTrue:[
       
   266                     readPosition := 1.
       
   267                 ] ifFalse:[
       
   268                     readPosition := readPosition + 1.
       
   269                 ].
       
   270             ].
       
   271         ] ifFalse:[
       
   272             rPos ~~ wPos ifTrue:[
       
   273                 contentsArray at:wPos put:el.
       
   274             ].
       
   275             wPos == sz ifTrue:[
       
   276                 wPos := 1.
       
   277             ] ifFalse:[
       
   278                 wPos := wPos + 1.
       
   279             ].
       
   280         ].    
       
   281         rPos == sz ifTrue:[
       
   282             rPos := 1.
       
   283         ] ifFalse:[
       
   284             rPos := rPos + 1.
       
   285         ].
       
   286     ].
       
   287     countRemoved == 0 ifTrue:[
       
   288         ^ exceptionalValue value
       
   289     ].
       
   290 
       
   291     wPos := wPos - 1.
       
   292     wPos == 0 ifTrue:[ wPos := sz].
       
   293     writePosition := wPos.
       
   294     tally := tally - countRemoved.
       
   295     ^ anElement
       
   296 
       
   297     "
       
   298      |q|
       
   299 
       
   300      q := Queue new:10.
       
   301      q nextPut:1; nextPut:2; nextPutAll:(3 to:10).
       
   302      q next.
       
   303      q nextPut:11.
       
   304      q next.
       
   305      q nextPut:12.
       
   306      q next.
       
   307      q removeIdentical:5.     
       
   308      q       
       
   309     "
       
   310 !
       
   311 
   246 removeLast
   312 removeLast
   247     "return the last value in the queue; 
   313     "return the last value in the queue; 
   248      Return nil, if the queue is empty"
   314      Return nil, if the queue is empty"
   249 
   315 
   250     |value pos "{ Class: SmallInteger }"|
   316     |value pos "{ Class: SmallInteger }"|
   347 ! !
   413 ! !
   348 
   414 
   349 !Queue class methodsFor:'documentation'!
   415 !Queue class methodsFor:'documentation'!
   350 
   416 
   351 version
   417 version
   352     ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.28 2003-06-11 08:37:18 cg Exp $'
   418     ^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.29 2004-11-19 10:04:51 penk Exp $'
   353 ! !
   419 ! !