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