author | Claus Gittinger <cg@exept.de> |
Wed, 11 Jun 2003 10:37:18 +0200 | |
changeset 1245 | 99e070f7c351 |
parent 1121 | e7136d381499 |
child 1494 | 8f617564556b |
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 |
" |
|
12 |
||
903 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
30 | 15 |
Collection subclass:#Queue |
254 | 16 |
instanceVariableNames:'contentsArray readPosition writePosition tally' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Ordered' |
|
5 | 20 |
! |
21 |
||
582 | 22 |
!Queue class methodsFor:'documentation'! |
30 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
47 | 27 |
All Rights Reserved |
5 | 28 |
|
30 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
1245 | 40 |
Queues provides a simple implementation of a queue, |
41 |
where elements are added at one end and removed at the other. |
|
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:] |
55 |
All of queues functionality is also provided by the OrderedCollection (OC) |
|
56 |
class; OC could easily simulate a queue (using #addLast: / #removeFirst). |
|
57 |
The reason for providing Queue is not any speed advantage (actually, |
|
58 |
OC seems to be a tiny bit faster). |
|
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 |
||
162 |
!Queue methodsFor:'accessing'! |
|
163 |
||
164 |
next |
|
39 | 165 |
"return the next value in the queue; |
166 |
Return nil, if the queue is empty" |
|
5 | 167 |
|
47 | 168 |
|value pos "{ Class: SmallInteger }"| |
5 | 169 |
|
170 |
(tally == 0) ifTrue:[^ nil]. |
|
47 | 171 |
|
172 |
pos := readPosition. |
|
173 |
||
174 |
value := contentsArray at:pos. |
|
647 | 175 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
47 | 176 |
pos := pos + 1. |
177 |
pos > contentsArray size ifTrue:[pos := 1]. |
|
178 |
readPosition := pos. |
|
5 | 179 |
tally := tally - 1. |
180 |
^ value |
|
181 |
! |
|
182 |
||
183 |
nextPut:anObject |
|
184 |
"enter anObject into the queue - if the queue is full, report an error" |
|
185 |
||
47 | 186 |
|sz pos "{ Class: SmallInteger }" | |
187 |
||
188 |
sz := contentsArray size. |
|
189 |
pos := writePosition. |
|
190 |
||
191 |
(tally == sz) ifTrue:[ |
|
786 | 192 |
self error:'queue is full' mayProceed:true. |
193 |
^ self |
|
5 | 194 |
]. |
47 | 195 |
|
196 |
contentsArray at:pos put:anObject. |
|
197 |
pos := pos + 1. |
|
198 |
pos > sz ifTrue:[pos := 1]. |
|
199 |
writePosition := pos. |
|
5 | 200 |
tally := tally + 1 |
39 | 201 |
! |
202 |
||
203 |
nextPutAll:aCollection |
|
204 |
"enter all elements from aCollection into the queue." |
|
205 |
||
206 |
aCollection do:[:element | self nextPut:element] |
|
122 | 207 |
! |
208 |
||
1121 | 209 |
nextPutFirst:anObject |
210 |
|sz pos "{ Class: SmallInteger }" | |
|
211 |
||
212 |
tally == 0 ifTrue:[ |
|
213 |
self nextPut:anObject. |
|
214 |
^ self |
|
215 |
]. |
|
216 |
||
217 |
sz := contentsArray size. |
|
218 |
(tally == sz) ifTrue:[ |
|
219 |
self error:'queue is full' mayProceed:true. |
|
220 |
^ self |
|
221 |
]. |
|
222 |
pos := readPosition - 1. |
|
223 |
pos < 1 ifTrue:[pos := sz]. |
|
224 |
contentsArray at:pos put:anObject. |
|
225 |
readPosition := pos. |
|
226 |
||
227 |
tally := tally + 1 |
|
228 |
! |
|
229 |
||
122 | 230 |
peek |
231 |
"return the next value in the queue without removing it. |
|
232 |
If the queue is empty, return nil." |
|
233 |
||
234 |
(tally == 0) ifTrue:[^ nil]. |
|
235 |
^ contentsArray at:readPosition. |
|
396 | 236 |
! |
237 |
||
647 | 238 |
removeAll |
742 | 239 |
"remove all elements in the queue; return the receiver" |
647 | 240 |
|
241 |
tally := 0. |
|
242 |
readPosition := writePosition := 1. |
|
243 |
contentsArray atAllPut:nil "/ to help the garbage collector |
|
244 |
! |
|
245 |
||
396 | 246 |
removeLast |
247 |
"return the last value in the queue; |
|
248 |
Return nil, if the queue is empty" |
|
249 |
||
250 |
|value pos "{ Class: SmallInteger }"| |
|
251 |
||
252 |
(tally == 0) ifTrue:[^ nil]. |
|
253 |
||
254 |
pos := writePosition. |
|
255 |
pos == 1 ifTrue:[ |
|
256 |
pos := contentsArray size |
|
257 |
] ifFalse:[ |
|
258 |
pos := pos - 1. |
|
259 |
]. |
|
260 |
||
261 |
value := contentsArray at:pos. |
|
647 | 262 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
396 | 263 |
writePosition := pos. |
264 |
tally := tally - 1. |
|
265 |
^ value |
|
266 |
||
267 |
"Created: 22.6.1996 / 18:49:41 / cg" |
|
39 | 268 |
! ! |
269 |
||
903 | 270 |
!Queue methodsFor:'accessing-protocol compatibility'! |
686
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
271 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
272 |
addLast:someObject |
904 | 273 |
"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
|
274 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
275 |
self nextPut:someObject. |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
276 |
^ someObject |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
277 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
278 |
"Created: / 27.8.1998 / 11:15:29 / cg" |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
279 |
! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
280 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
281 |
removeFirst |
904 | 282 |
"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
|
283 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
284 |
^ self next |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
285 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
286 |
"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
|
287 |
! ! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
288 |
|
50 | 289 |
!Queue methodsFor:'enumerating'! |
39 | 290 |
|
291 |
do:aBlock |
|
292 |
"evaluate the argument, aBlock for each element in the queue" |
|
293 |
||
582 | 294 |
|n "{ Class: SmallInteger }" |
295 |
pos "{ Class: SmallInteger }"| |
|
39 | 296 |
|
297 |
pos := readPosition. |
|
582 | 298 |
n := tally. |
299 |
1 to:n do:[:i | |
|
300 |
aBlock value:(contentsArray at:pos). |
|
301 |
pos := pos + 1. |
|
302 |
pos > contentsArray size ifTrue:[ |
|
303 |
pos := 1 |
|
304 |
] |
|
39 | 305 |
] |
582 | 306 |
|
307 |
"Modified: 18.10.1997 / 16:24:01 / cg" |
|
5 | 308 |
! ! |
309 |
||
122 | 310 |
!Queue methodsFor:'initialization'! |
311 |
||
312 |
init:size |
|
313 |
"initialize the receiver for size entries" |
|
314 |
||
315 |
contentsArray := Array new:size. |
|
316 |
readPosition := writePosition := 1. |
|
317 |
tally := 0. |
|
318 |
! ! |
|
319 |
||
5 | 320 |
!Queue methodsFor:'queries'! |
321 |
||
122 | 322 |
capacity |
323 |
"return the number of elements the queue can hold" |
|
324 |
||
325 |
^ contentsArray size |
|
326 |
! |
|
327 |
||
904 | 328 |
size |
329 |
"return the number of elements in the queue" |
|
330 |
||
331 |
^ tally |
|
332 |
! ! |
|
333 |
||
334 |
!Queue methodsFor:'testing'! |
|
335 |
||
30 | 336 |
isEmpty |
337 |
"return true, if there are no elements in the queue" |
|
338 |
||
339 |
^ tally == 0 |
|
340 |
! |
|
341 |
||
342 |
isFull |
|
343 |
"return true, if the queue is full i.e. if writing is not |
|
344 |
possible" |
|
345 |
||
346 |
^ tally == contentsArray size |
|
122 | 347 |
! ! |
81 | 348 |
|
582 | 349 |
!Queue class methodsFor:'documentation'! |
131 | 350 |
|
351 |
version |
|
1245 | 352 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.28 2003-06-11 08:37:18 cg Exp $' |
131 | 353 |
! ! |