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