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