author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 09 Nov 2010 16:24:28 +0000 | |
branch | jv |
changeset 17807 | 06cc6c49e291 |
parent 17800 | 142da80d8c82 |
child 17814 | b75a7f0c346b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
6135 | 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 |
" |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
13 |
|
1 | 14 |
Object subclass:#Collection |
11331 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
|
17 |
NotEnoughElementsSignal RecursiveCollectionStoreStringSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Abstract' |
|
1 | 20 |
! |
21 |
||
2097 | 22 |
!Collection class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6135 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
Abstract superclass for all collections. |
|
41 |
This abstract class provides functionality common to all collections, |
|
42 |
without knowing how the concrete class implements things. Thus, all |
|
43 |
methods found here depend on some basic mechanisms to be defined in the |
|
44 |
concrete class. |
|
45 |
These basic methods are usually defined as #subclassResponsibility here. |
|
46 |
Some methods are also redefined for better performance. |
|
1289 | 47 |
|
1325 | 48 |
Subclasses should at least implement: |
49 |
do: - enumerate elements |
|
50 |
||
51 |
they should implement one of the following set of access messages: |
|
52 |
keyed collections: |
|
53 |
at:ifAbsent: - fetching an element |
|
54 |
at: - fetching an element |
|
55 |
at:put: - storing an element |
|
56 |
||
57 |
unkeyed collections: |
|
58 |
add: - add an element |
|
59 |
remove:ifAbsent: - remove an element |
|
60 |
||
61 |
||
1289 | 62 |
[author:] |
63 |
Claus Gittinger |
|
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
2097 | 67 |
!Collection class methodsFor:'initialization'! |
10 | 68 |
|
69 |
initialize |
|
70 |
"setup the signal" |
|
71 |
||
54 | 72 |
InvalidKeySignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
73 |
InvalidKeySignal := Error newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
74 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
75 |
InvalidKeySignal notifierString:'invalid key:'. |
345 | 76 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
77 |
ValueNotFoundSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
78 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
79 |
ValueNotFoundSignal notifierString:'value not found:'. |
155 | 80 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
81 |
NotEnoughElementsSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
82 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
83 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
348 | 84 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
85 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
86 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
87 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
54 | 88 |
] |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
89 |
|
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
90 |
"Modified: / 8.11.1997 / 19:18:17 / cg" |
10 | 91 |
! ! |
92 |
||
2097 | 93 |
!Collection class methodsFor:'instance creation'! |
155 | 94 |
|
17800 | 95 |
combiningEach:collection1 withEach:collection2 using:aTwoArgBlock |
96 |
"evaluate aTwoArgBlock for each combination of elements from collection1 |
|
97 |
and collection2 and return an instance of the receiver containing all those elements" |
|
98 |
||
99 |
|newColl idx useIndex newEl| |
|
100 |
||
101 |
newColl := self new:(collection1 size * collection2 size). |
|
102 |
useIndex := newColl isFixedSize. |
|
103 |
idx := 1. |
|
104 |
collection1 do:[:e1 | |
|
105 |
collection2 do:[:e2 | |
|
106 |
newEl := aTwoArgBlock value:e1 value:e2. |
|
107 |
||
108 |
useIndex ifTrue:[ |
|
109 |
newColl at:idx put:(aTwoArgBlock value:e1 value:e2). |
|
110 |
idx := idx + 1. |
|
111 |
] ifFalse:[ |
|
112 |
newColl add:(aTwoArgBlock value:e1 value:e2) |
|
113 |
]. |
|
114 |
] |
|
115 |
]. |
|
116 |
^ newColl |
|
117 |
||
118 |
" |
|
119 |
Set combiningEach:#(1 2 3 4 5) withEach:(10 to:100 by:10) using:[:a :b | a * b] |
|
120 |
Set combiningEach:#(1 2 3 4 5) withEach:#(1 2 3 4 5) using:[:a :b | a * b] |
|
121 |
" |
|
122 |
||
123 |
"Created: / 25-08-2010 / 17:21:47 / cg" |
|
124 |
! |
|
125 |
||
602 | 126 |
new:size withAll:element |
5626 | 127 |
"return a new Collection of size, where all elements are |
602 | 128 |
initialized to element" |
348 | 129 |
|
602 | 130 |
|newCollection| |
92 | 131 |
|
602 | 132 |
newCollection := self new:size. |
133 |
size timesRepeat:[newCollection add:element] |
|
134 |
! |
|
1 | 135 |
|
5626 | 136 |
newFrom:aCollection |
137 |
"Return an instance of me containing the same elements as aCollection." |
|
138 |
||
139 |
^ self withAll:aCollection |
|
140 |
||
141 |
" |
|
142 |
Bag newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
143 |
Set newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
144 |
" |
|
145 |
! |
|
146 |
||
1 | 147 |
with:anObject |
148 |
"return a new Collection with one element:anObject" |
|
149 |
||
150 |
|newCollection| |
|
151 |
||
152 |
newCollection := self new. |
|
153 |
newCollection add:anObject. |
|
154 |
^ newCollection |
|
155 |
! |
|
156 |
||
157 |
with:firstObject with:secondObject |
|
158 |
"return a new Collection with two elements:firstObject and secondObject" |
|
159 |
||
160 |
|newCollection| |
|
161 |
||
162 |
newCollection := self new. |
|
1112 | 163 |
newCollection add:firstObject; add:secondObject. |
1 | 164 |
^ newCollection |
165 |
! |
|
166 |
||
167 |
with:firstObject with:secondObject with:thirdObject |
|
168 |
"return a new Collection with three elements" |
|
169 |
||
170 |
|newCollection| |
|
171 |
||
172 |
newCollection := self new. |
|
1112 | 173 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 174 |
^ newCollection |
175 |
! |
|
176 |
||
177 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
178 |
"return a new Collection with four elements" |
|
179 |
||
180 |
|newCollection| |
|
181 |
||
182 |
newCollection := self new. |
|
1112 | 183 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 184 |
^ newCollection |
185 |
! |
|
186 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
187 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
345 | 188 |
"return a new Collection with five elements" |
189 |
||
190 |
|newCollection| |
|
191 |
||
192 |
newCollection := self new. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
193 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
194 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
195 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
196 |
"Modified: 22.1.1997 / 19:34:01 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
197 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
198 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
199 |
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:
2167
diff
changeset
|
200 |
"return a new Collection with size elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
201 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
202 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
203 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
204 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
205 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6. |
345 | 206 |
^ newCollection |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
207 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
208 |
"Created: 22.1.1997 / 19:34:14 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
209 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
210 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
211 |
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:
2167
diff
changeset
|
212 |
"return a new Collection with seven elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
213 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
214 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
215 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
216 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
217 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
218 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
219 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
220 |
"Created: 22.1.1997 / 19:34:24 / cg" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
221 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
222 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
223 |
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:
2167
diff
changeset
|
224 |
"return a new Collection with eight elements" |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
225 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
226 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
227 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
228 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
229 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7; add:a8. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
230 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
231 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
232 |
"Created: 22.1.1997 / 19:34:34 / cg" |
345 | 233 |
! |
234 |
||
1 | 235 |
withAll:aCollection |
236 |
"return a new Collection with all elements taken from the argument, |
|
237 |
aCollection" |
|
238 |
||
239 |
|newCollection| |
|
240 |
||
241 |
newCollection := self new. |
|
242 |
newCollection addAll:aCollection. |
|
243 |
^newCollection |
|
9159 | 244 |
! |
245 |
||
246 |
withSize:n |
|
247 |
"return a new collection which really provides space for n elements. |
|
248 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
249 |
||
250 |
^ self new:n |
|
251 |
! ! |
|
252 |
||
253 |
!Collection class methodsFor:'Compatibility-Squeak'! |
|
254 |
||
255 |
ofSize:n |
|
256 |
"return a new collection which really provides space for n elements. |
|
257 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
258 |
||
259 |
^ self withSize:n |
|
1 | 260 |
! ! |
261 |
||
2097 | 262 |
!Collection class methodsFor:'Signal constants'! |
10 | 263 |
|
602 | 264 |
emptyCollectionSignal |
265 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 266 |
|
602 | 267 |
^ EmptyCollectionSignal |
308 | 268 |
! |
269 |
||
602 | 270 |
invalidKeySignal |
271 |
"return the signal used to report bad key usage" |
|
308 | 272 |
|
602 | 273 |
^ InvalidKeySignal |
201 | 274 |
! |
275 |
||
602 | 276 |
notEnoughElementsSignal |
277 |
"return the signal used to report attempts for an operation, for which |
|
278 |
there are not enough elements in the collection" |
|
249 | 279 |
|
602 | 280 |
^ NotEnoughElementsSignal |
404 | 281 |
! |
282 |
||
602 | 283 |
valueNotFoundSignal |
284 |
"return the signal used to report a nonexisting element." |
|
404 | 285 |
|
602 | 286 |
^ ValueNotFoundSignal |
287 |
! ! |
|
404 | 288 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
289 |
!Collection class methodsFor:'misc ui support'! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
290 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
291 |
iconInBrowserSymbol |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
292 |
<resource: #programImage> |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
293 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
294 |
^ #containerClassBrowserIcon |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
295 |
! ! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
296 |
|
2097 | 297 |
!Collection class methodsFor:'queries'! |
404 | 298 |
|
602 | 299 |
growIsCheap |
300 |
"return true, if this collection can easily grow |
|
301 |
(i.e. without a need for become:). |
|
302 |
Returns true here; this method is redefined in fix-size |
|
303 |
collections" |
|
404 | 304 |
|
602 | 305 |
^ true |
8893 | 306 |
! |
307 |
||
308 |
isAbstract |
|
11231 | 309 |
"Return if this class is an abstract class. |
310 |
True is returned for Collection here; false for subclasses. |
|
311 |
Abstract subclasses must redefine again." |
|
312 |
||
8893 | 313 |
^ self == Collection |
257 | 314 |
! ! |
315 |
||
7261 | 316 |
!Collection methodsFor:'Compatibility-Dolphin'! |
6411 | 317 |
|
318 |
identityIncludes:anObject |
|
7807 | 319 |
"return true, if the argument, anObject is in the collection. |
320 |
Same as #includesIdentical for Dolphin compatibility." |
|
321 |
||
6411 | 322 |
^ self includesIdentical:anObject. |
6869 | 323 |
! |
324 |
||
325 |
includesAnyOf:aCollection |
|
326 |
"same as #includesAny for Dolphin compatibility." |
|
327 |
||
328 |
^ self includesAny:aCollection |
|
329 |
||
330 |
" |
|
331 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
332 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
333 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
334 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
335 |
" |
|
17735 | 336 |
! |
337 |
||
338 |
symmetricDifference:aCollection |
|
339 |
"return a new set containing all elements, |
|
340 |
which are contained in either the receiver or aCollection, but not in both. |
|
341 |
Same as xor: - for compatibility" |
|
342 |
||
343 |
^ self xor:aCollection |
|
344 |
||
345 |
" |
|
346 |
|c1 c2| |
|
347 |
||
348 |
c1 := #( foo bar baz baloo ). |
|
349 |
c2 := #( foe bar banana baloo ). |
|
350 |
c1 symmetricDifference:c2. |
|
351 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
352 |
" |
|
6411 | 353 |
! ! |
354 |
||
7261 | 355 |
!Collection methodsFor:'Compatibility-Squeak'! |
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
356 |
|
9354 | 357 |
addIfNotPresent:anObject |
7011 | 358 |
"Include anObject as one of the receiver's elements, but only if there |
359 |
is no such element already. Anwser anObject." |
|
360 |
||
361 |
(self includes: anObject) ifFalse: [self add: anObject]. |
|
362 |
^ anObject |
|
363 |
! |
|
364 |
||
6149 | 365 |
anyOne |
8858 | 366 |
"return any element from the collection. |
367 |
Report an error if there is none. |
|
6149 | 368 |
Same as #anElement - for Squeak compatibility" |
369 |
||
370 |
^ self anElement |
|
371 |
! |
|
372 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
373 |
difference: aCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
374 |
"Answer the set-theoretic difference of two collections." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
375 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
376 |
^ self reject:[:each | aCollection includes: each] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
377 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
378 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
379 |
#(0 2 4 6 8) difference:#(2 4) |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
380 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
381 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
382 |
|
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
383 |
gather:aBlock |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
384 |
"return an Array, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
385 |
containing all elements as returned from applying aBlock to each element if the receiver, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
386 |
where the block returns a collection of to-be-added elements. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
387 |
This could also be called: collectAllAsArray:" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
388 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
389 |
^ self gather:aBlock as:Array |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
390 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
391 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
392 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
393 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
394 |
! |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
395 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
396 |
gather:aBlock as:aClass |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
397 |
"return an instance of the collection-class aClass, |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
398 |
containing all elements as returned from applying aBlock to each element if the receiver. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
399 |
where the block returns a collection of to-be-added elements. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
400 |
This could also be called: collectAll:as:" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
401 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
402 |
^ aClass streamContents:[:s | |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
403 |
self do: [:ea | s nextPutAll: (aBlock value: ea)] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
404 |
] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
405 |
|
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
406 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
407 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] as:OrderedCollection |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
408 |
(Set withAll:#(10 20 30 10 20 40)) collectAll:[:el | Array with:el with:el * 2] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
409 |
" |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
410 |
! |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
411 |
|
9354 | 412 |
groupBy:keyBlock having:selectBlock |
17742 | 413 |
"Like in SQL operation - Split the receiver's contents into collections of |
9354 | 414 |
elements for which keyBlock returns the same results, and return those |
415 |
collections allowed by selectBlock. " |
|
416 |
||
9319 | 417 |
|result key| |
17742 | 418 |
|
9319 | 419 |
result := OrderedDictionary new. |
17742 | 420 |
self do:[:e | |
421 |
key := keyBlock value: e. |
|
422 |
(result includesKey: key) ifFalse: [ |
|
423 |
result at: key put: OrderedCollection new |
|
424 |
]. |
|
425 |
(result at: key) add: e |
|
426 |
]. |
|
9319 | 427 |
^ result := result select: selectBlock |
428 |
||
9354 | 429 |
" |
430 |
#(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a |true] |
|
431 |
" |
|
9319 | 432 |
! |
433 |
||
9065 | 434 |
ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
435 |
|action| |
|
436 |
||
437 |
action := self isEmpty ifTrue:[ ifEmptyValue ] ifFalse:[ ifNotEmptyValue ]. |
|
438 |
||
439 |
action isBlock ifTrue:[ |
|
440 |
action numArgs == 1 ifTrue:[ |
|
441 |
^ action value:self |
|
442 |
] |
|
443 |
]. |
|
444 |
^ action value |
|
445 |
! |
|
446 |
||
447 |
ifEmpty:ifEmptyValue ifNotEmptyDo:ifNotEmptyValue |
|
448 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
449 |
! |
|
450 |
||
451 |
ifEmptyDo:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
452 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
453 |
! |
|
454 |
||
455 |
ifNotEmpty:ifNotEmptyValue |
|
456 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
457 |
! |
|
458 |
||
459 |
ifNotEmptyDo:ifNotEmptyValue |
|
460 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
461 |
! |
|
462 |
||
463 |
ifNotEmptyDo:ifNotEmptyValue ifEmpty:ifEmptyValue |
|
464 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
465 |
! |
|
466 |
||
11280 | 467 |
intersection:aCollection |
468 |
^ self intersect:aCollection |
|
469 |
||
470 |
"Created: / 22-10-2008 / 21:29:27 / cg" |
|
471 |
! |
|
472 |
||
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
473 |
withIndexDo:aTwoArgBlock |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
474 |
"evaluate the argument, aBlock for every element in the collection, |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
475 |
passing both element and index as arguments." |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
476 |
|
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
477 |
^ self keysAndValuesDo:[:key :index | |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
478 |
aTwoArgBlock value:index value:key |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
479 |
]. |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
480 |
! ! |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
481 |
|
1 | 482 |
!Collection methodsFor:'accessing'! |
483 |
||
602 | 484 |
anElement |
6966 | 485 |
"return any element from the collection, |
486 |
report an error if there is none" |
|
602 | 487 |
|
488 |
self do: [:each | ^ each]. |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
489 |
self emptyCollectionError. |
602 | 490 |
! |
491 |
||
17797 | 492 |
at:aKey ifNilOrAbsentPut:valueBlock |
493 |
"try to fetch the element at aKey. If either the key is invalid (as in a Dictionary) |
|
494 |
or there is no element stored under that key (as in an Array), set the element |
|
495 |
from the valueBlock and return it. |
|
496 |
Useful for lazy initialization of collections." |
|
497 |
||
498 |
|val| |
|
499 |
||
500 |
val := self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ]. |
|
501 |
val isNil ifTrue:[ |
|
502 |
self at:aKey put:(val := valueBlock value). |
|
503 |
]. |
|
504 |
^ val |
|
505 |
||
506 |
" |
|
507 |
|d| |
|
508 |
||
509 |
d := Dictionary new. |
|
510 |
d at:#foo ifNilOrAbsentPut:[ 'hello' ]. |
|
511 |
d |
|
512 |
" |
|
513 |
||
514 |
" |
|
515 |
|a| |
|
516 |
||
517 |
a := Array new:10. |
|
518 |
a at:1 ifNilOrAbsentPut:[ 'hello' ]. |
|
519 |
a |
|
520 |
" |
|
521 |
||
522 |
"Created: / 24-08-2010 / 17:07:21 / cg" |
|
523 |
! |
|
524 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
525 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
526 |
"put anObject into all indexes from indexCollection in the receiver. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
527 |
This abstract implementation requires that the receiver supports |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
528 |
access via a key (and indexCollection contains valid keys). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
529 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
530 |
Notice: This operation modifies the receiver, NOT a copy; |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
531 |
therefore the change may affect all others referencing the receiver." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
532 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
533 |
indexCollection do:[:index | self at:index put:anObject] |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
534 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
535 |
" |
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
536 |
args: indexCollection : <Collection of keys<object> > |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
537 |
returns: self |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
538 |
" |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
539 |
|
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
540 |
" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
541 |
(Array new:10) atAll:(1 to:5) put:0 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
542 |
(Array new:10) atAll:#(1 5 6 9) put:0 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
543 |
(Dictionary new) atAll:#(foo bar baz) put:'hello' |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
544 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
545 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
546 |
(Set new) atAll:#(foo bar baz) put:'hello' |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
547 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
548 |
|
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
549 |
"Modified: / 20.5.1998 / 14:50:46 / cg" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
550 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
551 |
|
4985 | 552 |
fifth |
553 |
"return the fifth element of the collection. |
|
554 |
For unordered collections, this simply returns the fifth |
|
555 |
element when enumerating them. |
|
556 |
This should be redefined in subclasses." |
|
557 |
||
558 |
^ self nth:5 |
|
559 |
||
560 |
" |
|
561 |
#(1 2 3 4 5) fifth |
|
562 |
" |
|
563 |
! |
|
564 |
||
68 | 565 |
first |
566 |
"return the first element of the collection. |
|
4855 | 567 |
For unordered collections, this simply returns the first |
568 |
element when enumerating them. |
|
68 | 569 |
This should be redefined in subclasses." |
570 |
||
571 |
self do:[:e | ^ e]. |
|
572 |
||
573 |
"error if collection is empty" |
|
155 | 574 |
^ self emptyCollectionError |
68 | 575 |
! |
576 |
||
5887 | 577 |
first:n |
578 |
"return the n first elements of the collection. |
|
579 |
Raises an error if there are not enough elements in the receiver. |
|
580 |
For unordered collections, this simply returns the first |
|
581 |
n elements when enumerating them |
|
582 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
583 |
This should be redefined in subclasses." |
|
584 |
||
585 |
|coll remain| |
|
586 |
||
587 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
588 |
||
589 |
coll := OrderedCollection new. |
|
590 |
remain := n. |
|
591 |
self do:[:e | |
|
592 |
coll add:e. |
|
593 |
remain := remain - 1. |
|
594 |
remain == 0 ifTrue:[ |
|
595 |
^ coll |
|
596 |
]. |
|
597 |
]. |
|
598 |
||
599 |
"error if collection has not enough elements" |
|
600 |
^ self notEnoughElementsError |
|
601 |
||
602 |
" |
|
603 |
#(1 2 3 4 5) first:3 |
|
604 |
#(1 2 3 4 5) asSet first:3 |
|
605 |
" |
|
606 |
! |
|
607 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
608 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
609 |
"return the first element of the collection. |
10593 | 610 |
If its empty, return the exceptionValue. |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
611 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
612 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
613 |
self isEmpty ifTrue:[^ exceptionValue value]. |
10593 | 614 |
^ self first |
615 |
||
616 |
"Modified: / 04-06-2007 / 22:36:14 / cg" |
|
617 |
! |
|
618 |
||
619 |
firstOrNil |
|
620 |
"return the first element of the collection. |
|
621 |
If its empty, return nil. |
|
622 |
(i.e. dont trigger an error as done in #first)" |
|
623 |
||
624 |
^ self firstIfEmpty:nil |
|
625 |
||
626 |
"Created: / 04-06-2007 / 22:36:07 / cg" |
|
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
627 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
628 |
|
4985 | 629 |
fourth |
630 |
"return the fourth element of the collection. |
|
631 |
For unordered collections, this simply returns the fourth |
|
632 |
element when enumerating them. |
|
633 |
This should be redefined in subclasses." |
|
634 |
||
635 |
^ self nth:4 |
|
636 |
||
637 |
" |
|
638 |
#(1 2 3 4) fourth |
|
639 |
" |
|
640 |
! |
|
641 |
||
68 | 642 |
last |
643 |
"return the last element of the collection. |
|
644 |
This should be redefined in subclasses." |
|
645 |
||
646 |
|theLastOne any| |
|
647 |
||
648 |
any := false. |
|
649 |
self do:[:e | any := true. theLastOne := e]. |
|
650 |
any ifTrue:[ |
|
6135 | 651 |
^ theLastOne |
68 | 652 |
]. |
653 |
||
654 |
"error if collection is empty" |
|
155 | 655 |
^ self emptyCollectionError |
68 | 656 |
! |
657 |
||
5887 | 658 |
last:n |
659 |
"return the n last elements of the collection. |
|
660 |
Raises an error if there are not enough elements in the receiver. |
|
661 |
For unordered collections, this simply returns the last |
|
662 |
n elements when enumerating them |
|
663 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
664 |
This should be redefined in subclasses since the implementation here is VERY inefficient." |
|
665 |
||
666 |
|coll remain| |
|
667 |
||
668 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
669 |
||
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
670 |
coll := OrderedCollection new:n. |
5887 | 671 |
remain := n. |
672 |
self do:[:e | |
|
673 |
remain > 0 ifTrue:[ |
|
674 |
remain := remain - 1. |
|
675 |
] ifFalse:[ |
|
676 |
coll removeFirst. |
|
677 |
]. |
|
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
678 |
coll add:e. |
5887 | 679 |
]. |
680 |
||
681 |
remain ~~ 0 ifTrue:[ |
|
682 |
"error if collection has not enough elements" |
|
683 |
^ self notEnoughElementsError |
|
684 |
]. |
|
685 |
^ coll |
|
686 |
||
687 |
" |
|
688 |
#(1 2 3 4 5) last:3 |
|
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
689 |
#(1 2 3 4 5 6 7 8 9 0) asSet last:3 |
5887 | 690 |
'hello world' last:5 |
691 |
" |
|
692 |
! |
|
693 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
694 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
695 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
696 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
697 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
698 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
699 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
700 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
701 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
702 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
703 |
"Created: 6.2.1996 / 15:27:49 / cg" |
4855 | 704 |
! |
705 |
||
9073 | 706 |
median |
707 |
"Return the middle element, or as close as we can get." |
|
708 |
||
709 |
^ self asSortedCollection median |
|
710 |
||
711 |
" |
|
712 |
#(10 35 20 45 30 5) median |
|
713 |
" |
|
714 |
! |
|
715 |
||
4985 | 716 |
nth:n |
717 |
"return the nth element of the collection. |
|
718 |
For unordered collections, this simply returns the nth |
|
719 |
element when enumerating them. |
|
720 |
This should be redefined in subclasses." |
|
721 |
||
722 |
|count| |
|
723 |
||
724 |
count := 1. |
|
725 |
self do:[:e | |
|
726 |
count == n ifTrue:[^ e]. |
|
727 |
count := count + 1 |
|
728 |
]. |
|
729 |
||
5266 | 730 |
"error if collection is smaller" |
731 |
^ self notEnoughElementsError |
|
4985 | 732 |
|
733 |
" |
|
734 |
#(1 2 3 4) nth:3 |
|
5266 | 735 |
#(1 2 3 4) nth:5 |
736 |
||
737 |
#(1 2 3 4) asSet nth:3 |
|
738 |
#(1 2 3 4) asSet nth:5 |
|
4985 | 739 |
" |
740 |
! |
|
741 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
742 |
order |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
743 |
"report an error that only OrderedXXX's have an order" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
744 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
745 |
self shouldNotImplement |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
746 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
747 |
|
4855 | 748 |
second |
749 |
"return the second element of the collection. |
|
750 |
For unordered collections, this simply returns the second |
|
751 |
element when enumerating them. |
|
7129 | 752 |
This should be redefined in subclasses." |
753 |
||
754 |
^ self nth:2 |
|
4855 | 755 |
|
4856 | 756 |
" |
757 |
#(1 2 3) second |
|
758 |
" |
|
6135 | 759 |
! |
760 |
||
761 |
seventh |
|
762 |
"return the seventh element of the collection. |
|
763 |
For unordered collections, this simply returns the sixth |
|
764 |
element when enumerating them. |
|
765 |
This should be redefined in subclasses." |
|
766 |
||
767 |
^ self nth:7 |
|
768 |
||
769 |
" |
|
770 |
#(1 2 3 4 5 6 7) seventh |
|
771 |
" |
|
772 |
||
773 |
"Created: / 1.11.2001 / 16:43:29 / cg" |
|
4856 | 774 |
! |
775 |
||
4985 | 776 |
sixth |
777 |
"return the sixth element of the collection. |
|
778 |
For unordered collections, this simply returns the sixth |
|
779 |
element when enumerating them. |
|
780 |
This should be redefined in subclasses." |
|
781 |
||
782 |
^ self nth:6 |
|
783 |
||
784 |
" |
|
785 |
#(1 2 3 4 5 6 7) sixth |
|
786 |
" |
|
787 |
! |
|
788 |
||
4856 | 789 |
third |
790 |
"return the third element of the collection. |
|
791 |
For unordered collections, this simply returns the third |
|
792 |
element when enumerating them. |
|
793 |
This should be redefined in subclasses." |
|
794 |
||
4985 | 795 |
^ self nth:3 |
4856 | 796 |
|
797 |
||
798 |
" |
|
799 |
#(1 2 3) third |
|
800 |
" |
|
17747 | 801 |
! |
802 |
||
803 |
values |
|
804 |
"return a collection containing all values of the receiver. |
|
805 |
This is to make value access to an OrderedDictionary compatible with any-Collection" |
|
806 |
||
807 |
|aCollection| |
|
808 |
||
809 |
aCollection := OrderedCollection new. |
|
810 |
self do:[:value| aCollection add:value]. |
|
811 |
^ aCollection |
|
1 | 812 |
! ! |
813 |
||
814 |
!Collection methodsFor:'adding & removing'! |
|
815 |
||
816 |
add:anObject |
|
88 | 817 |
"add the argument, anObject to the receiver. |
818 |
If the receiver is ordered, the position of the new element is undefined |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
819 |
(i.e. don't depend on where it will be put). |
2351 | 820 |
An error is raised here - it is to be implemented by a concrete subclass." |
1 | 821 |
|
822 |
^ self subclassResponsibility |
|
2351 | 823 |
|
824 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 825 |
! |
826 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
827 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
828 |
"add the argument, anObject anInteger times to the receiver. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
829 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
830 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
831 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
832 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
833 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
834 |
"Created: 11.5.1996 / 12:13:48 / cg" |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
835 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
836 |
|
1 | 837 |
addAll:aCollection |
1163 | 838 |
"add all elements of the argument, aCollection to the receiver. |
7752 | 839 |
Returns the argument, aCollection (sigh)." |
1 | 840 |
|
841 |
aCollection do:[:element | |
|
1163 | 842 |
self add:element |
1 | 843 |
]. |
844 |
^ aCollection |
|
88 | 845 |
|
846 |
" |
|
847 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
848 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
849 |
" |
|
1163 | 850 |
|
851 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 852 |
! |
853 |
||
12 | 854 |
addAllFirst:aCollection |
855 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 856 |
of the receiver. Returns the argument, aCollection." |
12 | 857 |
|
858 |
aCollection reverseDo:[:element | |
|
1163 | 859 |
self addFirst:element |
12 | 860 |
]. |
861 |
^ aCollection |
|
862 |
||
863 |
" |
|
864 |
|c| |
|
865 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 866 |
c addAllFirst:#(9 8 7 6 5). |
867 |
c |
|
12 | 868 |
" |
1163 | 869 |
|
870 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 871 |
! |
872 |
||
602 | 873 |
addAllLast:aCollection |
1163 | 874 |
"add all elements of the argument, aCollection to the receiver. |
875 |
Returns the argument, aCollection." |
|
602 | 876 |
|
877 |
aCollection do:[:element | |
|
1163 | 878 |
self addLast:element |
602 | 879 |
]. |
880 |
^ aCollection |
|
881 |
||
882 |
" |
|
883 |
|c| |
|
884 |
c := #(4 3 2 1) asOrderedCollection. |
|
885 |
c addAllLast:#(9 8 7 6 5) |
|
886 |
" |
|
1163 | 887 |
|
888 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 889 |
! |
890 |
||
891 |
addFirst:anObject |
|
892 |
"add the argument, anObject to the receiver. |
|
2351 | 893 |
If the receiver is ordered, the new element will be added at the beginning. |
894 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 895 |
|
896 |
^ self subclassResponsibility |
|
2351 | 897 |
|
898 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 899 |
! |
900 |
||
602 | 901 |
addLast:anObject |
902 |
"add the argument, anObject to the receiver. |
|
1163 | 903 |
If the receiver is ordered, the new element will be added at the end. |
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
904 |
Return the argument, anObject. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
905 |
|
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
906 |
This usually has the same semantics as #add:. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
907 |
OrderedSet and OrderedDictionary redefine this, to move anObject to |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
908 |
the end, even if it is already present in the collection." |
602 | 909 |
|
910 |
^ self add:anObject |
|
1163 | 911 |
|
912 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 913 |
! |
914 |
||
3124 | 915 |
contents:aCollection |
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
916 |
"set my contents from aCollection |
5265 | 917 |
- this may be redefined in a concrete subclass for more performance" |
3124 | 918 |
|
6042
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
919 |
aCollection == self ifTrue:[ |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
920 |
"/ self error:'should not happen'. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
921 |
^ self |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
922 |
]. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
923 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
924 |
self removeAll. |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
925 |
aCollection notNil ifTrue:[ |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
926 |
self addAll:aCollection |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
927 |
] |
3124 | 928 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
929 |
"Modified: / 17.8.1998 / 10:18:43 / cg" |
3124 | 930 |
! |
931 |
||
1 | 932 |
remove:anObject |
2351 | 933 |
"search for the first element, which is equal to anObject; |
934 |
if found, remove and return it. |
|
935 |
If not found, report a 'value not found'-error. |
|
936 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 937 |
|
1163 | 938 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
939 |
||
2351 | 940 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 941 |
! |
942 |
||
602 | 943 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 944 |
"search for the first element, which is equal to anObject; |
945 |
if found, remove and return it. |
|
946 |
If not found, return the the value of the exceptionBlock. |
|
947 |
Uses equality compare (=) to search for the occurrence. |
|
948 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 949 |
|
950 |
^ self subclassResponsibility |
|
2351 | 951 |
|
952 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 953 |
! |
954 |
||
134 | 955 |
removeAll |
1163 | 956 |
"remove all elements from the receiver. Returns the receiver. |
134 | 957 |
This should be reimplemented in subclasses for better |
958 |
performance." |
|
959 |
||
2383 | 960 |
[self isEmpty] whileFalse:[ |
1163 | 961 |
self removeFirst |
134 | 962 |
]. |
1163 | 963 |
|
2383 | 964 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 965 |
! |
966 |
||
1 | 967 |
removeAll:aCollection |
17795 | 968 |
"remove all elements from the receiver which are equal to any in aCollection. |
1163 | 969 |
Return the argument, aCollection. |
5267 | 970 |
Raises an error, if some element-to-remove is not in the receiver. |
971 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1163 | 972 |
|
360 | 973 |
Notice: for some collections (those not tuned for |
1163 | 974 |
resizing themself) this may be very slow. |
975 |
If the number of removed elements is big compared to to |
|
976 |
the receivers size, it may be better to copy the |
|
977 |
ones which are not to be removed into a new collection." |
|
1 | 978 |
|
979 |
aCollection do:[:element | self remove:element]. |
|
980 |
^ aCollection |
|
1163 | 981 |
|
5268 | 982 |
" |
983 |
|coll| |
|
984 |
||
985 |
coll := #(1 2 3 4 5 6) asSet. |
|
986 |
coll removeAll:#(4 5 6). |
|
987 |
coll |
|
988 |
" |
|
989 |
||
990 |
"raises an error: |
|
991 |
|coll| |
|
992 |
||
993 |
coll := #(1 2 3 4 5 6) asSet. |
|
994 |
coll removeAll:#(4 5 6 7 8). |
|
995 |
coll |
|
996 |
" |
|
997 |
||
998 |
"no error raised: |
|
999 |
|coll| |
|
1000 |
||
1001 |
coll := #(1 2 3 4 5 6) asSet. |
|
1002 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1003 |
coll |
|
1004 |
" |
|
1005 |
||
17795 | 1006 |
"Modified: / 05-08-2010 / 13:50:33 / cg" |
348 | 1007 |
! |
1008 |
||
5265 | 1009 |
removeAllFoundIn:aCollection |
17795 | 1010 |
"remove all elements from the receiver which are equal to any in aCollection. |
5267 | 1011 |
No error is raised, if some element-to-remove is not in the receiver. |
1012 |
(see also: #removeAll:, which does raise an error)." |
|
4980 | 1013 |
|
1014 |
aCollection do:[:each | self remove:each ifAbsent:[]]. |
|
5265 | 1015 |
^ aCollection |
5268 | 1016 |
|
1017 |
" |
|
1018 |
|coll| |
|
1019 |
||
1020 |
coll := #(1 2 3 4 5 6) asSet. |
|
1021 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1022 |
coll |
|
1023 |
" |
|
1024 |
||
1025 |
"raises an error: |
|
1026 |
|coll| |
|
1027 |
||
1028 |
coll := #(1 2 3 4 5 6) asSet. |
|
1029 |
coll removeAll:#(4 5 6 7 8). |
|
1030 |
coll |
|
1031 |
" |
|
1032 |
||
17795 | 1033 |
"Modified: / 05-08-2010 / 13:51:05 / cg" |
1034 |
! |
|
1035 |
||
1036 |
removeAllIdentical:aCollection |
|
1037 |
"remove all elements from the receiver which are in aCollection. |
|
1038 |
Return the argument, aCollection. |
|
1039 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1040 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1041 |
||
1042 |
Notice: for some collections (those not tuned for |
|
1043 |
resizing themself) this may be very slow. |
|
1044 |
If the number of removed elements is big compared to to |
|
1045 |
the receivers size, it may be better to copy the |
|
1046 |
ones which are not to be removed into a new collection." |
|
1047 |
||
1048 |
aCollection do:[:element | self removeIdentical:element]. |
|
1049 |
^ aCollection |
|
1050 |
||
1051 |
" |
|
1052 |
|coll| |
|
1053 |
||
1054 |
coll := #(1 2 3 4 5 6) asSet. |
|
1055 |
coll removeAll:#(4 5 6). |
|
1056 |
coll |
|
1057 |
" |
|
1058 |
||
1059 |
"raises an error: |
|
1060 |
|coll| |
|
1061 |
||
1062 |
coll := #(1 2 3 4 5 6) asSet. |
|
1063 |
coll removeAll:#(4 5 6 7 8). |
|
1064 |
coll |
|
1065 |
" |
|
1066 |
||
1067 |
"no error raised: |
|
1068 |
|coll| |
|
1069 |
||
1070 |
coll := #(1 2 3 4 5 6) asSet. |
|
1071 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1072 |
coll |
|
1073 |
" |
|
1074 |
||
1075 |
"Created: / 05-08-2010 / 13:51:51 / cg" |
|
1076 |
! |
|
1077 |
||
1078 |
removeAllIdenticalFoundIn:aCollection |
|
1079 |
"remove all elements from the receiver which are in aCollection. |
|
1080 |
No error is raised, if some element-to-remove is not in the receiver. |
|
1081 |
(see also: #removeAll:, which does raise an error)." |
|
1082 |
||
1083 |
aCollection do:[:each | self removeIdentical:each ifAbsent:[]]. |
|
1084 |
^ aCollection |
|
1085 |
||
1086 |
" |
|
1087 |
|coll| |
|
1088 |
||
1089 |
coll := #(1 2 3 4 5 6) asSet. |
|
1090 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1091 |
coll |
|
1092 |
" |
|
1093 |
||
1094 |
"raises an error: |
|
1095 |
|coll| |
|
1096 |
||
1097 |
coll := #(1 2 3 4 5 6) asSet. |
|
1098 |
coll removeAll:#(4 5 6 7 8). |
|
1099 |
coll |
|
1100 |
" |
|
1101 |
||
1102 |
"Created: / 05-08-2010 / 13:52:21 / cg" |
|
4980 | 1103 |
! |
1104 |
||
17746 | 1105 |
removeAllKeys:aCollection |
1106 |
"remove all keys of the argument, aCollection from the receiver. |
|
1107 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1108 |
Notice: only works for keyed collections, such as dictionaries." |
|
1109 |
||
1110 |
aCollection do:[:element | self removeKey:element]. |
|
1111 |
! |
|
1112 |
||
5265 | 1113 |
removeAllSuchThat:aBlock |
4980 | 1114 |
"Apply the condition to each element and remove it if the condition is true. |
5265 | 1115 |
Return a collection of removed elements. |
5267 | 1116 |
First elements-to-remove are collected, then removed in one operation." |
4980 | 1117 |
|
5267 | 1118 |
|removedElements| |
4980 | 1119 |
|
5267 | 1120 |
removedElements := OrderedCollection new. |
1121 |
self do:[:element | |
|
4980 | 1122 |
(aBlock value:element) ifTrue: [ |
5267 | 1123 |
removedElements add:element |
4980 | 1124 |
] |
1125 |
]. |
|
5267 | 1126 |
self removeAll:removedElements. |
1127 |
^ removedElements |
|
1128 |
||
1129 |
" |
|
1130 |
|coll| |
|
1131 |
||
1132 |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection. |
|
1133 |
coll removeAllSuchThat:[:el | el even]. |
|
1134 |
coll |
|
1135 |
" |
|
1136 |
" |
|
1137 |
|coll| |
|
1138 |
||
1139 |
coll := #(1 2 3 4 5 6 7 8 9 10) asSet. |
|
1140 |
coll removeAllSuchThat:[:el | el even]. |
|
1141 |
coll |
|
1142 |
" |
|
1143 |
||
4980 | 1144 |
! |
1145 |
||
348 | 1146 |
removeFirst |
1147 |
"remove the first element from the receiver. |
|
1148 |
Return the removed element." |
|
1149 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1150 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1151 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1152 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1153 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1154 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1155 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1156 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1157 |
(Set with:3 with:2 with:1) removeFirst |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1158 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1159 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1160 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 1161 |
! |
1162 |
||
1163 |
removeFirst:n |
|
1164 |
"remove the first n elements from the receiver. |
|
5265 | 1165 |
Return a collection of removed elements. |
360 | 1166 |
Notice: for some collections (those not tuned for |
5265 | 1167 |
resizing themself) this may be very slow." |
348 | 1168 |
|
1169 |
|ret| |
|
1170 |
||
1171 |
self size < n ifTrue:[ |
|
5265 | 1172 |
^ self notEnoughElementsError |
348 | 1173 |
]. |
1174 |
ret := Array new:n. |
|
1175 |
1 to:n do:[:i | |
|
5265 | 1176 |
ret at:i put:(self removeFirst). |
348 | 1177 |
]. |
1178 |
^ ret |
|
1179 |
! |
|
1180 |
||
2351 | 1181 |
removeIdentical:anObject |
1182 |
"search for the first element, which is identical to anObject; |
|
1183 |
if found, remove and return it. |
|
1184 |
If not found, report a 'value not found'-error. |
|
1185 |
Uses identity compare (==) to search for the occurrence." |
|
1186 |
||
1187 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
1188 |
||
1189 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
1190 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
1191 |
! |
|
1192 |
||
1193 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
1194 |
"search for the first element, which is identical to anObject; |
|
1195 |
if found, remove and return it. |
|
1196 |
If not found, return the the value of the exceptionBlock. |
|
1197 |
Uses identity compare (==) to search for the occurrence. |
|
1198 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1199 |
||
1200 |
^ self subclassResponsibility |
|
1201 |
||
1202 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
1203 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
1204 |
! |
|
1205 |
||
602 | 1206 |
removeLast |
1207 |
"remove the last element from the receiver. |
|
2351 | 1208 |
Return the removed element. |
1209 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 1210 |
|
1211 |
^ self subclassResponsibility |
|
2351 | 1212 |
|
1213 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 1214 |
! |
1215 |
||
348 | 1216 |
removeLast:n |
11331 | 1217 |
"remove the last n elements from the receiver collection. |
5265 | 1218 |
Return a collection of removed elements. |
11331 | 1219 |
Notice: for some collections this may be very slow |
1220 |
(those not tuned for resizing themself)." |
|
348 | 1221 |
|
1222 |
|ret| |
|
1223 |
||
1224 |
self size < n ifTrue:[ |
|
5265 | 1225 |
^ self notEnoughElementsError |
348 | 1226 |
]. |
1227 |
ret := Array new:n. |
|
1228 |
n to:1 by:-1 do:[:i | |
|
5265 | 1229 |
ret at:i put:(self removeLast). |
348 | 1230 |
]. |
1231 |
^ ret |
|
1 | 1232 |
! ! |
1233 |
||
8858 | 1234 |
!Collection methodsFor:'bulk operations'! |
1235 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1236 |
abs |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1237 |
"Absolute value of all elements in the collection" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1238 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1239 |
^ self collect: [:a | a abs] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1240 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1241 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1242 |
TestCase assert:( { 1. -2. -3. 4 } abs = { 1. 2. 3. 4 }). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1243 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1244 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1245 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1246 |
negated |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1247 |
"Negated value of all elements in the collection" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1248 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1249 |
^ self collect:[:a | a negated] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1250 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1251 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1252 |
TestCase assert:( { 1. -2. -3. 4 } negated = { -1. 2. 3. -4 }). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1253 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1254 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1255 |
|
11468 | 1256 |
product |
11498 | 1257 |
"multiply up all elements." |
8858 | 1258 |
|
11468 | 1259 |
self emptyCheck. |
1260 |
^ self |
|
11469 | 1261 |
inject:(self first class unity) |
1262 |
into:[:accum :each | accum * each]. |
|
8858 | 1263 |
|
1264 |
" |
|
11468 | 1265 |
TestCase should:[ Array new sum ] raise:Error. |
1266 |
||
1267 |
TestCase assert:( { 1 } product == 1). |
|
1268 |
TestCase assert:( { 6 } product == 6). |
|
1269 |
TestCase assert:( { 1. 2. 3. 4. 5. } product = 5 factorial ) |
|
1270 |
" |
|
1271 |
! |
|
1272 |
||
1273 |
sum |
|
11498 | 1274 |
"sum up all elements; return 0 for an empty collection." |
11468 | 1275 |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1276 |
self isEmpty ifTrue:[^ 0]. |
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1277 |
|
11468 | 1278 |
^ self |
1279 |
inject:(self first class zero) |
|
1280 |
into:[:accum :each | accum + each]. |
|
1281 |
||
1282 |
||
1283 |
" |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1284 |
TestCase assert: ( { } sum = 0 ). |
11468 | 1285 |
TestCase assert: ( { 1 } sum = 1 ). |
1286 |
TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ). |
|
1287 |
TestCase assert: ( (1 to:10) sum = 55 ). |
|
1288 |
TestCase assert: ( 'abc' asByteArray sum = 294 ). |
|
1289 |
TestCase assert: ( { 10 +/- 2. |
|
1290 |
20 +/- 4. |
|
1291 |
100 +/- 10 } sum = (130 +/- 16) ). |
|
1292 |
||
1293 |
TestCase assert: ( { (1 / 9). |
|
1294 |
(1 / 7). |
|
1295 |
} sum = (16 / 63) ). |
|
8858 | 1296 |
" |
11514 | 1297 |
! |
1298 |
||
1299 |
sum:aBlock |
|
17797 | 1300 |
"for each element in the receiver, evaluate the argument, aBlock and sum up the results. |
1301 |
Return the total sum or 0 for an empty collection. |
|
11514 | 1302 |
Similar to (self collect...) sum, but avoids creation of an intermediate collection." |
1303 |
||
1304 |
|sum| |
|
1305 |
||
1306 |
self do:[:element | |
|
1307 |
|thisValue| |
|
1308 |
||
1309 |
thisValue := aBlock value:element. |
|
1310 |
sum isNil ifTrue:[ |
|
1311 |
sum := thisValue |
|
1312 |
] ifFalse:[ |
|
1313 |
sum := sum + thisValue |
|
1314 |
]. |
|
1315 |
]. |
|
1316 |
^ sum ? 0 |
|
1317 |
||
1318 |
" |
|
1319 |
((1 to:10) collect:[:n | n squared]) sum |
|
1320 |
((1 to:10) sum:[:n | n squared]) |
|
1321 |
" |
|
17797 | 1322 |
|
1323 |
"Modified: / 23-08-2010 / 18:19:42 / cg" |
|
8858 | 1324 |
! ! |
1325 |
||
602 | 1326 |
!Collection methodsFor:'converting'! |
1327 |
||
1328 |
asArray |
|
1329 |
"return a new Array with the collections elements" |
|
1330 |
||
1331 |
|anArray |
|
1332 |
index "{ Class: SmallInteger }" | |
|
1 | 1333 |
|
602 | 1334 |
anArray := Array new:(self size). |
1335 |
index := 1. |
|
1336 |
self do:[:each | |
|
6135 | 1337 |
anArray at:index put:each. |
1338 |
index := index + 1 |
|
602 | 1339 |
]. |
1340 |
^ anArray |
|
1 | 1341 |
! |
1342 |
||
602 | 1343 |
asBag |
1344 |
"return a new Bag with the receiver collections elements" |
|
1 | 1345 |
|
602 | 1346 |
^ self addAllTo:(Bag new) |
1 | 1347 |
! |
1348 |
||
602 | 1349 |
asByteArray |
1350 |
"return a new ByteArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1351 |
(which must convert to 8bit integers in the range 0..255)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1352 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1353 |
^ self asIntegerArray:ByteArray |
1 | 1354 |
! |
1355 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1356 |
asCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1357 |
"return myself as a Collection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1358 |
I am already a Collection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1359 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1360 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1361 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1362 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1363 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1364 |
|
7906 | 1365 |
asDictionary |
1366 |
"return a new Dictionary with the receiver collections elements" |
|
1367 |
||
1368 |
|d| |
|
1369 |
||
1370 |
d := Dictionary new. |
|
1371 |
self keysAndValuesDo:[:k :v | d at:k put:v]. |
|
1372 |
^ d |
|
1373 |
||
1374 |
" |
|
1375 |
#(10 20 30 40 50 60 70 80 90) asDictionary |
|
1376 |
" |
|
1377 |
! |
|
1378 |
||
602 | 1379 |
asDoubleArray |
1380 |
"return a new DoubleArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1381 |
(which must convert to 64bit floats)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1382 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1383 |
^ self asIntegerArray:DoubleArray |
602 | 1384 |
! |
68 | 1385 |
|
11412 | 1386 |
asFlatOrderedCollection |
1387 |
|coll| |
|
1388 |
||
1389 |
coll := OrderedCollection new. |
|
1390 |
self flatDo:[:el | coll add:el]. |
|
1391 |
^ coll. |
|
1392 |
||
1393 |
" |
|
1394 |
#( |
|
1395 |
(1 2 3) |
|
1396 |
4 5 |
|
1397 |
(6) |
|
1398 |
7 |
|
1399 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) asFlatOrderedCollection |
|
1400 |
" |
|
1401 |
! |
|
1402 |
||
602 | 1403 |
asFloatArray |
1404 |
"return a new FloatArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1405 |
(which must convert to 32bit floats)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1406 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1407 |
^ self asIntegerArray:FloatArray |
602 | 1408 |
! |
1409 |
||
1410 |
asIdentitySet |
|
1411 |
"return a new IdentitySet with the receiver collections elements" |
|
1412 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1413 |
^ self addAllNonNilElementsTo:(IdentitySet new:self size) |
68 | 1414 |
! |
1415 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1416 |
asIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1417 |
"return a new IntegerArray with the collection's elements |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1418 |
(which must convert to 32bit integers in the range 0..16rFFFFFFFF)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1419 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1420 |
^ self asIntegerArray:IntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1421 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1422 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1423 |
asIntegerArray:arrayClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1424 |
"return a new Array with the collection's elements" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1425 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1426 |
|anIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1427 |
index "{ Class: SmallInteger }" | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1428 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1429 |
anIntegerArray := arrayClass new:(self size). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1430 |
index := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1431 |
self do:[:each | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1432 |
anIntegerArray at:index put:each asInteger. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1433 |
index := index + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1434 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1435 |
^ anIntegerArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1436 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1437 |
|
2167 | 1438 |
asList |
1439 |
"return a new List with the receiver collections elements" |
|
1440 |
||
1441 |
^ self addAllTo:(List new:self size) |
|
1442 |
||
1443 |
"Created: 14.2.1997 / 16:25:23 / cg" |
|
1444 |
! |
|
1445 |
||
8050 | 1446 |
asLongIntegerArray |
1447 |
"return a new LongIntegerArray with the collections elements |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1448 |
(which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1449 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1450 |
^ self asIntegerArray:LongIntegerArray |
8050 | 1451 |
! |
1452 |
||
602 | 1453 |
asOrderedCollection |
1454 |
"return a new OrderedCollection with the receiver collections elements" |
|
1455 |
||
1456 |
^ self addAllTo:(OrderedCollection new:self size) |
|
1457 |
! |
|
1458 |
||
11469 | 1459 |
asRunArray |
1460 |
"return a new RunArray with the collections elements" |
|
1461 |
||
1462 |
^ RunArray from:self. |
|
1463 |
||
1464 |
"/ |runs lastElement occurrences| |
|
1465 |
"/ |
|
1466 |
"/ runs := RunArray new. |
|
1467 |
"/ occurrences := 0. |
|
1468 |
"/ self do:[:each | |
|
1469 |
"/ each == lastElement ifTrue:[ |
|
1470 |
"/ occurrences := occurrences + 1 |
|
1471 |
"/ ] ifFalse:[ |
|
1472 |
"/ runs add:lastElement withOccurrences:occurrences. |
|
1473 |
"/ occurrences := 1. |
|
1474 |
"/ lastElement := each |
|
1475 |
"/ ]. |
|
1476 |
"/ ]. |
|
1477 |
"/ occurrences ~~ 0 ifTrue:[ |
|
1478 |
"/ runs add:lastElement withOccurrences:occurrences |
|
1479 |
"/ ]. |
|
1480 |
"/ ^ runs |
|
1394 | 1481 |
|
1482 |
" |
|
11469 | 1483 |
#(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray |
1394 | 1484 |
" |
11469 | 1485 |
|
1486 |
"Modified: / 7.4.1998 / 09:50:54 / cg" |
|
1394 | 1487 |
! |
1488 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1489 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1490 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1491 |
I am already a Collection, but not sequenceable." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1492 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1493 |
^ self asArray |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1494 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1495 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1496 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1497 |
|
602 | 1498 |
asSet |
1499 |
"return a new Set with the receiver collections elements" |
|
1500 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1501 |
^ self addAllNonNilElementsTo:(Set new:self size) |
602 | 1502 |
! |
1503 |
||
9353 | 1504 |
asSharedCollection |
1505 |
"return a shared collection on the receiver. |
|
1506 |
This implements synchronized access to me." |
|
1507 |
||
1508 |
^ SharedCollection for:self. |
|
1509 |
! |
|
1510 |
||
602 | 1511 |
asSortedCollection |
1512 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 1513 |
|
602 | 1514 |
|aSortedCollection| |
1515 |
||
1516 |
aSortedCollection := SortedCollection new:self size. |
|
1517 |
aSortedCollection addAll:self. |
|
1518 |
^ aSortedCollection |
|
1519 |
! |
|
1520 |
||
1521 |
asSortedCollection:sortBlock |
|
1522 |
"return a new SortedCollection with the receiver collections elements, |
|
1523 |
using sortBlock for comparing" |
|
1524 |
||
1525 |
|aSortedCollection| |
|
1526 |
||
1527 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
1528 |
aSortedCollection addAll:self. |
|
1529 |
^ aSortedCollection |
|
1 | 1530 |
! |
1531 |
||
2919 | 1532 |
asSortedStrings |
1533 |
"Create & return a SortedCollection that sorts the receivers |
|
1534 |
elements according to the locales collating policy. |
|
1535 |
This is currently not really support - strings are sorted |
|
1536 |
without caring for the locale." |
|
1537 |
||
1538 |
|aSortedCollection| |
|
1539 |
||
1540 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1541 |
aSortedCollection addAll:self. |
|
1542 |
^ aSortedCollection |
|
1543 |
||
1544 |
"Created: 13.9.1997 / 09:36:22 / cg" |
|
1545 |
"Modified: 13.9.1997 / 09:43:00 / cg" |
|
1546 |
! |
|
1547 |
||
1548 |
asSortedStrings:sortBlock |
|
1549 |
"Create & return a SortedCollection that sorts the receivers |
|
3061 | 1550 |
elements using sortBlock and according to the locales collating policy, |
1551 |
which is passed as first arg to sortBlock. |
|
2919 | 1552 |
This is currently not really support - strings are sorted |
1553 |
without caring for the locale." |
|
1554 |
||
1555 |
|aSortedCollection| |
|
1556 |
||
1557 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1558 |
aSortedCollection sortBlock:sortBlock. |
|
1559 |
aSortedCollection addAll:self. |
|
1560 |
^ aSortedCollection |
|
1561 |
||
3061 | 1562 |
"Created: / 13.9.1997 / 09:36:45 / cg" |
1563 |
"Modified: / 27.10.1997 / 16:39:48 / cg" |
|
2919 | 1564 |
! |
1565 |
||
1566 |
asSortedStrings:sortBlock with:aCollationPolicy |
|
1567 |
"Create & return a SortedCollection that sorts the receivers |
|
1568 |
elements using sortBlock and according to the specified locales collating policy. |
|
1569 |
This is currently not really support - strings are sorted |
|
1570 |
without caring for the locale." |
|
1571 |
||
1572 |
|aSortedCollection| |
|
1573 |
||
1574 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1575 |
aSortedCollection sortBlock:sortBlock. |
|
1576 |
aSortedCollection addAll:self. |
|
1577 |
^ aSortedCollection |
|
1578 |
||
1579 |
"Created: 13.9.1997 / 09:37:21 / cg" |
|
1580 |
"Modified: 13.9.1997 / 09:45:27 / cg" |
|
1581 |
! |
|
1582 |
||
1583 |
asSortedStringsWith: aCollationPolicy |
|
1584 |
"Create & return a SortedCollection that sorts the receivers |
|
1585 |
elements according to the specified locales collating policy. |
|
1586 |
This is currently not really support - strings are sorted |
|
1587 |
without caring for the locale." |
|
1588 |
||
1589 |
|aSortedCollection| |
|
1590 |
||
1591 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1592 |
aSortedCollection addAll:self. |
|
1593 |
^ aSortedCollection |
|
1594 |
||
1595 |
"Created: 13.9.1997 / 09:37:50 / cg" |
|
1596 |
"Modified: 13.9.1997 / 09:44:08 / cg" |
|
1597 |
! |
|
1598 |
||
602 | 1599 |
asString |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1600 |
"return a String with the collection's elements |
602 | 1601 |
(which must convert to characters)" |
1602 |
||
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1603 |
|string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1604 |
index "{ Class: SmallInteger }" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1605 |
char bitsPerCharacter| |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1606 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1607 |
string := String new:(self size). |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1608 |
bitsPerCharacter := string bitsPerCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1609 |
|
602 | 1610 |
index := 1. |
1611 |
self do:[:each | |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1612 |
char := each asCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1613 |
char bitsPerCharacter > bitsPerCharacter ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1614 |
char bitsPerCharacter == 16 ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1615 |
string := Unicode16String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1616 |
] ifFalse:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1617 |
string := Unicode32String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1618 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1619 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1620 |
string at:index put:char. |
6135 | 1621 |
index := index + 1 |
360 | 1622 |
]. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1623 |
^ string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1624 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1625 |
" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1626 |
#(80 81 82) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1627 |
#(16r8000 16r8001 16r8002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1628 |
#(16r800000 16r800001 16r800002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1629 |
" |
602 | 1630 |
! |
1631 |
||
1632 |
asStringCollection |
|
1413 | 1633 |
"return a new string collection containing the elements; |
1634 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 1635 |
|
1636 |
^ self addAllTo:(StringCollection new) |
|
1413 | 1637 |
|
1638 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 1639 |
! |
1640 |
||
8160 | 1641 |
asUnicodeString |
1642 |
"return a String with the collections elements |
|
1643 |
(which must convert to characters)" |
|
1644 |
||
1645 |
|aString |
|
1646 |
index "{ Class: SmallInteger }" | |
|
1647 |
||
1648 |
aString := UnicodeString uninitializedNew:self size. |
|
1649 |
index := 1. |
|
1650 |
self do:[:each | |
|
1651 |
aString at:index put:each asCharacter. |
|
1652 |
index := index + 1 |
|
1653 |
]. |
|
1654 |
^ aString |
|
1655 |
||
1656 |
" |
|
1657 |
#(16r440 16r443 16r441 16r441 16r43A 16r438 16r439 16r20 16r44F 16r437 16r44B 16r43A) asUnicodeString |
|
1658 |
" |
|
1659 |
! |
|
1660 |
||
7885 | 1661 |
asWordArray |
1662 |
"return a new WordArray with the collections elements |
|
1663 |
(which must convert to integers in the range 0..16rFFFF)." |
|
1664 |
||
1665 |
|aWordArray |
|
1666 |
index "{ Class: SmallInteger }" | |
|
1667 |
||
1668 |
aWordArray := WordArray new:(self size). |
|
1669 |
index := 1. |
|
1670 |
self do:[:each | |
|
1671 |
aWordArray at:index put:each asInteger. |
|
1672 |
index := index + 1 |
|
1673 |
]. |
|
1674 |
^ aWordArray |
|
1675 |
! |
|
1676 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1677 |
copyAs:collectionClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1678 |
"return a new instance of collectionClass with the receiver collection's elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1679 |
This is similar to copy as:collectionClass, to ensure that we get a new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1680 |
(unshared) collection, but avoids the copy if the receiver is not already an |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1681 |
instance of collectionClass." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1682 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1683 |
|newColl| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1684 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1685 |
newColl := self as:collectionClass. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1686 |
newColl == self ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1687 |
^ self copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1688 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1689 |
^ newColl. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1690 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1691 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1692 |
|coll1 coll2| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1693 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1694 |
coll1 := #(1 2 3 4 5). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1695 |
coll2 := coll1 copyAs:Array. '-- will generate a copy'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1696 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1697 |
self assert:(coll1 = coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1698 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1699 |
coll1 := #(1 2 3 4 5). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1700 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate an OC'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1701 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1702 |
self assert:(coll1 isSameSequenceAs: coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1703 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1704 |
coll1 := #(1 2 3 4 5) asOrderedCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1705 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate a copy'. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1706 |
self assert:(coll1 ~~ coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1707 |
self assert:(coll1 = coll2). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1708 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1709 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1710 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1711 |
copyAsOrderedCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1712 |
"return a new OrderedCollection with the receiver collection's elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1713 |
This is similar to copy asOrderedCollection, to ensure that we get a new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1714 |
(unshared) collection, but avoids the copy if the receiver is not already an |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1715 |
OrderedCollection." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1716 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1717 |
|newColl| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1718 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1719 |
newColl := self asOrderedCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1720 |
newColl == self ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1721 |
^ self copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1722 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1723 |
^ newColl. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1724 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1725 |
|
7905 | 1726 |
keysAndValues |
1727 |
|assocs| |
|
1728 |
||
1729 |
assocs := OrderedCollection new. |
|
1730 |
self keysAndValuesDo:[:k :v | |
|
1731 |
assocs add:(k -> v). |
|
1732 |
]. |
|
1733 |
^ assocs. |
|
1734 |
||
1735 |
" |
|
1736 |
#(10 20 30 40 50 60) keysAndValues |
|
1737 |
" |
|
1738 |
! |
|
1739 |
||
9410 | 1740 |
literalArrayEncoding |
1741 |
"encode myself as an array literal, from which a copy of the receiver |
|
1742 |
can be reconstructed with #decodeAsLiteralArray." |
|
1743 |
||
1744 |
|encoding idx| |
|
1745 |
||
1746 |
encoding := Array new:self size + 1. |
|
1747 |
encoding at:1 put:self class name. |
|
1748 |
idx := 2. |
|
1749 |
self do:[:element| |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1750 |
encoding at:idx put:element literalArrayEncoding. |
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1751 |
idx := idx + 1. |
9410 | 1752 |
]. |
1753 |
^ encoding |
|
1754 |
||
1755 |
||
1756 |
" |
|
1757 |
(Array with:(Color red:50 green:50 blue:50) |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1758 |
with:(1 @ 2) |
9410 | 1759 |
) literalArrayEncoding decodeAsLiteralArray |
1760 |
" |
|
1761 |
||
1762 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
1763 |
! |
|
1764 |
||
602 | 1765 |
readStream |
1766 |
"return a stream for reading from the receiver" |
|
1767 |
||
1768 |
^ ReadStream on:self |
|
88 | 1769 |
|
1770 |
" |
|
602 | 1771 |
|s| |
1772 |
||
1773 |
s := 'hello world' readStream. |
|
1774 |
s next:5. |
|
1775 |
s next. |
|
1776 |
(s next:5) inspect |
|
88 | 1777 |
" |
1 | 1778 |
! |
1779 |
||
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1780 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1781 |
"return a stream for reading from the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1782 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1783 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1784 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1785 |
^ self readStream |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1786 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1787 |
|
8631 | 1788 |
readWriteStream |
1789 |
"return a stream for reading and writing from/to the receiver" |
|
1790 |
||
1791 |
^ ReadWriteStream with:self |
|
1792 |
||
1793 |
" |
|
11436 | 1794 |
'hello world' readWriteStream |
1795 |
nextPutAll:'+Foo'; |
|
1796 |
contents |
|
1797 |
||
1798 |
'hello world' readWriteStream |
|
1799 |
setToEnd; |
|
1800 |
nextPutAll:'+Foo'; |
|
1801 |
contents |
|
8631 | 1802 |
" |
1803 |
! |
|
1804 |
||
602 | 1805 |
writeStream |
1806 |
"return a stream for writing onto the receiver" |
|
1807 |
||
1808 |
^ WriteStream on:self |
|
2 | 1809 |
|
602 | 1810 |
" |
1811 |
|s| |
|
1812 |
||
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1813 |
s := #() writeStream. |
602 | 1814 |
s nextPut:1. |
1815 |
s nextPut:2. |
|
1816 |
s nextPut:3. |
|
1817 |
s contents inspect |
|
1818 |
" |
|
88 | 1819 |
|
1820 |
" |
|
602 | 1821 |
|s| |
2 | 1822 |
|
602 | 1823 |
s := OrderedCollection new writeStream. |
1824 |
s nextPut:1. |
|
1825 |
s nextPut:2. |
|
1826 |
s nextPut:3. |
|
1827 |
s contents inspect |
|
1828 |
" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1829 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1830 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1831 |
writeStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1832 |
"return a stream for writing onto the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1833 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1834 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1835 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1836 |
^ self writeStream |
602 | 1837 |
! ! |
1 | 1838 |
|
602 | 1839 |
!Collection methodsFor:'copying'! |
1840 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1841 |
copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1842 |
"return a copy of the receiver. |
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
1843 |
Redefined to pass the original as argument to the postCopyFrom method." |
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1844 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1845 |
^ self shallowCopy postCopyFrom:self |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1846 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1847 |
"Created: / 19.4.1998 / 20:02:53 / cg" |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1848 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1849 |
|
602 | 1850 |
copyEmpty |
1851 |
"return a copy of the receiver with no elements. |
|
1852 |
This is used by copying and enumeration methods |
|
1853 |
to get a new instance which is similar to the receiver." |
|
1854 |
||
1855 |
^ self species new |
|
1 | 1856 |
! |
1857 |
||
602 | 1858 |
copyEmpty:size |
1859 |
"return a copy of the receiver with no elements, but space for |
|
1860 |
size elements. This is used by copying and enumeration methods |
|
1861 |
to get a new instance which is similar to the receiver. |
|
1862 |
This method should be redefined in subclasses with instance |
|
1863 |
variables, which should be put into the copy too. |
|
1864 |
For example, SortedCollection has to copy its sortBlock into the |
|
1865 |
new collection." |
|
1 | 1866 |
|
602 | 1867 |
^ self species new:size |
360 | 1868 |
! |
1869 |
||
602 | 1870 |
copyEmptyAndGrow:size |
1871 |
"return a copy of the receiver with size nil elements. |
|
1872 |
This is used by copying and enumeration methods |
|
1873 |
to get a new instance which is similar to the receiver." |
|
155 | 1874 |
|
602 | 1875 |
^ (self copyEmpty:size) grow:size |
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1876 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1877 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1878 |
postCopyFrom:original |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1879 |
"sent to a freshly copied object to give it a chance to adjust things. |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1880 |
Notice, that for Sets/Dicts etc. a rehash is not needed, since the copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1881 |
will have the same hash key as the receiver (as long as ST/X provides the |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1882 |
setHash: functionality)." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1883 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1884 |
"for ST-80 compatibility, we try postCopy here ..." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1885 |
^ self postCopy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1886 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1887 |
"Created: / 19.4.1998 / 19:59:42 / cg" |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1888 |
"Modified: / 19.4.1998 / 20:03:57 / cg" |
1 | 1889 |
! ! |
1890 |
||
1891 |
!Collection methodsFor:'enumerating'! |
|
1892 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1893 |
addAllNonNilElementsTo:aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1894 |
"add all nonNil elements of the receiver to aCollection. |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1895 |
Return aCollection." |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1896 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1897 |
self do:[:each | each notNil ifTrue:[aCollection add:each]]. |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1898 |
^ aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1899 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1900 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1901 |
#(1 2 3 4 5 1 2 3 4 5 nil) asOrderedCollection addAllNonNilElementsTo:Set new |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1902 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1903 |
! |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1904 |
|
602 | 1905 |
addAllTo:aCollection |
1906 |
"add all elements of the receiver, to aCollection. |
|
1907 |
Return aCollection." |
|
77 | 1908 |
|
602 | 1909 |
self do:[:each | aCollection add:each]. |
1910 |
^ aCollection |
|
5254 | 1911 |
|
1912 |
" |
|
1913 |
#(1 2 3 4 5 1 2 3 4 5) addAllTo:Set new |
|
1914 |
" |
|
1915 |
||
1916 |
"Modified: / 11.2.2000 / 11:22:14 / cg" |
|
77 | 1917 |
! |
1918 |
||
1 | 1919 |
collect:aBlock |
1920 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1921 |
and return a new collection with the results" |
|
1922 |
||
1923 |
|newCollection| |
|
1924 |
||
201 | 1925 |
newCollection := self species new. |
1926 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 1927 |
^ newCollection |
68 | 1928 |
|
1929 |
" |
|
1930 |
#(1 2 3 4) collect:[:n | n * 2] |
|
1931 |
" |
|
1 | 1932 |
! |
1933 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1934 |
collect:aBlock as:aClass |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1935 |
"like collect, but use an instance of aClass to collect the results. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1936 |
Also avoids the need for an extra intermediate collection which is created with |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1937 |
the standard coding: 'self asXXXX collect:[...]" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1938 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1939 |
|newCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1940 |
idx "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1941 |
sz "{ Class:SmallInteger }"| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1942 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1943 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1944 |
newCollection := (aClass new:sz) grow:sz. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1945 |
newCollection isSequenceable ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1946 |
idx := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1947 |
self do:[:el | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1948 |
newCollection at:idx put:(aBlock value:el). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1949 |
idx := idx + 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1950 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1951 |
] ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1952 |
self do:[:el | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1953 |
newCollection add:(aBlock value:el). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1954 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1955 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1956 |
^ newCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1957 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1958 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1959 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1960 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1961 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1962 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1963 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1964 |
collect:collectBlock thenReject:rejectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1965 |
"combination of collect followed by reject. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1966 |
May be redefined by some subclasses for optimal performance |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1967 |
(avoiding the creation of intermediate garbage)" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1968 |
|
17780 | 1969 |
^ self collect:collectBlock thenSelect:[:el | (rejectBlock value:el) not] |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1970 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1971 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1972 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenReject:[:i | i > 10] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1973 |
" |
17780 | 1974 |
|
1975 |
"Modified: / 11-07-2010 / 17:03:16 / cg" |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1976 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1977 |
|
4890 | 1978 |
collect:collectBlock thenSelect:selectBlock |
1979 |
"combination of collect followed by select. |
|
1980 |
May be redefined by some subclasses for optimal performance |
|
7164 | 1981 |
(avoiding the creation of intermediate garbage)" |
4890 | 1982 |
|
4892 | 1983 |
^ (self collect:collectBlock) select:selectBlock |
1984 |
||
1985 |
" |
|
1986 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10] |
|
1987 |
" |
|
4890 | 1988 |
! |
1989 |
||
6867 | 1990 |
collectAll:aBlock |
1991 |
"for each element in the receiver, evaluate the argument, aBlock. |
|
9315 | 1992 |
The block is supposed to return a collection, whose elements are collected. |
8608 | 1993 |
The species of the returned collection is that of the first returned |
1994 |
partial result." |
|
6867 | 1995 |
|
1996 |
|result| |
|
1997 |
||
1998 |
self do:[:element | |
|
1999 |
|individualResult| |
|
2000 |
||
2001 |
individualResult := aBlock value:element. |
|
2002 |
result isNil ifTrue:[ |
|
2003 |
result := individualResult speciesForAdding new. |
|
2004 |
]. |
|
2005 |
result addAll:individualResult. |
|
2006 |
]. |
|
2007 |
||
8609
daa0d6682135
collectAll: now returns an empty collection if
Claus Gittinger <cg@exept.de>
parents:
8608
diff
changeset
|
2008 |
^ result ? #() |
6867 | 2009 |
|
2010 |
" |
|
2011 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] |
|
7629 | 2012 |
#(1 2 3 4) collectAll:[:n | Array with:n with:n squared ] |
8607 | 2013 |
#(1 2 3 4) collectAll:[:n | 1 to:n ] |
2014 |
(Array with:Point with:Rectangle) collectAll:[:c | c instVarNames ] |
|
7629 | 2015 |
" |
6867 | 2016 |
! |
2017 |
||
602 | 2018 |
count:aBlock |
2019 |
"count elements, for which aBlock returns true. |
|
2020 |
Return the sum." |
|
2021 |
||
7336 | 2022 |
|count| |
2023 |
||
2024 |
count := 0. |
|
2025 |
self do:[:element | |
|
2026 |
(aBlock value:element) ifTrue:[count := count + 1] |
|
2027 |
]. |
|
2028 |
^ count |
|
77 | 2029 |
|
2030 |
" |
|
602 | 2031 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
7336 | 2032 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
77 | 2033 |
" |
2034 |
! |
|
2035 |
||
1 | 2036 |
detect:aBlock |
2037 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2038 |
the block returns true; in this case return the element which caused |
|
2039 |
the true evaluation. |
|
345 | 2040 |
If none of the evaluations returns true, report an error" |
1 | 2041 |
|
2042 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 2043 |
|
2044 |
" |
|
2045 |
#(1 2 3 4) detect:[:n | n odd] |
|
2046 |
#(2 4 6 8) detect:[:n | n odd] |
|
2047 |
" |
|
1 | 2048 |
! |
2049 |
||
8750 | 2050 |
detect:generatorBlock forWhich:testBlock ifNone:exceptionBlock |
2051 |
"evaluate generatorBlock for each element in the receiver until |
|
2052 |
testBlock returns true for it; |
|
2053 |
in this case return the value from generatorBlock, which caused the true evaluation. |
|
2054 |
If none of the test evaluations returns true, return the result of the |
|
2055 |
evaluation of the exceptionBlock" |
|
2056 |
||
2057 |
self do:[:each | |
|
2058 |
|val| |
|
2059 |
||
2060 |
val := generatorBlock value:each. |
|
2061 |
(testBlock value:val) ifTrue:[^ val]. |
|
2062 |
]. |
|
2063 |
^ exceptionBlock value |
|
2064 |
||
2065 |
" |
|
2066 |
#(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
2067 |
#( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
8751 | 2068 |
|
2069 |
#( 'st' 'c' 'java' ) |
|
2070 |
detect:[:ext | 'Foo' asFilename withSuffix:ext] |
|
2071 |
forWhich:[:fn | fn exists] |
|
2072 |
ifNone:nil |
|
8750 | 2073 |
" |
2074 |
! |
|
2075 |
||
9842 | 2076 |
detect:aOneArgBlock ifNone:exceptionBlock |
1 | 2077 |
"evaluate the argument, aBlock for each element in the receiver until |
2078 |
the block returns true; in this case return the element which caused |
|
2079 |
the true evaluation. |
|
2080 |
If none of the evaluations returns true, return the result of the |
|
2081 |
evaluation of the exceptionBlock" |
|
2082 |
||
2083 |
self do:[:each | |
|
9842 | 2084 |
(aOneArgBlock value:each) ifTrue:[^ each]. |
1 | 2085 |
]. |
2086 |
^ exceptionBlock value |
|
68 | 2087 |
|
2088 |
" |
|
2089 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
2090 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
2091 |
" |
|
9842 | 2092 |
|
2093 |
"Modified: / 13-09-2006 / 11:17:42 / cg" |
|
1 | 2094 |
! |
2095 |
||
421 | 2096 |
detectLast:aBlock |
2097 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2098 |
the block returns true; in this case return the element which caused |
|
2099 |
the true evaluation. The elements are processed in reverse order. |
|
2100 |
If none of the evaluations returns true, report an error" |
|
2101 |
||
2102 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
2103 |
||
2104 |
" |
|
2105 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
2106 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
2107 |
" |
|
2108 |
! |
|
2109 |
||
2110 |
detectLast:aBlock ifNone:exceptionBlock |
|
2111 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2112 |
the block returns true; in this case return the element which caused |
|
2113 |
the true evaluation. The elements are processed in reverse order. |
|
2114 |
If none of the evaluations returns true, return the result of the |
|
2115 |
evaluation of the exceptionBlock" |
|
2116 |
||
2117 |
self reverseDo:[:each | |
|
6135 | 2118 |
(aBlock value:each) ifTrue:[^ each]. |
421 | 2119 |
]. |
2120 |
^ exceptionBlock value |
|
2121 |
||
2122 |
" |
|
2123 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2124 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2125 |
" |
|
2126 |
! |
|
2127 |
||
602 | 2128 |
do:aBlock |
2129 |
"evaluate the argument, aBlock for each element" |
|
2130 |
||
2131 |
^ self subclassResponsibility |
|
2132 |
! |
|
2133 |
||
4394 | 2134 |
do:aBlock inBetweenDo:betweenBlock |
2135 |
"evaluate the argument, aBlock for each element. |
|
2136 |
Between elements (i.e. after each except for the last), |
|
2137 |
evaluate betweenBlock. |
|
5254 | 2138 |
This is a utility helper for collection printers |
2139 |
(for example, to print a space between elements)." |
|
4394 | 2140 |
|
8359 | 2141 |
<resource: #obsolete> |
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2142 |
self obsoleteMethodWarning:'use #do:separatedBy:'. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2143 |
^ self do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2144 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2145 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2146 |
#(1 2 3 4) do:[:el | Transcript show:el] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2147 |
inBetweenDo:[ Transcript show:'-'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2148 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2149 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2150 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2151 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2152 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2153 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2154 |
associations |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2155 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2156 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2157 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2158 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2159 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2160 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2161 |
! |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2162 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2163 |
do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2164 |
"evaluate the argument, aBlock for each element. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2165 |
Between elements (i.e. after each except for the last), |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2166 |
evaluate betweenBlock. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2167 |
This is a utility helper for collection printers |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2168 |
(for example, to print a space between elements)." |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2169 |
|
4394 | 2170 |
|first| |
2171 |
||
2172 |
first := true. |
|
2173 |
self do:[:element | |
|
2174 |
first ifTrue:[ |
|
2175 |
first := false |
|
2176 |
] ifFalse:[ |
|
2177 |
betweenBlock value. |
|
2178 |
]. |
|
2179 |
aBlock value:element |
|
2180 |
]. |
|
2181 |
||
2182 |
" |
|
2183 |
#(1 2 3 4) do:[:el | Transcript show:el] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2184 |
separatedBy:[ Transcript show:'-'] |
5619 | 2185 |
|
2186 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2187 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2188 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2189 |
|
2190 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2191 |
associations |
|
2192 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2193 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2194 |
|
4394 | 2195 |
" |
5254 | 2196 |
|
2197 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
|
4394 | 2198 |
! |
2199 |
||
1212 | 2200 |
doWithExit:aBlock |
2201 |
"evaluate the argument, aBlock for each element. |
|
2202 |
Passes an additional exit object, which can be used to leave |
|
2203 |
the loop early, by sending it a #value: message. |
|
2204 |
Returns nil or the value passed to the exit>>value: message. |
|
2205 |
||
8662 | 2206 |
Notice, that this is different to a return statement in the block, |
2207 |
which returns from the enclosed method, NOT only from the block." |
|
2208 |
||
2209 |
|exit| |
|
2210 |
||
2211 |
exit := [:exitValue | ^exitValue]. |
|
2212 |
self do:[:el | |
|
2213 |
aBlock value:el value:exit |
|
2214 |
]. |
|
2215 |
^ nil |
|
1212 | 2216 |
|
2217 |
"Example: |
|
2218 |
search for the first element which is >= 99; return it. |
|
2219 |
remaining elements are not processed: |
|
2220 |
||
2221 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2222 |
doWithExit:[:element :exit | |
|
1422 | 2223 |
Transcript showCR:element. |
8662 | 2224 |
element >= 99 ifTrue:[exit value:element]] |
1212 | 2225 |
|
2226 |
#(1 2 3 4 5 6 7 8 9) |
|
2227 |
doWithExit:[:element :exit | |
|
1422 | 2228 |
Transcript showCR:element. |
1212 | 2229 |
element >= 99 ifTrue:[exit value:element]] |
2230 |
" |
|
2231 |
||
2232 |
"to demonstrate the difference to returning from the block: |
|
2233 |
this works: |
|
2234 |
||
2235 |
|el| |
|
2236 |
||
2237 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2238 |
doWithExit:[:element :exit | |
|
2239 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 2240 |
Transcript showCR:el. |
1212 | 2241 |
|
2242 |
||
1422 | 2243 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 2244 |
|
2245 |
|el| |
|
2246 |
||
2247 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2248 |
do:[:element | |
|
2249 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 2250 |
Transcript showCR:el. |
1212 | 2251 |
" |
2252 |
||
2253 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
2254 |
! |
|
2255 |
||
11412 | 2256 |
flatDo:aBlock |
2257 |
"for each element of the collection, if its a scalar, evaluate aBlock for it; |
|
2258 |
otherwise, recursively invoke flatDo on the collection." |
|
2259 |
||
2260 |
self do:[:each | |
|
2261 |
(each isNonByteCollection) ifTrue:[ |
|
2262 |
each flatDo:aBlock |
|
2263 |
] ifFalse:[ |
|
2264 |
aBlock value:each |
|
2265 |
]. |
|
2266 |
]. |
|
2267 |
||
2268 |
" |
|
2269 |
#( |
|
2270 |
(1 2 3) |
|
2271 |
4 5 |
|
2272 |
(6) |
|
2273 |
7 |
|
2274 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) flatDo:[:el | Transcript showCR:el] |
|
2275 |
" |
|
2276 |
! |
|
2277 |
||
1 | 2278 |
inject:thisValue into:binaryBlock |
1262 | 2279 |
"starting with thisValue for value, pass this value and each element |
2280 |
to binaryBlock, replacing value with the result returned from the block |
|
2281 |
in the next iteration." |
|
2282 |
||
1 | 2283 |
|nextValue| |
2284 |
||
2285 |
nextValue := thisValue. |
|
2286 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
2287 |
^ nextValue |
|
68 | 2288 |
|
1262 | 2289 |
"sum up the elements of a collection: |
2290 |
||
68 | 2291 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
2292 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 2293 |
|
2294 |
find the minimum: |
|
2295 |
||
2296 |
|coll| |
|
2297 |
coll := #(1 99 -15 20 100). |
|
2298 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 2299 |
" |
1262 | 2300 |
|
2301 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 2302 |
! |
2303 |
||
602 | 2304 |
keysAndValuesDo:aTwoArgBlock |
2305 |
"evaluate the argument, aBlock for every element in the collection, |
|
2306 |
passing both index and element as arguments." |
|
2307 |
||
2308 |
^ self errorNotKeyed |
|
2309 |
! |
|
2310 |
||
1359 | 2311 |
keysAndValuesReverseDo:aTwoArgBlock |
2312 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
2313 |
passing both index and element as arguments." |
|
2314 |
||
2315 |
^ self errorNotKeyed |
|
2316 |
||
2317 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
2318 |
! |
|
2319 |
||
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2320 |
keysAndValuesSelect:selectBlockWith2Args thenCollect:collectBlockWith2Args |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2321 |
|collected| |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2322 |
|
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2323 |
collected := OrderedCollection new. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2324 |
self keysAndValuesDo:[:eachKey :eachValue | |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2325 |
(selectBlockWith2Args value:eachKey value:eachValue) ifTrue:[ |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2326 |
collected add:(collectBlockWith2Args value:eachKey value:eachValue) |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2327 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2328 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2329 |
^ collected |
9077
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2330 |
|
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2331 |
" |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2332 |
#(10 20 30 40) keysAndValuesSelect:[:idx :val | idx > 2] thenCollect:[:idx :val | idx->val] |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2333 |
" |
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2334 |
! |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2335 |
|
17797 | 2336 |
keysDo:aBlock |
2337 |
"evaluate the argument, aBlock for every key in the collection." |
|
2338 |
||
2339 |
self keysAndValuesDo:[:k :v | aBlock value:k] |
|
2340 |
||
2341 |
"Created: / 24-08-2010 / 10:12:14 / cg" |
|
2342 |
! |
|
2343 |
||
7454 | 2344 |
map:selector |
2345 |
"for lisp fans - similar to collect" |
|
2346 |
||
2347 |
^ self collect:[:eachElement | eachElement perform:selector]. |
|
2348 |
||
2349 |
" |
|
2350 |
#(1 2 3 4) map:#negated |
|
2351 |
" |
|
2352 |
! |
|
2353 |
||
2354 |
map:selector with:arg |
|
2355 |
"for lisp fans - similar to collect" |
|
2356 |
||
2357 |
^ self collect:[:eachElement | eachElement perform:selector with:arg]. |
|
2358 |
||
2359 |
" |
|
2360 |
#(1 2 3 4) map:#+ with:1 |
|
2361 |
" |
|
2362 |
! |
|
2363 |
||
8863 | 2364 |
nonNilElementsDo:aBlock |
2365 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
2366 |
||
2367 |
self do:[:each | |
|
2368 |
each notNil ifTrue:[ |
|
2369 |
aBlock value:each. |
|
2370 |
] |
|
2371 |
] |
|
2372 |
! |
|
2373 |
||
10750 | 2374 |
pairsDo:aTwoArgBlock |
2375 |
"evaluate the argument, aTwoArgBlock for every element in the collection, |
|
602 | 2376 |
which is supposed to consist of 2-element collections. |
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2377 |
The block is called with 2 arguments for each collection in the receiver. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2378 |
CONFUSION ATTACK: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2379 |
this is different from pairWiseDo:. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2380 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
2381 |
(sigh: but we were first, so they should have adapted...)" |
602 | 2382 |
|
2383 |
self do:[:aPair | |
|
10750 | 2384 |
aTwoArgBlock value:(aPair at:1) value:(aPair at:2). |
602 | 2385 |
] |
10750 | 2386 |
|
602 | 2387 |
" |
6555 | 2388 |
#( |
2389 |
(1 one) |
|
2390 |
(2 two) |
|
2391 |
(3 three) |
|
2392 |
(4 four) |
|
2393 |
(5 five) |
|
2394 |
(6 six) |
|
2395 |
) pairsDo: |
|
2396 |
[:num :sym | |
|
2397 |
Transcript show:num; show:' is: '; showCR:sym] |
|
602 | 2398 |
|
2399 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 2400 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 2401 |
" |
10750 | 2402 |
|
2403 |
"Modified: / 20-10-2007 / 17:17:50 / cg" |
|
602 | 2404 |
! |
2405 |
||
1 | 2406 |
reject:aBlock |
2407 |
"return a new collection with all elements from the receiver, for which |
|
2408 |
the argument aBlock evaluates to false" |
|
2409 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2410 |
^ self select:[:element | (aBlock value:element) not] |
68 | 2411 |
|
2412 |
" |
|
2413 |
#(1 2 3 4) reject:[:e | e odd] |
|
2414 |
(1 to:10) reject:[:e | e even] |
|
2415 |
" |
|
1 | 2416 |
! |
2417 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2418 |
reject:rejectBlock thenCollect:collectBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2419 |
"combination of reject followed by collect. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2420 |
May be redefined by some subclasses for optimal performance |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2421 |
(avoiding the creation of intermediate garbage)" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2422 |
|
17780 | 2423 |
^ self select:[:el | (rejectBlock value:el) not] thenCollect:collectBlock |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2424 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2425 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2426 |
#(1 2 3 4 5 6 7) reject:[:i | i even] thenCollect:[:i | i * 2] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2427 |
" |
17780 | 2428 |
|
2429 |
"Modified: / 11-07-2010 / 17:04:07 / cg" |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2430 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2431 |
|
602 | 2432 |
reverseDo:aBlock |
2433 |
"evaluate the argument, aBlock for each element in reverse order." |
|
2434 |
||
2435 |
"it could be defined in terms of do: - but very inefficient. |
|
2436 |
Better force programmer to define a better version ..." |
|
2437 |
||
2438 |
^ self subclassResponsibility |
|
2439 |
! |
|
2440 |
||
1 | 2441 |
select:aBlock |
2442 |
"return a new collection with all elements from the receiver, for which |
|
5265 | 2443 |
the argument aBlock evaluates to true. |
2444 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
1 | 2445 |
|
17795 | 2446 |
^ self select:aBlock as:(self species) |
2447 |
||
2448 |
" |
|
2449 |
#(1 2 3 4) select:[:e | e odd] |
|
2450 |
(1 to:10) select:[:e | e even] |
|
2451 |
" |
|
2452 |
||
2453 |
"Modified: / 07-08-2010 / 16:26:40 / cg" |
|
2454 |
! |
|
2455 |
||
2456 |
select:aBlock as:aCollectionClass |
|
2457 |
"return a new collection with all elements from the receiver, for which |
|
2458 |
the argument aBlock evaluates to true. |
|
2459 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
2460 |
||
1 | 2461 |
|newCollection| |
2462 |
||
17795 | 2463 |
newCollection := aCollectionClass new. |
1 | 2464 |
self do:[:each | |
5265 | 2465 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 2466 |
]. |
2467 |
^ newCollection |
|
68 | 2468 |
|
2469 |
" |
|
17795 | 2470 |
#(1 2 3 4) select:[:e | e odd] as:OrderedCollection |
2471 |
(1 to:10) select:[:e | e even] as:OrderedCollection |
|
68 | 2472 |
" |
17795 | 2473 |
|
2474 |
"Created: / 07-08-2010 / 16:26:15 / cg" |
|
68 | 2475 |
! |
2476 |
||
2477 |
select:aBlock ifNone:exceptionBlock |
|
2478 |
"try a new collection with all elements from the receiver, for which |
|
2479 |
the argument aBlock evaluates to true. If none of the elements passes |
|
5265 | 2480 |
the check of aBlock, return the result of evaluating exceptionBlock. |
2481 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
68 | 2482 |
|
2483 |
|newCollection| |
|
2484 |
||
2485 |
newCollection := self select:aBlock. |
|
2486 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
2487 |
^ newCollection |
|
2488 |
||
2489 |
" |
|
2490 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
2491 |
" |
|
12 | 2492 |
! |
2493 |
||
4892 | 2494 |
select:selectBlock thenCollect:collectBlock |
2495 |
"combination of select followed by collect. |
|
2496 |
May be redefined by some subclasses for optimal performance |
|
5844 | 2497 |
(avoiding the creation of intermediate garbage)" |
4892 | 2498 |
|
2499 |
^ (self select:selectBlock) collect:collectBlock |
|
2500 |
||
2501 |
" |
|
2502 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2] |
|
2503 |
" |
|
2504 |
! |
|
2505 |
||
6695 | 2506 |
select:selectBlock thenDo:doBlock |
2507 |
"combination of select followed by do |
|
2508 |
Avoids the creation of intermediate garbage" |
|
2509 |
||
2510 |
self do:[:eachElement | |
|
2511 |
(selectBlock value:eachElement) ifTrue:[ |
|
2512 |
doBlock value:eachElement |
|
2513 |
] |
|
2514 |
]. |
|
2515 |
||
2516 |
" |
|
2517 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i] |
|
2518 |
" |
|
2519 |
! |
|
2520 |
||
362 | 2521 |
triplesDo:aBlock |
2522 |
"evaluate the argument, aBlock for every element in the collection, |
|
2523 |
which is supposed to consist of 3-element collections. |
|
2524 |
The block is called with 3 arguments for each collection in the receiver." |
|
2525 |
||
2526 |
self do:[:aTriple | |
|
2639 | 2527 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
362 | 2528 |
] |
2529 |
" |
|
6555 | 2530 |
#( |
2531 |
(1 one eins) |
|
2532 |
(2 two zwei) |
|
2533 |
(3 three drei) |
|
2534 |
(4 four vier) |
|
2535 |
(5 five #'fuenf') |
|
2536 |
(6 six sechs) |
|
2537 |
) |
|
2538 |
triplesDo:[:num :sym1 :sym2 | |
|
2539 |
Transcript show:num; space; show:sym1; space; showCR:sym2 |
|
2540 |
] |
|
362 | 2541 |
" |
2639 | 2542 |
|
2543 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
1 | 2544 |
! |
2545 |
||
602 | 2546 |
with:aCollection collect:aTwoArgBlock |
2547 |
"evaluate the argument, aBlock for successive elements from |
|
2548 |
each the receiver and the argument, aSequenceableCollection; |
|
2549 |
The second argument, aBlock must be a two-argument block, which is |
|
2550 |
evaluated for each element-pair. |
|
2551 |
Collect the results and return a collection containing them. |
|
2552 |
This method fails if neither the receiver nor aCollection is |
|
2553 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 2554 |
|
17747 | 2555 |
|newCollection| |
1 | 2556 |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2557 |
newCollection := self speciesForAdding new. |
17747 | 2558 |
self with:aCollection do:[:el1 :el2 | |
2559 |
newCollection add:(aTwoArgBlock value:el1 value:el2). |
|
602 | 2560 |
]. |
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2561 |
^ newCollection as:self species |
201 | 2562 |
|
2563 |
" |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2564 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
17747 | 2565 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
2566 |
" |
|
2567 |
! |
|
2568 |
||
2569 |
with:aCollection count:aTwoArgBlock |
|
2570 |
"evaluate the argument, aBlock for successive elements from |
|
2571 |
each the receiver and the argument, aSequenceableCollection. |
|
2572 |
Count, how often the second argument, aTwoArgBlock returns true. |
|
2573 |
This method fails if neither the receiver nor aCollection is |
|
2574 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2575 |
||
2576 |
|count "{ Class: SmallInteger }"| |
|
2577 |
||
2578 |
count := 0. |
|
2579 |
self with:aCollection do:[:el1 :el2 | |
|
2580 |
(aTwoArgBlock value:el1 value:el2) ifTrue:[ |
|
2581 |
count := count + 1 |
|
2582 |
] |
|
2583 |
]. |
|
2584 |
^ count |
|
2585 |
||
2586 |
" |
|
2587 |
(1 to:3) with:#(1 3 3) count:[:n1 :n2 | n1 = n2] |
|
201 | 2588 |
" |
2 | 2589 |
! |
2590 |
||
602 | 2591 |
with:aCollection do:aTwoArgBlock |
2592 |
"evaluate the argument, aBlock for successive elements from |
|
2593 |
each the receiver and the argument, aSequenceableCollection. |
|
2594 |
The second argument, aBlock must be a two-argument block. |
|
2595 |
This method fails if neither the receiver nor aCollection is |
|
2596 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2597 |
||
2598 |
|index "{ Class: SmallInteger }"| |
|
2 | 2599 |
|
602 | 2600 |
index := 1. |
2601 |
aCollection isSequenceable ifFalse:[ |
|
6135 | 2602 |
self isSequenceable ifFalse:[ |
2603 |
^ self error:'neither collection is sequenceable'. |
|
2604 |
]. |
|
2605 |
aCollection do:[:element | |
|
2606 |
aTwoArgBlock value:(self at:index) value:element. |
|
2607 |
index := index + 1 |
|
2608 |
] |
|
602 | 2609 |
] ifTrue:[ |
6135 | 2610 |
self do:[:element | |
2611 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
2612 |
index := index + 1 |
|
2613 |
] |
|
602 | 2614 |
] |
201 | 2615 |
|
2616 |
" |
|
602 | 2617 |
(1 to:3) with:#(one two three) do:[:num :sym | |
6135 | 2618 |
Transcript showCR:(num->sym) |
602 | 2619 |
] |
2620 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
6135 | 2621 |
Transcript showCR:(num->sym) |
602 | 2622 |
]. |
2623 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
6135 | 2624 |
Transcript showCR:(num->sym) |
602 | 2625 |
] |
201 | 2626 |
" |
1 | 2627 |
! ! |
2628 |
||
5874 | 2629 |
!Collection methodsFor:'enumerating-tests'! |
2630 |
||
2631 |
allSatisfy:aBlock |
|
2632 |
"evaluate aBlock for each of the receiver's elements. |
|
2633 |
Return true, if aBlock returns true for all elements, false otherwise |
|
2634 |
(i.e. false if any element fails to satisfy the block-condition). |
|
2635 |
This is an ANSI renomer of #conform:" |
|
2636 |
||
2637 |
^ self conform:aBlock. |
|
2638 |
||
2639 |
" |
|
2640 |
#(1 2 3 4 5) allSatisfy:[:el | el odd] |
|
2641 |
#(2 4 6 8 10) allSatisfy:[:el | el odd] |
|
2642 |
#(2 4 6 8 10) allSatisfy:[:el | el even] |
|
2643 |
" |
|
2644 |
||
2645 |
! |
|
2646 |
||
2647 |
anySatisfy:aBlock |
|
2648 |
"evaluate aBlock for each of the receiver's elements. |
|
2649 |
Return true, if aBlock ever returns true, false otherwise |
|
2650 |
(i.e. if any element satisfies the block-condition). |
|
2651 |
This is an ANSI renomer of #contains:" |
|
2652 |
||
2653 |
^ self contains:aBlock. |
|
2654 |
||
2655 |
" |
|
2656 |
#(1 2 3 4 5) anySatisfy:[:el | el odd] |
|
2657 |
#(2 4 6 8 10) anySatisfy:[:el | el odd] |
|
2658 |
" |
|
2659 |
||
2660 |
! |
|
2661 |
||
9842 | 2662 |
conform:aOneArgBlock |
5874 | 2663 |
"return true, if every element conforms to some condition. |
2664 |
I.e. return false, if aBlock returns false for any element; |
|
2665 |
true otherwise." |
|
2666 |
||
9842 | 2667 |
self do:[:element | |
2668 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
2669 |
]. |
|
5874 | 2670 |
^ true |
9842 | 2671 |
|
2672 |
" |
|
2673 |
#(1 2 3 4 5) conform:[:el | el even] |
|
2674 |
#(2 4 6 8 10) conform:[:el | el even] |
|
2675 |
" |
|
2676 |
||
2677 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
5874 | 2678 |
! |
2679 |
||
9842 | 2680 |
contains:aOneArgBlock |
2681 |
"evaluate aOneArgBlock for each of the receiver's elements. |
|
5874 | 2682 |
Return true, if aBlock ever returns true, false otherwise." |
2683 |
||
9842 | 2684 |
self do:[:element | |
2685 |
(aOneArgBlock value:element) ifTrue:[^ true] |
|
2686 |
]. |
|
2687 |
^ false |
|
5874 | 2688 |
|
2689 |
" |
|
2690 |
#(1 2 3 4 5) contains:[:el | el odd] |
|
2691 |
#(2 4 6 8 10) contains:[:el | el odd] |
|
2692 |
" |
|
2693 |
||
9842 | 2694 |
"Modified: / 13-09-2006 / 11:18:36 / cg" |
8577 | 2695 |
! |
2696 |
||
2697 |
noneSatisfy:aBlock |
|
2698 |
"evaluate aBlock for each of the receiver's elements. |
|
2699 |
Return true, if aBlock returns false for all elements, false otherwise |
|
2700 |
(i.e. false if any element satisfies the block-condition)." |
|
2701 |
||
9843 | 2702 |
^ (self contains:aBlock) not |
8577 | 2703 |
|
2704 |
" |
|
2705 |
#(1 2 3 4 5) noneSatisfy:[:el | el odd] |
|
2706 |
#(2 4 6 8 10) noneSatisfy:[:el | el odd] |
|
2707 |
#(2 4 6 8 10) noneSatisfy:[:el | el even] |
|
2708 |
" |
|
9843 | 2709 |
|
2710 |
"Modified: / 13-09-2006 / 11:19:57 / cg" |
|
5874 | 2711 |
! ! |
2712 |
||
602 | 2713 |
!Collection methodsFor:'error handling'! |
2714 |
||
2715 |
emptyCheck |
|
2716 |
"check if the receiver is empty; report an error if so" |
|
2717 |
||
11475
f7e1a355df17
changed #emptyCheck (is isEmpty instead of size == 0)
Claus Gittinger <cg@exept.de>
parents:
11470
diff
changeset
|
2718 |
self isEmpty ifTrue:[ |
6135 | 2719 |
^ self emptyCollectionError. |
602 | 2720 |
]. |
2721 |
! |
|
2722 |
||
2723 |
emptyCollectionError |
|
2724 |
"report an error that the operation is not allowed for |
|
2725 |
empty collections" |
|
2726 |
||
2727 |
^ EmptyCollectionSignal raise |
|
2728 |
! |
|
2729 |
||
2730 |
errorInvalidKey:aKey |
|
2731 |
"report an error that the given key was invalid" |
|
2732 |
||
2733 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
2734 |
! |
|
2735 |
||
2736 |
errorNotKeyed |
|
2737 |
"report an error that keyed access methods are not allowed" |
|
2738 |
||
2739 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
2740 |
! |
|
2741 |
||
2742 |
errorValueNotFound:anObject |
|
2743 |
"report an error that an object was not found in the collection" |
|
2744 |
||
6757 | 2745 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
3075 | 2746 |
|
2747 |
"Modified: / 30.10.1997 / 15:52:18 / cg" |
|
602 | 2748 |
! |
2749 |
||
2750 |
notEnoughElementsError |
|
2751 |
"report an error that the operation is not allowed, |
|
2752 |
since not enough elements are in the collection" |
|
2753 |
||
2754 |
^ NotEnoughElementsSignal raise |
|
2755 |
! ! |
|
2756 |
||
2757 |
!Collection methodsFor:'growing'! |
|
2758 |
||
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2759 |
changeCapacityTo:newSize |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2760 |
newSize > self capacity ifTrue:[ |
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
2761 |
self grow:newSize |
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2762 |
]. |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2763 |
|
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2764 |
"Created: / 30.10.2001 / 17:56:50 / cg" |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2765 |
! |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
2766 |
|
602 | 2767 |
grow |
2768 |
"make the receiver larger" |
|
2769 |
||
2770 |
self grow:(self size + self growSize) |
|
2771 |
! |
|
2772 |
||
2773 |
grow:howBig |
|
2774 |
"change the receivers size" |
|
2775 |
||
2776 |
^ self subclassResponsibility |
|
2777 |
! |
|
2778 |
||
2779 |
growSize |
|
2780 |
"return a suitable size increment for growing. |
|
2781 |
The default returned here may be (and is) redefined in subclasses." |
|
2782 |
||
2783 |
^ self size max:2 |
|
2784 |
! ! |
|
2785 |
||
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
2786 |
|
11412 | 2787 |
!Collection methodsFor:'operations'! |
2788 |
||
2789 |
decrementAt:aKey |
|
2790 |
self at:aKey put:(self at:aKey) - 1. |
|
2791 |
! |
|
2792 |
||
2793 |
incrementAt:aKey |
|
2794 |
self at:aKey put:(self at:aKey) + 1. |
|
2795 |
! ! |
|
2796 |
||
1 | 2797 |
!Collection methodsFor:'printing & storing'! |
2798 |
||
17807 | 2799 |
displayOn:aGCOrStream |
2800 |
"print a representation of the receiver on aGCOrStream for display in inspectors etc." |
|
2801 |
||
2802 |
|noneYet total limit| |
|
2803 |
||
2804 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
2805 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
2806 |
(aGCOrStream isStream and:[aGCOrStream ~~ Transcript]) ifFalse:[ |
|
2807 |
^ super displayOn:aGCOrStream |
|
2808 |
]. |
|
2809 |
||
2810 |
aGCOrStream nextPutAll:self displayStringName; nextPut:$(. |
|
602 | 2811 |
|
2812 |
thisContext isRecursive ifTrue:[ |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2813 |
'Collection [error]: displayString of self referencing collection.' errorPrintCR. |
17807 | 2814 |
aGCOrStream nextPutAll:'"recursive")'. |
2815 |
^ self. |
|
602 | 2816 |
]. |
2817 |
||
2818 |
noneYet := true. |
|
2819 |
total := 0. |
|
2820 |
limit := self maxPrint. |
|
2821 |
||
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2822 |
self printElementsDo: [:element | |
1413 | 2823 |
noneYet ifTrue:[ |
2824 |
noneYet := false. |
|
2825 |
] ifFalse:[ |
|
17807 | 2826 |
aGCOrStream space |
1413 | 2827 |
]. |
17807 | 2828 |
element displayOn:aGCOrStream. |
1413 | 2829 |
total := total + 1. |
2830 |
(total > limit) ifTrue:[ |
|
17807 | 2831 |
aGCOrStream nextPutAll:'... )'. |
2832 |
^ self |
|
1413 | 2833 |
] |
602 | 2834 |
]. |
17807 | 2835 |
aGCOrStream nextPutAll:')'. |
1413 | 2836 |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2837 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2838 |
#(1 2 3 'hello' $a) asOrderedCollection displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2839 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2840 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2841 |
at:#foo put:'bar'; yourself) displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2842 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2843 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2844 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
3964
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2845 |
"Modified: / 2.2.1999 / 22:39:44 / cg" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2846 |
! |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2847 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2848 |
displayStringName |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2849 |
"redefinable helper for displayString" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2850 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2851 |
^ (self class name) |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2852 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
2853 |
"Created: / 2.2.1999 / 22:39:33 / cg" |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2854 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2855 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2856 |
maxPrint |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2857 |
"the print-limit; printOn: will try to not produce more output |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2858 |
than the limit defined here." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2859 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2860 |
^ 5000 |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2861 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2862 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2863 |
printElementsDo:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2864 |
"perform aBlock (1 arg) for all elements. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2865 |
Used in #printOn:. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2866 |
Subclasses (e.g. Dictionary) may redefine this." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2867 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2868 |
^ self do:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2869 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2870 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2871 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2872 |
|
11599 | 2873 |
printElementsOn:aStream |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2874 |
"append a user readable representation of the receiver to aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2875 |
The text appended is not meant to be read back for reconstruction of |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2876 |
the receiver. Also, this method limits the size of generated string. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2877 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2878 |
|
3402 | 2879 |
|limit firstOne s| |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2880 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2881 |
thisContext isRecursive ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2882 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2883 |
aStream nextPutAll:'#("recursive")'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2884 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2885 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2886 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2887 |
aStream nextPut:$(. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2888 |
firstOne := true. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2889 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2890 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2891 |
if aStream is not positionable, create an temporary positionable stream |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2892 |
(needed for limit calculation) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2893 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2894 |
aStream isPositionable ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2895 |
s := aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2896 |
] ifFalse:[ |
7685
535a69a7cd69
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7629
diff
changeset
|
2897 |
s := WriteStream on:(String uninitializedNew:50). |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2898 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
2899 |
limit := s position1Based + self maxPrint. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2900 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2901 |
self printElementsDo:[:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2902 |
firstOne ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2903 |
s space |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2904 |
] ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2905 |
firstOne := false |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2906 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
2907 |
(s position1Based >= limit) ifTrue:[ |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2908 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2909 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2910 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2911 |
aStream nextPutAll:'...etc...)'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2912 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2913 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2914 |
element printOn:s. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2915 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2916 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2917 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2918 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2919 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2920 |
aStream nextPut:$) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2921 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2922 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2923 |
#(1 2 3 'hello' $a) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2924 |
(Array new:100000) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2925 |
(Array new:100000) printOn:Stdout |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2926 |
(Array new:100000) printString size |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2927 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2928 |
at:#foo put:'bar'; yourself) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2929 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2930 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2931 |
|a| |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2932 |
a := Array new:3. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2933 |
a at:2 put:a. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2934 |
a printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2935 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2936 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2937 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
2938 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
602 | 2939 |
! |
2940 |
||
11599 | 2941 |
printOn:aStream |
2942 |
"append a user readable representation of the receiver to aStream. |
|
2943 |
The text appended is not meant to be read back for reconstruction of |
|
2944 |
the receiver. Also, this method limits the size of generated string. |
|
2945 |
" |
|
2946 |
||
2947 |
thisContext isRecursive ifTrue:[ |
|
2948 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
|
2949 |
aStream nextPutAll:'#("recursive")'. |
|
2950 |
^ self |
|
2951 |
]. |
|
2952 |
||
2953 |
aStream nextPutAll:self class name. |
|
2954 |
self printElementsOn:aStream. |
|
2955 |
||
2956 |
" |
|
2957 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
2958 |
(Array new:100000) printOn:Transcript |
|
2959 |
(Array new:100000) printOn:Stdout |
|
2960 |
(Array new:100000) printString size |
|
2961 |
(Dictionary new at:#hello put:'world'; |
|
2962 |
at:#foo put:'bar'; yourself) printOn:Transcript |
|
2963 |
" |
|
2964 |
" |
|
2965 |
|a| |
|
2966 |
a := Array new:3. |
|
2967 |
a at:2 put:a. |
|
2968 |
a printOn:Transcript |
|
2969 |
" |
|
2970 |
||
2971 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
|
2972 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
|
2973 |
! |
|
2974 |
||
1 | 2975 |
storeOn:aStream |
88 | 2976 |
"output a printed representation onto the argument, aStream. |
2977 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
2978 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
2979 |
(use storeBinaryOn: to handle those)." |
|
1 | 2980 |
|
5254 | 2981 |
|notEmpty| |
1 | 2982 |
|
44 | 2983 |
thisContext isRecursive ifTrue:[ |
5498 | 2984 |
Object recursiveStoreStringSignal raiseRequestWith:self. |
2289 | 2985 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 2986 |
aStream nextPutAll:'#recursive'. |
2987 |
^ self |
|
44 | 2988 |
]. |
2989 |
||
2990 |
aStream nextPutAll:'('. |
|
1 | 2991 |
aStream nextPutAll:(self class name). |
44 | 2992 |
aStream nextPutAll:' new'. |
5254 | 2993 |
notEmpty := false. |
1 | 2994 |
self do:[:element | |
1413 | 2995 |
aStream nextPutAll:' add:'. |
2996 |
element storeOn:aStream. |
|
2997 |
aStream nextPutAll:';'. |
|
5254 | 2998 |
notEmpty := true |
1 | 2999 |
]. |
5254 | 3000 |
notEmpty ifTrue:[aStream nextPutAll:' yourself']. |
1 | 3001 |
aStream nextPut:$) |
44 | 3002 |
|
88 | 3003 |
" |
3004 |
OrderedCollection new storeOn:Transcript |
|
3005 |
(1 to:10) storeOn:Transcript |
|
3006 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
3007 |
" |
|
293 | 3008 |
" |
3009 |
|s| |
|
3010 |
||
3011 |
s := Set new. |
|
3012 |
s add:1; add:'hello'; add:s. |
|
3013 |
s storeOn:Transcript |
|
3014 |
" |
|
1413 | 3015 |
|
5254 | 3016 |
"Modified: / 11.2.2000 / 11:24:56 / cg" |
1 | 3017 |
! ! |
602 | 3018 |
|
3019 |
!Collection methodsFor:'queries'! |
|
3020 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3021 |
defaultElement |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3022 |
^ nil |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3023 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3024 |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3025 |
largest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3026 |
"return the n largest elements" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3027 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3028 |
|mySize loopAction actionForFirstN actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3029 |
nLargest sz_nLargest minInLargest| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3030 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3031 |
mySize := self size. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3032 |
n > mySize ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3033 |
self notEnoughElementsError |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3034 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3035 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3036 |
(n < 50) ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3037 |
n == 1 ifTrue:[ ^ Array with:self max ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3038 |
n == 2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3039 |
|l1 l2| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3040 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3041 |
loopAction := [:el | l1 := el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3042 |
loopAction := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3043 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3044 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3045 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3046 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3047 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3048 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3049 |
loopAction := actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3050 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3051 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3052 |
actionForRest := [:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3053 |
el > l2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3054 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3055 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3056 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3057 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3058 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3059 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3060 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3061 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3062 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3063 |
^ Array with:l2 with:l1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3064 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3065 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3066 |
nLargest := SortedCollection new:n. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3067 |
sz_nLargest := 0. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3068 |
actionForFirstN := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3069 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3070 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3071 |
sz_nLargest := sz_nLargest + 1. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3072 |
sz_nLargest == n ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3073 |
loopAction := actionForRest. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3074 |
minInLargest := nLargest min. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3075 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3076 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3077 |
actionForRest := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3078 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3079 |
el > minInLargest ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3080 |
nLargest removeFirst. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3081 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3082 |
minInLargest := nLargest min |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3083 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3084 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3085 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3086 |
loopAction := actionForFirstN. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3087 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3088 |
^ nLargest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3089 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3090 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3091 |
^ self asSortedCollection copyTo:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3092 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3093 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3094 |
#(10 35 20 45 30 5) largest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3095 |
#(10 35 20 45 30 5) largest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3096 |
#(10 35 20 45 30 5) largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3097 |
#(10 35 20 45 30 5) largest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3098 |
#(10 35 20 45 30 5) largest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3099 |
#(10 35 20 45 30 5) largest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3100 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3101 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3102 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3103 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3104 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3105 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3106 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3107 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3108 |
data asSortedCollection at:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3109 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3110 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3111 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3112 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3113 |
data largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3114 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3115 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3116 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3117 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3118 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3119 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3120 |
|
602 | 3121 |
longestCommonPrefix |
1246 | 3122 |
"return the longest common prefix of my elements. |
3123 |
Typically used with string collections." |
|
3124 |
||
5532 | 3125 |
^ self longestCommonPrefixIgnoreCase:false |
3126 |
||
3127 |
" |
|
3128 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
3129 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
3130 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
3131 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
3132 |
" |
|
3133 |
||
3134 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
|
3135 |
! |
|
3136 |
||
3137 |
longestCommonPrefixIgnoreCase:ignoreCase |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3138 |
"return the longest common prefix of my elements (which must be sequenceableCollections). |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3139 |
Typically used with string collections, |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3140 |
especially with completion of selectors or filenames." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3141 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3142 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3143 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3144 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3145 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3146 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3147 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3148 |
longest := longest commonPrefixWith:eachCollection ignoreCase:ignoreCase |
1246 | 3149 |
] |
602 | 3150 |
]. |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3151 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3152 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3153 |
"/ |longest try allMatching matchLen| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3154 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3155 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3156 |
"/ find the longest common prefix |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3157 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3158 |
"/ matchLen := 0. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3159 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3160 |
"/ aName size > matchLen ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3161 |
"/ matchLen := aName size. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3162 |
"/ try := aName |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3163 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3164 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3165 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3166 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3167 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3168 |
"/ [true] whileTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3169 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3170 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3171 |
"/ ((ignoreCase not and:[aName startsWith:try]) |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3172 |
"/ or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3173 |
"/ allMatching := false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3174 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3175 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3176 |
"/ allMatching ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3177 |
"/ ^ try |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3178 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3179 |
"/ matchLen := matchLen - 1. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3180 |
"/ matchLen == 0 ifTrue:[^ '']. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3181 |
"/ try := try copyTo:matchLen. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3182 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3183 |
"/ ^ try |
602 | 3184 |
|
3185 |
" |
|
5532 | 3186 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true |
3187 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
3188 |
#('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3189 |
#('AAA' 'A11' 'AA2') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3190 |
#('AAA' 'BBB' 'CCC') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3191 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3192 |
! |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3193 |
|
6506 | 3194 |
longestCommonSuffix |
3195 |
"return the longest common suffix (tail) of my elements. |
|
3196 |
Typically used with string collections." |
|
3197 |
||
3198 |
^ self longestCommonSuffixIgnoreCase:false |
|
3199 |
! |
|
3200 |
||
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3201 |
longestCommonSuffixIgnoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3202 |
"return the longest common suffix (tail) of my elements |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3203 |
(which must be sequenceableCollections)." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3204 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3205 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3206 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3207 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3208 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3209 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3210 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3211 |
longest := longest commonSuffixWith:eachCollection ignoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3212 |
] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3213 |
]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3214 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3215 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3216 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3217 |
#('Array' 'ByteArray' 'BigArray') longestCommonSuffixIgnoreCase:true |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3218 |
#('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3219 |
#('AAA' 'BBB' 'CCC') longestCommonSuffixIgnoreCase:false |
602 | 3220 |
" |
5557 | 3221 |
! |
3222 |
||
7821 | 3223 |
max |
3224 |
"return the maximum value in the receiver collection, |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3225 |
using #< to compare elements. |
7821 | 3226 |
Raises an error, if the receiver is empty." |
3227 |
||
3228 |
self emptyCheck. |
|
3229 |
||
17780 | 3230 |
^ self |
3231 |
inject:nil |
|
3232 |
into:[:maxSoFar :this | |
|
3233 |
(maxSoFar isNil or:[maxSoFar < this]) |
|
3234 |
ifTrue:[this] |
|
3235 |
ifFalse:[maxSoFar] |
|
3236 |
] |
|
7821 | 3237 |
|
3238 |
" |
|
3239 |
#(15 1 -9 10 5) max |
|
3240 |
(1 to:15) max |
|
3241 |
(-1 to:-15 by:-1) max |
|
3242 |
(-1 to:-15 by:-4) max |
|
3243 |
(0 to:15 by:4) max |
|
3244 |
" |
|
3245 |
||
17780 | 3246 |
"Modified: / 11-07-2010 / 17:05:25 / cg" |
7821 | 3247 |
! |
3248 |
||
17797 | 3249 |
maxApplying:aBlock |
3250 |
"return the maximum value from applying aBlock to each element in the receiver collection, |
|
3251 |
using < to compare elements. |
|
3252 |
Raises an error, if the receiver is empty." |
|
3253 |
||
3254 |
self emptyCheck. |
|
3255 |
||
3256 |
^ self |
|
3257 |
inject:nil |
|
3258 |
into:[:maxSoFar :this | |
|
3259 |
|v| |
|
3260 |
||
3261 |
v := aBlock value:this. |
|
3262 |
(maxSoFar isNil or:[maxSoFar < v]) |
|
3263 |
ifTrue:[v] |
|
3264 |
ifFalse:[maxSoFar] |
|
3265 |
] |
|
3266 |
||
3267 |
" |
|
3268 |
#(15 1 -9 -20 10 5) max -> 15 |
|
3269 |
#(15 1 -9 -20 10 5) maxApplying:[:el | el abs] -> 20 |
|
3270 |
" |
|
3271 |
||
3272 |
"Created: / 23-08-2010 / 11:02:50 / cg" |
|
3273 |
! |
|
3274 |
||
7821 | 3275 |
min |
3276 |
"return the minimum value in the receiver collection, |
|
3277 |
using < to compare elements. |
|
3278 |
Raises an error, if the receiver is empty." |
|
3279 |
||
3280 |
self emptyCheck. |
|
3281 |
||
17780 | 3282 |
^ self |
3283 |
inject:nil |
|
3284 |
into:[:minSoFar :this | |
|
3285 |
(minSoFar isNil or:[this < minSoFar]) |
|
3286 |
ifTrue:[this] |
|
3287 |
ifFalse:[minSoFar] |
|
3288 |
] |
|
7821 | 3289 |
|
3290 |
" |
|
3291 |
#(15 1 -9 10 5) min |
|
3292 |
(1 to:15) min |
|
3293 |
(-1 to:-15 by:-1) min |
|
3294 |
(-1 to:-15 by:-4) min |
|
3295 |
" |
|
3296 |
||
17780 | 3297 |
"Modified: / 11-07-2010 / 17:06:38 / cg" |
7821 | 3298 |
! |
3299 |
||
17797 | 3300 |
minApplying:aBlock |
3301 |
"return the minimum value from applying aBlock to each element in the receiver collection, |
|
3302 |
using < to compare elements. |
|
3303 |
Raises an error, if the receiver is empty." |
|
3304 |
||
3305 |
self emptyCheck. |
|
3306 |
||
3307 |
^ self |
|
3308 |
inject:nil |
|
3309 |
into:[:minSoFar :this | |
|
3310 |
|v| |
|
3311 |
||
3312 |
v := aBlock value:this. |
|
3313 |
(minSoFar isNil or:[v < minSoFar]) |
|
3314 |
ifTrue:[v] |
|
3315 |
ifFalse:[minSoFar] |
|
3316 |
] |
|
3317 |
||
3318 |
" |
|
3319 |
#(15 -1 -9 10 5) min -> -9 |
|
3320 |
#(15 -1 -9 10 5) minApplying:[:el | el abs] -> 1 |
|
3321 |
" |
|
3322 |
||
3323 |
"Created: / 23-08-2010 / 11:01:42 / cg" |
|
3324 |
! |
|
3325 |
||
7821 | 3326 |
minMax |
3327 |
"return the minimum and maximum values in the receiver collection |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3328 |
as a two element array, using #< to compare elements. |
7821 | 3329 |
Raises an error, if the receiver is empty." |
3330 |
||
3331 |
|min max| |
|
3332 |
||
3333 |
self emptyCheck. |
|
3334 |
||
3335 |
min := max := nil. |
|
3336 |
self do:[:each | |
|
3337 |
min isNil ifTrue:[ |
|
3338 |
min := max := each |
|
3339 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3340 |
max < each ifTrue:[ |
7821 | 3341 |
max := each |
3342 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3343 |
each < min ifTrue:[ |
7821 | 3344 |
min := each |
3345 |
] |
|
3346 |
] |
|
3347 |
] |
|
3348 |
]. |
|
3349 |
^ Array with:min with:max |
|
3350 |
||
3351 |
" |
|
3352 |
#(15 1 -9 10 5) minMax |
|
3353 |
(1 to:15) minMax |
|
3354 |
(-1 to:-15 by:-1) minMax |
|
3355 |
(-1 to:-15 by:-4) minMax |
|
3356 |
(0 to:15 by:4) minMax |
|
3357 |
" |
|
3358 |
! |
|
3359 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3360 |
nthLargest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3361 |
"return the n-largest element" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3362 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3363 |
^ (self largest:n) first |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3364 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3365 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3366 |
#(10 35 20 45 30 5) nthLargest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3367 |
#(10 35 20 45 30 5) nthLargest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3368 |
#(10 35 20 45 30 5) nthLargest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3369 |
#(10 35 20 45 30 5) nthLargest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3370 |
#(10 35 20 45 30 5) nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3371 |
#(10 35 20 45 30 5) nthLargest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3372 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3373 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3374 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3375 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3376 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3377 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3378 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3379 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3380 |
data asSortedCollection at:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3381 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3382 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3383 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3384 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3385 |
data nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3386 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3387 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3388 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3389 |
Transcript show:'nthMost -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3390 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3391 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3392 |
|
5557 | 3393 |
size |
3394 |
"return the number of elements in the receiver. |
|
3395 |
This is usually redefined in subclasses for more performance." |
|
3396 |
||
3397 |
|count "{ Class: SmallInteger }" | |
|
3398 |
||
3399 |
count := 0. |
|
3400 |
self do:[:element | |
|
6135 | 3401 |
count := count + 1 |
5557 | 3402 |
]. |
3403 |
^ count |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3404 |
! |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3405 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3406 |
speciesForAdding |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3407 |
"like species, but redefined for collections which cannot grow easily. |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3408 |
Used by functions which create a growing collection (see collect:with:, for example)" |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3409 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3410 |
^ self species |
602 | 3411 |
! ! |
3412 |
||
11706 | 3413 |
!Collection methodsFor:'searching'! |
3414 |
||
3415 |
findFirst:aBlock |
|
3416 |
"find the index of the first element, for which evaluation of the argument, aBlock |
|
3417 |
returns true; return its index or 0 if none detected. |
|
3418 |
This is much like #detect, however, here an INDEX is returned, |
|
3419 |
while #detect returns the element." |
|
3420 |
||
3421 |
^ self findFirst:aBlock ifNone:0 |
|
3422 |
||
3423 |
" |
|
3424 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
|
3425 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
3426 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
3427 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
3428 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
|
3429 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
3430 |
" |
|
3431 |
||
3432 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
3433 |
! ! |
|
3434 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3435 |
!Collection methodsFor:'set operations'! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3436 |
|
7169 | 3437 |
\ aCollection |
8606 | 3438 |
"return a new set containing all elements of the receiver, |
3439 |
which are NOT also contained in the aCollection |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3440 |
For large collections you better use a Set for aCollection" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3441 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3442 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3443 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3444 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3445 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3446 |
self do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3447 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3448 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3449 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3450 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3451 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3452 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3453 |
" |
7169 | 3454 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) asSet |
3455 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) |
|
3456 |
('hello' \ 'l') asString |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3457 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3458 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3459 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3460 |
intersect:aCollection |
8606 | 3461 |
"return a new set containing all elements of the receiver, |
3462 |
which are also contained in the argument collection. |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3463 |
For large collections you better use a Set for self" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3464 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3465 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3466 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3467 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3468 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3469 |
(self includes:element) ifTrue:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3470 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3471 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3472 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3473 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3474 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3475 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3476 |
#(0 1 2 3 4 5 6 7 8 9) asSet intersect:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3477 |
#(0 1 2 3 4 5 6 7 8 9) intersect:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3478 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3479 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3480 |
|
7169 | 3481 |
union:aCollection |
3482 |
"return a new set containing all elements of the receiver |
|
3483 |
plus those of the aCollection" |
|
3484 |
||
3485 |
|newCollection| |
|
3486 |
||
3487 |
newCollection := self speciesForAdding new. |
|
3488 |
newCollection addAll:self. |
|
3489 |
newCollection addAll:aCollection. |
|
3490 |
^ newCollection |
|
3491 |
||
3492 |
" |
|
3493 |
#(0 2 4 6 8) union:#(1 3 5 7) |
|
11384 | 3494 |
#(0 2 4 6 8) union:#(0 1 3 5 7) |
7169 | 3495 |
" |
3496 |
! |
|
3497 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3498 |
xor:aCollection |
8606 | 3499 |
"return a new set containing all elements, |
3500 |
which are contained in either the receiver or aCollection, but not in both. |
|
3501 |
||
3502 |
For large collections you better use Sets for both self and aCollection" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3503 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3504 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3505 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3506 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3507 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3508 |
(self includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3509 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3510 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3511 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3512 |
self do:[:element| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3513 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3514 |
newCollection add:element. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3515 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3516 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3517 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3518 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3519 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3520 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3521 |
#(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11) |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3522 |
" |
17735 | 3523 |
|
3524 |
" |
|
3525 |
|c1 c2| |
|
3526 |
||
3527 |
c1 := #( foo bar baz baloo ). |
|
3528 |
c2 := #( foe bar banana baloo ). |
|
3529 |
c1 symmetricDifference:c2. |
|
3530 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
3531 |
" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3532 |
! ! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3533 |
|
10811 | 3534 |
!Collection methodsFor:'sorting & reordering'! |
3535 |
||
3536 |
topologicalSort |
|
3537 |
"Sort a partial ordered collection. |
|
3538 |
The receiver consists of tupels defining a partial order. |
|
3539 |
Use the algorithm by R. E. Tarjan from 1972. |
|
3540 |
Answer an OrderedCollection containing the sorted items" |
|
3541 |
||
3542 |
|graph roots sorted count| |
|
3543 |
||
3544 |
"create a graph. |
|
3545 |
For each node there is an entry containing the number of references |
|
3546 |
and a list of arcs to other nodes" |
|
3547 |
||
3548 |
graph := Dictionary new. |
|
3549 |
||
3550 |
self do:[:eachTuple| |
|
3551 |
|node1| |
|
3552 |
node1 := eachTuple first. |
|
3553 |
graph at:node1 ifAbsentPut:[OrderedCollection with:0]. |
|
3554 |
1 to:(eachTuple size-1) do:[:i| |
|
3555 |
|node2 n2| |
|
3556 |
||
3557 |
node1 := eachTuple at:i. |
|
3558 |
node2 := eachTuple at:i+1. |
|
3559 |
"add the node" |
|
3560 |
graph at:node2 ifAbsentPut:[OrderedCollection with:0]. |
|
3561 |
"add an arc from node1 to node2" |
|
3562 |
(graph at:node1) add:node2. |
|
3563 |
"increment count on incoming arcs of node2" |
|
3564 |
n2 := graph at:node2. |
|
3565 |
n2 at:1 put:(n2 first + 1). |
|
3566 |
]. |
|
3567 |
]. |
|
3568 |
||
3569 |
"now find the root nodes (having zero incoming arcs)" |
|
3570 |
roots := OrderedCollection new. |
|
3571 |
graph keysAndValuesDo:[:eachKey :eachValue| |
|
3572 |
eachValue first = 0 ifTrue:[ |
|
3573 |
roots add:eachKey |
|
3574 |
]. |
|
3575 |
]. |
|
3576 |
||
3577 |
"for each root, out it to the sorted list and remove it from the graph. |
|
3578 |
This may cause referenced nodes to be moved to the root" |
|
3579 |
||
3580 |
sorted := OrderedCollection new:graph size. |
|
3581 |
[roots notEmpty] whileTrue:[ |
|
3582 |
|root eachEntry| |
|
3583 |
||
3584 |
root := roots removeFirst. |
|
3585 |
sorted add:root. |
|
3586 |
eachEntry := graph removeKey:root. |
|
3587 |
2 to:eachEntry size do:[:i| |
|
3588 |
|eachChild eachChildGraph| |
|
3589 |
eachChild := eachEntry at:i. |
|
3590 |
eachChildGraph := graph at:eachChild. |
|
3591 |
count := eachChildGraph at:1. |
|
3592 |
eachChildGraph at:1 put:(count - 1). |
|
3593 |
count = 1 ifTrue:[ |
|
3594 |
roots add:eachChild. |
|
3595 |
]. |
|
3596 |
]. |
|
3597 |
]. |
|
3598 |
||
3599 |
graph notEmpty ifTrue:[ |
|
3600 |
self error:'cycle in ordering' mayProceed:true. |
|
3601 |
]. |
|
3602 |
||
3603 |
^ sorted |
|
3604 |
||
3605 |
" |
|
3606 |
#((1 2) (1 3) (3 2)) topologicalSort |
|
11384 | 3607 |
#((1 2) (1 3) (3 2) (4)) topologicalSort |
3608 |
||
3609 |
with cycles: |
|
10811 | 3610 |
#((1 2) (1 3) (3 1)) topologicalSort |
11384 | 3611 |
#((1 2) (2 3) (3 1)) topologicalSort |
3612 |
#((1 2) (2 3) (3 4) (4 1)) topologicalSort |
|
11385
c3d908e81ec7
changed #topologicalSort - comment
Stefan Vogel <sv@exept.de>
parents:
11384
diff
changeset
|
3613 |
#((1 1) (1 2) (1 3) (3 2) (4)) topologicalSort |
10811 | 3614 |
|
3615 |
(Smalltalk allClasses collect:[:eachClass| |
|
3616 |
Array with:eachClass superclass with:eachClass |
|
3617 |
]) topologicalSort |
|
3618 |
" |
|
3619 |
! ! |
|
3620 |
||
602 | 3621 |
!Collection methodsFor:'testing'! |
3622 |
||
3623 |
capacity |
|
3624 |
"return the number of elements, that the receiver is |
|
3625 |
prepared to take. For most collections, this is the actual |
|
3626 |
size. However, some have more space preallocated to allow |
|
3627 |
for faster adding of elements. |
|
3628 |
Not used by the system; added for ST-80 compatibility." |
|
3629 |
||
3630 |
^ self size |
|
3631 |
! |
|
3632 |
||
3633 |
includes:anElement |
|
3634 |
"return true, if an object equal to the argument, anObject is in the list. |
|
3635 |
This compares using #= (i.e. it does not look for the object itself, |
|
3636 |
instead, one that compares equal). |
|
7807 | 3637 |
See #includesIdentical: when identity is asked for. |
3638 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 3639 |
|
3640 |
self do:[:element | |
|
6135 | 3641 |
(anElement = element) ifTrue:[^ true]. |
602 | 3642 |
]. |
3643 |
^ false |
|
3644 |
! |
|
3645 |
||
3646 |
includesAll:aCollection |
|
3647 |
"return true, if the the receiver includes all elements of |
|
3648 |
the argument, aCollection; false if any is missing. |
|
3649 |
Notice: this method has O-square runtime behavior and may be |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3650 |
slow for big receivers/args. |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3651 |
Think about using a Set, or Dictionary." |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3652 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3653 |
^ aCollection conform:[:element | (self includes:element)] |
602 | 3654 |
|
3655 |
" |
|
3656 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
3657 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
3658 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
3659 |
" |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3660 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3661 |
"Modified: / 13-10-2006 / 12:54:50 / cg" |
602 | 3662 |
! |
3663 |
||
3664 |
includesAny:aCollection |
|
3665 |
"return true, if the the receiver includes any elements of |
|
3666 |
the argument, aCollection; false if it includes none. |
|
7807 | 3667 |
Notice: this method has O^2(N) runtime behavior and may be |
3668 |
slow for big receivers/args. |
|
3669 |
Think about using a Set or Dictionary. |
|
3670 |
Some speedup is also possible, by arranging highly |
|
6135 | 3671 |
probable elements towards the beginning of aCollection, |
3672 |
to avoid useless searches." |
|
602 | 3673 |
|
3674 |
aCollection do:[:element | |
|
6135 | 3675 |
(self includes:element) ifTrue:[^ true]. |
602 | 3676 |
]. |
3677 |
^ false |
|
3678 |
||
3679 |
" |
|
3680 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
3681 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
3682 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
3683 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
3684 |
" |
|
3685 |
! |
|
3686 |
||
10504 | 3687 |
includesAnyIdentical:aCollection |
3688 |
"return true, if the the receiver includes any elements of |
|
3689 |
the argument, aCollection; false if it includes none. |
|
3690 |
Use identity compare for comparing. |
|
3691 |
Notice: this method has O^2(N) runtime behavior and may be |
|
3692 |
slow for big receivers/args. |
|
3693 |
Think about using a Set or Dictionary. |
|
3694 |
Some speedup is also possible, by arranging highly |
|
3695 |
probable elements towards the beginning of aCollection, |
|
3696 |
to avoid useless searches." |
|
3697 |
||
3698 |
aCollection do:[:element | |
|
3699 |
(self includesIdentical:element) ifTrue:[^ true]. |
|
3700 |
]. |
|
3701 |
^ false |
|
3702 |
||
3703 |
" |
|
3704 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(1 2 3) |
|
3705 |
#('hello' 'there' 'world') includesAnyIdentical:#('hello' 'world') |
|
3706 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(7 8 9) |
|
3707 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(8 9 10) |
|
3708 |
" |
|
3709 |
! |
|
3710 |
||
602 | 3711 |
includesIdentical:anElement |
7807 | 3712 |
"return true, if the argument, anObject is in the collection. |
602 | 3713 |
This compares using #== (i.e. object identity). |
7807 | 3714 |
See #includes: when equality is asked for. |
3715 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 3716 |
|
3717 |
self do:[:element | |
|
6135 | 3718 |
(anElement == element) ifTrue:[^ true]. |
602 | 3719 |
]. |
3720 |
^ false |
|
3721 |
! |
|
3722 |
||
9005 | 3723 |
isCollection |
3724 |
"return true, if the receiver is some kind of collection; |
|
3725 |
true is returned here - the method is redefined from Object." |
|
3726 |
||
3727 |
^ true |
|
3728 |
! |
|
3729 |
||
602 | 3730 |
isEmpty |
3731 |
"return true, if the receiver is empty" |
|
3732 |
||
3733 |
^ self size == 0 |
|
3734 |
! |
|
3735 |
||
6181 | 3736 |
isEmptyOrNil |
8833 | 3737 |
"return true if I am nil or an empty collection - true here, if the receivers size is 0, |
3738 |
(from Sqeak)" |
|
6181 | 3739 |
|
3740 |
^ self isEmpty |
|
3741 |
||
3742 |
"Created: / 13.11.2001 / 13:17:12 / cg" |
|
6182 | 3743 |
"Modified: / 13.11.2001 / 13:28:35 / cg" |
6181 | 3744 |
! |
3745 |
||
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3746 |
isNilOrEmptyCollection |
8833 | 3747 |
"return true if I am nil or an empty collection - false here. |
3748 |
Obsolete, use isEmptyOrNil." |
|
3749 |
||
3750 |
<resource:#obsolete> |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3751 |
|
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3752 |
^ self isEmpty |
6182 | 3753 |
|
3754 |
"Modified: / 13.11.2001 / 13:27:55 / cg" |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3755 |
! |
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
3756 |
|
9005 | 3757 |
isNonByteCollection |
3758 |
"return true, if the receiver is some kind of collection, but not a String, ByteArray etc.; |
|
3759 |
true is returned here - the method is redefined from Object." |
|
3760 |
||
3761 |
^ true |
|
3762 |
! |
|
3763 |
||
3764 |
isSorted |
|
3765 |
"return true, if the receiver is sorted. |
|
3766 |
Collections which hold their elements in sorted order |
|
3767 |
should return true. Some algorithms (quicksort) degenerate when |
|
3768 |
operating on sorted collections and can be avoided if this information |
|
3769 |
is given. The default returned here (false) should not hurt. |
|
3770 |
I.e. you should NEVER depend on that in your application." |
|
3771 |
||
3772 |
^ false |
|
3773 |
||
3774 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
3775 |
! |
|
3776 |
||
3777 |
isSortedBy:aBlock |
|
3778 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock). |
|
3779 |
Collections which hold their elements in sorted order |
|
3780 |
should return true. Some algorithms (quicksort) degenerate when |
|
3781 |
operating on sorted collections and can be avoided if this information |
|
3782 |
is given. The default returned here (false) should not hurt. |
|
3783 |
I.e. you should NEVER depend on that in your application." |
|
3784 |
||
3785 |
^ false |
|
3786 |
||
3787 |
! |
|
3788 |
||
3789 |
isSortedCollection |
|
3790 |
"return true, if the receiver is a sortedCollection." |
|
3791 |
||
3792 |
^ false |
|
3793 |
||
3794 |
! |
|
3795 |
||
9247 | 3796 |
isWeakCollection |
3797 |
"return true, if the receiver has weak references to its elements." |
|
3798 |
||
3799 |
^ false |
|
3800 |
! |
|
3801 |
||
602 | 3802 |
notEmpty |
3803 |
"return true, if the receiver is not empty" |
|
3804 |
||
3805 |
^ self isEmpty not |
|
3806 |
! |
|
3807 |
||
6932 | 3808 |
notEmptyOrNil |
3809 |
"Squeak compatibility: |
|
3810 |
return true if I am neither nil nor an empty collection." |
|
3811 |
||
3812 |
^ self notEmpty |
|
3813 |
! |
|
3814 |
||
602 | 3815 |
occurrencesOf:anElement |
3816 |
"return the number of occurrences of the argument, anElement in |
|
5048 | 3817 |
the receiver. Uses #= (i.e. equality) compare." |
602 | 3818 |
|
3819 |
|count "{ Class: SmallInteger }" | |
|
3820 |
||
3821 |
count := 0. |
|
3822 |
self do:[:element | |
|
5048 | 3823 |
(anElement = element) ifTrue:[ |
3824 |
count := count + 1 |
|
3825 |
]. |
|
602 | 3826 |
]. |
3827 |
^ count |
|
8274 | 3828 |
! |
3829 |
||
3830 |
occurrencesOfAny:aCollectionOfElements |
|
3831 |
"return the number of occurrences of any in aCollectionOfElements in the receiver. |
|
3832 |
Uses #= (i.e. equality) compare. |
|
3833 |
Should be redefined in subclass(es) if ever used heavily." |
|
3834 |
||
3835 |
|count "{ Class: SmallInteger }" | |
|
3836 |
||
3837 |
count := 0. |
|
8275 | 3838 |
aCollectionOfElements do:[:element | |
3839 |
count := count + (self occurrencesOf:element) |
|
8274 | 3840 |
]. |
3841 |
^ count |
|
3842 |
||
3843 |
" |
|
3844 |
#(1 4 6 8 4 1) occurrencesOfAny:#(1 4) |
|
3845 |
#(1 4 6 8 4 1) occurrencesOfAny:#(2 5) |
|
3846 |
'hello world' occurrencesOfAny:'hel' |
|
3847 |
" |
|
602 | 3848 |
! ! |
3849 |
||
4680 | 3850 |
!Collection methodsFor:'tracing'! |
3851 |
||
3852 |
traceInto:aRequestor level:level from:referrer |
|
3853 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
3854 |
||
3855 |
^ aRequestor traceCollection:self level:level from:referrer |
|
3856 |
||
3857 |
||
3858 |
! ! |
|
3859 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3860 |
!Collection methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3861 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3862 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3863 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3864 |
^ aVisitor visitCollection:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3865 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
3866 |
|
2097 | 3867 |
!Collection class methodsFor:'documentation'! |
628 | 3868 |
|
3869 |
version |
|
17807 | 3870 |
^ '$Id: Collection.st 10590 2010-11-09 16:24:28Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3871 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3872 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3873 |
version_CVS |
17807 | 3874 |
^ 'Header: /cvs/stx/stx/libbasic/Collection.st,v 1.252 2010-09-21 06:58:29 stefan Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
3875 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
3876 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17747
diff
changeset
|
3877 |
version_SVN |
17807 | 3878 |
^ '$Id: Collection.st 10590 2010-11-09 16:24:28Z vranyj1 $' |
628 | 3879 |
! ! |
6867 | 3880 |
|
602 | 3881 |
Collection initialize! |
17767 | 3882 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
3883 |
|
17780 | 3884 |
|
17795 | 3885 |
|
17797 | 3886 |
|
17800 | 3887 |
|
17807 | 3888 |