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.
|
|
173 |
pos := pos + 1.
|
|
174 |
pos > contentsArray size ifTrue:[pos := 1].
|
|
175 |
readPosition := pos.
|
5
|
176 |
tally := tally - 1.
|
|
177 |
^ value
|
|
178 |
!
|
|
179 |
|
|
180 |
nextPut:anObject
|
|
181 |
"enter anObject into the queue - if the queue is full, report an error"
|
|
182 |
|
47
|
183 |
|sz pos "{ Class: SmallInteger }" |
|
|
184 |
|
|
185 |
sz := contentsArray size.
|
|
186 |
pos := writePosition.
|
|
187 |
|
|
188 |
(tally == sz) ifTrue:[
|
|
189 |
self error:'queue is full'.
|
|
190 |
^ self
|
5
|
191 |
].
|
47
|
192 |
|
|
193 |
contentsArray at:pos put:anObject.
|
|
194 |
pos := pos + 1.
|
|
195 |
pos > sz ifTrue:[pos := 1].
|
|
196 |
writePosition := pos.
|
5
|
197 |
tally := tally + 1
|
39
|
198 |
!
|
|
199 |
|
|
200 |
nextPutAll:aCollection
|
|
201 |
"enter all elements from aCollection into the queue."
|
|
202 |
|
|
203 |
aCollection do:[:element | self nextPut:element]
|
122
|
204 |
!
|
|
205 |
|
|
206 |
peek
|
|
207 |
"return the next value in the queue without removing it.
|
|
208 |
If the queue is empty, return nil."
|
|
209 |
|
|
210 |
(tally == 0) ifTrue:[^ nil].
|
|
211 |
^ contentsArray at:readPosition.
|
396
|
212 |
!
|
|
213 |
|
|
214 |
removeLast
|
|
215 |
"return the last value in the queue;
|
|
216 |
Return nil, if the queue is empty"
|
|
217 |
|
|
218 |
|value pos "{ Class: SmallInteger }"|
|
|
219 |
|
|
220 |
(tally == 0) ifTrue:[^ nil].
|
|
221 |
|
|
222 |
pos := writePosition.
|
|
223 |
pos == 1 ifTrue:[
|
|
224 |
pos := contentsArray size
|
|
225 |
] ifFalse:[
|
|
226 |
pos := pos - 1.
|
|
227 |
].
|
|
228 |
|
|
229 |
value := contentsArray at:pos.
|
|
230 |
writePosition := pos.
|
|
231 |
tally := tally - 1.
|
|
232 |
^ value
|
|
233 |
|
|
234 |
"Created: 22.6.1996 / 18:49:41 / cg"
|
39
|
235 |
! !
|
|
236 |
|
50
|
237 |
!Queue methodsFor:'enumerating'!
|
39
|
238 |
|
|
239 |
do:aBlock
|
|
240 |
"evaluate the argument, aBlock for each element in the queue"
|
|
241 |
|
582
|
242 |
|n "{ Class: SmallInteger }"
|
|
243 |
pos "{ Class: SmallInteger }"|
|
39
|
244 |
|
|
245 |
pos := readPosition.
|
582
|
246 |
n := tally.
|
|
247 |
1 to:n do:[:i |
|
|
248 |
aBlock value:(contentsArray at:pos).
|
|
249 |
pos := pos + 1.
|
|
250 |
pos > contentsArray size ifTrue:[
|
|
251 |
pos := 1
|
|
252 |
]
|
39
|
253 |
]
|
582
|
254 |
|
|
255 |
"Modified: 18.10.1997 / 16:24:01 / cg"
|
5
|
256 |
! !
|
|
257 |
|
122
|
258 |
!Queue methodsFor:'initialization'!
|
|
259 |
|
|
260 |
init:size
|
|
261 |
"initialize the receiver for size entries"
|
|
262 |
|
|
263 |
contentsArray := Array new:size.
|
|
264 |
readPosition := writePosition := 1.
|
|
265 |
tally := 0.
|
|
266 |
! !
|
|
267 |
|
5
|
268 |
!Queue methodsFor:'queries'!
|
|
269 |
|
122
|
270 |
capacity
|
|
271 |
"return the number of elements the queue can hold"
|
|
272 |
|
|
273 |
^ contentsArray size
|
|
274 |
!
|
|
275 |
|
30
|
276 |
isEmpty
|
|
277 |
"return true, if there are no elements in the queue"
|
|
278 |
|
|
279 |
^ tally == 0
|
|
280 |
!
|
|
281 |
|
|
282 |
isFull
|
|
283 |
"return true, if the queue is full i.e. if writing is not
|
|
284 |
possible"
|
|
285 |
|
|
286 |
^ tally == contentsArray size
|
|
287 |
!
|
|
288 |
|
5
|
289 |
size
|
|
290 |
"return the number of elements in the queue"
|
|
291 |
|
|
292 |
^ tally
|
122
|
293 |
! !
|
81
|
294 |
|
582
|
295 |
!Queue class methodsFor:'documentation'!
|
131
|
296 |
|
|
297 |
version
|
587
|
298 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.20 1997-11-02 17:51:14 cg Exp $'
|
131
|
299 |
! !
|