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