author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:42:15 +0200 | |
changeset 3903 | 5730e66fd49c |
parent 3448 | d51b708eb5d7 |
child 4035 | c6509f2ef728 |
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 |
" |
|
3448 | 39 |
Queues provide a simple implementation of a queue, |
1245 | 40 |
where elements are added at one end and removed at the other. |
3448 | 41 |
|
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:] |
3448 | 55 |
All of queue's functionality is also provided by the OrderedCollection (OC) |
587 | 56 |
class; OC could easily simulate a queue (using #addLast: / #removeFirst). |
3448 | 57 |
The reason for providing Queue is not any speed advantage |
58 |
(actually, OC seems to be even a tiny bit faster). |
|
587 | 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 |
||
2361 | 162 |
!Queue class methodsFor:'defaults'! |
163 |
||
164 |
defaultSize |
|
165 |
^ 50 |
|
166 |
! ! |
|
167 |
||
5 | 168 |
!Queue methodsFor:'accessing'! |
169 |
||
2163 | 170 |
at:index |
171 |
"return an element from the queue - indexing starts at 1 with the element |
|
172 |
which would next be fetched" |
|
173 |
||
174 |
(index between:1 and:tally) ifFalse:[ self subscriptBoundsError:index ]. |
|
175 |
^ contentsArray at:((readPosition+index-1-1) \\ contentsArray size)+1. |
|
176 |
||
177 |
" |
|
178 |
|q| |
|
179 |
||
180 |
q := Queue new:10. |
|
181 |
(1 to:5) do:[:i | q add:i]. |
|
182 |
(6 to:100) do:[:i | q removeFirst. q add:i ]. |
|
2164 | 183 |
self assert:(q at:1) = 96. |
184 |
self assert:(q at:2) = 97. |
|
185 |
self assert:(q at:3) = 98. |
|
186 |
self assert:(q at:4) = 99. |
|
187 |
self assert:(q at:5) = 100. |
|
188 |
self should:[ q at:6 ] raise:Error. |
|
2163 | 189 |
" |
190 |
! |
|
191 |
||
5 | 192 |
next |
39 | 193 |
"return the next value in the queue; |
194 |
Return nil, if the queue is empty" |
|
5 | 195 |
|
3432 | 196 |
^ self nextOrNil |
197 |
! |
|
198 |
||
199 |
nextOrNil |
|
200 |
"return the next value in the queue; |
|
201 |
Return nil, if the queue is empty" |
|
202 |
||
47 | 203 |
|value pos "{ Class: SmallInteger }"| |
5 | 204 |
|
205 |
(tally == 0) ifTrue:[^ nil]. |
|
47 | 206 |
|
207 |
pos := readPosition. |
|
208 |
||
209 |
value := contentsArray at:pos. |
|
647 | 210 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
47 | 211 |
pos := pos + 1. |
212 |
pos > contentsArray size ifTrue:[pos := 1]. |
|
213 |
readPosition := pos. |
|
5 | 214 |
tally := tally - 1. |
215 |
^ value |
|
216 |
! |
|
217 |
||
218 |
nextPut:anObject |
|
219 |
"enter anObject into the queue - if the queue is full, report an error" |
|
220 |
||
47 | 221 |
|sz pos "{ Class: SmallInteger }" | |
222 |
||
223 |
sz := contentsArray size. |
|
224 |
pos := writePosition. |
|
225 |
||
226 |
(tally == sz) ifTrue:[ |
|
786 | 227 |
self error:'queue is full' mayProceed:true. |
228 |
^ self |
|
5 | 229 |
]. |
47 | 230 |
|
231 |
contentsArray at:pos put:anObject. |
|
232 |
pos := pos + 1. |
|
233 |
pos > sz ifTrue:[pos := 1]. |
|
234 |
writePosition := pos. |
|
1497 | 235 |
tally := tally + 1. |
39 | 236 |
! |
237 |
||
238 |
nextPutAll:aCollection |
|
239 |
"enter all elements from aCollection into the queue." |
|
240 |
||
241 |
aCollection do:[:element | self nextPut:element] |
|
122 | 242 |
! |
243 |
||
1121 | 244 |
nextPutFirst:anObject |
245 |
|sz pos "{ Class: SmallInteger }" | |
|
246 |
||
247 |
tally == 0 ifTrue:[ |
|
248 |
self nextPut:anObject. |
|
249 |
^ self |
|
250 |
]. |
|
251 |
||
252 |
sz := contentsArray size. |
|
253 |
(tally == sz) ifTrue:[ |
|
254 |
self error:'queue is full' mayProceed:true. |
|
255 |
^ self |
|
256 |
]. |
|
257 |
pos := readPosition - 1. |
|
258 |
pos < 1 ifTrue:[pos := sz]. |
|
259 |
contentsArray at:pos put:anObject. |
|
260 |
readPosition := pos. |
|
261 |
||
262 |
tally := tally + 1 |
|
263 |
! |
|
264 |
||
122 | 265 |
peek |
266 |
"return the next value in the queue without removing it. |
|
267 |
If the queue is empty, return nil." |
|
268 |
||
269 |
(tally == 0) ifTrue:[^ nil]. |
|
270 |
^ contentsArray at:readPosition. |
|
396 | 271 |
! |
272 |
||
3432 | 273 |
peekOrNil |
274 |
"return the next value in the queue without removing it. |
|
275 |
If the queue is empty, return nil." |
|
276 |
||
277 |
(tally == 0) ifTrue:[^ nil]. |
|
278 |
^ contentsArray at:readPosition. |
|
279 |
! |
|
280 |
||
647 | 281 |
removeAll |
742 | 282 |
"remove all elements in the queue; return the receiver" |
647 | 283 |
|
284 |
tally := 0. |
|
285 |
readPosition := writePosition := 1. |
|
286 |
contentsArray atAllPut:nil "/ to help the garbage collector |
|
287 |
! |
|
288 |
||
1494 | 289 |
removeIdentical:anElement ifAbsent:exceptionalValue |
290 |
|rPos "{ Class: SmallInteger }" |
|
291 |
wPos "{ Class: SmallInteger }" |
|
292 |
countRemoved |
|
293 |
el sz| |
|
294 |
||
1497 | 295 |
|
296 |
(tally == 0) ifTrue:[ |
|
297 |
^ exceptionalValue value |
|
298 |
]. |
|
1494 | 299 |
sz := contentsArray size. |
300 |
||
301 |
rPos := wPos := readPosition. |
|
302 |
||
303 |
countRemoved := 0. |
|
1497 | 304 |
|
305 |
1 to:tally do:[:index| |
|
1494 | 306 |
el := contentsArray at:rPos. |
307 |
el == anElement ifTrue:[ |
|
308 |
countRemoved := countRemoved + 1. |
|
1497 | 309 |
contentsArray at:wPos put:nil. |
1494 | 310 |
] ifFalse:[ |
311 |
rPos ~~ wPos ifTrue:[ |
|
312 |
contentsArray at:wPos put:el. |
|
313 |
]. |
|
314 |
wPos == sz ifTrue:[ |
|
315 |
wPos := 1. |
|
316 |
] ifFalse:[ |
|
317 |
wPos := wPos + 1. |
|
318 |
]. |
|
319 |
]. |
|
320 |
rPos == sz ifTrue:[ |
|
321 |
rPos := 1. |
|
322 |
] ifFalse:[ |
|
323 |
rPos := rPos + 1. |
|
324 |
]. |
|
325 |
]. |
|
326 |
countRemoved == 0 ifTrue:[ |
|
327 |
^ exceptionalValue value |
|
328 |
]. |
|
329 |
||
1497 | 330 |
tally = countRemoved ifTrue:[ |
331 |
wPos := readPosition. |
|
332 |
]. |
|
1494 | 333 |
writePosition := wPos. |
334 |
tally := tally - countRemoved. |
|
335 |
^ anElement |
|
336 |
||
337 |
" |
|
338 |
|q| |
|
339 |
||
340 |
q := Queue new:10. |
|
341 |
q nextPut:1; nextPut:2; nextPutAll:(3 to:10). |
|
342 |
q next. |
|
343 |
q nextPut:11. |
|
344 |
q next. |
|
345 |
q nextPut:12. |
|
346 |
q next. |
|
347 |
q removeIdentical:5. |
|
1497 | 348 |
q |
349 |
||
350 |
|q| |
|
351 |
||
352 |
q := Queue new:10. |
|
353 |
q nextPut:1; nextPut:2; nextPutAll:(3 to:8). |
|
354 |
self assert:(q next == 1). |
|
355 |
self assert:(q next == 2). |
|
356 |
q removeIdentical:5. |
|
357 |
self assert:(q next == 3). |
|
358 |
self assert:(q next == 4). |
|
359 |
self assert:(q next == 6). |
|
360 |
self assert:(q next == 7). |
|
361 |
self assert:(q next == 8). |
|
362 |
self assert:(q isEmpty). |
|
1494 | 363 |
q |
1497 | 364 |
|
365 |
|q| |
|
366 |
||
367 |
q := Queue new:10. |
|
368 |
q nextPut:1; nextPut:2. |
|
369 |
self assert:(q next == 1). |
|
370 |
q removeIdentical:2. |
|
371 |
self assert:(q isEmpty). |
|
372 |
q nextPut:3. |
|
373 |
self assert:(q isEmpty not). |
|
374 |
self assert:(q next == 3). |
|
375 |
self assert:(q isEmpty). |
|
376 |
||
377 |
|q| |
|
378 |
||
379 |
q := Queue new:10. |
|
380 |
q nextPut:1; nextPut:2; nextPut:3. |
|
381 |
self assert:(q next == 1). |
|
382 |
q removeIdentical:3. |
|
383 |
self assert:(q isEmpty not). |
|
384 |
q nextPut:4. |
|
385 |
q removeIdentical:4. |
|
386 |
q nextPut:5. |
|
387 |
self assert:(q isEmpty not). |
|
388 |
self assert:(q next == 2). |
|
389 |
self assert:(q next == 5). |
|
390 |
self assert:(q isEmpty). |
|
391 |
||
1494 | 392 |
" |
393 |
! |
|
394 |
||
396 | 395 |
removeLast |
396 |
"return the last value in the queue; |
|
397 |
Return nil, if the queue is empty" |
|
398 |
||
399 |
|value pos "{ Class: SmallInteger }"| |
|
400 |
||
401 |
(tally == 0) ifTrue:[^ nil]. |
|
402 |
||
403 |
pos := writePosition. |
|
404 |
pos == 1 ifTrue:[ |
|
405 |
pos := contentsArray size |
|
406 |
] ifFalse:[ |
|
407 |
pos := pos - 1. |
|
408 |
]. |
|
409 |
||
410 |
value := contentsArray at:pos. |
|
647 | 411 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
396 | 412 |
writePosition := pos. |
413 |
tally := tally - 1. |
|
414 |
^ value |
|
415 |
||
416 |
"Created: 22.6.1996 / 18:49:41 / cg" |
|
39 | 417 |
! ! |
418 |
||
903 | 419 |
!Queue methodsFor:'accessing-protocol compatibility'! |
686
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
420 |
|
1918
38fa135c92ef
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
1497
diff
changeset
|
421 |
add:someObject |
904 | 422 |
"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
|
423 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
424 |
self nextPut:someObject. |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
425 |
^ someObject |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
426 |
! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
427 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
428 |
removeFirst |
904 | 429 |
"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
|
430 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
431 |
^ self next |
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 |
"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
|
434 |
! ! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
435 |
|
50 | 436 |
!Queue methodsFor:'enumerating'! |
39 | 437 |
|
438 |
do:aBlock |
|
439 |
"evaluate the argument, aBlock for each element in the queue" |
|
440 |
||
582 | 441 |
|n "{ Class: SmallInteger }" |
442 |
pos "{ Class: SmallInteger }"| |
|
39 | 443 |
|
444 |
pos := readPosition. |
|
582 | 445 |
n := tally. |
446 |
1 to:n do:[:i | |
|
447 |
aBlock value:(contentsArray at:pos). |
|
448 |
pos := pos + 1. |
|
449 |
pos > contentsArray size ifTrue:[ |
|
450 |
pos := 1 |
|
451 |
] |
|
39 | 452 |
] |
582 | 453 |
|
454 |
"Modified: 18.10.1997 / 16:24:01 / cg" |
|
5 | 455 |
! ! |
456 |
||
122 | 457 |
!Queue methodsFor:'initialization'! |
458 |
||
459 |
init:size |
|
460 |
"initialize the receiver for size entries" |
|
461 |
||
462 |
contentsArray := Array new:size. |
|
463 |
readPosition := writePosition := 1. |
|
464 |
tally := 0. |
|
465 |
! ! |
|
466 |
||
5 | 467 |
!Queue methodsFor:'queries'! |
468 |
||
122 | 469 |
capacity |
470 |
"return the number of elements the queue can hold" |
|
471 |
||
472 |
^ contentsArray size |
|
473 |
! |
|
474 |
||
904 | 475 |
size |
476 |
"return the number of elements in the queue" |
|
477 |
||
478 |
^ tally |
|
2148
5fe0c7beef0c
Define #species, so that #collect: and #select return OrderedCollection
Stefan Vogel <sv@exept.de>
parents:
1918
diff
changeset
|
479 |
! |
5fe0c7beef0c
Define #species, so that #collect: and #select return OrderedCollection
Stefan Vogel <sv@exept.de>
parents:
1918
diff
changeset
|
480 |
|
5fe0c7beef0c
Define #species, so that #collect: and #select return OrderedCollection
Stefan Vogel <sv@exept.de>
parents:
1918
diff
changeset
|
481 |
species |
5fe0c7beef0c
Define #species, so that #collect: and #select return OrderedCollection
Stefan Vogel <sv@exept.de>
parents:
1918
diff
changeset
|
482 |
"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
|
483 |
|
5fe0c7beef0c
Define #species, so that #collect: and #select return OrderedCollection
Stefan Vogel <sv@exept.de>
parents:
1918
diff
changeset
|
484 |
^ OrderedCollection |
904 | 485 |
! ! |
486 |
||
487 |
!Queue methodsFor:'testing'! |
|
488 |
||
30 | 489 |
isEmpty |
490 |
"return true, if there are no elements in the queue" |
|
491 |
||
492 |
^ tally == 0 |
|
493 |
! |
|
494 |
||
3021 | 495 |
isFixedSize |
496 |
"return true if the receiver cannot grow" |
|
497 |
||
498 |
^ false |
|
499 |
! |
|
500 |
||
30 | 501 |
isFull |
502 |
"return true, if the queue is full i.e. if writing is not |
|
503 |
possible" |
|
504 |
||
505 |
^ tally == contentsArray size |
|
122 | 506 |
! ! |
81 | 507 |
|
582 | 508 |
!Queue class methodsFor:'documentation'! |
131 | 509 |
|
510 |
version |
|
3448 | 511 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.38 2014-11-26 08:50:35 cg Exp $' |
2361 | 512 |
! |
513 |
||
514 |
version_CVS |
|
3448 | 515 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.38 2014-11-26 08:50:35 cg Exp $' |
131 | 516 |
! ! |
3021 | 517 |