author | Claus Gittinger <cg@exept.de> |
Fri, 10 Jan 1997 16:17:43 +0100 | |
changeset 2125 | b438ee11118f |
parent 1998 | 9749a5f87238 |
child 2146 | 27069b34fb5a |
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 |
||
2125 | 20 |
!ArrayedCollection class methodsFor:'documentation'! |
82 | 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: |
1998 | 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 |
|
1998 | 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). |
|
1171 | 59 |
|
1998 | 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:] |
1998 | 66 |
Claus Gittinger |
1289 | 67 |
|
1283 | 68 |
[see also:] |
1998 | 69 |
OrderedCollection |
82 | 70 |
" |
92 | 71 |
! ! |
72 |
||
2125 | 73 |
!ArrayedCollection class methodsFor:'instance creation'! |
1 | 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:[ |
1998 | 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:[ |
1998 | 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 |
||
2125 | 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 |
|
1998 | 207 |
"{ Pragma: +optSpace }" |
208 |
||
602 | 209 |
"report an error that size of the collection cannot be changed. |
1171 | 210 |
This is not used right now (instead, a warning is sent to stderr |
1579 | 211 |
in the #grow method); however, future versions of ST/X may no longer |
1171 | 212 |
allow fixed size collection to grow. |
213 |
Read the documentation on why things are that way ..." |
|
602 | 214 |
|
215 |
^ self error:'cannot change size' |
|
1171 | 216 |
|
1579 | 217 |
"Modified: 18.7.1996 / 21:39:09 / cg" |
602 | 218 |
! ! |
219 |
||
220 |
!ArrayedCollection methodsFor:'printing & storing'! |
|
221 |
||
222 |
storeOn:aStream |
|
223 |
"output a printed representation (which can be re-read with readFrom:) |
|
224 |
onto the argument aStream. Redefined to output index access." |
|
225 |
||
226 |
|index "{ Class: SmallInteger }"| |
|
227 |
||
228 |
thisContext isRecursive ifTrue:[ |
|
1998 | 229 |
Transcript showCR:'Error: storeOn: of self referencing collection.'. |
230 |
aStream nextPutAll:'#recursive'. |
|
231 |
^ self |
|
602 | 232 |
]. |
233 |
||
234 |
aStream nextPutAll:'(('; nextPutAll:self class name; nextPutAll:' new:'. |
|
235 |
self size printOn:aStream. |
|
236 |
aStream nextPutAll:')'. |
|
237 |
index := 1. |
|
238 |
self do:[:element | |
|
1998 | 239 |
aStream nextPutAll:' at:'. index printOn:aStream. aStream nextPutAll:' put:('. |
240 |
element storeOn:aStream. |
|
241 |
aStream nextPutAll:');'. |
|
242 |
index := index + 1 |
|
602 | 243 |
]. |
244 |
index > 1 ifTrue:[aStream nextPutAll:' yourself']. |
|
245 |
aStream nextPut:$) |
|
246 |
||
247 |
" |
|
248 |
(Array with:(1@2) with:(1->2)) storeString |
|
249 |
" |
|
1414 | 250 |
|
251 |
"Modified: 18.5.1996 / 15:37:30 / cg" |
|
602 | 252 |
! ! |
253 |
||
2 | 254 |
!ArrayedCollection methodsFor:'resizing'! |
1 | 255 |
|
2 | 256 |
grow:newSize |
125 | 257 |
"grow the receiver i.e. cut off everything after newSize. |
258 |
Warning: this may be a slow operation due to the use of become |
|
259 |
- you should write your collection classes to avoid the use of become. |
|
155 | 260 |
You have been warned." |
2 | 261 |
|
599 | 262 |
|newArray oldSize sender| |
1 | 263 |
|
27 | 264 |
oldSize := self size. |
265 |
(newSize ~~ oldSize) ifTrue:[ |
|
2125 | 266 |
InfoPrinting ifTrue:[ |
267 |
"/ |
|
268 |
"/ output a warning - you should rewrite your application |
|
269 |
"/ to use some collection which implements grow: more efficient |
|
270 |
"/ (i.e. use OrderedCollection instead of Array ..) |
|
271 |
"/ |
|
272 |
'ArrayedCollection [info]: slow grow operation (' infoPrint. |
|
273 |
self class name infoPrint. ') via ' infoPrint. |
|
274 |
sender := thisContext sender. |
|
275 |
sender methodPrintString infoPrint. |
|
276 |
' from ' infoPrint. sender sender methodPrintString infoPrintCR. |
|
277 |
]. |
|
360 | 278 |
|
2125 | 279 |
newArray := self species new:newSize. |
280 |
newArray replaceFrom:1 to:(newSize min:oldSize) with:self. |
|
281 |
self become:newArray. |
|
2 | 282 |
] |
125 | 283 |
|
284 |
" |
|
590
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
285 |
#(1 2 3 4 5 6) add:7 |
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
286 |
#(1 2 3 4 5 6) remove:5 |
125 | 287 |
#(1 2 3 4 5 6) copy grow:3 |
155 | 288 |
#(1 2 3 4 5 6) copy grow:10 |
125 | 289 |
'hello world' copy grow:5 |
155 | 290 |
'hello' copy grow:20 |
125 | 291 |
" |
1428 | 292 |
|
2125 | 293 |
"Modified: 10.1.1997 / 15:14:43 / cg" |
360 | 294 |
! |
295 |
||
296 |
removeAll |
|
1998 | 297 |
"{ Pragma: +optSpace }" |
298 |
||
1164 | 299 |
"remove all elements from the receiver. Returns the receiver. |
300 |
||
1111 | 301 |
For ArrayedCollections (which are actually fixed-size collections), |
302 |
this is a slow operation, since a #become: is required to update |
|
303 |
all owners. Better use a collection which is prepared for growing |
|
304 |
(i.e. an OrderedCollection). |
|
305 |
We output a warning message here, to remind you about that." |
|
306 |
||
2125 | 307 |
'ArrayedCollection [info]: slow removeAll operation (' infoPrint. |
1428 | 308 |
self class name infoPrint. ')' infoPrintCR. |
360 | 309 |
|
310 |
self become:(self copyEmpty) |
|
311 |
||
312 |
" |
|
313 |
#(1 2 3 4 5) copy removeAll |
|
314 |
#(1 2 3 4 5) removeAll |
|
315 |
" |
|
1111 | 316 |
|
2125 | 317 |
"Modified: 10.1.1997 / 15:14:55 / cg" |
2 | 318 |
! ! |
1 | 319 |
|
602 | 320 |
!ArrayedCollection methodsFor:'testing'! |
321 |
||
322 |
includesKey:anIndex |
|
323 |
"return true, if anIndex is a valid key. |
|
324 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
|
325 |
however, having a common protocol with indexed collections |
|
326 |
often simplifies things." |
|
327 |
||
328 |
^ (anIndex >= 1) and:[anIndex <= self size] |
|
1 | 329 |
|
602 | 330 |
" |
331 |
#(1 2 3) includesKey:4 |
|
332 |
#(1 2 3) includesKey:3 |
|
333 |
" |
|
334 |
! |
|
1 | 335 |
|
602 | 336 |
size |
337 |
"redefined to re-enable size->basicSize forwarding |
|
338 |
(it is cought in SequencableCollection)" |
|
339 |
||
340 |
^ self basicSize |
|
1 | 341 |
! ! |
342 |
||
2125 | 343 |
!ArrayedCollection class methodsFor:'documentation'! |
628 | 344 |
|
345 |
version |
|
2125 | 346 |
^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.40 1997-01-10 15:17:43 cg Exp $' |
628 | 347 |
! ! |