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