1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
3 |
All Rights Reserved
|
1
|
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 |
|
|
13 |
SequenceableCollection subclass:#ArrayedCollection
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Abstract'
|
|
18 |
!
|
|
19 |
|
|
20 |
ArrayedCollection comment:'
|
82
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
22 |
All Rights Reserved
|
92
|
23 |
|
379
|
24 |
$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.21 1995-08-10 12:25:45 claus Exp $
|
82
|
25 |
'!
|
1
|
26 |
|
82
|
27 |
!ArrayedCollection class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
32 |
All Rights Reserved
|
1
|
33 |
|
82
|
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 |
!
|
1
|
42 |
|
82
|
43 |
version
|
|
44 |
"
|
379
|
45 |
$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.21 1995-08-10 12:25:45 claus Exp $
|
|
46 |
$Revision: 1.21 $
|
82
|
47 |
"
|
|
48 |
!
|
|
49 |
|
|
50 |
documentation
|
|
51 |
"
|
249
|
52 |
ArrayedCollection is an abstract superclass for all collections where
|
|
53 |
the elements can be accessed via an integer index.
|
360
|
54 |
And the collection is a fixed size collection. Those fixed size collections
|
|
55 |
cannot easily grow, since they store the elements directly within the
|
|
56 |
object and a grow operation can only be done by becoming another object.
|
|
57 |
(other collections keep a reference to the physical container, which
|
|
58 |
can be easily replaced)
|
|
59 |
|
|
60 |
Notice: currently, ST/X supports growing fix-size collections
|
|
61 |
(such as Arrays, ByteArrays and Strings). However, this
|
|
62 |
can only be done in a very slow way (using become).
|
|
63 |
Therefore, you SHOULD rewrite any application that does this
|
|
64 |
to make use of OrderedCollection or any other collection which
|
|
65 |
can grow faster.
|
|
66 |
To remind you of that, a warning message is sent to the
|
|
67 |
standard error whenever such an operation is performed.
|
|
68 |
|
|
69 |
Also note, that some other smalltalk systems do NOT allow
|
|
70 |
fix size collection to change their size, and that future
|
|
71 |
ST/X versions may be changed to trigger an error (instead of a
|
|
72 |
warning) in those situations.
|
82
|
73 |
"
|
|
74 |
! !
|
1
|
75 |
|
92
|
76 |
!ArrayedCollection class methodsFor:'queries'!
|
|
77 |
|
|
78 |
growIsCheap
|
|
79 |
"return true, if this collection can easily grow
|
|
80 |
(i.e. without a need for become:).
|
|
81 |
Since this is the superclass of all indexed fix-size collections,
|
|
82 |
return false here."
|
|
83 |
|
|
84 |
^ false
|
|
85 |
! !
|
|
86 |
|
1
|
87 |
!ArrayedCollection class methodsFor:'instance creation'!
|
|
88 |
|
|
89 |
with:element
|
|
90 |
"return a new SequenceableCollection with one element:anObject"
|
|
91 |
|
|
92 |
|newCollection|
|
|
93 |
|
|
94 |
newCollection := self new:1.
|
|
95 |
newCollection at:1 put:element.
|
|
96 |
^newCollection
|
125
|
97 |
|
|
98 |
"
|
|
99 |
OrderedCollection with:1
|
|
100 |
SortedCollection with:99
|
|
101 |
"
|
1
|
102 |
!
|
|
103 |
|
|
104 |
with:first with:second
|
|
105 |
"return a new SequenceableCollection with two elements"
|
|
106 |
|
|
107 |
|newCollection|
|
|
108 |
|
|
109 |
newCollection := self new:2.
|
|
110 |
newCollection at:1 put:first.
|
|
111 |
newCollection at:2 put:second.
|
|
112 |
^newCollection
|
125
|
113 |
|
|
114 |
"
|
|
115 |
OrderedCollection with:1 with:2
|
|
116 |
SortedCollection with:99 with:3
|
|
117 |
"
|
1
|
118 |
!
|
|
119 |
|
|
120 |
with:first with:second with:third
|
|
121 |
"return a new SequenceableCollection with three elements"
|
|
122 |
|
|
123 |
|newCollection|
|
|
124 |
|
|
125 |
newCollection := self new:3.
|
|
126 |
newCollection at:1 put:first.
|
|
127 |
newCollection at:2 put:second.
|
|
128 |
newCollection at:3 put:third.
|
|
129 |
^newCollection
|
125
|
130 |
|
|
131 |
"
|
|
132 |
OrderedCollection with:1 with:2 with:3
|
|
133 |
SortedCollection with:99 with:3 with:301
|
|
134 |
"
|
1
|
135 |
!
|
|
136 |
|
|
137 |
with:first with:second with:third with:forth
|
|
138 |
"return a new SequenceableCollection with four elements"
|
|
139 |
|
|
140 |
|newCollection|
|
|
141 |
|
|
142 |
newCollection := self new:4.
|
|
143 |
newCollection at:1 put:first.
|
|
144 |
newCollection at:2 put:second.
|
|
145 |
newCollection at:3 put:third.
|
|
146 |
newCollection at:4 put:forth.
|
|
147 |
^newCollection
|
|
148 |
!
|
|
149 |
|
|
150 |
with:one with:two with:three with:four with:five
|
|
151 |
"return a new SequenceableCollection with five elements"
|
|
152 |
|
|
153 |
|newCollection|
|
|
154 |
|
|
155 |
newCollection := self new:5.
|
|
156 |
newCollection at:1 put:one.
|
|
157 |
newCollection at:2 put:two.
|
|
158 |
newCollection at:3 put:three.
|
|
159 |
newCollection at:4 put:four.
|
|
160 |
newCollection at:5 put:five.
|
|
161 |
^newCollection
|
|
162 |
!
|
|
163 |
|
|
164 |
withAll:aCollection
|
|
165 |
"return a new Collection with all elements taken from the argument,
|
|
166 |
aCollection"
|
|
167 |
|
|
168 |
|newCollection newSize
|
|
169 |
index "{ Class: SmallInteger }" |
|
|
170 |
|
|
171 |
newSize := aCollection size.
|
|
172 |
newCollection := self new:newSize.
|
359
|
173 |
aCollection isSequenceable ifTrue:[
|
155
|
174 |
"aCollection has indexed elements"
|
|
175 |
newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1
|
1
|
176 |
] ifFalse:[
|
155
|
177 |
"must enumerate the elements"
|
|
178 |
index := 1.
|
|
179 |
aCollection do:[:element |
|
|
180 |
newCollection at:index put:element.
|
|
181 |
index := index + 1
|
|
182 |
]
|
1
|
183 |
].
|
|
184 |
^ newCollection
|
125
|
185 |
|
|
186 |
"
|
|
187 |
OrderedCollection withAll:#(1 2 3 4 5)
|
|
188 |
SortedCollection withAll:#(99 17 53 1 101)
|
|
189 |
"
|
1
|
190 |
! !
|
|
191 |
|
2
|
192 |
!ArrayedCollection methodsFor:'testing'!
|
1
|
193 |
|
68
|
194 |
includesKey:anIndex
|
|
195 |
"return true, if anIndex is a valid key.
|
|
196 |
NOTICE: in ST-80, this message is only defined for Dictionaries,
|
155
|
197 |
however, having a common protocol with indexed collections
|
|
198 |
often simplifies things."
|
68
|
199 |
|
|
200 |
^ (anIndex >= 1) and:[anIndex <= self size]
|
|
201 |
|
|
202 |
"
|
|
203 |
#(1 2 3) includesKey:4
|
|
204 |
#(1 2 3) includesKey:3
|
|
205 |
"
|
|
206 |
!
|
|
207 |
|
2
|
208 |
size
|
|
209 |
"redefined to re-enable size->basicSize forwarding
|
|
210 |
(it is cought in SequencableCollection)"
|
|
211 |
|
|
212 |
^ self basicSize
|
|
213 |
! !
|
|
214 |
|
249
|
215 |
!ArrayedCollection methodsFor:'copying'!
|
|
216 |
|
|
217 |
copyEmptyAndGrow:size
|
|
218 |
"return a new instance of the receivers species with size
|
|
219 |
nilled elements and any named instance variables copied."
|
|
220 |
|
|
221 |
"special case for Array, which has no named instance vars"
|
|
222 |
|
356
|
223 |
|cls|
|
|
224 |
|
|
225 |
(cls := self class) instSize == 0 ifTrue:[
|
|
226 |
^ cls new:size
|
249
|
227 |
].
|
|
228 |
^ super copyEmptyAndGrow:size
|
|
229 |
! !
|
|
230 |
|
2
|
231 |
!ArrayedCollection methodsFor:'resizing'!
|
1
|
232 |
|
2
|
233 |
grow:newSize
|
125
|
234 |
"grow the receiver i.e. cut off everything after newSize.
|
|
235 |
Warning: this may be a slow operation due to the use of become
|
|
236 |
- you should write your collection classes to avoid the use of become.
|
155
|
237 |
You have been warned."
|
2
|
238 |
|
27
|
239 |
|newArray oldSize|
|
1
|
240 |
|
27
|
241 |
oldSize := self size.
|
|
242 |
(newSize ~~ oldSize) ifTrue:[
|
360
|
243 |
"/
|
|
244 |
"/ output a warning - you should rewrite your application
|
|
245 |
"/ to use some collection which implements grow: more efficient
|
|
246 |
"/ (i.e. use OrderedCollection instead of Array ..)
|
|
247 |
"/
|
|
248 |
'ARRCOLL: Warning: slow grow operation (' infoPrint.
|
|
249 |
self class name infoPrint. ')' infoPrintNL.
|
|
250 |
|
155
|
251 |
newArray := self species new:newSize.
|
|
252 |
newArray replaceFrom:1 to:(newSize min:oldSize) with:self.
|
|
253 |
self become:newArray.
|
2
|
254 |
]
|
125
|
255 |
|
|
256 |
"
|
|
257 |
#(1 2 3 4 5 6) copy grow:3
|
155
|
258 |
#(1 2 3 4 5 6) copy grow:10
|
125
|
259 |
'hello world' copy grow:5
|
155
|
260 |
'hello' copy grow:20
|
125
|
261 |
"
|
360
|
262 |
!
|
|
263 |
|
|
264 |
removeAll
|
|
265 |
'ARRCOLL: Warning: slow remove operation (' infoPrint.
|
|
266 |
self class name infoPrint. ')' infoPrintNL.
|
|
267 |
|
|
268 |
self become:(self copyEmpty)
|
|
269 |
|
|
270 |
"
|
|
271 |
#(1 2 3 4 5) copy removeAll
|
|
272 |
#(1 2 3 4 5) removeAll
|
|
273 |
"
|
2
|
274 |
! !
|
1
|
275 |
|
2
|
276 |
!ArrayedCollection methodsFor:'error handling'!
|
1
|
277 |
|
|
278 |
fixedSizeError
|
68
|
279 |
"report an error that size of the collection cannot be changed.
|
|
280 |
This is obsolete now."
|
1
|
281 |
|
|
282 |
^ self error:'cannot change size'
|
|
283 |
! !
|
|
284 |
|
213
|
285 |
!ArrayedCollection methodsFor:'printing & storing'!
|
1
|
286 |
|
|
287 |
storeOn:aStream
|
280
|
288 |
"output a printed representation (which can be re-read with readFrom:)
|
|
289 |
onto the argument aStream. Redefined to output index access."
|
1
|
290 |
|
|
291 |
|index "{ Class: SmallInteger }"|
|
|
292 |
|
68
|
293 |
thisContext isRecursive ifTrue:[
|
155
|
294 |
Transcript showCr:'Error: storeOn: of self referencing collection.'.
|
293
|
295 |
aStream nextPutAll:'#recursive'.
|
155
|
296 |
^ self
|
68
|
297 |
].
|
|
298 |
|
333
|
299 |
aStream nextPutAll:'(('; nextPutAll:self class name; nextPutAll:' new:'.
|
1
|
300 |
self size printOn:aStream.
|
|
301 |
aStream nextPutAll:')'.
|
|
302 |
index := 1.
|
|
303 |
self do:[:element |
|
155
|
304 |
aStream nextPutAll:' at:'.
|
|
305 |
index printOn:aStream.
|
|
306 |
aStream nextPutAll:' put:('.
|
|
307 |
element storeOn:aStream.
|
|
308 |
aStream nextPutAll:');'.
|
|
309 |
index := index + 1
|
1
|
310 |
].
|
|
311 |
index > 1 ifTrue:[aStream nextPutAll:' yourself'].
|
|
312 |
aStream nextPut:$)
|
280
|
313 |
|
|
314 |
"
|
293
|
315 |
(Array with:(1@2) with:(1->2)) storeString
|
280
|
316 |
"
|
1
|
317 |
! !
|