author | Claus Gittinger <cg@exept.de> |
Mon, 23 Oct 2006 16:30:30 +0200 | |
changeset 10119 | 3d8028cacf74 |
parent 9086 | cd770483c45d |
child 10797 | 5408c171f52f |
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 |
||
5497 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
SequenceableCollection subclass:#ArrayedCollection |
1111 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Abstract' |
|
1 | 20 |
! |
21 |
||
2125 | 22 |
!ArrayedCollection class methodsFor:'documentation'! |
82 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 27 |
All Rights Reserved |
1 | 28 |
|
82 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
82 | 38 |
documentation |
39 |
" |
|
249 | 40 |
ArrayedCollection is an abstract superclass for all collections where |
1171 | 41 |
the elements can be accessed via an integer index, |
42 |
AND the collection is a fixed size collection. |
|
43 |
Those fixed size collections cannot easily grow, since they store the |
|
44 |
elements directly within the object and a grow operation can only be done |
|
45 |
by #becoming another object. |
|
360 | 46 |
(other collections keep a reference to the physical container, which |
47 |
can be easily replaced) |
|
48 |
||
2146 | 49 |
[Warning:] |
50 |
currently, ST/X supports growing fix-size collections |
|
51 |
(such as Arrays, ByteArrays and Strings). However, this |
|
52 |
is done in a very slow way (using #become). |
|
53 |
Become is a very slow operation in a direct-pointer smalltalk |
|
54 |
system. |
|
55 |
\ |
|
56 |
Therefore, you SHOULD rewrite any application that does this, |
|
57 |
to make use of OrderedCollections or any other collection which |
|
58 |
can grow faster. |
|
59 |
To remind you of that, a warning message is sent to the |
|
60 |
standard error whenever such an operation is performed (see #grow). |
|
61 |
\ |
|
62 |
Also note, that some other smalltalk systems do NOT allow |
|
63 |
fix size collection to change their size, and that future |
|
64 |
ST/X versions may be changed to trigger an error (instead of a |
|
65 |
warning) in those situations. |
|
1283 | 66 |
|
1289 | 67 |
[author:] |
2146 | 68 |
Claus Gittinger |
1289 | 69 |
|
1283 | 70 |
[see also:] |
2146 | 71 |
OrderedCollection Array |
82 | 72 |
" |
92 | 73 |
! ! |
74 |
||
2125 | 75 |
!ArrayedCollection class methodsFor:'instance creation'! |
1 | 76 |
|
5626 | 77 |
newFrom:aCollection |
78 |
"Return an instance of me containing the same elements as aCollection." |
|
79 |
||
80 |
|newArray idx sz| |
|
81 |
||
82 |
sz := aCollection size. |
|
83 |
newArray := self new:sz. |
|
84 |
aCollection isSequenceable ifTrue:[ |
|
85 |
newArray replaceFrom:1 to:sz with:aCollection startingAt:1 |
|
86 |
] ifFalse:[ |
|
87 |
idx := 1. |
|
88 |
aCollection do:[:element | |
|
89 |
newArray at:idx put:element. |
|
90 |
idx := idx + 1. |
|
91 |
]. |
|
92 |
]. |
|
93 |
^ newArray |
|
94 |
||
95 |
" |
|
96 |
Array newFrom: #[1 2 3] |
|
97 |
#[1 2 3] as: Array |
|
98 |
#[1 2 3] as: ByteArray |
|
99 |
#($c $h $r) as: String |
|
100 |
#($c $h $r) as: Text |
|
101 |
" |
|
102 |
! |
|
103 |
||
1 | 104 |
with:element |
105 |
"return a new SequenceableCollection with one element:anObject" |
|
106 |
||
107 |
|newCollection| |
|
108 |
||
109 |
newCollection := self new:1. |
|
110 |
newCollection at:1 put:element. |
|
111 |
^newCollection |
|
125 | 112 |
|
113 |
" |
|
114 |
OrderedCollection with:1 |
|
115 |
SortedCollection with:99 |
|
116 |
" |
|
1 | 117 |
! |
118 |
||
119 |
with:first with:second |
|
120 |
"return a new SequenceableCollection with two elements" |
|
121 |
||
122 |
|newCollection| |
|
123 |
||
124 |
newCollection := self new:2. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
125 |
newCollection at:1 put:first; at:2 put:second. |
1 | 126 |
^newCollection |
125 | 127 |
|
128 |
" |
|
129 |
OrderedCollection with:1 with:2 |
|
130 |
SortedCollection with:99 with:3 |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
131 |
Array with:1 with:2 |
125 | 132 |
" |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
133 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
134 |
"Modified: 22.1.1997 / 19:35:43 / cg" |
1 | 135 |
! |
136 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
137 |
with:a1 with:a2 with:a3 |
1 | 138 |
"return a new SequenceableCollection with three elements" |
139 |
||
140 |
|newCollection| |
|
141 |
||
142 |
newCollection := self new:3. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
143 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
144 |
^ newCollection |
125 | 145 |
|
146 |
" |
|
147 |
OrderedCollection with:1 with:2 with:3 |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
148 |
Array with:1 with:2 with:3 |
125 | 149 |
" |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
150 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
151 |
"Modified: 22.1.1997 / 19:35:47 / cg" |
1 | 152 |
! |
153 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
154 |
with:a1 with:a2 with:a3 with:a4 |
1 | 155 |
"return a new SequenceableCollection with four elements" |
156 |
||
157 |
|newCollection| |
|
158 |
||
159 |
newCollection := self new:4. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
160 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3; at:4 put:a4. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
161 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
162 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
163 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
164 |
OrderedCollection with:1 with:2 with:3 with:4 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
165 |
Array with:1 with:2 with:3 with:4 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
166 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
167 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
168 |
"Modified: 22.1.1997 / 19:35:52 / cg" |
1 | 169 |
! |
170 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
171 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
1 | 172 |
"return a new SequenceableCollection with five elements" |
173 |
||
174 |
|newCollection| |
|
175 |
||
176 |
newCollection := self new:5. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
177 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3; at:4 put:a4; |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
178 |
at:5 put:a5. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
179 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
180 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
181 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
182 |
OrderedCollection with:1 with:2 with:3 with:4 with:5 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
183 |
Array with:1 with:2 with:3 with:4 with:5 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
184 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
185 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
186 |
"Modified: 22.1.1997 / 19:35:57 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
187 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
188 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
189 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
190 |
"return a new SequenceableCollection with six elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
191 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
192 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
193 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
194 |
newCollection := self new:6. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
195 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3; at:4 put:a4; |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
196 |
at:5 put:a5; at:6 put:a6. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
197 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
198 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
199 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
200 |
OrderedCollection with:1 with:2 with:3 with:4 with:5 with:6 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
201 |
Array with:1 with:2 with:3 with:4 with:5 with:6 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
202 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
203 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
204 |
"Modified: 22.1.1997 / 19:36:03 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
205 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
206 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
207 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
208 |
"return a new SequenceableCollection with seven elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
209 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
210 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
211 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
212 |
newCollection := self new:7. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
213 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3; at:4 put:a4; |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
214 |
at:5 put:a5; at:6 put:a6; at:7 put:a7. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
215 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
216 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
217 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
218 |
OrderedCollection with:1 with:2 with:3 with:4 with:5 with:6 with:7 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
219 |
Array with:1 with:2 with:3 with:4 with:5 with:6 with:7 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
220 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
221 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
222 |
"Modified: 22.1.1997 / 19:36:20 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
223 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
224 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
225 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 with:a8 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
226 |
"return a new SequenceableCollection with eight elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
227 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
228 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
229 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
230 |
newCollection := self new:8. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
231 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3; at:4 put:a4; |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
232 |
at:5 put:a5; at:6 put:a6; at:7 put:a7; at:8 put:a8. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
233 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
234 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
235 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
236 |
OrderedCollection with:1 with:2 with:3 with:4 with:5 with:6 with:7 with:8 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
237 |
Array with:1 with:2 with:3 with:4 with:5 with:6 with:7 with:8 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
238 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
239 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
240 |
"Modified: 22.1.1997 / 19:35:21 / cg" |
1 | 241 |
! |
242 |
||
243 |
withAll:aCollection |
|
244 |
"return a new Collection with all elements taken from the argument, |
|
245 |
aCollection" |
|
246 |
||
247 |
|newCollection newSize |
|
248 |
index "{ Class: SmallInteger }" | |
|
249 |
||
250 |
newSize := aCollection size. |
|
251 |
newCollection := self new:newSize. |
|
359 | 252 |
aCollection isSequenceable ifTrue:[ |
1998 | 253 |
"/ |
254 |
"/ aCollection has indexed elements |
|
255 |
"/ a block-replace may be faster |
|
256 |
"/ |
|
257 |
newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1 |
|
1 | 258 |
] ifFalse:[ |
1998 | 259 |
"/ |
260 |
"/ must enumerate the elements individually |
|
261 |
"/ |
|
262 |
index := 1. |
|
263 |
aCollection do:[:element | |
|
264 |
newCollection at:index put:element. |
|
265 |
index := index + 1 |
|
266 |
] |
|
1 | 267 |
]. |
268 |
^ newCollection |
|
125 | 269 |
|
270 |
" |
|
271 |
OrderedCollection withAll:#(1 2 3 4 5) |
|
272 |
SortedCollection withAll:#(99 17 53 1 101) |
|
273 |
" |
|
1171 | 274 |
|
275 |
"Modified: 13.4.1996 / 12:14:38 / cg" |
|
7254 | 276 |
! |
277 |
||
278 |
withSize:size |
|
279 |
"return a new collection of size. |
|
280 |
For variable size collections, this is different from #new:, |
|
281 |
in that #new: creates an empty collection with preallocated size, |
|
282 |
while #withSize: creates a non empty one." |
|
283 |
||
284 |
^ self new:size. |
|
285 |
||
286 |
" |
|
287 |
(OrderedCollection new:10) |
|
288 |
(OrderedCollection withSize:10) |
|
289 |
(Array new:10) |
|
290 |
(Array withSize:10) |
|
291 |
" |
|
1 | 292 |
! ! |
293 |
||
2125 | 294 |
!ArrayedCollection class methodsFor:'queries'! |
68 | 295 |
|
602 | 296 |
growIsCheap |
297 |
"return true, if this collection can easily grow |
|
298 |
(i.e. without a need for become:). |
|
299 |
Since this is the superclass of all indexed fix-size collections, |
|
300 |
return false here." |
|
68 | 301 |
|
602 | 302 |
^ false |
8893 | 303 |
! |
304 |
||
305 |
isAbstract |
|
306 |
^ self == ArrayedCollection |
|
2 | 307 |
! ! |
308 |
||
249 | 309 |
!ArrayedCollection methodsFor:'copying'! |
310 |
||
311 |
copyEmptyAndGrow:size |
|
312 |
"return a new instance of the receivers species with size |
|
313 |
nilled elements and any named instance variables copied." |
|
314 |
||
315 |
"special case for Array, which has no named instance vars" |
|
316 |
||
356 | 317 |
|cls| |
318 |
||
319 |
(cls := self class) instSize == 0 ifTrue:[ |
|
320 |
^ cls new:size |
|
249 | 321 |
]. |
322 |
^ super copyEmptyAndGrow:size |
|
323 |
! ! |
|
324 |
||
602 | 325 |
!ArrayedCollection methodsFor:'error handling'! |
326 |
||
327 |
fixedSizeError |
|
1998 | 328 |
"{ Pragma: +optSpace }" |
329 |
||
602 | 330 |
"report an error that size of the collection cannot be changed. |
1171 | 331 |
This is not used right now (instead, a warning is sent to stderr |
1579 | 332 |
in the #grow method); however, future versions of ST/X may no longer |
1171 | 333 |
allow fixed size collection to grow. |
334 |
Read the documentation on why things are that way ..." |
|
602 | 335 |
|
336 |
^ self error:'cannot change size' |
|
1171 | 337 |
|
1579 | 338 |
"Modified: 18.7.1996 / 21:39:09 / cg" |
602 | 339 |
! ! |
340 |
||
341 |
!ArrayedCollection methodsFor:'printing & storing'! |
|
342 |
||
343 |
storeOn:aStream |
|
344 |
"output a printed representation (which can be re-read with readFrom:) |
|
345 |
onto the argument aStream. Redefined to output index access." |
|
346 |
||
347 |
|index "{ Class: SmallInteger }"| |
|
348 |
||
349 |
thisContext isRecursive ifTrue:[ |
|
5498 | 350 |
Object recursiveStoreStringSignal raiseRequestWith:self. |
2290 | 351 |
'ArrayedCollection [error]: storeOn: of self referencing collection.' errorPrintCR. |
352 |
aStream nextPutAll:'#recursive'. |
|
353 |
^ self |
|
602 | 354 |
]. |
355 |
||
356 |
aStream nextPutAll:'(('; nextPutAll:self class name; nextPutAll:' new:'. |
|
357 |
self size printOn:aStream. |
|
358 |
aStream nextPutAll:')'. |
|
359 |
index := 1. |
|
360 |
self do:[:element | |
|
2290 | 361 |
aStream nextPutAll:' at:'. index printOn:aStream. aStream nextPutAll:' put:('. |
362 |
element storeOn:aStream. |
|
363 |
aStream nextPutAll:');'. |
|
364 |
index := index + 1 |
|
602 | 365 |
]. |
366 |
index > 1 ifTrue:[aStream nextPutAll:' yourself']. |
|
367 |
aStream nextPut:$) |
|
368 |
||
369 |
" |
|
370 |
(Array with:(1@2) with:(1->2)) storeString |
|
371 |
" |
|
1414 | 372 |
|
2290 | 373 |
"Modified: 28.1.1997 / 00:39:59 / cg" |
602 | 374 |
! ! |
375 |
||
5557 | 376 |
!ArrayedCollection methodsFor:'queries'! |
377 |
||
378 |
size |
|
379 |
"redefined to re-enable size->basicSize forwarding |
|
380 |
(it is caught in SequencableCollection)" |
|
381 |
||
382 |
^ self basicSize |
|
6548 | 383 |
! |
384 |
||
385 |
speciesForAdding |
|
386 |
"redefined here, since grow is not cheap. |
|
387 |
Used by functions which create a growing collection (see collect:with:, for example)" |
|
388 |
||
389 |
^ OrderedCollection |
|
5557 | 390 |
! ! |
391 |
||
2 | 392 |
!ArrayedCollection methodsFor:'resizing'! |
1 | 393 |
|
2 | 394 |
grow:newSize |
125 | 395 |
"grow the receiver i.e. cut off everything after newSize. |
396 |
Warning: this may be a slow operation due to the use of become |
|
397 |
- you should write your collection classes to avoid the use of become. |
|
155 | 398 |
You have been warned." |
2 | 399 |
|
9086 | 400 |
|newArray oldSize| |
1 | 401 |
|
27 | 402 |
oldSize := self size. |
403 |
(newSize ~~ oldSize) ifTrue:[ |
|
2125 | 404 |
InfoPrinting ifTrue:[ |
405 |
"/ |
|
406 |
"/ output a warning - you should rewrite your application |
|
407 |
"/ to use some collection which implements grow: more efficient |
|
408 |
"/ (i.e. use OrderedCollection instead of Array ..) |
|
409 |
"/ |
|
9086 | 410 |
'ArrayedCollection [warning]: slow grow operation (' errorPrint. |
411 |
self class name infoPrint. ') ' errorPrintCR. |
|
412 |
self showWhereWeCameFrom. |
|
2125 | 413 |
]. |
360 | 414 |
|
2125 | 415 |
newArray := self species new:newSize. |
416 |
newArray replaceFrom:1 to:(newSize min:oldSize) with:self. |
|
417 |
self become:newArray. |
|
2 | 418 |
] |
125 | 419 |
|
420 |
" |
|
590
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
421 |
#(1 2 3 4 5 6) add:7 |
3ffb78fb2716
report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents:
528
diff
changeset
|
422 |
#(1 2 3 4 5 6) remove:5 |
125 | 423 |
#(1 2 3 4 5 6) copy grow:3 |
155 | 424 |
#(1 2 3 4 5 6) copy grow:10 |
125 | 425 |
'hello world' copy grow:5 |
155 | 426 |
'hello' copy grow:20 |
125 | 427 |
" |
1428 | 428 |
|
2125 | 429 |
"Modified: 10.1.1997 / 15:14:43 / cg" |
360 | 430 |
! |
431 |
||
432 |
removeAll |
|
1998 | 433 |
"{ Pragma: +optSpace }" |
434 |
||
1164 | 435 |
"remove all elements from the receiver. Returns the receiver. |
436 |
||
1111 | 437 |
For ArrayedCollections (which are actually fixed-size collections), |
438 |
this is a slow operation, since a #become: is required to update |
|
439 |
all owners. Better use a collection which is prepared for growing |
|
440 |
(i.e. an OrderedCollection). |
|
441 |
We output a warning message here, to remind you about that." |
|
442 |
||
2125 | 443 |
'ArrayedCollection [info]: slow removeAll operation (' infoPrint. |
1428 | 444 |
self class name infoPrint. ')' infoPrintCR. |
360 | 445 |
|
446 |
self become:(self copyEmpty) |
|
447 |
||
448 |
" |
|
449 |
#(1 2 3 4 5) copy removeAll |
|
450 |
#(1 2 3 4 5) removeAll |
|
451 |
" |
|
1111 | 452 |
|
2125 | 453 |
"Modified: 10.1.1997 / 15:14:55 / cg" |
2 | 454 |
! ! |
1 | 455 |
|
2125 | 456 |
!ArrayedCollection class methodsFor:'documentation'! |
628 | 457 |
|
458 |
version |
|
9086 | 459 |
^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.55 2006-01-31 22:27:02 cg Exp $' |
628 | 460 |
! ! |