author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 22:06:12 +0200 | |
changeset 24369 | e796791ea838 |
parent 22670 | f1dd82a47855 |
child 24381 | 1a1c13d81925 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
14058
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
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 |
||
19100 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
SequenceableCollection subclass:#ArrayedCollection |
14112 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Abstract' |
|
1 | 21 |
! |
22 |
||
2125 | 23 |
!ArrayedCollection class methodsFor:'documentation'! |
82 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
14058
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
28 |
All Rights Reserved |
1 | 29 |
|
82 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
82 | 39 |
documentation |
40 |
" |
|
20180 | 41 |
ArrayedCollection is an abstract superclass for all collections where |
1171 | 42 |
the elements can be accessed via an integer index, |
17122 | 43 |
AND the collection is a fixed size collection. |
44 |
||
20180 | 45 |
Those fixed size collections cannot easily grow, since they store the |
46 |
elements directly within the object and a grow operation can only be done |
|
1171 | 47 |
by #becoming another object. |
20180 | 48 |
(many other collections keep a reference to the physical container, |
17122 | 49 |
which can be easily replaced) |
360 | 50 |
|
20180 | 51 |
[Warning:] |
2146 | 52 |
currently, ST/X supports growing fix-size collections |
20180 | 53 |
(such as Arrays, ByteArrays and Strings). |
17122 | 54 |
However, this is done in a very slow way (using #become). |
55 |
Become is a very slow operation in a direct-pointer Smalltalk system |
|
56 |
(which ST/X and many other 'modern' Smalltalks are). |
|
57 |
||
2146 | 58 |
Therefore, you SHOULD rewrite any application that does this, |
17122 | 59 |
to make use of OrderedCollections or any other collection which can grow fast. |
19100 | 60 |
To remind you of that, a warning message is sent to stdErr, |
61 |
whenever such an operation is performed (see #grow). |
|
17122 | 62 |
|
20180 | 63 |
Also note that some other Smalltalk systems do NOT allow |
2146 | 64 |
fix size collection to change their size, and that future |
20180 | 65 |
ST/X versions may be changed to trigger an error |
19100 | 66 |
(instead of a warning) in those situations. |
1283 | 67 |
|
1289 | 68 |
[author:] |
2146 | 69 |
Claus Gittinger |
1289 | 70 |
|
1283 | 71 |
[see also:] |
2146 | 72 |
OrderedCollection Array |
82 | 73 |
" |
92 | 74 |
! ! |
75 |
||
2125 | 76 |
!ArrayedCollection class methodsFor:'instance creation'! |
1 | 77 |
|
5626 | 78 |
newFrom:aCollection |
79 |
"Return an instance of me containing the same elements as aCollection." |
|
80 |
||
81 |
|newArray idx sz| |
|
82 |
||
83 |
sz := aCollection size. |
|
84 |
newArray := self new:sz. |
|
85 |
aCollection isSequenceable ifTrue:[ |
|
86 |
newArray replaceFrom:1 to:sz with:aCollection startingAt:1 |
|
87 |
] ifFalse:[ |
|
88 |
idx := 1. |
|
89 |
aCollection do:[:element | |
|
90 |
newArray at:idx put:element. |
|
91 |
idx := idx + 1. |
|
92 |
]. |
|
93 |
]. |
|
94 |
^ newArray |
|
95 |
||
96 |
" |
|
97 |
Array newFrom: #[1 2 3] |
|
98 |
#[1 2 3] as: Array |
|
99 |
#[1 2 3] as: ByteArray |
|
100 |
#($c $h $r) as: String |
|
101 |
#($c $h $r) as: Text |
|
102 |
" |
|
103 |
! |
|
104 |
||
16711 | 105 |
newWithSize:size |
106 |
"return a new collection of size. |
|
107 |
For variable size collections, this is different from #new:, |
|
108 |
in that #new: creates an empty collection with preallocated size, |
|
109 |
while #withSize: creates a non empty one." |
|
110 |
||
111 |
^ self new:size. |
|
112 |
||
113 |
" |
|
114 |
(OrderedCollection new:10) |
|
115 |
(OrderedCollection newWithSize:10) |
|
116 |
(Array new:10) |
|
117 |
(Array newWithSize:10) |
|
118 |
" |
|
119 |
! |
|
120 |
||
1 | 121 |
with:element |
122 |
"return a new SequenceableCollection with one element:anObject" |
|
123 |
||
124 |
|newCollection| |
|
125 |
||
126 |
newCollection := self new:1. |
|
127 |
newCollection at:1 put:element. |
|
128 |
^newCollection |
|
125 | 129 |
|
130 |
" |
|
131 |
OrderedCollection with:1 |
|
132 |
SortedCollection with:99 |
|
133 |
" |
|
1 | 134 |
! |
135 |
||
136 |
with:first with:second |
|
137 |
"return a new SequenceableCollection with two elements" |
|
138 |
||
139 |
|newCollection| |
|
140 |
||
141 |
newCollection := self new:2. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
142 |
newCollection at:1 put:first; at:2 put:second. |
1 | 143 |
^newCollection |
125 | 144 |
|
145 |
" |
|
146 |
OrderedCollection with:1 with:2 |
|
147 |
SortedCollection with:99 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 |
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:43 / 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 |
1 | 155 |
"return a new SequenceableCollection with three elements" |
156 |
||
157 |
|newCollection| |
|
158 |
||
159 |
newCollection := self new:3. |
|
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. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
161 |
^ newCollection |
125 | 162 |
|
163 |
" |
|
164 |
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
|
165 |
Array with:1 with:2 with:3 |
125 | 166 |
" |
2225
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:47 / 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 |
1 | 172 |
"return a new SequenceableCollection with four elements" |
173 |
||
174 |
|newCollection| |
|
175 |
||
176 |
newCollection := self new:4. |
|
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 |
^ 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 |
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 |
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:52 / cg" |
1 | 186 |
! |
187 |
||
2225
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 |
1 | 189 |
"return a new SequenceableCollection with five elements" |
190 |
||
191 |
|newCollection| |
|
192 |
||
193 |
newCollection := self new:5. |
|
2225
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. |
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 |
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 |
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:35:57 / 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 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
207 |
"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
|
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:6. |
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. |
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 |
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 |
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:03 / 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 |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
225 |
"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
|
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:7. |
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. |
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 |
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 |
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:36:20 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
240 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
241 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
242 |
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
|
243 |
"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
|
244 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
245 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
246 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
247 |
newCollection := self new:8. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
248 |
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
|
249 |
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
|
250 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
251 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
252 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
253 |
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
|
254 |
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
|
255 |
" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
256 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2146
diff
changeset
|
257 |
"Modified: 22.1.1997 / 19:35:21 / cg" |
1 | 258 |
! |
259 |
||
14013
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
260 |
with:a1 with:a2 with:a3 with:a4 with:a5 with:a6 with:a7 with:a8 with:a9 |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
261 |
"return a new SequenceableCollection with nine elements" |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
262 |
|
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
263 |
|newCollection| |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
264 |
|
14015
579bff670366
changed: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
14013
diff
changeset
|
265 |
newCollection := self new:9. |
14013
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
266 |
newCollection at:1 put:a1; at:2 put:a2; at:3 put:a3; at:4 put:a4; |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
267 |
at:5 put:a5; at:6 put:a6; at:7 put:a7; at:8 put:a8; |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
268 |
at:9 put:a9. |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
269 |
^ newCollection |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
270 |
|
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
271 |
" |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
272 |
OrderedCollection with:1 with:2 with:3 with:4 with:5 with:6 with:7 with:8 with:9 |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
273 |
Array with:1 with:2 with:3 with:4 with:5 with:6 with:7 with:8w ith:9 |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
274 |
" |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
275 |
|
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
276 |
"Created: / 14-02-2012 / 17:43:07 / cg" |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
277 |
! |
412c81f15eb4
added: #with:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
11471
diff
changeset
|
278 |
|
1 | 279 |
withAll:aCollection |
280 |
"return a new Collection with all elements taken from the argument, |
|
281 |
aCollection" |
|
282 |
||
283 |
|newCollection newSize |
|
284 |
index "{ Class: SmallInteger }" | |
|
285 |
||
286 |
newSize := aCollection size. |
|
287 |
newCollection := self new:newSize. |
|
359 | 288 |
aCollection isSequenceable ifTrue:[ |
14058
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
289 |
"/ |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
290 |
"/ aCollection has indexed elements |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
291 |
"/ a block-replace may be faster |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
292 |
"/ |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
293 |
newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1 |
1 | 294 |
] ifFalse:[ |
14058
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
295 |
"/ |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
296 |
"/ must enumerate the elements individually |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
297 |
"/ |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
298 |
index := 1. |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
299 |
aCollection do:[:element | |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
300 |
newCollection at:index put:element. |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
301 |
index := index + 1 |
670252a21fb9
changed: #inspector2TabForHexDump
Claus Gittinger <cg@exept.de>
parents:
14057
diff
changeset
|
302 |
] |
1 | 303 |
]. |
304 |
^ newCollection |
|
125 | 305 |
|
306 |
" |
|
307 |
OrderedCollection withAll:#(1 2 3 4 5) |
|
308 |
SortedCollection withAll:#(99 17 53 1 101) |
|
309 |
" |
|
1171 | 310 |
|
311 |
"Modified: 13.4.1996 / 12:14:38 / cg" |
|
1 | 312 |
! ! |
313 |
||
2125 | 314 |
!ArrayedCollection class methodsFor:'queries'! |
68 | 315 |
|
602 | 316 |
growIsCheap |
317 |
"return true, if this collection can easily grow |
|
318 |
(i.e. without a need for become:). |
|
319 |
Since this is the superclass of all indexed fix-size collections, |
|
320 |
return false here." |
|
68 | 321 |
|
602 | 322 |
^ false |
8893 | 323 |
! |
324 |
||
325 |
isAbstract |
|
11230 | 326 |
"Return if this class is an abstract class. |
327 |
True is returned for ArrayedCollection here; false for subclasses. |
|
19458 | 328 |
Abstract subclasses must redefine this again." |
11230 | 329 |
|
8893 | 330 |
^ self == ArrayedCollection |
2 | 331 |
! ! |
332 |
||
17211 | 333 |
!ArrayedCollection methodsFor:'adding & removing'! |
10797 | 334 |
|
335 |
addAll:aCollection |
|
336 |
"add all elements of the argument, aCollection to the receiver. |
|
337 |
Returns the argument, aCollection (sigh). |
|
338 |
||
339 |
Redefined here, to perform only a single slow grow operation" |
|
340 |
||
341 |
|mySize| |
|
342 |
||
343 |
mySize := self size. |
|
344 |
self grow:mySize + aCollection size. |
|
345 |
||
346 |
aCollection do:[:element | |
|
347 |
mySize := mySize + 1. |
|
348 |
self at:mySize put:element |
|
349 |
]. |
|
350 |
^ aCollection |
|
351 |
||
352 |
" |
|
353 |
#(1 2 3 4) addAll:#(5 6 7 8); yourself |
|
354 |
" |
|
17212 | 355 |
! |
356 |
||
357 |
removeAll |
|
358 |
"{ Pragma: +optSpace }" |
|
359 |
||
360 |
"remove all elements from the receiver. Returns the receiver. |
|
361 |
||
362 |
For ArrayedCollections (which are actually fixed-size collections), |
|
363 |
this is a slow operation, since a #become: is required to update |
|
364 |
all owners. Better use a collection which is prepared for growing |
|
365 |
(i.e. an OrderedCollection). |
|
366 |
We output a warning message here, to remind you about that." |
|
367 |
||
368 |
'ArrayedCollection [info]: slow removeAll operation (' infoPrint. |
|
369 |
self class name infoPrint. ')' infoPrintCR. |
|
370 |
||
371 |
self become:(self copyEmpty) |
|
372 |
||
373 |
" |
|
374 |
#(1 2 3 4 5) copy removeAll |
|
375 |
#(1 2 3 4 5) removeAll |
|
376 |
" |
|
377 |
||
378 |
"Modified: 10.1.1997 / 15:14:55 / cg" |
|
22670 | 379 |
! |
380 |
||
381 |
removeAllSuchThat:aBlock |
|
382 |
"remove all elements that meet a test criteria as specified in aBlock. |
|
383 |
The argument, aBlock is evaluated for successive elements and all those, |
|
384 |
for which it returns true, are removed. |
|
385 |
Return a collection containing the removed elements. |
|
386 |
Redefined to do a single become operation." |
|
387 |
||
388 |
|keptElements removedElements element sz "{ Class:SmallInteger }"| |
|
389 |
||
390 |
sz := self size. |
|
391 |
keptElements := self speciesForAdding new:sz. |
|
392 |
||
393 |
1 to:sz do:[:index | |
|
394 |
element := self at:index. |
|
395 |
(aBlock value:element) ifTrue:[ |
|
396 |
removedElements isNil ifTrue:[ |
|
397 |
removedElements := self speciesForAdding new. |
|
398 |
]. |
|
399 |
removedElements add:element. |
|
400 |
] ifFalse:[ |
|
401 |
keptElements add:element. |
|
402 |
]. |
|
403 |
]. |
|
404 |
||
405 |
removedElements isNil ifTrue:[ |
|
406 |
^ #(). |
|
407 |
]. |
|
408 |
||
409 |
'ArrayedCollection [info]: slow removeAllSuchThat operation (' infoPrint. |
|
410 |
self class name infoPrint. ')' infoPrintCR. |
|
411 |
||
412 |
keptElements := (self species withAll:keptElements) postCopyFrom:self. |
|
413 |
self become:keptElements. |
|
414 |
^ removedElements |
|
415 |
||
416 |
" |
|
417 |
|coll| |
|
418 |
||
419 |
coll := Array withAll:(1 to:10). |
|
420 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
|
421 |
Transcript showCR:coll |
|
422 |
" |
|
423 |
||
424 |
"Created: / 13-04-2018 / 14:02:24 / stefan" |
|
10797 | 425 |
! ! |
426 |
||
249 | 427 |
!ArrayedCollection methodsFor:'copying'! |
428 |
||
429 |
copyEmptyAndGrow:size |
|
14615 | 430 |
"return a new instance of the receiver's species with size |
249 | 431 |
nilled elements and any named instance variables copied." |
432 |
||
14112 | 433 |
"Performance optimization for classes like Array, String, ..., |
434 |
which have no named instance vars" |
|
249 | 435 |
|
14112 | 436 |
self class instSize == 0 ifTrue:[ |
437 |
^ self species new:size |
|
249 | 438 |
]. |
439 |
^ super copyEmptyAndGrow:size |
|
440 |
! ! |
|
441 |
||
602 | 442 |
!ArrayedCollection methodsFor:'error handling'! |
443 |
||
444 |
fixedSizeError |
|
1998 | 445 |
"{ Pragma: +optSpace }" |
446 |
||
602 | 447 |
"report an error that size of the collection cannot be changed. |
1171 | 448 |
This is not used right now (instead, a warning is sent to stderr |
1579 | 449 |
in the #grow method); however, future versions of ST/X may no longer |
1171 | 450 |
allow fixed size collection to grow. |
451 |
Read the documentation on why things are that way ..." |
|
602 | 452 |
|
453 |
^ self error:'cannot change size' |
|
1171 | 454 |
|
1579 | 455 |
"Modified: 18.7.1996 / 21:39:09 / cg" |
602 | 456 |
! ! |
457 |
||
17212 | 458 |
!ArrayedCollection methodsFor:'growing'! |
459 |
||
460 |
grow:newSize |
|
461 |
"grow the receiver i.e. cut off everything after newSize. |
|
462 |
Warning: this may be a slow operation due to the use of become |
|
463 |
- you should write your collection classes to avoid the use of become. |
|
464 |
You have been warned." |
|
465 |
||
466 |
|newArray oldSize| |
|
467 |
||
468 |
oldSize := self size. |
|
469 |
(newSize ~~ oldSize) ifTrue:[ |
|
470 |
InfoPrinting ifTrue:[ |
|
471 |
"/ |
|
472 |
"/ output a warning - you should rewrite your application |
|
473 |
"/ to use some collection which implements grow: more efficient |
|
474 |
"/ (i.e. use OrderedCollection instead of Array ..) |
|
475 |
"/ |
|
476 |
'ArrayedCollection [warning]: slow grow operation (' errorPrint. |
|
477 |
self class name infoPrint. ') ' errorPrintCR. |
|
478 |
Context showWhereWeCameFrom. |
|
479 |
]. |
|
480 |
||
481 |
newArray := self species new:newSize. |
|
482 |
newArray replaceFrom:1 to:(newSize min:oldSize) with:self. |
|
483 |
self become:newArray. |
|
484 |
] |
|
485 |
||
486 |
" |
|
487 |
#(1 2 3 4 5 6) add:7 |
|
488 |
#(1 2 3 4 5 6) remove:5 |
|
489 |
#(1 2 3 4 5 6) copy grow:3 |
|
490 |
#(1 2 3 4 5 6) copy grow:10 |
|
491 |
'hello world' copy grow:5 |
|
492 |
'hello' copy grow:20 |
|
493 |
" |
|
494 |
||
495 |
"Modified: 10.1.1997 / 15:14:43 / cg" |
|
496 |
! ! |
|
497 |
||
14112 | 498 |
|
602 | 499 |
!ArrayedCollection methodsFor:'printing & storing'! |
500 |
||
501 |
storeOn:aStream |
|
502 |
"output a printed representation (which can be re-read with readFrom:) |
|
503 |
onto the argument aStream. Redefined to output index access." |
|
504 |
||
505 |
|index "{ Class: SmallInteger }"| |
|
506 |
||
507 |
thisContext isRecursive ifTrue:[ |
|
15855 | 508 |
RecursiveStoreError raiseRequestWith:self. |
2290 | 509 |
'ArrayedCollection [error]: storeOn: of self referencing collection.' errorPrintCR. |
510 |
aStream nextPutAll:'#recursive'. |
|
511 |
^ self |
|
602 | 512 |
]. |
513 |
||
514 |
aStream nextPutAll:'(('; nextPutAll:self class name; nextPutAll:' new:'. |
|
515 |
self size printOn:aStream. |
|
21481 | 516 |
aStream nextPut:$). |
602 | 517 |
index := 1. |
518 |
self do:[:element | |
|
2290 | 519 |
aStream nextPutAll:' at:'. index printOn:aStream. aStream nextPutAll:' put:('. |
520 |
element storeOn:aStream. |
|
521 |
aStream nextPutAll:');'. |
|
522 |
index := index + 1 |
|
602 | 523 |
]. |
524 |
index > 1 ifTrue:[aStream nextPutAll:' yourself']. |
|
525 |
aStream nextPut:$) |
|
526 |
||
527 |
" |
|
528 |
(Array with:(1@2) with:(1->2)) storeString |
|
529 |
" |
|
1414 | 530 |
|
21481 | 531 |
"Modified: / 28-01-1997 / 00:39:59 / cg" |
532 |
"Modified: / 17-02-2017 / 10:51:53 / stefan" |
|
602 | 533 |
! ! |
534 |
||
5557 | 535 |
!ArrayedCollection methodsFor:'queries'! |
536 |
||
537 |
size |
|
538 |
"redefined to re-enable size->basicSize forwarding |
|
539 |
(it is caught in SequencableCollection)" |
|
540 |
||
541 |
^ self basicSize |
|
6548 | 542 |
! |
543 |
||
544 |
speciesForAdding |
|
545 |
"redefined here, since grow is not cheap. |
|
546 |
Used by functions which create a growing collection (see collect:with:, for example)" |
|
547 |
||
548 |
^ OrderedCollection |
|
5557 | 549 |
! ! |
550 |
||
2125 | 551 |
!ArrayedCollection class methodsFor:'documentation'! |
628 | 552 |
|
553 |
version |
|
19100 | 554 |
^ '$Header$' |
17212 | 555 |
! |
556 |
||
557 |
version_CVS |
|
19100 | 558 |
^ '$Header$' |
628 | 559 |
! ! |
14615 | 560 |