author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 18:00:32 +0100 | |
changeset 628 | 7aa563e4c64a |
parent 602 | 771ab7a8c4bf |
child 916 | a050d17c7e1f |
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 |
|
602 | 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. |
|
85 |
newCollection at:1 put:first. |
|
86 |
newCollection at:2 put:second. |
|
87 |
^newCollection |
|
125 | 88 |
|
89 |
" |
|
90 |
OrderedCollection with:1 with:2 |
|
91 |
SortedCollection with:99 with:3 |
|
92 |
" |
|
1 | 93 |
! |
94 |
||
95 |
with:first with:second with:third |
|
96 |
"return a new SequenceableCollection with three elements" |
|
97 |
||
98 |
|newCollection| |
|
99 |
||
100 |
newCollection := self new:3. |
|
101 |
newCollection at:1 put:first. |
|
102 |
newCollection at:2 put:second. |
|
103 |
newCollection at:3 put:third. |
|
104 |
^newCollection |
|
125 | 105 |
|
106 |
" |
|
107 |
OrderedCollection with:1 with:2 with:3 |
|
108 |
SortedCollection with:99 with:3 with:301 |
|
109 |
" |
|
1 | 110 |
! |
111 |
||
112 |
with:first with:second with:third with:forth |
|
113 |
"return a new SequenceableCollection with four elements" |
|
114 |
||
115 |
|newCollection| |
|
116 |
||
117 |
newCollection := self new:4. |
|
118 |
newCollection at:1 put:first. |
|
119 |
newCollection at:2 put:second. |
|
120 |
newCollection at:3 put:third. |
|
121 |
newCollection at:4 put:forth. |
|
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. |
|
131 |
newCollection at:1 put:one. |
|
132 |
newCollection at:2 put:two. |
|
133 |
newCollection at:3 put:three. |
|
134 |
newCollection at:4 put:four. |
|
135 |
newCollection at:5 put:five. |
|
136 |
^newCollection |
|
137 |
! |
|
138 |
||
139 |
withAll:aCollection |
|
140 |
"return a new Collection with all elements taken from the argument, |
|
141 |
aCollection" |
|
142 |
||
143 |
|newCollection newSize |
|
144 |
index "{ Class: SmallInteger }" | |
|
145 |
||
146 |
newSize := aCollection size. |
|
147 |
newCollection := self new:newSize. |
|
359 | 148 |
aCollection isSequenceable ifTrue:[ |
155 | 149 |
"aCollection has indexed elements" |
150 |
newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1 |
|
1 | 151 |
] ifFalse:[ |
155 | 152 |
"must enumerate the elements" |
153 |
index := 1. |
|
154 |
aCollection do:[:element | |
|
155 |
newCollection at:index put:element. |
|
156 |
index := index + 1 |
|
157 |
] |
|
1 | 158 |
]. |
159 |
^ newCollection |
|
125 | 160 |
|
161 |
" |
|
162 |
OrderedCollection withAll:#(1 2 3 4 5) |
|
163 |
SortedCollection withAll:#(99 17 53 1 101) |
|
164 |
" |
|
1 | 165 |
! ! |
166 |
||
602 | 167 |
!ArrayedCollection class methodsFor:'queries'! |
68 | 168 |
|
602 | 169 |
growIsCheap |
170 |
"return true, if this collection can easily grow |
|
171 |
(i.e. without a need for become:). |
|
172 |
Since this is the superclass of all indexed fix-size collections, |
|
173 |
return false here." |
|
68 | 174 |
|
602 | 175 |
^ false |
2 | 176 |
! ! |
177 |
||
249 | 178 |
!ArrayedCollection methodsFor:'copying'! |
179 |
||
180 |
copyEmptyAndGrow:size |
|
181 |
"return a new instance of the receivers species with size |
|
182 |
nilled elements and any named instance variables copied." |
|
183 |
||
184 |
"special case for Array, which has no named instance vars" |
|
185 |
||
356 | 186 |
|cls| |
187 |
||
188 |
(cls := self class) instSize == 0 ifTrue:[ |
|
189 |
^ cls new:size |
|
249 | 190 |
]. |
191 |
^ super copyEmptyAndGrow:size |
|
192 |
! ! |
|
193 |
||
602 | 194 |
!ArrayedCollection methodsFor:'error handling'! |
195 |
||
196 |
fixedSizeError |
|
197 |
"report an error that size of the collection cannot be changed. |
|
198 |
This is obsolete now." |
|
199 |
||
200 |
^ self error:'cannot change size' |
|
201 |
! ! |
|
202 |
||
203 |
!ArrayedCollection methodsFor:'printing & storing'! |
|
204 |
||
205 |
storeOn:aStream |
|
206 |
"output a printed representation (which can be re-read with readFrom:) |
|
207 |
onto the argument aStream. Redefined to output index access." |
|
208 |
||
209 |
|index "{ Class: SmallInteger }"| |
|
210 |
||
211 |
thisContext isRecursive ifTrue:[ |
|
212 |
Transcript showCr:'Error: storeOn: of self referencing collection.'. |
|
213 |
aStream nextPutAll:'#recursive'. |
|
214 |
^ self |
|
215 |
]. |
|
216 |
||
217 |
aStream nextPutAll:'(('; nextPutAll:self class name; nextPutAll:' new:'. |
|
218 |
self size printOn:aStream. |
|
219 |
aStream nextPutAll:')'. |
|
220 |
index := 1. |
|
221 |
self do:[:element | |
|
222 |
aStream nextPutAll:' at:'. |
|
223 |
index printOn:aStream. |
|
224 |
aStream nextPutAll:' put:('. |
|
225 |
element storeOn:aStream. |
|
226 |
aStream nextPutAll:');'. |
|
227 |
index := index + 1 |
|
228 |
]. |
|
229 |
index > 1 ifTrue:[aStream nextPutAll:' yourself']. |
|
230 |
aStream nextPut:$) |
|
231 |
||
232 |
" |
|
233 |
(Array with:(1@2) with:(1->2)) storeString |
|
234 |
" |
|
235 |
! ! |
|
236 |
||
2 | 237 |
!ArrayedCollection methodsFor:'resizing'! |
1 | 238 |
|
2 | 239 |
grow:newSize |
125 | 240 |
"grow the receiver i.e. cut off everything after newSize. |
241 |
Warning: this may be a slow operation due to the use of become |
|
242 |
- you should write your collection classes to avoid the use of become. |
|
155 | 243 |
You have been warned." |
2 | 244 |
|
599 | 245 |
|newArray oldSize sender| |
1 | 246 |
|
27 | 247 |
oldSize := self size. |
248 |
(newSize ~~ oldSize) ifTrue:[ |
|
360 | 249 |
"/ |
250 |
"/ output a warning - you should rewrite your application |
|
251 |
"/ to use some collection which implements grow: more efficient |
|
252 |
"/ (i.e. use OrderedCollection instead of Array ..) |
|
253 |
"/ |
|
254 |
'ARRCOLL: Warning: slow grow operation (' infoPrint. |
|
590
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
255 |
self class name infoPrint. ') via ' infoPrint. |
599 | 256 |
sender := thisContext sender. |
257 |
sender methodPrintString infoPrint. |
|
258 |
' from ' infoPrint. sender sender methodPrintString infoPrintNL. |
|
360 | 259 |
|
155 | 260 |
newArray := self species new:newSize. |
261 |
newArray replaceFrom:1 to:(newSize min:oldSize) with:self. |
|
262 |
self become:newArray. |
|
2 | 263 |
] |
125 | 264 |
|
265 |
" |
|
590
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
266 |
#(1 2 3 4 5 6) add:7 |
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
267 |
#(1 2 3 4 5 6) remove:5 |
125 | 268 |
#(1 2 3 4 5 6) copy grow:3 |
155 | 269 |
#(1 2 3 4 5 6) copy grow:10 |
125 | 270 |
'hello world' copy grow:5 |
155 | 271 |
'hello' copy grow:20 |
125 | 272 |
" |
360 | 273 |
! |
274 |
||
275 |
removeAll |
|
276 |
'ARRCOLL: Warning: slow remove operation (' infoPrint. |
|
277 |
self class name infoPrint. ')' infoPrintNL. |
|
278 |
||
279 |
self become:(self copyEmpty) |
|
280 |
||
281 |
" |
|
282 |
#(1 2 3 4 5) copy removeAll |
|
283 |
#(1 2 3 4 5) removeAll |
|
284 |
" |
|
2 | 285 |
! ! |
1 | 286 |
|
602 | 287 |
!ArrayedCollection methodsFor:'testing'! |
288 |
||
289 |
includesKey:anIndex |
|
290 |
"return true, if anIndex is a valid key. |
|
291 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
|
292 |
however, having a common protocol with indexed collections |
|
293 |
often simplifies things." |
|
294 |
||
295 |
^ (anIndex >= 1) and:[anIndex <= self size] |
|
1 | 296 |
|
602 | 297 |
" |
298 |
#(1 2 3) includesKey:4 |
|
299 |
#(1 2 3) includesKey:3 |
|
300 |
" |
|
301 |
! |
|
1 | 302 |
|
602 | 303 |
size |
304 |
"redefined to re-enable size->basicSize forwarding |
|
305 |
(it is cought in SequencableCollection)" |
|
306 |
||
307 |
^ self basicSize |
|
1 | 308 |
! ! |
309 |
||
628 | 310 |
!ArrayedCollection class methodsFor:'documentation'! |
311 |
||
312 |
version |
|
313 |
^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.28 1995-11-23 16:57:30 cg Exp $' |
|
314 |
! ! |