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 }"| |