author | Claus Gittinger <cg@exept.de> |
Wed, 18 Jul 2007 14:16:40 +0200 | |
changeset 1891 | af37298a4b38 |
parent 1497 | 5db4d4288c2c |
child 1918 | 38fa135c92ef |
permissions | -rw-r--r-- |
5 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
47 | 3 |
All Rights Reserved |
5 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
903 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
30 | 15 |
Collection subclass:#Queue |
254 | 16 |
instanceVariableNames:'contentsArray readPosition writePosition tally' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Ordered' |
|
5 | 20 |
! |
21 |
||
582 | 22 |
!Queue class methodsFor:'documentation'! |
30 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
47 | 27 |
All Rights Reserved |
5 | 28 |
|
30 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
1245 | 40 |
Queues provides a simple implementation of a queue, |
41 |
where elements are added at one end and removed at the other. |
|
74 | 42 |
Access protocol is somewhat like a streams protocol, i.e. access |
43 |
is by #nextPut: and #next. |
|
44 |
The queue is created with a size argument, defining how many elements |
|
45 |
are to be stored. It will report an error if the queue ever becomes full |
|
46 |
and another element is to be added. Likewise, it will report an error |
|
47 |
if its empty and an element is to be removed. |
|
48 |
||
587 | 49 |
It is NOT safe when two processes access Queues simultanously, |
74 | 50 |
since accesses to the internals are not protected against process-switches. |
587 | 51 |
See SharedQueue for a class which IS safe with processes and blocks |
74 | 52 |
on write when full or on read when empty. |
254 | 53 |
|
587 | 54 |
[Implementation note:] |
55 |
All of queues functionality is also provided by the OrderedCollection (OC) |
|
56 |
class; OC could easily simulate a queue (using #addLast: / #removeFirst). |
|
57 |
The reason for providing Queue is not any speed advantage (actually, |
|
58 |
OC seems to be a tiny bit faster). |
|
59 |
The point is that an implementation of SharedQueue as a subclass of OC |
|
60 |
would require that many OC methods had to be blocked and/or redefined in |
|
61 |
such a subclass, to care for simultaneous access. |
|
62 |
Since queue implements a much more lightweight protocol, |
|
63 |
the sharedQueue implementation is much cleaner when based on a more |
|
64 |
lightweight Queue class. |
|
65 |
||
254 | 66 |
[author:] |
67 |
Claus Gittinger |
|
30 | 68 |
" |
587 | 69 |
! |
70 |
||
71 |
examples |
|
72 |
" |
|
73 |
adding at one end, removing at the other ... |
|
74 |
[exBegin] |
|
75 |
|q element | |
|
76 |
||
77 |
q := Queue new:10. |
|
78 |
1 to:5 do:[:i | |
|
79 |
Transcript showCR:('adding ' , i printString). |
|
80 |
q nextPut:i |
|
81 |
]. |
|
82 |
||
83 |
[q notEmpty] whileTrue:[ |
|
84 |
element := q next. |
|
85 |
Transcript showCR:('removed ' , element printString). |
|
86 |
]. |
|
87 |
[exEnd] |
|
88 |
||
89 |
||
90 |
||
91 |
timing; Queue vs. OrderedCollection |
|
92 |
[exBegin] |
|
93 |
|q oc tQueue tOC | |
|
94 |
||
95 |
q := Queue new:100. |
|
96 |
tQueue := Time millisecondsToRun:[ |
|
97 |
1000 timesRepeat:[ |
|
98 |
1 to:100 do:[:i | |
|
99 |
q nextPut:i |
|
100 |
]. |
|
101 |
[q isEmpty] whileFalse:[ |
|
102 |
q next |
|
103 |
]. |
|
104 |
] |
|
105 |
]. |
|
106 |
||
107 |
oc := OrderedCollection new:100. |
|
108 |
tOC := Time millisecondsToRun:[ |
|
109 |
1000 timesRepeat:[ |
|
110 |
1 to:100 do:[:i | |
|
111 |
oc addLast:i |
|
112 |
]. |
|
113 |
[oc isEmpty] whileFalse:[ |
|
114 |
oc removeFirst |
|
115 |
]. |
|
116 |
] |
|
117 |
]. |
|
118 |
Transcript showCR:('queue time: ' , tQueue printString , ' ms'). |
|
119 |
Transcript showCR:('oc time : ' , tOC printString , ' ms'). |
|
120 |
[exEnd] |
|
121 |
||
122 |
||
123 |
" |
|
30 | 124 |
! ! |
5 | 125 |
|
582 | 126 |
!Queue class methodsFor:'instance creation'! |
5 | 127 |
|
122 | 128 |
new |
129 |
"return a new queue with space for some elements" |
|
130 |
||
131 |
^ self new:50 |
|
132 |
! |
|
133 |
||
5 | 134 |
new:size |
135 |
"return a new queue with space for size elements" |
|
136 |
||
137 |
^ super new init:size |
|
138 |
||
139 |
" |
|
140 |
|q| |
|
141 |
||
142 |
q := Queue new. |
|
39 | 143 |
(1 to:5) do:[:i | q nextPut:i]. |
144 |
Transcript show:(q next); space. |
|
145 |
Transcript show:(q next); space. |
|
146 |
Transcript show:(q next); space. |
|
147 |
Transcript show:(q next); space. |
|
148 |
q nextPutAll:(6 to:10). |
|
149 |
Transcript show:(q next); space. |
|
150 |
Transcript show:(q next); space. |
|
151 |
Transcript show:(q next); space. |
|
152 |
Transcript show:(q next); space. |
|
153 |
Transcript show:(q next); space. |
|
154 |
Transcript show:(q next); space. |
|
155 |
Transcript show:(q next); space. |
|
156 |
Transcript show:(q next); space. |
|
157 |
Transcript show:(q next); space. |
|
350 | 158 |
Transcript showCR:(q next). |
5 | 159 |
" |
160 |
! ! |
|
161 |
||
162 |
!Queue methodsFor:'accessing'! |
|
163 |
||
164 |
next |
|
39 | 165 |
"return the next value in the queue; |
166 |
Return nil, if the queue is empty" |
|
5 | 167 |
|
47 | 168 |
|value pos "{ Class: SmallInteger }"| |
5 | 169 |
|
170 |
(tally == 0) ifTrue:[^ nil]. |
|
47 | 171 |
|
172 |
pos := readPosition. |
|
173 |
||
174 |
value := contentsArray at:pos. |
|
647 | 175 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
47 | 176 |
pos := pos + 1. |
177 |
pos > contentsArray size ifTrue:[pos := 1]. |
|
178 |
readPosition := pos. |
|
5 | 179 |
tally := tally - 1. |
180 |
^ value |
|
181 |
! |
|
182 |
||
183 |
nextPut:anObject |
|
184 |
"enter anObject into the queue - if the queue is full, report an error" |
|
185 |
||
47 | 186 |
|sz pos "{ Class: SmallInteger }" | |
187 |
||
188 |
sz := contentsArray size. |
|
189 |
pos := writePosition. |
|
190 |
||
191 |
(tally == sz) ifTrue:[ |
|
786 | 192 |
self error:'queue is full' mayProceed:true. |
193 |
^ self |
|
5 | 194 |
]. |
47 | 195 |
|
196 |
contentsArray at:pos put:anObject. |
|
197 |
pos := pos + 1. |
|
198 |
pos > sz ifTrue:[pos := 1]. |
|
199 |
writePosition := pos. |
|
1497 | 200 |
tally := tally + 1. |
39 | 201 |
! |
202 |
||
203 |
nextPutAll:aCollection |
|
204 |
"enter all elements from aCollection into the queue." |
|
205 |
||
206 |
aCollection do:[:element | self nextPut:element] |
|
122 | 207 |
! |
208 |
||
1121 | 209 |
nextPutFirst:anObject |
210 |
|sz pos "{ Class: SmallInteger }" | |
|
211 |
||
212 |
tally == 0 ifTrue:[ |
|
213 |
self nextPut:anObject. |
|
214 |
^ self |
|
215 |
]. |
|
216 |
||
217 |
sz := contentsArray size. |
|
218 |
(tally == sz) ifTrue:[ |
|
219 |
self error:'queue is full' mayProceed:true. |
|
220 |
^ self |
|
221 |
]. |
|
222 |
pos := readPosition - 1. |
|
223 |
pos < 1 ifTrue:[pos := sz]. |
|
224 |
contentsArray at:pos put:anObject. |
|
225 |
readPosition := pos. |
|
226 |
||
227 |
tally := tally + 1 |
|
228 |
! |
|
229 |
||
122 | 230 |
peek |
231 |
"return the next value in the queue without removing it. |
|
232 |
If the queue is empty, return nil." |
|
233 |
||
234 |
(tally == 0) ifTrue:[^ nil]. |
|
235 |
^ contentsArray at:readPosition. |
|
396 | 236 |
! |
237 |
||
647 | 238 |
removeAll |
742 | 239 |
"remove all elements in the queue; return the receiver" |
647 | 240 |
|
241 |
tally := 0. |
|
242 |
readPosition := writePosition := 1. |
|
243 |
contentsArray atAllPut:nil "/ to help the garbage collector |
|
244 |
! |
|
245 |
||
1494 | 246 |
removeIdentical:anElement ifAbsent:exceptionalValue |
247 |
|rPos "{ Class: SmallInteger }" |
|
248 |
wPos "{ Class: SmallInteger }" |
|
249 |
countRemoved |
|
250 |
el sz| |
|
251 |
||
1497 | 252 |
|
253 |
(tally == 0) ifTrue:[ |
|
254 |
^ exceptionalValue value |
|
255 |
]. |
|
1494 | 256 |
sz := contentsArray size. |
257 |
||
258 |
rPos := wPos := readPosition. |
|
259 |
||
260 |
countRemoved := 0. |
|
1497 | 261 |
|
262 |
1 to:tally do:[:index| |
|
1494 | 263 |
el := contentsArray at:rPos. |
264 |
el == anElement ifTrue:[ |
|
265 |
countRemoved := countRemoved + 1. |
|
1497 | 266 |
contentsArray at:wPos put:nil. |
1494 | 267 |
] ifFalse:[ |
268 |
rPos ~~ wPos ifTrue:[ |
|
269 |
contentsArray at:wPos put:el. |
|
270 |
]. |
|
271 |
wPos == sz ifTrue:[ |
|
272 |
wPos := 1. |
|
273 |
] ifFalse:[ |
|
274 |
wPos := wPos + 1. |
|
275 |
]. |
|
276 |
]. |
|
277 |
rPos == sz ifTrue:[ |
|
278 |
rPos := 1. |
|
279 |
] ifFalse:[ |
|
280 |
rPos := rPos + 1. |
|
281 |
]. |
|
282 |
]. |
|
283 |
countRemoved == 0 ifTrue:[ |
|
284 |
^ exceptionalValue value |
|
285 |
]. |
|
286 |
||
1497 | 287 |
tally = countRemoved ifTrue:[ |
288 |
wPos := readPosition. |
|
289 |
]. |
|
1494 | 290 |
writePosition := wPos. |
291 |
tally := tally - countRemoved. |
|
292 |
^ anElement |
|
293 |
||
294 |
" |
|
295 |
|q| |
|
296 |
||
297 |
q := Queue new:10. |
|
298 |
q nextPut:1; nextPut:2; nextPutAll:(3 to:10). |
|
299 |
q next. |
|
300 |
q nextPut:11. |
|
301 |
q next. |
|
302 |
q nextPut:12. |
|
303 |
q next. |
|
304 |
q removeIdentical:5. |
|
1497 | 305 |
q |
306 |
||
307 |
|q| |
|
308 |
||
309 |
q := Queue new:10. |
|
310 |
q nextPut:1; nextPut:2; nextPutAll:(3 to:8). |
|
311 |
self assert:(q next == 1). |
|
312 |
self assert:(q next == 2). |
|
313 |
q removeIdentical:5. |
|
314 |
self assert:(q next == 3). |
|
315 |
self assert:(q next == 4). |
|
316 |
self assert:(q next == 6). |
|
317 |
self assert:(q next == 7). |
|
318 |
self assert:(q next == 8). |
|
319 |
self assert:(q isEmpty). |
|
1494 | 320 |
q |
1497 | 321 |
|
322 |
|q| |
|
323 |
||
324 |
q := Queue new:10. |
|
325 |
q nextPut:1; nextPut:2. |
|
326 |
self assert:(q next == 1). |
|
327 |
q removeIdentical:2. |
|
328 |
self assert:(q isEmpty). |
|
329 |
q nextPut:3. |
|
330 |
self assert:(q isEmpty not). |
|
331 |
self assert:(q next == 3). |
|
332 |
self assert:(q isEmpty). |
|
333 |
||
334 |
|q| |
|
335 |
||
336 |
q := Queue new:10. |
|
337 |
q nextPut:1; nextPut:2; nextPut:3. |
|
338 |
self assert:(q next == 1). |
|
339 |
q removeIdentical:3. |
|
340 |
self assert:(q isEmpty not). |
|
341 |
q nextPut:4. |
|
342 |
q removeIdentical:4. |
|
343 |
q nextPut:5. |
|
344 |
self assert:(q isEmpty not). |
|
345 |
self assert:(q next == 2). |
|
346 |
self assert:(q next == 5). |
|
347 |
self assert:(q isEmpty). |
|
348 |
||
1494 | 349 |
" |
350 |
! |
|
351 |
||
396 | 352 |
removeLast |
353 |
"return the last value in the queue; |
|
354 |
Return nil, if the queue is empty" |
|
355 |
||
356 |
|value pos "{ Class: SmallInteger }"| |
|
357 |
||
358 |
(tally == 0) ifTrue:[^ nil]. |
|
359 |
||
360 |
pos := writePosition. |
|
361 |
pos == 1 ifTrue:[ |
|
362 |
pos := contentsArray size |
|
363 |
] ifFalse:[ |
|
364 |
pos := pos - 1. |
|
365 |
]. |
|
366 |
||
367 |
value := contentsArray at:pos. |
|
647 | 368 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
396 | 369 |
writePosition := pos. |
370 |
tally := tally - 1. |
|
371 |
^ value |
|
372 |
||
373 |
"Created: 22.6.1996 / 18:49:41 / cg" |
|
39 | 374 |
! ! |
375 |
||
903 | 376 |
!Queue methodsFor:'accessing-protocol compatibility'! |
686
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
377 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
378 |
addLast:someObject |
904 | 379 |
"same as #nextPut: - for protocol compatibility with other collections" |
686
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
380 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
381 |
self nextPut:someObject. |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
382 |
^ someObject |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
383 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
384 |
"Created: / 27.8.1998 / 11:15:29 / cg" |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
385 |
! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
386 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
387 |
removeFirst |
904 | 388 |
"same as #next - for protocol compatibility with other collections" |
686
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
389 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
390 |
^ self next |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
391 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
392 |
"Created: / 27.8.1998 / 11:15:48 / cg" |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
393 |
! ! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
394 |
|
50 | 395 |
!Queue methodsFor:'enumerating'! |
39 | 396 |
|
397 |
do:aBlock |
|
398 |
"evaluate the argument, aBlock for each element in the queue" |
|
399 |
||
582 | 400 |
|n "{ Class: SmallInteger }" |
401 |
pos "{ Class: SmallInteger }"| |
|
39 | 402 |
|
403 |
pos := readPosition. |
|
582 | 404 |
n := tally. |
405 |
1 to:n do:[:i | |
|
406 |
aBlock value:(contentsArray at:pos). |
|
407 |
pos := pos + 1. |
|
408 |
pos > contentsArray size ifTrue:[ |
|
409 |
pos := 1 |
|
410 |
] |
|
39 | 411 |
] |
582 | 412 |
|
413 |
"Modified: 18.10.1997 / 16:24:01 / cg" |
|
5 | 414 |
! ! |
415 |
||
122 | 416 |
!Queue methodsFor:'initialization'! |
417 |
||
418 |
init:size |
|
419 |
"initialize the receiver for size entries" |
|
420 |
||
421 |
contentsArray := Array new:size. |
|
422 |
readPosition := writePosition := 1. |
|
423 |
tally := 0. |
|
424 |
! ! |
|
425 |
||
5 | 426 |
!Queue methodsFor:'queries'! |
427 |
||
122 | 428 |
capacity |
429 |
"return the number of elements the queue can hold" |
|
430 |
||
431 |
^ contentsArray size |
|
432 |
! |
|
433 |
||
904 | 434 |
size |
435 |
"return the number of elements in the queue" |
|
436 |
||
437 |
^ tally |
|
438 |
! ! |
|
439 |
||
440 |
!Queue methodsFor:'testing'! |
|
441 |
||
30 | 442 |
isEmpty |
443 |
"return true, if there are no elements in the queue" |
|
444 |
||
445 |
^ tally == 0 |
|
446 |
! |
|
447 |
||
448 |
isFull |
|
449 |
"return true, if the queue is full i.e. if writing is not |
|
450 |
possible" |
|
451 |
||
452 |
^ tally == contentsArray size |
|
122 | 453 |
! ! |
81 | 454 |
|
582 | 455 |
!Queue class methodsFor:'documentation'! |
131 | 456 |
|
457 |
version |
|
1497 | 458 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.30 2004-11-24 09:31:46 penk Exp $' |
131 | 459 |
! ! |