5
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
5
|
14 |
instanceVariableNames:'contentsArray readPosition writePosition tally'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Ordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
Queue comment:'
|
30
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
33
|
23 |
|
39
|
24 |
$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.5 1994-08-22 12:12:17 claus Exp $
|
30
|
25 |
'!
|
|
26 |
|
|
27 |
!Queue class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
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 |
"
|
39
|
45 |
$Header: /cvs/stx/stx/libbasic2/Queue.st,v 1.5 1994-08-22 12:12:17 claus Exp $
|
30
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Queues provides a simple implementation of a queue - it is not
|
|
52 |
safe when two processes access Queues simultanously since accesses
|
|
53 |
to the internals are not protected against process-switches.
|
|
54 |
See SharedQueue for a class supporting this.
|
|
55 |
"
|
|
56 |
! !
|
5
|
57 |
|
|
58 |
!Queue class methodsFor:'instance creation'!
|
|
59 |
|
|
60 |
new:size
|
|
61 |
"return a new queue with space for size elements"
|
|
62 |
|
|
63 |
^ super new init:size
|
|
64 |
|
|
65 |
"
|
|
66 |
|q|
|
|
67 |
|
|
68 |
q := Queue new.
|
39
|
69 |
(1 to:5) do:[:i | q nextPut:i].
|
|
70 |
Transcript show:(q next); space.
|
|
71 |
Transcript show:(q next); space.
|
|
72 |
Transcript show:(q next); space.
|
|
73 |
Transcript show:(q next); space.
|
|
74 |
q nextPutAll:(6 to:10).
|
|
75 |
Transcript show:(q next); space.
|
|
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 |
Transcript show:(q next); space.
|
|
81 |
Transcript show:(q next); space.
|
|
82 |
Transcript show:(q next); space.
|
|
83 |
Transcript show:(q next); space.
|
|
84 |
Transcript showCr:(q next).
|
5
|
85 |
"
|
|
86 |
!
|
|
87 |
|
|
88 |
new
|
|
89 |
"return a new queue with space for 10 elements"
|
|
90 |
|
|
91 |
^ self new:10
|
|
92 |
! !
|
|
93 |
|
|
94 |
!Queue methodsFor:'initialization'!
|
|
95 |
|
|
96 |
init:size
|
14
|
97 |
"initialize the receiver for size entries"
|
|
98 |
|
5
|
99 |
contentsArray := Array new:size.
|
|
100 |
readPosition := writePosition := 1.
|
|
101 |
tally := 0.
|
|
102 |
! !
|
|
103 |
|
|
104 |
!Queue methodsFor:'accessing'!
|
|
105 |
|
|
106 |
next
|
39
|
107 |
"return the next value in the queue;
|
|
108 |
Return nil, if the queue is empty"
|
5
|
109 |
|
|
110 |
|value|
|
|
111 |
|
|
112 |
(tally == 0) ifTrue:[^ nil].
|
|
113 |
value := contentsArray at:readPosition.
|
|
114 |
readPosition := readPosition + 1.
|
|
115 |
readPosition > contentsArray size ifTrue:[
|
|
116 |
readPosition := 1
|
|
117 |
].
|
|
118 |
tally := tally - 1.
|
|
119 |
^ value
|
|
120 |
!
|
|
121 |
|
|
122 |
nextPut:anObject
|
|
123 |
"enter anObject into the queue - if the queue is full, report an error"
|
|
124 |
|
|
125 |
(tally == contentsArray size) ifTrue:[
|
|
126 |
self error:'queue is full'.
|
|
127 |
^ self
|
|
128 |
].
|
|
129 |
contentsArray at:writePosition put:anObject.
|
|
130 |
writePosition := writePosition + 1.
|
|
131 |
writePosition > contentsArray size ifTrue:[
|
|
132 |
writePosition := 1
|
|
133 |
].
|
|
134 |
tally := tally + 1
|
39
|
135 |
!
|
|
136 |
|
|
137 |
nextPutAll:aCollection
|
|
138 |
"enter all elements from aCollection into the queue."
|
|
139 |
|
|
140 |
aCollection do:[:element | self nextPut:element]
|
|
141 |
! !
|
|
142 |
|
|
143 |
!Queue methodsFor:'enumeration'!
|
|
144 |
|
|
145 |
do:aBlock
|
|
146 |
"evaluate the argument, aBlock for each element in the queue"
|
|
147 |
|
|
148 |
|pos endPos|
|
|
149 |
|
|
150 |
pos := readPosition.
|
|
151 |
endPos := writePosition.
|
|
152 |
1 to:tally do:[:i |
|
|
153 |
aBlock value:(contentsArray at:pos).
|
|
154 |
pos := pos + 1.
|
|
155 |
pos > contentsArray size ifTrue:[
|
|
156 |
pos := 1
|
|
157 |
]
|
|
158 |
]
|
5
|
159 |
! !
|
|
160 |
|
|
161 |
!Queue methodsFor:'queries'!
|
|
162 |
|
30
|
163 |
isEmpty
|
|
164 |
"return true, if there are no elements in the queue"
|
|
165 |
|
|
166 |
^ tally == 0
|
|
167 |
!
|
|
168 |
|
|
169 |
isFull
|
|
170 |
"return true, if the queue is full i.e. if writing is not
|
|
171 |
possible"
|
|
172 |
|
|
173 |
^ tally == contentsArray size
|
|
174 |
!
|
|
175 |
|
5
|
176 |
size
|
|
177 |
"return the number of elements in the queue"
|
|
178 |
|
|
179 |
^ tally
|
|
180 |
! !
|