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