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 |
|
30
|
20 |
!Queue class methodsFor:'documentation'!
|
|
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
|
|
39 |
elements are enterred at one end and removed at the other.
|
|
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 |
|
|
47 |
It is not safe when two processes access Queues simultanously,
|
|
48 |
since accesses to the internals are not protected against process-switches.
|
|
49 |
|
|
50 |
See SharedQueue for a class which is safe with processes and blocks
|
|
51 |
on write when full or on read when empty.
|
254
|
52 |
|
|
53 |
[author:]
|
|
54 |
Claus Gittinger
|
30
|
55 |
"
|
|
56 |
! !
|
5
|
57 |
|
|
58 |
!Queue class methodsFor:'instance creation'!
|
|
59 |
|
122
|
60 |
new
|
|
61 |
"return a new queue with space for some elements"
|
|
62 |
|
|
63 |
^ self new:50
|
|
64 |
!
|
|
65 |
|
5
|
66 |
new:size
|
|
67 |
"return a new queue with space for size elements"
|
|
68 |
|
|
69 |
^ super new init:size
|
|
70 |
|
|
71 |
"
|
|
72 |
|q|
|
|
73 |
|
|
74 |
q := Queue new.
|
39
|
75 |
(1 to:5) do:[:i | q nextPut:i].
|
|
76 |
Transcript show:(q next); space.
|
|
77 |
Transcript show:(q next); space.
|
|
78 |
Transcript show:(q next); space.
|
|
79 |
Transcript show:(q next); space.
|
|
80 |
q nextPutAll:(6 to:10).
|
|
81 |
Transcript show:(q next); space.
|
|
82 |
Transcript show:(q next); space.
|
|
83 |
Transcript show:(q next); space.
|
|
84 |
Transcript show:(q next); space.
|
|
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 showCr:(q next).
|
5
|
91 |
"
|
|
92 |
! !
|
|
93 |
|
|
94 |
!Queue methodsFor:'accessing'!
|
|
95 |
|
|
96 |
next
|
39
|
97 |
"return the next value in the queue;
|
|
98 |
Return nil, if the queue is empty"
|
5
|
99 |
|
47
|
100 |
|value pos "{ Class: SmallInteger }"|
|
5
|
101 |
|
|
102 |
(tally == 0) ifTrue:[^ nil].
|
47
|
103 |
|
|
104 |
pos := readPosition.
|
|
105 |
|
|
106 |
value := contentsArray at:pos.
|
|
107 |
pos := pos + 1.
|
|
108 |
pos > contentsArray size ifTrue:[pos := 1].
|
|
109 |
readPosition := pos.
|
5
|
110 |
tally := tally - 1.
|
|
111 |
^ value
|
|
112 |
!
|
|
113 |
|
|
114 |
nextPut:anObject
|
|
115 |
"enter anObject into the queue - if the queue is full, report an error"
|
|
116 |
|
47
|
117 |
|sz pos "{ Class: SmallInteger }" |
|
|
118 |
|
|
119 |
sz := contentsArray size.
|
|
120 |
pos := writePosition.
|
|
121 |
|
|
122 |
(tally == sz) ifTrue:[
|
|
123 |
self error:'queue is full'.
|
|
124 |
^ self
|
5
|
125 |
].
|
47
|
126 |
|
|
127 |
contentsArray at:pos put:anObject.
|
|
128 |
pos := pos + 1.
|
|
129 |
pos > sz ifTrue:[pos := 1].
|
|
130 |
writePosition := pos.
|
5
|
131 |
tally := tally + 1
|
39
|
132 |
!
|
|
133 |
|
|
134 |
nextPutAll:aCollection
|
|
135 |
"enter all elements from aCollection into the queue."
|
|
136 |
|
|
137 |
aCollection do:[:element | self nextPut:element]
|
122
|
138 |
!
|
|
139 |
|
|
140 |
peek
|
|
141 |
"return the next value in the queue without removing it.
|
|
142 |
If the queue is empty, return nil."
|
|
143 |
|
|
144 |
(tally == 0) ifTrue:[^ nil].
|
|
145 |
^ contentsArray at:readPosition.
|
39
|
146 |
! !
|
|
147 |
|
50
|
148 |
!Queue methodsFor:'enumerating'!
|
39
|
149 |
|
|
150 |
do:aBlock
|
|
151 |
"evaluate the argument, aBlock for each element in the queue"
|
|
152 |
|
|
153 |
|pos endPos|
|
|
154 |
|
|
155 |
pos := readPosition.
|
|
156 |
endPos := writePosition.
|
|
157 |
1 to:tally do:[:i |
|
47
|
158 |
aBlock value:(contentsArray at:pos).
|
|
159 |
pos := pos + 1.
|
|
160 |
pos > contentsArray size ifTrue:[
|
|
161 |
pos := 1
|
|
162 |
]
|
39
|
163 |
]
|
5
|
164 |
! !
|
|
165 |
|
122
|
166 |
!Queue methodsFor:'initialization'!
|
|
167 |
|
|
168 |
init:size
|
|
169 |
"initialize the receiver for size entries"
|
|
170 |
|
|
171 |
contentsArray := Array new:size.
|
|
172 |
readPosition := writePosition := 1.
|
|
173 |
tally := 0.
|
|
174 |
! !
|
|
175 |
|
5
|
176 |
!Queue methodsFor:'queries'!
|
|
177 |
|
122
|
178 |
capacity
|
|
179 |
"return the number of elements the queue can hold"
|
|
180 |
|
|
181 |
^ contentsArray size
|
|
182 |
!
|
|
183 |
|
30
|
184 |
isEmpty
|
|
185 |
"return true, if there are no elements in the queue"
|
|
186 |
|
|
187 |
^ tally == 0
|
|
188 |
!
|
|
189 |
|
|
190 |
isFull
|
|
191 |
"return true, if the queue is full i.e. if writing is not
|
|
192 |
possible"
|
|
193 |
|
|
194 |
^ tally == contentsArray size
|
|
195 |
!
|
|
196 |
|
5
|
197 |
size
|
|
198 |
"return the number of elements in the queue"
|
|
199 |
|
|
200 |
^ tally
|
122
|
201 |
! !
|
81
|
202 |
|
131
|
203 |
!Queue class methodsFor:'documentation'!
|
|
204 |
|
|
205 |
version
|
254
|
206 |
^ '$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.16 1996-04-25 16:15:14 cg Exp $'
|
131
|
207 |
! !
|