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