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