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