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
|
47
|
14 |
instanceVariableNames:'contentsArray readPosition writePosition tally'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Ordered'
|
5
|
18 |
!
|
|
19 |
|
|
20 |
Queue comment:'
|
30
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
47
|
22 |
All Rights Reserved
|
33
|
23 |
|
81
|
24 |
$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.10 1995-07-22 21:48:34 claus Exp $
|
30
|
25 |
'!
|
|
26 |
|
|
27 |
!Queue class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
47
|
32 |
All Rights Reserved
|
5
|
33 |
|
30
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
81
|
45 |
$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.10 1995-07-22 21:48:34 claus Exp $
|
30
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
74
|
51 |
Queues provides a simple implementation of a queue, where
|
|
52 |
elements are enterred at one end and removed at the other.
|
|
53 |
Access protocol is somewhat like a streams protocol, i.e. access
|
|
54 |
is by #nextPut: and #next.
|
|
55 |
The queue is created with a size argument, defining how many elements
|
|
56 |
are to be stored. It will report an error if the queue ever becomes full
|
|
57 |
and another element is to be added. Likewise, it will report an error
|
|
58 |
if its empty and an element is to be removed.
|
|
59 |
|
|
60 |
It is not safe when two processes access Queues simultanously,
|
|
61 |
since accesses to the internals are not protected against process-switches.
|
|
62 |
|
|
63 |
See SharedQueue for a class which is safe with processes and blocks
|
|
64 |
on write when full or on read when empty.
|
30
|
65 |
"
|
|
66 |
! !
|
5
|
67 |
|
|
68 |
!Queue class methodsFor:'instance creation'!
|
|
69 |
|
|
70 |
new:size
|
|
71 |
"return a new queue with space for size elements"
|
|
72 |
|
|
73 |
^ super new init:size
|
|
74 |
|
|
75 |
"
|
|
76 |
|q|
|
|
77 |
|
|
78 |
q := Queue new.
|
39
|
79 |
(1 to:5) do:[:i | q nextPut:i].
|
|
80 |
Transcript show:(q next); space.
|
|
81 |
Transcript show:(q next); space.
|
|
82 |
Transcript show:(q next); space.
|
|
83 |
Transcript show:(q next); space.
|
|
84 |
q nextPutAll:(6 to:10).
|
|
85 |
Transcript show:(q next); space.
|
|
86 |
Transcript show:(q next); space.
|
|
87 |
Transcript show:(q next); space.
|
|
88 |
Transcript show:(q next); space.
|
|
89 |
Transcript show:(q next); space.
|
|
90 |
Transcript show:(q next); space.
|
|
91 |
Transcript show:(q next); space.
|
|
92 |
Transcript show:(q next); space.
|
|
93 |
Transcript show:(q next); space.
|
|
94 |
Transcript showCr:(q next).
|
5
|
95 |
"
|
|
96 |
!
|
|
97 |
|
|
98 |
new
|
81
|
99 |
"return a new queue with space for some elements"
|
5
|
100 |
|
81
|
101 |
^ self new:50
|
5
|
102 |
! !
|
|
103 |
|
|
104 |
!Queue methodsFor:'initialization'!
|
|
105 |
|
|
106 |
init:size
|
14
|
107 |
"initialize the receiver for size entries"
|
|
108 |
|
5
|
109 |
contentsArray := Array new:size.
|
|
110 |
readPosition := writePosition := 1.
|
|
111 |
tally := 0.
|
|
112 |
! !
|
|
113 |
|
|
114 |
!Queue methodsFor:'accessing'!
|
|
115 |
|
75
|
116 |
peek
|
|
117 |
"return the next value in the queue without removing it.
|
|
118 |
If the queue is empty, return nil."
|
|
119 |
|
|
120 |
(tally == 0) ifTrue:[^ nil].
|
|
121 |
^ contentsArray at:readPosition.
|
|
122 |
!
|
|
123 |
|
5
|
124 |
next
|
39
|
125 |
"return the next value in the queue;
|
|
126 |
Return nil, if the queue is empty"
|
5
|
127 |
|
47
|
128 |
|value pos "{ Class: SmallInteger }"|
|
5
|
129 |
|
|
130 |
(tally == 0) ifTrue:[^ nil].
|
47
|
131 |
|
|
132 |
pos := readPosition.
|
|
133 |
|
|
134 |
value := contentsArray at:pos.
|
|
135 |
pos := pos + 1.
|
|
136 |
pos > contentsArray size ifTrue:[pos := 1].
|
|
137 |
readPosition := pos.
|
5
|
138 |
tally := tally - 1.
|
|
139 |
^ value
|
|
140 |
!
|
|
141 |
|
|
142 |
nextPut:anObject
|
|
143 |
"enter anObject into the queue - if the queue is full, report an error"
|
|
144 |
|
47
|
145 |
|sz pos "{ Class: SmallInteger }" |
|
|
146 |
|
|
147 |
sz := contentsArray size.
|
|
148 |
pos := writePosition.
|
|
149 |
|
|
150 |
(tally == sz) ifTrue:[
|
|
151 |
self error:'queue is full'.
|
|
152 |
^ self
|
5
|
153 |
].
|
47
|
154 |
|
|
155 |
contentsArray at:pos put:anObject.
|
|
156 |
pos := pos + 1.
|
|
157 |
pos > sz ifTrue:[pos := 1].
|
|
158 |
writePosition := pos.
|
5
|
159 |
tally := tally + 1
|
39
|
160 |
!
|
|
161 |
|
|
162 |
nextPutAll:aCollection
|
|
163 |
"enter all elements from aCollection into the queue."
|
|
164 |
|
|
165 |
aCollection do:[:element | self nextPut:element]
|
|
166 |
! !
|
|
167 |
|
50
|
168 |
!Queue methodsFor:'enumerating'!
|
39
|
169 |
|
|
170 |
do:aBlock
|
|
171 |
"evaluate the argument, aBlock for each element in the queue"
|
|
172 |
|
|
173 |
|pos endPos|
|
|
174 |
|
|
175 |
pos := readPosition.
|
|
176 |
endPos := writePosition.
|
|
177 |
1 to:tally do:[:i |
|
47
|
178 |
aBlock value:(contentsArray at:pos).
|
|
179 |
pos := pos + 1.
|
|
180 |
pos > contentsArray size ifTrue:[
|
|
181 |
pos := 1
|
|
182 |
]
|
39
|
183 |
]
|
5
|
184 |
! !
|
|
185 |
|
|
186 |
!Queue methodsFor:'queries'!
|
|
187 |
|
30
|
188 |
isEmpty
|
|
189 |
"return true, if there are no elements in the queue"
|
|
190 |
|
|
191 |
^ tally == 0
|
|
192 |
!
|
|
193 |
|
|
194 |
isFull
|
|
195 |
"return true, if the queue is full i.e. if writing is not
|
|
196 |
possible"
|
|
197 |
|
|
198 |
^ tally == contentsArray size
|
|
199 |
!
|
|
200 |
|
5
|
201 |
size
|
|
202 |
"return the number of elements in the queue"
|
|
203 |
|
|
204 |
^ tally
|
81
|
205 |
!
|
|
206 |
|
|
207 |
capacity
|
|
208 |
"return the number of elements the queue can hold"
|
|
209 |
|
|
210 |
^ contentsArray size
|
5
|
211 |
! !
|