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