author | Claus Gittinger <cg@exept.de> |
Fri, 23 Apr 1999 14:38:25 +0200 | |
changeset 742 | e750820e9f1d |
parent 686 | d2daab8e8c8a |
child 786 | c136082ea7e7 |
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 |
||
30 | 13 |
Collection subclass:#Queue |
254 | 14 |
instanceVariableNames:'contentsArray readPosition writePosition tally' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Ordered' |
|
5 | 18 |
! |
19 |
||
582 | 20 |
!Queue class methodsFor:'documentation'! |
30 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
47 | 25 |
All Rights Reserved |
5 | 26 |
|
30 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
74 | 38 |
Queues provides a simple implementation of a queue, where |
587 | 39 |
elements are added at one end and removed at the other. |
74 | 40 |
Access protocol is somewhat like a streams protocol, i.e. access |
41 |
is by #nextPut: and #next. |
|
42 |
The queue is created with a size argument, defining how many elements |
|
43 |
are to be stored. It will report an error if the queue ever becomes full |
|
44 |
and another element is to be added. Likewise, it will report an error |
|
45 |
if its empty and an element is to be removed. |
|
46 |
||
587 | 47 |
It is NOT safe when two processes access Queues simultanously, |
74 | 48 |
since accesses to the internals are not protected against process-switches. |
587 | 49 |
See SharedQueue for a class which IS safe with processes and blocks |
74 | 50 |
on write when full or on read when empty. |
254 | 51 |
|
587 | 52 |
[Implementation note:] |
53 |
All of queues functionality is also provided by the OrderedCollection (OC) |
|
54 |
class; OC could easily simulate a queue (using #addLast: / #removeFirst). |
|
55 |
The reason for providing Queue is not any speed advantage (actually, |
|
56 |
OC seems to be a tiny bit faster). |
|
57 |
The point is that an implementation of SharedQueue as a subclass of OC |
|
58 |
would require that many OC methods had to be blocked and/or redefined in |
|
59 |
such a subclass, to care for simultaneous access. |
|
60 |
Since queue implements a much more lightweight protocol, |
|
61 |
the sharedQueue implementation is much cleaner when based on a more |
|
62 |
lightweight Queue class. |
|
63 |
||
254 | 64 |
[author:] |
65 |
Claus Gittinger |
|
30 | 66 |
" |
587 | 67 |
! |
68 |
||
69 |
examples |
|
70 |
" |
|
71 |
adding at one end, removing at the other ... |
|
72 |
[exBegin] |
|
73 |
|q element | |
|
74 |
||
75 |
q := Queue new:10. |
|
76 |
1 to:5 do:[:i | |
|
77 |
Transcript showCR:('adding ' , i printString). |
|
78 |
q nextPut:i |
|
79 |
]. |
|
80 |
||
81 |
[q notEmpty] whileTrue:[ |
|
82 |
element := q next. |
|
83 |
Transcript showCR:('removed ' , element printString). |
|
84 |
]. |
|
85 |
[exEnd] |
|
86 |
||
87 |
||
88 |
||
89 |
timing; Queue vs. OrderedCollection |
|
90 |
[exBegin] |
|
91 |
|q oc tQueue tOC | |
|
92 |
||
93 |
q := Queue new:100. |
|
94 |
tQueue := Time millisecondsToRun:[ |
|
95 |
1000 timesRepeat:[ |
|
96 |
1 to:100 do:[:i | |
|
97 |
q nextPut:i |
|
98 |
]. |
|
99 |
[q isEmpty] whileFalse:[ |
|
100 |
q next |
|
101 |
]. |
|
102 |
] |
|
103 |
]. |
|
104 |
||
105 |
oc := OrderedCollection new:100. |
|
106 |
tOC := Time millisecondsToRun:[ |
|
107 |
1000 timesRepeat:[ |
|
108 |
1 to:100 do:[:i | |
|
109 |
oc addLast:i |
|
110 |
]. |
|
111 |
[oc isEmpty] whileFalse:[ |
|
112 |
oc removeFirst |
|
113 |
]. |
|
114 |
] |
|
115 |
]. |
|
116 |
Transcript showCR:('queue time: ' , tQueue printString , ' ms'). |
|
117 |
Transcript showCR:('oc time : ' , tOC printString , ' ms'). |
|
118 |
[exEnd] |
|
119 |
||
120 |
||
121 |
" |
|
30 | 122 |
! ! |
5 | 123 |
|
582 | 124 |
!Queue class methodsFor:'instance creation'! |
5 | 125 |
|
122 | 126 |
new |
127 |
"return a new queue with space for some elements" |
|
128 |
||
129 |
^ self new:50 |
|
130 |
! |
|
131 |
||
5 | 132 |
new:size |
133 |
"return a new queue with space for size elements" |
|
134 |
||
135 |
^ super new init:size |
|
136 |
||
137 |
" |
|
138 |
|q| |
|
139 |
||
140 |
q := Queue new. |
|
39 | 141 |
(1 to:5) do:[:i | q nextPut:i]. |
142 |
Transcript show:(q next); space. |
|
143 |
Transcript show:(q next); space. |
|
144 |
Transcript show:(q next); space. |
|
145 |
Transcript show:(q next); space. |
|
146 |
q nextPutAll:(6 to:10). |
|
147 |
Transcript show:(q next); space. |
|
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. |
|
350 | 156 |
Transcript showCR:(q next). |
5 | 157 |
" |
158 |
! ! |
|
159 |
||
160 |
!Queue methodsFor:'accessing'! |
|
161 |
||
162 |
next |
|
39 | 163 |
"return the next value in the queue; |
164 |
Return nil, if the queue is empty" |
|
5 | 165 |
|
47 | 166 |
|value pos "{ Class: SmallInteger }"| |
5 | 167 |
|
168 |
(tally == 0) ifTrue:[^ nil]. |
|
47 | 169 |
|
170 |
pos := readPosition. |
|
171 |
||
172 |
value := contentsArray at:pos. |
|
647 | 173 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
47 | 174 |
pos := pos + 1. |
175 |
pos > contentsArray size ifTrue:[pos := 1]. |
|
176 |
readPosition := pos. |
|
5 | 177 |
tally := tally - 1. |
178 |
^ value |
|
179 |
! |
|
180 |
||
181 |
nextPut:anObject |
|
182 |
"enter anObject into the queue - if the queue is full, report an error" |
|
183 |
||
47 | 184 |
|sz pos "{ Class: SmallInteger }" | |
185 |
||
186 |
sz := contentsArray size. |
|
187 |
pos := writePosition. |
|
188 |
||
189 |
(tally == sz) ifTrue:[ |
|
190 |
self error:'queue is full'. |
|
191 |
^ self |
|
5 | 192 |
]. |
47 | 193 |
|
194 |
contentsArray at:pos put:anObject. |
|
195 |
pos := pos + 1. |
|
196 |
pos > sz ifTrue:[pos := 1]. |
|
197 |
writePosition := pos. |
|
5 | 198 |
tally := tally + 1 |
39 | 199 |
! |
200 |
||
201 |
nextPutAll:aCollection |
|
202 |
"enter all elements from aCollection into the queue." |
|
203 |
||
204 |
aCollection do:[:element | self nextPut:element] |
|
122 | 205 |
! |
206 |
||
207 |
peek |
|
208 |
"return the next value in the queue without removing it. |
|
209 |
If the queue is empty, return nil." |
|
210 |
||
211 |
(tally == 0) ifTrue:[^ nil]. |
|
212 |
^ contentsArray at:readPosition. |
|
396 | 213 |
! |
214 |
||
647 | 215 |
removeAll |
742 | 216 |
"remove all elements in the queue; return the receiver" |
647 | 217 |
|
218 |
tally := 0. |
|
219 |
readPosition := writePosition := 1. |
|
220 |
contentsArray atAllPut:nil "/ to help the garbage collector |
|
221 |
! |
|
222 |
||
396 | 223 |
removeLast |
224 |
"return the last value in the queue; |
|
225 |
Return nil, if the queue is empty" |
|
226 |
||
227 |
|value pos "{ Class: SmallInteger }"| |
|
228 |
||
229 |
(tally == 0) ifTrue:[^ nil]. |
|
230 |
||
231 |
pos := writePosition. |
|
232 |
pos == 1 ifTrue:[ |
|
233 |
pos := contentsArray size |
|
234 |
] ifFalse:[ |
|
235 |
pos := pos - 1. |
|
236 |
]. |
|
237 |
||
238 |
value := contentsArray at:pos. |
|
647 | 239 |
contentsArray at:pos put:nil. "/ to help the garbage collector |
396 | 240 |
writePosition := pos. |
241 |
tally := tally - 1. |
|
242 |
^ value |
|
243 |
||
244 |
"Created: 22.6.1996 / 18:49:41 / cg" |
|
39 | 245 |
! ! |
246 |
||
686
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
247 |
!Queue methodsFor:'accessing - protocol compatibility'! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
248 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
249 |
addLast:someObject |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
250 |
"same as #nextPut: - for protocol compatibility with |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
251 |
other collections" |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
252 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
253 |
self nextPut:someObject. |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
254 |
^ someObject |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
255 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
256 |
"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
|
257 |
! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
258 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
259 |
removeFirst |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
260 |
"same as #next - for protocol compatibility with |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
261 |
other collections" |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
262 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
263 |
^ self next |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
264 |
|
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
265 |
"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
|
266 |
! ! |
d2daab8e8c8a
added #addLast: and #removeFirst for protocoll compatibility
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
267 |
|
50 | 268 |
!Queue methodsFor:'enumerating'! |
39 | 269 |
|
270 |
do:aBlock |
|
271 |
"evaluate the argument, aBlock for each element in the queue" |
|
272 |
||
582 | 273 |
|n "{ Class: SmallInteger }" |
274 |
pos "{ Class: SmallInteger }"| |
|
39 | 275 |
|
276 |
pos := readPosition. |
|
582 | 277 |
n := tally. |
278 |
1 to:n do:[:i | |
|
279 |
aBlock value:(contentsArray at:pos). |
|
280 |
pos := pos + 1. |
|
281 |
pos > contentsArray size ifTrue:[ |
|
282 |
pos := 1 |
|
283 |
] |
|
39 | 284 |
] |
582 | 285 |
|
286 |
"Modified: 18.10.1997 / 16:24:01 / cg" |
|
5 | 287 |
! ! |
288 |
||
122 | 289 |
!Queue methodsFor:'initialization'! |
290 |
||
291 |
init:size |
|
292 |
"initialize the receiver for size entries" |
|
293 |
||
294 |
contentsArray := Array new:size. |
|
295 |
readPosition := writePosition := 1. |
|
296 |
tally := 0. |
|
297 |
! ! |
|
298 |
||
5 | 299 |
!Queue methodsFor:'queries'! |
300 |
||
122 | 301 |
capacity |
302 |
"return the number of elements the queue can hold" |
|
303 |
||
304 |
^ contentsArray size |
|
305 |
! |
|
306 |
||
30 | 307 |
isEmpty |
308 |
"return true, if there are no elements in the queue" |
|
309 |
||
310 |
^ tally == 0 |
|
311 |
! |
|
312 |
||
313 |
isFull |
|
314 |
"return true, if the queue is full i.e. if writing is not |
|
315 |
possible" |
|
316 |
||
317 |
^ tally == contentsArray size |
|
318 |
! |
|
319 |
||
5 | 320 |
size |
321 |
"return the number of elements in the queue" |
|
322 |
||
323 |
^ tally |
|
122 | 324 |
! ! |
81 | 325 |
|
582 | 326 |
!Queue class methodsFor:'documentation'! |
131 | 327 |
|
328 |
version |
|
742 | 329 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.23 1999-04-23 12:37:58 cg Exp $' |
131 | 330 |
! ! |