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