author | Stefan Vogel <sv@exept.de> |
Thu, 09 Nov 2017 07:04:34 +0100 | |
changeset 22334 | 8e2e3cb23910 |
parent 22321 | 9d2e84a333a0 |
child 22445 | ff0ebbd11d41 |
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 |
|
17266 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
Object subclass:#Collection |
21650 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'EmptyCollectionSignal InvalidKeySignal NotEnoughElementsSignal |
|
19 |
ValueNotFoundSignal' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Collections-Abstract' |
|
1 | 22 |
! |
23 |
||
2097 | 24 |
!Collection class methodsFor:'documentation'! |
88 | 25 |
|
26 |
copyright |
|
27 |
" |
|
28 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6135 | 29 |
All Rights Reserved |
1 | 30 |
|
88 | 31 |
This software is furnished under a license and may be used |
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
! |
|
1 | 39 |
|
88 | 40 |
documentation |
41 |
" |
|
42 |
Abstract superclass for all collections. |
|
43 |
This abstract class provides functionality common to all collections, |
|
20151 | 44 |
without knowing how the concrete class implements things. |
17121 | 45 |
Thus, all methods found here depend on some basic mechanisms |
20151 | 46 |
to be defined in the concrete class. |
88 | 47 |
These basic methods are usually defined as #subclassResponsibility here. |
48 |
Some methods are also redefined for better performance. |
|
1289 | 49 |
|
1325 | 50 |
Subclasses should at least implement: |
51 |
do: - enumerate elements |
|
52 |
||
53 |
they should implement one of the following set of access messages: |
|
17121 | 54 |
For keyed collections: |
1325 | 55 |
at:ifAbsent: - fetching an element |
56 |
at: - fetching an element |
|
57 |
at:put: - storing an element |
|
58 |
||
17121 | 59 |
For unkeyed collections: |
1325 | 60 |
add: - add an element |
61 |
remove:ifAbsent: - remove an element |
|
62 |
||
17121 | 63 |
Given that the above is implemented in a concrete subclass, |
64 |
Collection provides protocol for enumeration, searching and others. |
|
65 |
However, for performance reasons, many of them are also redefined in |
|
66 |
concrete subclasses, as some can be implemented much faster if implementation |
|
67 |
details are known (for example, searching can be done faster if it is known that |
|
19306 | 68 |
elements are sorted or accessible by a key). |
1325 | 69 |
|
1289 | 70 |
[author:] |
71 |
Claus Gittinger |
|
88 | 72 |
" |
73 |
! ! |
|
1 | 74 |
|
2097 | 75 |
!Collection class methodsFor:'initialization'! |
10 | 76 |
|
77 |
initialize |
|
78 |
"setup the signal" |
|
79 |
||
54 | 80 |
InvalidKeySignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
81 |
InvalidKeySignal := Error newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
82 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
83 |
InvalidKeySignal notifierString:'invalid key:'. |
345 | 84 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
85 |
ValueNotFoundSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
86 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
87 |
ValueNotFoundSignal notifierString:'value not found:'. |
155 | 88 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
89 |
NotEnoughElementsSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
90 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
91 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
348 | 92 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
93 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
94 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
95 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
54 | 96 |
] |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
97 |
|
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
98 |
"Modified: / 8.11.1997 / 19:18:17 / cg" |
10 | 99 |
! ! |
100 |
||
2097 | 101 |
!Collection class methodsFor:'instance creation'! |
155 | 102 |
|
13049
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
103 |
combiningEach:collection1 withEach:collection2 using:aTwoArgBlock |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
104 |
"evaluate aTwoArgBlock for each combination of elements from collection1 |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
105 |
and collection2 and return an instance of the receiver containing all those elements" |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
106 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
107 |
|newColl idx useIndex newEl| |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
108 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
109 |
newColl := self new:(collection1 size * collection2 size). |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
110 |
useIndex := newColl isFixedSize. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
111 |
idx := 1. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
112 |
collection1 do:[:e1 | |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
113 |
collection2 do:[:e2 | |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
114 |
newEl := aTwoArgBlock value:e1 value:e2. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
115 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
116 |
useIndex ifTrue:[ |
21735 | 117 |
newColl at:idx put:newEl. |
13049
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
118 |
idx := idx + 1. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
119 |
] ifFalse:[ |
21735 | 120 |
newColl add:newEl |
13049
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
121 |
]. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
122 |
] |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
123 |
]. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
124 |
^ newColl |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
125 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
126 |
" |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
127 |
Set combiningEach:#(1 2 3 4 5) withEach:(10 to:100 by:10) using:[:a :b | a * b] |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
128 |
Set combiningEach:#(1 2 3 4 5) withEach:#(1 2 3 4 5) using:[:a :b | a * b] |
21735 | 129 |
Array combiningEach:#(1 2 3 4 5) withEach:#(1 2 3 4 5) using:[:a :b | a * b] |
13049
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
130 |
" |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
131 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
132 |
"Created: / 25-08-2010 / 17:21:47 / cg" |
21735 | 133 |
"Modified (comment): / 28-04-2017 / 14:38:41 / stefan" |
13049
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
134 |
! |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
135 |
|
602 | 136 |
new:size withAll:element |
5626 | 137 |
"return a new Collection of size, where all elements are |
602 | 138 |
initialized to element" |
348 | 139 |
|
602 | 140 |
|newCollection| |
92 | 141 |
|
602 | 142 |
newCollection := self new:size. |
143 |
size timesRepeat:[newCollection add:element] |
|
144 |
! |
|
1 | 145 |
|
5626 | 146 |
newFrom:aCollection |
147 |
"Return an instance of me containing the same elements as aCollection." |
|
148 |
||
149 |
^ self withAll:aCollection |
|
150 |
||
151 |
" |
|
152 |
Bag newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
153 |
Set newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
154 |
" |
|
155 |
! |
|
156 |
||
22298 | 157 |
newWithCapacity:n |
22306 | 158 |
"return a new empty Collection preferrably with capacity for n elements. |
22298 | 159 |
Redefined in StringCollection, where #new: returns a non-empty collection. |
22306 | 160 |
This does not work for ArrayedCollections, which will be not empty. |
161 |
||
162 |
We return an empty collection here, because there are subclasses |
|
163 |
which do not implement #new:." |
|
164 |
||
165 |
^ self new |
|
22298 | 166 |
|
167 |
"Created: / 09-10-2017 / 16:41:59 / stefan" |
|
22306 | 168 |
"Modified: / 10-10-2017 / 17:47:56 / stefan" |
22298 | 169 |
! |
170 |
||
171 |
newWithSize:n |
|
172 |
"return a new non-empty collection with n elements. |
|
173 |
Kludges around the inconsistent definition of #new: in |
|
174 |
returning an empty collection in OrderedCollection and Set |
|
175 |
and |
|
176 |
returning an non-empty collection in ArrayedCollectins and StringCollection." |
|
177 |
||
178 |
^ self new:n |
|
179 |
||
180 |
"Modified (comment): / 09-10-2017 / 16:56:34 / stefan" |
|
16710 | 181 |
! |
182 |
||
1 | 183 |
with:anObject |
184 |
"return a new Collection with one element:anObject" |
|
185 |
||
22298 | 186 |
^ (self newWithCapacity:1) |
187 |
add:anObject; |
|
188 |
yourself. |
|
189 |
||
190 |
"Modified: / 09-10-2017 / 16:45:59 / stefan" |
|
1 | 191 |
! |
192 |
||
193 |
with:firstObject with:secondObject |
|
194 |
"return a new Collection with two elements:firstObject and secondObject" |
|
195 |
||
22298 | 196 |
^ (self newWithCapacity:2) |
197 |
add:firstObject; add:secondObject; |
|
198 |
yourself. |
|
199 |
||
200 |
"Modified: / 09-10-2017 / 16:45:52 / stefan" |
|
1 | 201 |
! |
202 |
||
203 |
with:firstObject with:secondObject with:thirdObject |
|
204 |
"return a new Collection with three elements" |
|
205 |
||
22298 | 206 |
^ (self newWithCapacity:3) |
207 |
add:firstObject; add:secondObject; add:thirdObject; |
|
208 |
yourself. |
|
209 |
||
210 |
"Modified: / 09-10-2017 / 16:45:46 / stefan" |
|
1 | 211 |
! |
212 |
||
213 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
214 |
"return a new Collection with four elements" |
|
215 |
||
22298 | 216 |
^ (self newWithCapacity:4) |
217 |
add:firstObject; add:secondObject; add:thirdObject; add:fourthObject; |
|
218 |
yourself. |
|
219 |
||
220 |
"Modified: / 09-10-2017 / 16:45:37 / stefan" |
|
1 | 221 |
! |
222 |
||
2225
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 |
345 | 224 |
"return a new Collection with five elements" |
225 |
||
22298 | 226 |
^ (self newWithCapacity:5) |
227 |
add:a1; add:a2; add:a3; add:a4; add:a5; |
|
228 |
yourself. |
|
229 |
||
230 |
"Modified: / 22-01-1997 / 19:34:01 / cg" |
|
231 |
"Modified: / 09-10-2017 / 16:45:29 / stefan" |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
232 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
233 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
234 |
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
|
235 |
"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
|
236 |
|
22298 | 237 |
^ (self newWithCapacity:6) |
238 |
add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; |
|
239 |
yourself. |
|
240 |
||
241 |
"Created: / 22-01-1997 / 19:34:14 / cg" |
|
242 |
"Modified: / 09-10-2017 / 16:45:12 / stefan" |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
243 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
244 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
245 |
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
|
246 |
"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
|
247 |
|
22298 | 248 |
^ (self newWithCapacity:7) |
249 |
add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7; |
|
250 |
yourself. |
|
251 |
||
252 |
"Created: / 22-01-1997 / 19:34:24 / cg" |
|
253 |
"Modified: / 09-10-2017 / 16:44:59 / stefan" |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
254 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
255 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
256 |
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
|
257 |
"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
|
258 |
|
22298 | 259 |
^ (self newWithCapacity:8) |
260 |
add:a1; add:a2; add:a3; add:a4; add:a5; add:a6; add:a7; add:a8; |
|
261 |
yourself. |
|
262 |
||
263 |
"Created: / 22-01-1997 / 19:34:34 / cg" |
|
264 |
"Modified: / 09-10-2017 / 16:44:45 / stefan" |
|
345 | 265 |
! |
266 |
||
1 | 267 |
withAll:aCollection |
268 |
"return a new Collection with all elements taken from the argument, |
|
269 |
aCollection" |
|
270 |
||
22334 | 271 |
"/ Doesn't work with Iterator: |
272 |
"/ ^ (self newWithCapacity:aCollection size) |
|
273 |
||
274 |
^ self new |
|
20171 | 275 |
addAll:aCollection; |
276 |
yourself. |
|
22298 | 277 |
|
22334 | 278 |
|
22298 | 279 |
" |
280 |
OrderedCollection withAll:#(1 2 3 4 5 6) |
|
281 |
Set withAll:#(1 2 3 4 5 6) |
|
282 |
StringCollection withAll:#('line1' 'line2' 'line3') |
|
283 |
String withAll:#($a $b $c) |
|
22334 | 284 |
Set withAll:(Iterator on:[:whatToDo | 1 to:10 do:[:i | whatToDo value:i]]). |
285 |
" |
|
286 |
||
287 |
"Modified (comment): / 09-11-2017 / 07:03:53 / stefan" |
|
9159 | 288 |
! |
289 |
||
290 |
withSize:n |
|
21040 | 291 |
"obsolete: please use newWithSize:, for its better name" |
292 |
||
293 |
<resource: #obsolete> |
|
9159 | 294 |
|
16710 | 295 |
^ self newWithSize:n |
9159 | 296 |
! ! |
297 |
||
1 | 298 |
|
22045 | 299 |
|
2097 | 300 |
!Collection class methodsFor:'Signal constants'! |
10 | 301 |
|
602 | 302 |
emptyCollectionSignal |
303 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 304 |
|
602 | 305 |
^ EmptyCollectionSignal |
308 | 306 |
! |
307 |
||
602 | 308 |
invalidKeySignal |
309 |
"return the signal used to report bad key usage" |
|
308 | 310 |
|
602 | 311 |
^ InvalidKeySignal |
201 | 312 |
! |
313 |
||
602 | 314 |
notEnoughElementsSignal |
315 |
"return the signal used to report attempts for an operation, for which |
|
316 |
there are not enough elements in the collection" |
|
249 | 317 |
|
602 | 318 |
^ NotEnoughElementsSignal |
404 | 319 |
! |
320 |
||
602 | 321 |
valueNotFoundSignal |
322 |
"return the signal used to report a nonexisting element." |
|
404 | 323 |
|
602 | 324 |
^ ValueNotFoundSignal |
325 |
! ! |
|
404 | 326 |
|
14608 | 327 |
!Collection class methodsFor:'instance creation-streaming'! |
328 |
||
329 |
writeStreamClass |
|
330 |
"the type of stream used in writeStream" |
|
331 |
||
332 |
^ WriteStream |
|
333 |
||
334 |
" |
|
335 |
OrderedCollection writeStreamClass |
|
336 |
" |
|
337 |
||
338 |
"Created: / 09-01-2011 / 10:37:15 / cg" |
|
339 |
! ! |
|
340 |
||
12090 | 341 |
|
2097 | 342 |
!Collection class methodsFor:'queries'! |
404 | 343 |
|
602 | 344 |
growIsCheap |
345 |
"return true, if this collection can easily grow |
|
346 |
(i.e. without a need for become:). |
|
347 |
Returns true here; this method is redefined in fix-size |
|
348 |
collections" |
|
404 | 349 |
|
602 | 350 |
^ true |
8893 | 351 |
! |
352 |
||
353 |
isAbstract |
|
11231 | 354 |
"Return if this class is an abstract class. |
355 |
True is returned for Collection here; false for subclasses. |
|
19459 | 356 |
Abstract subclasses must redefine this again." |
11231 | 357 |
|
8893 | 358 |
^ self == Collection |
20387 | 359 |
! |
360 |
||
361 |
mutableClass |
|
362 |
"Return a version of me with mutable instances. |
|
363 |
Only redefined in the immutable collections (of which instances are |
|
364 |
created by the compiler)" |
|
365 |
||
366 |
^ self |
|
257 | 367 |
! ! |
368 |
||
18881 | 369 |
!Collection methodsFor:'Compatibility-ANSI'! |
6411 | 370 |
|
371 |
identityIncludes:anObject |
|
7807 | 372 |
"return true, if the argument, anObject is in the collection. |
18881 | 373 |
Same as #includesIdentical for Dolphin/ANSI compatibility." |
7807 | 374 |
|
6411 | 375 |
^ self includesIdentical:anObject. |
18881 | 376 |
! ! |
377 |
||
6411 | 378 |
|
7261 | 379 |
!Collection methodsFor:'Compatibility-Squeak'! |
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
380 |
|
13699 | 381 |
, aCollection |
14070 | 382 |
"return a new collection formed from concatenating the receiver with the argument" |
383 |
||
13699 | 384 |
^ self copy addAll: aCollection; yourself |
385 |
||
386 |
"Created: / 14-09-2011 / 16:32:06 / cg" |
|
387 |
! |
|
388 |
||
9354 | 389 |
addIfNotPresent:anObject |
7011 | 390 |
"Include anObject as one of the receiver's elements, but only if there |
391 |
is no such element already. Anwser anObject." |
|
392 |
||
393 |
(self includes: anObject) ifFalse: [self add: anObject]. |
|
394 |
^ anObject |
|
395 |
! |
|
396 |
||
6149 | 397 |
anyOne |
8858 | 398 |
"return any element from the collection. |
399 |
Report an error if there is none. |
|
6149 | 400 |
Same as #anElement - for Squeak compatibility" |
401 |
||
402 |
^ self anElement |
|
403 |
! |
|
404 |
||
13678 | 405 |
associationsDo: aBlock |
406 |
"cg: I think this is bad, but - well..." |
|
407 |
||
408 |
self do: aBlock |
|
409 |
||
410 |
"Created: / 12-09-2011 / 09:21:58 / cg" |
|
411 |
! |
|
412 |
||
15105 | 413 |
contents |
21735 | 414 |
"I am the contents of the collection" |
415 |
||
15105 | 416 |
^ self |
21735 | 417 |
|
418 |
"Modified (comment): / 28-04-2017 / 14:32:04 / stefan" |
|
15105 | 419 |
! |
420 |
||
12249 | 421 |
difference: aCollection |
422 |
"Answer the set-theoretic difference of two collections." |
|
423 |
||
21267 | 424 |
^ self \ aCollection |
12249 | 425 |
|
426 |
" |
|
427 |
#(0 2 4 6 8) difference:#(2 4) |
|
428 |
" |
|
21267 | 429 |
|
430 |
"Modified: / 20-01-2017 / 19:21:35 / stefan" |
|
12249 | 431 |
! |
432 |
||
12345 | 433 |
gather:aBlock |
434 |
"return an Array, |
|
16172 | 435 |
containing all elements as returned from applying aBlock to each element of the receiver, |
12345 | 436 |
where the block returns a collection of to-be-added elements. |
437 |
This could also be called: collectAllAsArray:" |
|
438 |
||
439 |
^ self gather:aBlock as:Array |
|
440 |
||
441 |
" |
|
442 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] |
|
443 |
" |
|
444 |
! |
|
445 |
||
446 |
gather:aBlock as:aClass |
|
447 |
"return an instance of the collection-class aClass, |
|
16172 | 448 |
containing all elements as returned from applying aBlock to each element of the receiver. |
12345 | 449 |
where the block returns a collection of to-be-added elements. |
450 |
This could also be called: collectAll:as:" |
|
451 |
||
452 |
^ aClass streamContents:[:s | |
|
453 |
self do: [:ea | s nextPutAll: (aBlock value: ea)] |
|
454 |
] |
|
455 |
||
456 |
" |
|
457 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] as:OrderedCollection |
|
458 |
(Set withAll:#(10 20 30 10 20 40)) collectAll:[:el | Array with:el with:el * 2] |
|
459 |
" |
|
460 |
! |
|
461 |
||
9354 | 462 |
groupBy:keyBlock having:selectBlock |
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
463 |
"Like in SQL operation - Split the receiver's contents into collections of |
9354 | 464 |
elements for which keyBlock returns the same results, and return those |
465 |
collections allowed by selectBlock. " |
|
466 |
||
21735 | 467 |
|result| |
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
468 |
|
9319 | 469 |
result := OrderedDictionary new. |
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
470 |
self do:[:e | |
21735 | 471 |
|resultColl key| |
472 |
||
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
473 |
key := keyBlock value: e. |
21735 | 474 |
resultColl := result at:key ifAbsentPut:[OrderedCollection new]. |
475 |
resultColl add: e |
|
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
476 |
]. |
21735 | 477 |
^ result select: selectBlock |
478 |
||
479 |
" |
|
480 |
#(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a | true] |
|
481 |
" |
|
482 |
||
483 |
"Modified (format): / 28-04-2017 / 13:56:37 / stefan" |
|
9319 | 484 |
! |
485 |
||
16753 | 486 |
ifEmpty:alternativeValue |
487 |
"return the receiver if not empty, alternativeValue otherwise" |
|
488 |
||
489 |
^ self size ~~ 0 ifTrue:[self] ifFalse:[alternativeValue value] |
|
490 |
||
491 |
" |
|
492 |
'foo' ifEmpty: 'bar' |
|
493 |
'' ifEmpty: 'bar' |
|
494 |
'' ifEmpty: [ Time now printString ] |
|
495 |
" |
|
496 |
! |
|
497 |
||
9065 | 498 |
ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
19459 | 499 |
"return ifNotEmptyValue if not empty, ifEmptyValue otherwise" |
500 |
||
9065 | 501 |
|action| |
502 |
||
503 |
action := self isEmpty ifTrue:[ ifEmptyValue ] ifFalse:[ ifNotEmptyValue ]. |
|
22298 | 504 |
(action isBlock and:[action argumentCount == 1]) ifTrue:[ |
505 |
^ action value:self |
|
9065 | 506 |
]. |
22298 | 507 |
^ action value |
508 |
||
509 |
"Modified: / 09-10-2017 / 17:16:07 / stefan" |
|
9065 | 510 |
! |
511 |
||
512 |
ifEmpty:ifEmptyValue ifNotEmptyDo:ifNotEmptyValue |
|
19459 | 513 |
"return ifNotEmptyValue if not empty, ifEmptyValue otherwise" |
514 |
||
9065 | 515 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
516 |
! |
|
517 |
||
518 |
ifEmptyDo:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
19459 | 519 |
"return ifNotEmptyValue if not empty, ifEmptyValue otherwise" |
520 |
||
9065 | 521 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
522 |
! |
|
523 |
||
524 |
ifNotEmpty:ifNotEmptyValue |
|
19459 | 525 |
"return ifNotEmptyValue if not empty, nil otherwise" |
526 |
||
9065 | 527 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
528 |
! |
|
529 |
||
530 |
ifNotEmptyDo:ifNotEmptyValue |
|
19459 | 531 |
"return ifNotEmptyValue if not empty, nil otherwise" |
532 |
||
9065 | 533 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
534 |
! |
|
535 |
||
536 |
ifNotEmptyDo:ifNotEmptyValue ifEmpty:ifEmptyValue |
|
19459 | 537 |
"return ifNotEmptyValue if not empty, ifEmptyValue otherwise" |
538 |
||
9065 | 539 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
540 |
! ! |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
541 |
|
22045 | 542 |
|
1 | 543 |
!Collection methodsFor:'accessing'! |
544 |
||
602 | 545 |
anElement |
6966 | 546 |
"return any element from the collection, |
17554 | 547 |
report an error if there is none. |
548 |
Use this to fetch the some element from a collection which is non-indexed or which |
|
549 |
has a non-numeric index. I.e. if someone gets an arbitrary collection which might be either indexable |
|
550 |
or not, anElement is a save way to access some element without a need to check for a proper key." |
|
602 | 551 |
|
552 |
self do: [:each | ^ each]. |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
553 |
self emptyCollectionError. |
17256 | 554 |
|
555 |
" |
|
17554 | 556 |
#() anElement -> Error |
557 |
#(1 2 3) anElement -> 1 |
|
558 |
#(1 2 3) asSet anElement -> one of them (undefined, which one) |
|
17256 | 559 |
" |
602 | 560 |
! |
561 |
||
21735 | 562 |
at:aKey ifAbsent:absentBlock |
563 |
"return the element at aKey if valid. |
|
564 |
If the key is not present, return the result of evaluating |
|
565 |
the exceptionblock. |
|
566 |
NOTICE: |
|
567 |
in ST-80, this message is only defined for Dictionaries, |
|
568 |
however, having a common protocol with indexed collections |
|
569 |
often simplifies things." |
|
570 |
||
571 |
^ [ |
|
572 |
self at:aKey |
|
573 |
] on:KeyNotFoundError do:absentBlock. |
|
574 |
||
575 |
"Created: / 28-04-2017 / 14:25:58 / stefan" |
|
576 |
! |
|
577 |
||
13046
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
578 |
at:aKey ifNilOrAbsentPut:valueBlock |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
579 |
"try to fetch the element at aKey. If either the key is invalid (as in a Dictionary) |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
580 |
or there is no element stored under that key (as in an Array), set the element |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
581 |
from the valueBlock and return it. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
582 |
Useful for lazy initialization of collections." |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
583 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
584 |
|val| |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
585 |
|
21735 | 586 |
val := self at:aKey ifAbsent:[]. |
13046
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
587 |
val isNil ifTrue:[ |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
588 |
self at:aKey put:(val := valueBlock value). |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
589 |
]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
590 |
^ val |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
591 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
592 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
593 |
|d| |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
594 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
595 |
d := Dictionary new. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
596 |
d at:#foo ifNilOrAbsentPut:[ 'hello' ]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
597 |
d |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
598 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
599 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
600 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
601 |
|a| |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
602 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
603 |
a := Array new:10. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
604 |
a at:1 ifNilOrAbsentPut:[ 'hello' ]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
605 |
a |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
606 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
607 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
608 |
"Created: / 24-08-2010 / 17:07:21 / cg" |
21735 | 609 |
"Modified: / 28-04-2017 / 14:09:43 / stefan" |
13046
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
610 |
! |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
611 |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
612 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
613 |
"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
|
614 |
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
|
615 |
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
|
616 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
617 |
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
|
618 |
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
|
619 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
620 |
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
|
621 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
622 |
" |
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
623 |
args: indexCollection : <Collection of keys<object> > |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
624 |
returns: self |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
625 |
" |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
626 |
|
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
627 |
" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
628 |
(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
|
629 |
(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
|
630 |
(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
|
631 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
632 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
633 |
(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
|
634 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
635 |
|
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
636 |
"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
|
637 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
638 |
|
19037 | 639 |
decrementAt:aKey |
640 |
"remove 1 from the count stored under aKey. |
|
641 |
If not yet present, assume 0 as initial counter value." |
|
642 |
||
643 |
self incrementAt:aKey by:-1 |
|
644 |
! |
|
645 |
||
4985 | 646 |
fifth |
647 |
"return the fifth element of the collection. |
|
648 |
For unordered collections, this simply returns the fifth |
|
649 |
element when enumerating them. |
|
650 |
This should be redefined in subclasses." |
|
651 |
||
652 |
^ self nth:5 |
|
653 |
||
654 |
" |
|
655 |
#(1 2 3 4 5) fifth |
|
656 |
" |
|
657 |
! |
|
658 |
||
68 | 659 |
first |
660 |
"return the first element of the collection. |
|
4855 | 661 |
For unordered collections, this simply returns the first |
662 |
element when enumerating them. |
|
68 | 663 |
This should be redefined in subclasses." |
664 |
||
665 |
self do:[:e | ^ e]. |
|
666 |
||
667 |
"error if collection is empty" |
|
155 | 668 |
^ self emptyCollectionError |
68 | 669 |
! |
670 |
||
5887 | 671 |
first:n |
672 |
"return the n first elements of the collection. |
|
17702 | 673 |
No longer raises an error if there are not enough elements; |
674 |
instead, returns what is there. |
|
675 |
||
5887 | 676 |
For unordered collections, this simply returns the first |
20211 | 677 |
n elements when enumerating them. |
5887 | 678 |
(Warning: the contents of the returned collection is not deterministic in this case). |
679 |
This should be redefined in subclasses." |
|
680 |
||
681 |
|coll remain| |
|
682 |
||
683 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
684 |
||
685 |
coll := OrderedCollection new. |
|
20211 | 686 |
remain := n. |
687 |
self do:[:e | |
|
13741 | 688 |
coll add:e. |
689 |
remain := remain - 1. |
|
690 |
remain == 0 ifTrue:[ |
|
691 |
^ coll |
|
692 |
]. |
|
693 |
]. |
|
17702 | 694 |
"/ OLD: |
695 |
"/ "error if collection has not enough elements" |
|
696 |
"/ ^ self notEnoughElementsError |
|
697 |
"/ NEW: |
|
698 |
"/ return what we have - no error if not enough elements |
|
699 |
^ coll. |
|
5887 | 700 |
|
701 |
" |
|
20211 | 702 |
#(1 2 3 4 5) first:3 |
703 |
#(1 2 3 4 5) asSet first:3 |
|
704 |
#(1 2 3) first:5 |
|
705 |
#(1 2 3) asSet first:5 |
|
5887 | 706 |
" |
13741 | 707 |
|
708 |
"Modified (format): / 29-09-2011 / 10:16:49 / cg" |
|
5887 | 709 |
! |
710 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
711 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
712 |
"return the first element of the collection. |
21421 | 713 |
If it's empty, return the exceptionValue. |
20790 | 714 |
(i.e. don't trigger an error as done in #first)" |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
715 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
716 |
self isEmpty ifTrue:[^ exceptionValue value]. |
10593 | 717 |
^ self first |
718 |
||
719 |
"Modified: / 04-06-2007 / 22:36:14 / cg" |
|
21421 | 720 |
"Modified (comment): / 13-02-2017 / 19:58:24 / cg" |
10593 | 721 |
! |
722 |
||
723 |
firstOrNil |
|
724 |
"return the first element of the collection. |
|
21421 | 725 |
If it's empty, return nil. |
20791 | 726 |
(i.e. don't trigger an error as done in #first)" |
10593 | 727 |
|
728 |
^ self firstIfEmpty:nil |
|
729 |
||
730 |
"Created: / 04-06-2007 / 22:36:07 / cg" |
|
21421 | 731 |
"Modified (comment): / 13-02-2017 / 19:58:28 / cg" |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
732 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
733 |
|
4985 | 734 |
fourth |
735 |
"return the fourth element of the collection. |
|
736 |
For unordered collections, this simply returns the fourth |
|
737 |
element when enumerating them. |
|
738 |
This should be redefined in subclasses." |
|
739 |
||
740 |
^ self nth:4 |
|
741 |
||
742 |
" |
|
743 |
#(1 2 3 4) fourth |
|
744 |
" |
|
745 |
! |
|
746 |
||
19037 | 747 |
incrementAt:aKey |
748 |
"add 1 to the count stored under aKey. |
|
749 |
If not yet present, assume 0 as initial counter value." |
|
750 |
||
751 |
self incrementAt:aKey by:1 |
|
752 |
! |
|
753 |
||
754 |
incrementAt:aKey by:count |
|
755 |
"add count to the count stored under aKey. |
|
756 |
If not yet present, assume 0 as initial counter value." |
|
757 |
||
758 |
self at:aKey put:(self at:aKey ifAbsent:[0])+count. |
|
759 |
! |
|
760 |
||
21631 | 761 |
keys |
762 |
"return the keys of the collection." |
|
763 |
||
764 |
^ self subclassResponsibility |
|
765 |
||
766 |
"Created: / 09-03-2017 / 10:24:18 / cg" |
|
767 |
! |
|
768 |
||
15386 | 769 |
keysSorted |
770 |
"return the keys as a sorted sequenceable collection. |
|
771 |
Some collections (which keep their keys already sorted) may |
|
772 |
redefine this method to return the keys as they are kept internally. |
|
773 |
The fallback here sorts them into an OrderedCollection" |
|
774 |
||
775 |
^ self keys asNewOrderedCollection sort |
|
776 |
! |
|
777 |
||
15411 | 778 |
keysSorted:aBlock |
779 |
"return the keys as a sorted sequenceable collection. |
|
780 |
Some collections (which keep their keys already sorted) may |
|
781 |
redefine this method to return the keys as they are kept internally. |
|
782 |
The fallback here sorts them into an OrderedCollection" |
|
783 |
||
784 |
^ self keys asNewOrderedCollection sort:aBlock |
|
785 |
! |
|
786 |
||
68 | 787 |
last |
788 |
"return the last element of the collection. |
|
19697 | 789 |
This is a slow fallback implementation, |
790 |
and should be redefined in subclasses which can do indexed accesses." |
|
68 | 791 |
|
792 |
|theLastOne any| |
|
793 |
||
22045 | 794 |
Logger info:'slow last (uses enumeration)'. |
795 |
||
68 | 796 |
any := false. |
797 |
self do:[:e | any := true. theLastOne := e]. |
|
798 |
any ifTrue:[ |
|
6135 | 799 |
^ theLastOne |
68 | 800 |
]. |
801 |
||
802 |
"error if collection is empty" |
|
155 | 803 |
^ self emptyCollectionError |
22045 | 804 |
|
805 |
"Modified: / 17-07-2017 / 17:12:24 / cg" |
|
68 | 806 |
! |
807 |
||
5887 | 808 |
last:n |
809 |
"return the n last elements of the collection. |
|
18176 | 810 |
No longer raises an error if there are not enough elements; |
811 |
instead, returns what is there. |
|
5887 | 812 |
For unordered collections, this simply returns the last |
20211 | 813 |
n elements when enumerating them |
5887 | 814 |
(Warning: the contents of the returned collection is not deterministic in this case). |
815 |
This should be redefined in subclasses since the implementation here is VERY inefficient." |
|
816 |
||
817 |
|coll remain| |
|
818 |
||
819 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
820 |
||
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
821 |
coll := OrderedCollection new:n. |
20211 | 822 |
remain := n. |
823 |
self do:[:e | |
|
13741 | 824 |
remain > 0 ifTrue:[ |
825 |
remain := remain - 1. |
|
826 |
] ifFalse:[ |
|
827 |
coll removeFirst. |
|
828 |
]. |
|
829 |
coll add:e. |
|
830 |
]. |
|
5887 | 831 |
|
18176 | 832 |
"/ OLD: |
833 |
"/ remain ~~ 0 ifTrue:[ |
|
834 |
"/ "error if collection has not enough elements" |
|
835 |
"/ ^ self notEnoughElementsError |
|
836 |
"/ ]. |
|
5887 | 837 |
^ coll |
838 |
||
839 |
" |
|
20211 | 840 |
#(1 2 3 4 5) last:3 |
841 |
#(1 2 3 4 5 6 7 8 9 0) asSet last:3 |
|
842 |
'hello world' last:5 |
|
843 |
'hello' last:10 |
|
844 |
'hello' asSet last:10 |
|
5887 | 845 |
" |
13741 | 846 |
|
847 |
"Modified (format): / 29-09-2011 / 10:16:22 / cg" |
|
5887 | 848 |
! |
849 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
850 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
851 |
"return the last element of the collection. |
20226 | 852 |
If it is empty, return the exceptionValue. |
15756 | 853 |
(i.e. don't trigger an error as done in #last)" |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
854 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
855 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
856 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
857 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
858 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
859 |
"Created: 6.2.1996 / 15:27:49 / cg" |
4855 | 860 |
! |
861 |
||
4985 | 862 |
nth:n |
863 |
"return the nth element of the collection. |
|
864 |
For unordered collections, this simply returns the nth |
|
865 |
element when enumerating them. |
|
16022 | 866 |
This should be redefined in subclasses which can accecss fast by numeric index (aka Array-like things)." |
4985 | 867 |
|
868 |
|count| |
|
869 |
||
870 |
count := 1. |
|
871 |
self do:[:e | |
|
872 |
count == n ifTrue:[^ e]. |
|
873 |
count := count + 1 |
|
874 |
]. |
|
875 |
||
5266 | 876 |
"error if collection is smaller" |
877 |
^ self notEnoughElementsError |
|
4985 | 878 |
|
879 |
" |
|
880 |
#(1 2 3 4) nth:3 |
|
5266 | 881 |
#(1 2 3 4) nth:5 |
882 |
||
883 |
#(1 2 3 4) asSet nth:3 |
|
884 |
#(1 2 3 4) asSet nth:5 |
|
4985 | 885 |
" |
886 |
! |
|
887 |
||
11916 | 888 |
order |
889 |
"report an error that only OrderedXXX's have an order" |
|
890 |
||
891 |
self shouldNotImplement |
|
892 |
! |
|
893 |
||
4855 | 894 |
second |
895 |
"return the second element of the collection. |
|
896 |
For unordered collections, this simply returns the second |
|
897 |
element when enumerating them. |
|
7129 | 898 |
This should be redefined in subclasses." |
899 |
||
900 |
^ self nth:2 |
|
4855 | 901 |
|
4856 | 902 |
" |
903 |
#(1 2 3) second |
|
904 |
" |
|
6135 | 905 |
! |
906 |
||
19697 | 907 |
secondLast |
908 |
"return the second last element of the collection. |
|
909 |
This is a slow fallback implementation, |
|
910 |
and should be redefined in subclasses which can do indexed accesses." |
|
911 |
||
912 |
|theSecondLastOne theLastOne cnt| |
|
913 |
||
22045 | 914 |
Logger info:'slow secondLast (uses enumeration)'. |
915 |
||
19697 | 916 |
cnt := 0. |
917 |
||
918 |
self do:[:e | cnt := cnt+1. theSecondLastOne := theLastOne. theLastOne := e]. |
|
919 |
cnt > 1 ifTrue:[ |
|
920 |
^ theSecondLastOne |
|
921 |
]. |
|
922 |
||
923 |
"error if collection did not enumerate at least 2 elements" |
|
924 |
^ self notEnoughElementsError |
|
22045 | 925 |
|
926 |
"Modified: / 17-07-2017 / 17:12:28 / cg" |
|
19697 | 927 |
! |
928 |
||
6135 | 929 |
seventh |
930 |
"return the seventh element of the collection. |
|
931 |
For unordered collections, this simply returns the sixth |
|
932 |
element when enumerating them. |
|
933 |
This should be redefined in subclasses." |
|
934 |
||
935 |
^ self nth:7 |
|
936 |
||
937 |
" |
|
938 |
#(1 2 3 4 5 6 7) seventh |
|
939 |
" |
|
940 |
||
941 |
"Created: / 1.11.2001 / 16:43:29 / cg" |
|
4856 | 942 |
! |
943 |
||
4985 | 944 |
sixth |
945 |
"return the sixth element of the collection. |
|
946 |
For unordered collections, this simply returns the sixth |
|
947 |
element when enumerating them. |
|
948 |
This should be redefined in subclasses." |
|
949 |
||
950 |
^ self nth:6 |
|
951 |
||
952 |
" |
|
953 |
#(1 2 3 4 5 6 7) sixth |
|
954 |
" |
|
955 |
! |
|
956 |
||
4856 | 957 |
third |
958 |
"return the third element of the collection. |
|
959 |
For unordered collections, this simply returns the third |
|
960 |
element when enumerating them. |
|
961 |
This should be redefined in subclasses." |
|
962 |
||
4985 | 963 |
^ self nth:3 |
4856 | 964 |
|
965 |
" |
|
966 |
#(1 2 3) third |
|
967 |
" |
|
13741 | 968 |
|
969 |
"Modified (format): / 29-09-2011 / 10:17:25 / cg" |
|
12666 | 970 |
! |
971 |
||
972 |
values |
|
973 |
"return a collection containing all values of the receiver. |
|
974 |
This is to make value access to an OrderedDictionary compatible with any-Collection" |
|
975 |
||
20171 | 976 |
^ OrderedCollection new |
977 |
addAll:self; |
|
978 |
yourself. |
|
979 |
||
980 |
" |
|
981 |
#(1 2 3 4 5) values |
|
982 |
#(1 2 3 4 5) asSet values |
|
983 |
#(1 2 3 4 5) asOrderedSet values |
|
984 |
" |
|
1 | 985 |
! ! |
986 |
||
987 |
!Collection methodsFor:'adding & removing'! |
|
988 |
||
989 |
add:anObject |
|
88 | 990 |
"add the argument, anObject to the receiver. |
991 |
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
|
992 |
(i.e. don't depend on where it will be put). |
2351 | 993 |
An error is raised here - it is to be implemented by a concrete subclass." |
1 | 994 |
|
995 |
^ self subclassResponsibility |
|
2351 | 996 |
|
997 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 998 |
! |
999 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1000 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1001 |
"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
|
1002 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1003 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1004 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1005 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1006 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1007 |
"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
|
1008 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
1009 |
|
1 | 1010 |
addAll:aCollection |
1163 | 1011 |
"add all elements of the argument, aCollection to the receiver. |
7752 | 1012 |
Returns the argument, aCollection (sigh)." |
1 | 1013 |
|
1014 |
aCollection do:[:element | |
|
1163 | 1015 |
self add:element |
1 | 1016 |
]. |
1017 |
^ aCollection |
|
88 | 1018 |
|
1019 |
" |
|
22013 | 1020 |
#(1 2 3 4) copy addAll:#(5 6 7 8); yourself |
1021 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8); yourself |
|
1022 |
" |
|
1023 |
||
1024 |
"Modified: / 12-04-1996 / 13:29:20 / cg" |
|
1025 |
"Modified (comment): / 13-07-2017 / 20:45:42 / cg" |
|
1 | 1026 |
! |
1027 |
||
14401
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1028 |
addAll:aCollectionOfObjects withOccurrences:anInteger |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1029 |
"add each element from aCollection, anInteger times to the receiver. |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1030 |
Returns the argument, aCollection (sigh)." |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1031 |
|
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1032 |
aCollectionOfObjects do:[:each | |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1033 |
self add:each withOccurrences:anInteger. |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1034 |
]. |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1035 |
^ aCollectionOfObjects |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1036 |
|
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1037 |
"Created: 11.5.1996 / 12:13:48 / cg" |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1038 |
! |
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
1039 |
|
12 | 1040 |
addAllFirst:aCollection |
1041 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 1042 |
of the receiver. Returns the argument, aCollection." |
12 | 1043 |
|
1044 |
aCollection reverseDo:[:element | |
|
1163 | 1045 |
self addFirst:element |
12 | 1046 |
]. |
1047 |
^ aCollection |
|
1048 |
||
1049 |
" |
|
1050 |
|c| |
|
1051 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 1052 |
c addAllFirst:#(9 8 7 6 5). |
1053 |
c |
|
12 | 1054 |
" |
1163 | 1055 |
|
1056 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 1057 |
! |
1058 |
||
602 | 1059 |
addAllLast:aCollection |
1163 | 1060 |
"add all elements of the argument, aCollection to the receiver. |
1061 |
Returns the argument, aCollection." |
|
602 | 1062 |
|
1063 |
aCollection do:[:element | |
|
1163 | 1064 |
self addLast:element |
602 | 1065 |
]. |
1066 |
^ aCollection |
|
1067 |
||
1068 |
" |
|
1069 |
|c| |
|
1070 |
c := #(4 3 2 1) asOrderedCollection. |
|
1071 |
c addAllLast:#(9 8 7 6 5) |
|
1072 |
" |
|
1163 | 1073 |
|
1074 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 1075 |
! |
1076 |
||
14528 | 1077 |
addAllNonNilElements:aCollection |
1078 |
"add all non-nil elements of the argument, aCollection to the receiver. |
|
1079 |
Use this, when operating on a Set, that should not hold nil. |
|
1080 |
Answer the argument, aCollection." |
|
1081 |
||
1082 |
aCollection do:[:eachElement | |
|
1083 |
eachElement notNil ifTrue:[ |
|
1084 |
self add:eachElement |
|
1085 |
]. |
|
1086 |
]. |
|
1087 |
^ aCollection |
|
1088 |
||
1089 |
" |
|
1090 |
#(1 2 3 4) asSet addAllNonNilElements:#(5 nil 6 7 8); yourself |
|
1091 |
" |
|
1092 |
! |
|
1093 |
||
22013 | 1094 |
addAllReversed:aCollection |
1095 |
"add all elements of the argument, aCollection in reverse order to the receiver. |
|
1096 |
Returns the argument, aCollection (sigh)." |
|
1097 |
||
1098 |
aCollection reverseDo:[:element | |
|
1099 |
self add:element |
|
1100 |
]. |
|
1101 |
^ aCollection |
|
1102 |
||
1103 |
" |
|
1104 |
#(1 2 3 4) copy addAllReversed:#(5 6 7 8); yourself |
|
1105 |
#(1 2 3 4) asOrderedCollection addAllReversed:#(5 6 7 8); yourself |
|
1106 |
" |
|
1107 |
||
1108 |
"Created: / 13-07-2017 / 20:45:17 / cg" |
|
1109 |
! |
|
1110 |
||
602 | 1111 |
addFirst:anObject |
1112 |
"add the argument, anObject to the receiver. |
|
2351 | 1113 |
If the receiver is ordered, the new element will be added at the beginning. |
1114 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 1115 |
|
1116 |
^ self subclassResponsibility |
|
2351 | 1117 |
|
1118 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 1119 |
! |
1120 |
||
602 | 1121 |
addLast:anObject |
1122 |
"add the argument, anObject to the receiver. |
|
1163 | 1123 |
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
|
1124 |
Return the argument, anObject. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
1125 |
|
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
1126 |
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
|
1127 |
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
|
1128 |
the end, even if it is already present in the collection." |
602 | 1129 |
|
1130 |
^ self add:anObject |
|
1163 | 1131 |
|
1132 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 1133 |
! |
1134 |
||
17409 | 1135 |
clearContents |
1136 |
"remove all elements from the receiver. Returns the receiver. |
|
1137 |
Subclasses may redefine this to keep the container." |
|
1138 |
||
1139 |
self removeAll. |
|
1140 |
! |
|
1141 |
||
3124 | 1142 |
contents:aCollection |
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
1143 |
"set my contents from aCollection |
5265 | 1144 |
- this may be redefined in a concrete subclass for more performance" |
3124 | 1145 |
|
6042
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
1146 |
aCollection == self ifTrue:[ |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
1147 |
"/ self error:'should not happen'. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
1148 |
^ self |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
1149 |
]. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
1150 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
1151 |
self removeAll. |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
1152 |
aCollection notNil ifTrue:[ |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
1153 |
self addAll:aCollection |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
1154 |
] |
3124 | 1155 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
1156 |
"Modified: / 17.8.1998 / 10:18:43 / cg" |
3124 | 1157 |
! |
1158 |
||
1 | 1159 |
remove:anObject |
2351 | 1160 |
"search for the first element, which is equal to anObject; |
1161 |
if found, remove and return it. |
|
1162 |
If not found, report a 'value not found'-error. |
|
1163 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 1164 |
|
1163 | 1165 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
1166 |
||
2351 | 1167 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 1168 |
! |
1169 |
||
602 | 1170 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 1171 |
"search for the first element, which is equal to anObject; |
1172 |
if found, remove and return it. |
|
20187 | 1173 |
If not found, return the value of the exceptionBlock. |
2351 | 1174 |
Uses equality compare (=) to search for the occurrence. |
1175 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 1176 |
|
1177 |
^ self subclassResponsibility |
|
2351 | 1178 |
|
1179 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 1180 |
! |
1181 |
||
134 | 1182 |
removeAll |
1163 | 1183 |
"remove all elements from the receiver. Returns the receiver. |
134 | 1184 |
This should be reimplemented in subclasses for better |
1185 |
performance." |
|
1186 |
||
2383 | 1187 |
[self isEmpty] whileFalse:[ |
1163 | 1188 |
self removeFirst |
134 | 1189 |
]. |
1163 | 1190 |
|
2383 | 1191 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 1192 |
! |
1193 |
||
1 | 1194 |
removeAll:aCollection |
12993 | 1195 |
"remove all elements from the receiver which are equal to any in aCollection. |
1163 | 1196 |
Return the argument, aCollection. |
5267 | 1197 |
Raises an error, if some element-to-remove is not in the receiver. |
1198 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1163 | 1199 |
|
360 | 1200 |
Notice: for some collections (those not tuned for |
1163 | 1201 |
resizing themself) this may be very slow. |
20151 | 1202 |
If the number of removed elements is big compared to |
19459 | 1203 |
the receiver's size, it may be better to copy the |
1163 | 1204 |
ones which are not to be removed into a new collection." |
1 | 1205 |
|
1206 |
aCollection do:[:element | self remove:element]. |
|
1207 |
^ aCollection |
|
1163 | 1208 |
|
5268 | 1209 |
" |
1210 |
|coll| |
|
1211 |
||
1212 |
coll := #(1 2 3 4 5 6) asSet. |
|
1213 |
coll removeAll:#(4 5 6). |
|
1214 |
coll |
|
1215 |
" |
|
1216 |
||
1217 |
"raises an error: |
|
1218 |
|coll| |
|
1219 |
||
1220 |
coll := #(1 2 3 4 5 6) asSet. |
|
1221 |
coll removeAll:#(4 5 6 7 8). |
|
1222 |
coll |
|
1223 |
" |
|
1224 |
||
1225 |
"no error raised: |
|
1226 |
|coll| |
|
1227 |
||
1228 |
coll := #(1 2 3 4 5 6) asSet. |
|
1229 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1230 |
coll |
|
1231 |
" |
|
1232 |
||
12993 | 1233 |
"Modified: / 05-08-2010 / 13:50:33 / cg" |
348 | 1234 |
! |
1235 |
||
5265 | 1236 |
removeAllFoundIn:aCollection |
12993 | 1237 |
"remove all elements from the receiver which are equal to any in aCollection. |
5267 | 1238 |
No error is raised, if some element-to-remove is not in the receiver. |
1239 |
(see also: #removeAll:, which does raise an error)." |
|
4980 | 1240 |
|
1241 |
aCollection do:[:each | self remove:each ifAbsent:[]]. |
|
5265 | 1242 |
^ aCollection |
5268 | 1243 |
|
1244 |
" |
|
1245 |
|coll| |
|
1246 |
||
1247 |
coll := #(1 2 3 4 5 6) asSet. |
|
1248 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1249 |
coll |
|
1250 |
" |
|
1251 |
||
1252 |
"raises an error: |
|
1253 |
|coll| |
|
1254 |
||
1255 |
coll := #(1 2 3 4 5 6) asSet. |
|
1256 |
coll removeAll:#(4 5 6 7 8). |
|
1257 |
coll |
|
1258 |
" |
|
1259 |
||
12993 | 1260 |
"Modified: / 05-08-2010 / 13:51:05 / cg" |
1261 |
! |
|
1262 |
||
1263 |
removeAllIdentical:aCollection |
|
1264 |
"remove all elements from the receiver which are in aCollection. |
|
1265 |
Return the argument, aCollection. |
|
1266 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1267 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1268 |
||
1269 |
Notice: for some collections (those not tuned for |
|
1270 |
resizing themself) this may be very slow. |
|
20151 | 1271 |
If the number of removed elements is big compared to |
19459 | 1272 |
the receiver's size, it may be better to copy the |
12993 | 1273 |
ones which are not to be removed into a new collection." |
1274 |
||
1275 |
aCollection do:[:element | self removeIdentical:element]. |
|
1276 |
^ aCollection |
|
1277 |
||
1278 |
" |
|
1279 |
|coll| |
|
1280 |
||
1281 |
coll := #(1 2 3 4 5 6) asSet. |
|
1282 |
coll removeAll:#(4 5 6). |
|
1283 |
coll |
|
1284 |
" |
|
1285 |
||
1286 |
"raises an error: |
|
1287 |
|coll| |
|
1288 |
||
1289 |
coll := #(1 2 3 4 5 6) asSet. |
|
1290 |
coll removeAll:#(4 5 6 7 8). |
|
1291 |
coll |
|
1292 |
" |
|
1293 |
||
1294 |
"no error raised: |
|
1295 |
|coll| |
|
1296 |
||
1297 |
coll := #(1 2 3 4 5 6) asSet. |
|
1298 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1299 |
coll |
|
1300 |
" |
|
1301 |
||
1302 |
"Created: / 05-08-2010 / 13:51:51 / cg" |
|
1303 |
! |
|
1304 |
||
1305 |
removeAllIdenticalFoundIn:aCollection |
|
1306 |
"remove all elements from the receiver which are in aCollection. |
|
1307 |
No error is raised, if some element-to-remove is not in the receiver. |
|
1308 |
(see also: #removeAll:, which does raise an error)." |
|
1309 |
||
1310 |
aCollection do:[:each | self removeIdentical:each ifAbsent:[]]. |
|
1311 |
^ aCollection |
|
1312 |
||
1313 |
" |
|
1314 |
|coll| |
|
1315 |
||
1316 |
coll := #(1 2 3 4 5 6) asSet. |
|
1317 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1318 |
coll |
|
1319 |
" |
|
1320 |
||
1321 |
"raises an error: |
|
1322 |
|coll| |
|
1323 |
||
1324 |
coll := #(1 2 3 4 5 6) asSet. |
|
1325 |
coll removeAll:#(4 5 6 7 8). |
|
1326 |
coll |
|
1327 |
" |
|
1328 |
||
1329 |
"Created: / 05-08-2010 / 13:52:21 / cg" |
|
4980 | 1330 |
! |
1331 |
||
5265 | 1332 |
removeAllSuchThat:aBlock |
4980 | 1333 |
"Apply the condition to each element and remove it if the condition is true. |
5265 | 1334 |
Return a collection of removed elements. |
5267 | 1335 |
First elements-to-remove are collected, then removed in one operation." |
4980 | 1336 |
|
5267 | 1337 |
|removedElements| |
4980 | 1338 |
|
15807 | 1339 |
self do:[:eachElement | |
21624 | 1340 |
(aBlock value:eachElement) ifTrue:[ |
1341 |
removedElements isNil ifTrue:[ |
|
1342 |
removedElements := OrderedCollection new. |
|
1343 |
]. |
|
15807 | 1344 |
removedElements add:eachElement |
4980 | 1345 |
] |
1346 |
]. |
|
21624 | 1347 |
removedElements notNil ifTrue:[ |
1348 |
self removeAllIdenticalFoundIn:removedElements. |
|
1349 |
^ removedElements. |
|
1350 |
]. |
|
1351 |
^ #(). |
|
5267 | 1352 |
|
1353 |
" |
|
1354 |
|coll| |
|
1355 |
||
15807 | 1356 |
coll := #(1 2 2 3 4 5 6 7 8 9 10) asOrderedCollection. |
5267 | 1357 |
coll removeAllSuchThat:[:el | el even]. |
1358 |
coll |
|
1359 |
" |
|
15807 | 1360 |
|
1361 |
" |
|
1362 |
|coll bla| |
|
1363 |
||
1364 |
bla := 'bla' copy. |
|
1365 |
coll := #(1 'bla' 3 4 5 6 7 8 9 10) asOrderedCollection. |
|
1366 |
coll add:bla. |
|
1367 |
coll removeAllSuchThat:[:el | el == bla]. |
|
1368 |
coll |
|
1369 |
" |
|
1370 |
||
5267 | 1371 |
" |
1372 |
|coll| |
|
1373 |
||
1374 |
coll := #(1 2 3 4 5 6 7 8 9 10) asSet. |
|
1375 |
coll removeAllSuchThat:[:el | el even]. |
|
1376 |
coll |
|
1377 |
" |
|
21624 | 1378 |
|
1379 |
"Modified (format): / 03-03-2017 / 18:59:57 / stefan" |
|
4980 | 1380 |
! |
1381 |
||
348 | 1382 |
removeFirst |
1383 |
"remove the first element from the receiver. |
|
1384 |
Return the removed element." |
|
1385 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1386 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1387 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1388 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1389 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1390 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1391 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1392 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1393 |
(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
|
1394 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1395 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1396 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 1397 |
! |
1398 |
||
1399 |
removeFirst:n |
|
1400 |
"remove the first n elements from the receiver. |
|
5265 | 1401 |
Return a collection of removed elements. |
360 | 1402 |
Notice: for some collections (those not tuned for |
5265 | 1403 |
resizing themself) this may be very slow." |
348 | 1404 |
|
1405 |
|ret| |
|
1406 |
||
1407 |
self size < n ifTrue:[ |
|
5265 | 1408 |
^ self notEnoughElementsError |
348 | 1409 |
]. |
1410 |
ret := Array new:n. |
|
1411 |
1 to:n do:[:i | |
|
5265 | 1412 |
ret at:i put:(self removeFirst). |
348 | 1413 |
]. |
1414 |
^ ret |
|
1415 |
! |
|
1416 |
||
2351 | 1417 |
removeIdentical:anObject |
1418 |
"search for the first element, which is identical to anObject; |
|
1419 |
if found, remove and return it. |
|
1420 |
If not found, report a 'value not found'-error. |
|
1421 |
Uses identity compare (==) to search for the occurrence." |
|
1422 |
||
1423 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
1424 |
||
1425 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
1426 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
1427 |
! |
|
1428 |
||
1429 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
1430 |
"search for the first element, which is identical to anObject; |
|
1431 |
if found, remove and return it. |
|
20187 | 1432 |
If not found, return the value of the exceptionBlock. |
2351 | 1433 |
Uses identity compare (==) to search for the occurrence. |
1434 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1435 |
||
1436 |
^ self subclassResponsibility |
|
1437 |
||
1438 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
1439 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
1440 |
! |
|
1441 |
||
602 | 1442 |
removeLast |
1443 |
"remove the last element from the receiver. |
|
2351 | 1444 |
Return the removed element. |
1445 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 1446 |
|
1447 |
^ self subclassResponsibility |
|
2351 | 1448 |
|
1449 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 1450 |
! |
1451 |
||
348 | 1452 |
removeLast:n |
11331 | 1453 |
"remove the last n elements from the receiver collection. |
5265 | 1454 |
Return a collection of removed elements. |
11331 | 1455 |
Notice: for some collections this may be very slow |
1456 |
(those not tuned for resizing themself)." |
|
348 | 1457 |
|
1458 |
|ret| |
|
1459 |
||
1460 |
self size < n ifTrue:[ |
|
5265 | 1461 |
^ self notEnoughElementsError |
348 | 1462 |
]. |
1463 |
ret := Array new:n. |
|
1464 |
n to:1 by:-1 do:[:i | |
|
5265 | 1465 |
ret at:i put:(self removeLast). |
348 | 1466 |
]. |
1467 |
^ ret |
|
21456 | 1468 |
! |
1469 |
||
1470 |
testAndAdd:anElement |
|
21650 | 1471 |
"Test, if the element is present in the receiver. |
21456 | 1472 |
Answer true, if the element did already exist in the collection, |
1473 |
false otherwise. |
|
21650 | 1474 |
If the element does not exist, add it to the collection. |
21456 | 1475 |
|
1476 |
WARNING: do not add elements while iterating over the receiver. |
|
1477 |
Iterate over a copy to do this." |
|
1478 |
||
1479 |
(self includes:anElement) ifTrue:[ |
|
1480 |
^ true. |
|
1481 |
]. |
|
1482 |
self add:anElement. |
|
1483 |
^ false. |
|
1484 |
||
1485 |
"Created: / 16-02-2017 / 13:41:58 / stefan" |
|
21650 | 1486 |
"Modified (comment): / 16-03-2017 / 16:48:06 / stefan" |
22059 | 1487 |
! |
1488 |
||
1489 |
unless:aCheckBlock add:anObject |
|
1490 |
"if aCheckBlock evaluates to false, |
|
1491 |
add the argument, anObject to the receiver. |
|
1492 |
Otherwise do nothing." |
|
1493 |
||
1494 |
aCheckBlock value ifFalse:[self add:anObject] |
|
1495 |
||
1496 |
"Created: / 18-07-2017 / 15:15:25 / cg" |
|
1497 |
! |
|
1498 |
||
1499 |
when:aCheckBlock add:anObject |
|
1500 |
"if aCheckBlock evaluates to true, |
|
1501 |
add the argument, anObject to the receiver. |
|
1502 |
Otherwise do nothing." |
|
1503 |
||
1504 |
aCheckBlock value ifTrue:[self add:anObject] |
|
1505 |
||
1506 |
"Created: / 18-07-2017 / 15:15:12 / cg" |
|
1 | 1507 |
! ! |
1508 |
||
8858 | 1509 |
!Collection methodsFor:'bulk operations'! |
1510 |
||
12116 | 1511 |
abs |
15743 | 1512 |
"absolute value of all elements in the collection. |
1513 |
Elements are supposed to be numeric" |
|
12116 | 1514 |
|
14312 | 1515 |
^ self collect:[:a | a abs] |
1516 |
||
1517 |
" |
|
1518 |
TestCase assert:( #(1 -2 -3 4) abs = #(1 2 3 4)). |
|
12116 | 1519 |
" |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1520 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1521 |
"Modified: / 18-03-2011 / 10:33:50 / cg" |
13316 | 1522 |
! |
1523 |
||
12115 | 1524 |
negated |
15743 | 1525 |
"negated value of all elements in the collection. |
1526 |
Elements are supposed to be numeric" |
|
12115 | 1527 |
|
1528 |
^ self collect:[:a | a negated] |
|
1529 |
||
1530 |
" |
|
14312 | 1531 |
TestCase assert:( #(1 -2 -3 4) negated = #(-1 2 3 -4)). |
12115 | 1532 |
" |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1533 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1534 |
"Modified: / 18-03-2011 / 10:33:53 / cg" |
12115 | 1535 |
! |
1536 |
||
11468 | 1537 |
product |
15743 | 1538 |
"return the product of all elements which are supposed to be numeric. |
22321 | 1539 |
Returns 1 for an empty receiver." |
1540 |
||
1541 |
self isEmpty ifTrue:[^ 1]. |
|
1542 |
^ self fold:[:accum :each | accum * each]. |
|
14312 | 1543 |
|
1544 |
" |
|
1545 |
TestCase should:[ Array new product ] raise:Error. |
|
1546 |
||
1547 |
TestCase assert:( #(1) product == 1). |
|
1548 |
TestCase assert:( #(6) product == 6). |
|
1549 |
TestCase assert:( #(1 2 3 4 5) product = 5 factorial ) |
|
11468 | 1550 |
" |
22321 | 1551 |
|
1552 |
"Modified: / 23-10-2017 / 00:07:28 / cg" |
|
11468 | 1553 |
! |
1554 |
||
1555 |
sum |
|
15743 | 1556 |
"return the sum of all elements which are supposed to be numeric. |
1557 |
Returns 0 for an empty receiver." |
|
11468 | 1558 |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1559 |
self isEmpty ifTrue:[^ 0]. |
22321 | 1560 |
^ self fold:[:accum :each | accum + each]. |
14312 | 1561 |
|
1562 |
" |
|
1563 |
TestCase assert: ( #() sum = 0 ). |
|
1564 |
TestCase assert: ( #(1) sum = 1 ). |
|
1565 |
TestCase assert: ( #(1 2 3 4) sum = 10 ). |
|
11468 | 1566 |
TestCase assert: ( (1 to:10) sum = 55 ). |
1567 |
TestCase assert: ( 'abc' asByteArray sum = 294 ). |
|
1568 |
TestCase assert: ( { 10 +/- 2. |
|
1569 |
20 +/- 4. |
|
1570 |
100 +/- 10 } sum = (130 +/- 16) ). |
|
1571 |
||
1572 |
TestCase assert: ( { (1 / 9). |
|
1573 |
(1 / 7). |
|
1574 |
} sum = (16 / 63) ). |
|
8858 | 1575 |
" |
22321 | 1576 |
|
1577 |
"Modified (format): / 23-10-2017 / 00:06:45 / cg" |
|
11514 | 1578 |
! |
1579 |
||
1580 |
sum:aBlock |
|
13041 | 1581 |
"for each element in the receiver, evaluate the argument, aBlock and sum up the results. |
1582 |
Return the total sum or 0 for an empty collection. |
|
11514 | 1583 |
Similar to (self collect...) sum, but avoids creation of an intermediate collection." |
1584 |
||
1585 |
|sum| |
|
1586 |
||
1587 |
self do:[:element | |
|
1588 |
|thisValue| |
|
1589 |
||
1590 |
thisValue := aBlock value:element. |
|
1591 |
sum isNil ifTrue:[ |
|
1592 |
sum := thisValue |
|
1593 |
] ifFalse:[ |
|
1594 |
sum := sum + thisValue |
|
1595 |
]. |
|
1596 |
]. |
|
1597 |
^ sum ? 0 |
|
1598 |
||
1599 |
" |
|
14312 | 1600 |
TestCase assert:( |
1601 |
((1 to:10) collect:[:n | n squared]) sum = ((1 to:10) sum:[:n | n squared]) |
|
1602 |
) |
|
11514 | 1603 |
" |
13041 | 1604 |
|
1605 |
"Modified: / 23-08-2010 / 18:19:42 / cg" |
|
8858 | 1606 |
! ! |
1607 |
||
15812 | 1608 |
!Collection methodsFor:'comparing'! |
1609 |
||
1610 |
identicalContentsAs:aCollection |
|
1611 |
"return true if the receiver and aCollection represent collections |
|
1612 |
with identical contents. This is much like #sameContentsAs:, but compares |
|
1613 |
elements using #== instead of #=." |
|
1614 |
||
1615 |
aCollection size ~~ self size ifTrue:[ |
|
1616 |
^ false |
|
1617 |
]. |
|
1618 |
||
1619 |
^ aCollection conform:[:e | (self includesIdentical:e)] |
|
1620 |
||
1621 |
" |
|
1622 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
|
1623 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1624 |
#($1 $2 $3 $4 $5) = '12345' |
|
1625 |
||
1626 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1627 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1628 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
1629 |
" |
|
1630 |
||
1631 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
|
1632 |
! |
|
1633 |
||
1634 |
sameContentsAs:aCollection |
|
1635 |
"answer true, if all the elements in self and aCollection |
|
1636 |
are common. This is not defined as #=, since we cannot redefine #hash |
|
1637 |
for aCollection." |
|
1638 |
||
1639 |
aCollection size ~~ self size ifTrue:[ |
|
1640 |
^ false |
|
1641 |
]. |
|
1642 |
||
1643 |
^ aCollection conform:[:e | (self includes:e)] |
|
1644 |
||
1645 |
" |
|
1646 |
#(1 2 3) asSet sameContentsAs: #(1 2 3) |
|
1647 |
#(1 2 3 4) asSet sameContentsAs: #(1 2 3) |
|
1648 |
#(1 2 3) asSet sameContentsAs: #(1 2 3 3) |
|
1649 |
#(1 2 3 'aa') asSet sameContentsAs: #(1 2 3 'aa') |
|
1650 |
#(1 2 3 'aa') asIdentitySet sameContentsAs: #(1 2 3 'aa') |
|
1651 |
#(1 2 3 #aa) asIdentitySet sameContentsAs: #(1 2 3 #aa) |
|
1652 |
" |
|
1653 |
! |
|
1654 |
||
1655 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
|
1656 |
"answer true, if all the elements in self and aCollection |
|
1657 |
are common. This is not defined as #=, since we cannot redefine #hash |
|
1658 |
for aCollection." |
|
1659 |
||
1660 |
aCollection size ~~ self size ifTrue:[ |
|
1661 |
^ false |
|
1662 |
]. |
|
1663 |
||
1664 |
^ aCollection conform:[:otherElement | |
|
1665 |
self contains:[:myElement | |
|
1666 |
compareBlock value:myElement value:otherElement |
|
1667 |
]. |
|
1668 |
]. |
|
1669 |
||
1670 |
" |
|
1671 |
#(1 2 3 4 5) asSet sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
|
1672 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) asSet whenComparedWith:[:a :b | a = b] |
|
1673 |
#(1 2 3 4 5) asSet sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1674 |
#(1 2 3 4 5) asSet sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1675 |
#(1 2 3 4 5) asSet sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a == b] |
|
1676 |
||
1677 |
#('Hello' 'ABC' 'worlD') asSet sameContentsAs: #('Hello' 'ABC' 'worlD') whenComparedWith:[:a :b | a sameAs:b] |
|
1678 |
" |
|
1679 |
! ! |
|
1680 |
||
602 | 1681 |
!Collection methodsFor:'converting'! |
1682 |
||
1683 |
asArray |
|
15060 | 1684 |
"return an Array with the collection's elements. |
1685 |
Notice: this is redefined in Array, where it returns the receiver. |
|
1686 |
Use asNewArray, if you intent to modify the returned collection." |
|
602 | 1687 |
|
1688 |
|anArray |
|
1689 |
index "{ Class: SmallInteger }" | |
|
1 | 1690 |
|
602 | 1691 |
anArray := Array new:(self size). |
1692 |
index := 1. |
|
1693 |
self do:[:each | |
|
6135 | 1694 |
anArray at:index put:each. |
1695 |
index := index + 1 |
|
602 | 1696 |
]. |
1697 |
^ anArray |
|
1 | 1698 |
! |
1699 |
||
15939 | 1700 |
asArrayOfType:arrayClass |
1701 |
"return a new instance of arrayClass with the collection's elements" |
|
1702 |
||
21082 | 1703 |
|anArrayInstance |
15939 | 1704 |
index "{ Class: SmallInteger }" | |
1705 |
||
21082 | 1706 |
anArrayInstance := arrayClass new:(self size). |
15939 | 1707 |
index := 1. |
1708 |
self do:[:each | |
|
21082 | 1709 |
anArrayInstance at:index put:each. |
15939 | 1710 |
index := index + 1 |
1711 |
]. |
|
21082 | 1712 |
^ anArrayInstance |
15939 | 1713 |
! |
1714 |
||
602 | 1715 |
asBag |
15939 | 1716 |
"return a new Bag with the receiver collection's elements" |
1 | 1717 |
|
602 | 1718 |
^ self addAllTo:(Bag new) |
1 | 1719 |
! |
1720 |
||
602 | 1721 |
asByteArray |
15939 | 1722 |
"return a new ByteArray with the collection's elements |
12119 | 1723 |
(which must convert to 8bit integers in the range 0..255)." |
1724 |
||
1725 |
^ self asIntegerArray:ByteArray |
|
1 | 1726 |
! |
1727 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1728 |
asCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1729 |
"return myself as a Collection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1730 |
I am already a Collection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1731 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1732 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1733 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1734 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1735 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1736 |
|
21215 | 1737 |
asCollectionDo:aBlock |
1738 |
"enumerate myself" |
|
1739 |
||
1740 |
self do:aBlock |
|
1741 |
! |
|
1742 |
||
7906 | 1743 |
asDictionary |
15060 | 1744 |
"return a Dictionary with the receiver collection's elements, |
1745 |
using the original keys of the receiver as dictionary key. |
|
1746 |
Notice: this is redefined in Dictionary, where it returns the receiver. |
|
19222 | 1747 |
Use asNewDictionary, if you intend to modify the returned collection. |
16789 | 1748 |
See associationsAsDictionary if you already have a collection of associations" |
7906 | 1749 |
|
1750 |
|d| |
|
1751 |
||
19222 | 1752 |
d := Dictionary new:self size. |
7906 | 1753 |
self keysAndValuesDo:[:k :v | d at:k put:v]. |
1754 |
^ d |
|
1755 |
||
1756 |
" |
|
14928 | 1757 |
#(10 20 30 40 50 60 70 80 90) asDictionary |
7906 | 1758 |
" |
1759 |
! |
|
1760 |
||
602 | 1761 |
asDoubleArray |
15939 | 1762 |
"return a new DoubleArray with the collection's elements |
12119 | 1763 |
(which must convert to 64bit floats)." |
1764 |
||
15939 | 1765 |
^ self asArrayOfType:DoubleArray |
602 | 1766 |
! |
68 | 1767 |
|
11412 | 1768 |
asFlatOrderedCollection |
13319
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1769 |
"return a new ordered collection containing all elements of the receiver |
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1770 |
and recursively of all collectionelements from the receiver" |
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1771 |
|
11412 | 1772 |
|coll| |
1773 |
||
1774 |
coll := OrderedCollection new. |
|
1775 |
self flatDo:[:el | coll add:el]. |
|
1776 |
^ coll. |
|
1777 |
||
1778 |
" |
|
1779 |
#( |
|
1780 |
(1 2 3) |
|
1781 |
4 5 |
|
1782 |
(6) |
|
1783 |
7 |
|
13319
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1784 |
(8 (9 10) 11 12 (13 (14 (15) 16))) |
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1785 |
) asFlatOrderedCollection |
11412 | 1786 |
" |
13319
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1787 |
|
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1788 |
"Modified: / 20-03-2011 / 22:02:36 / cg" |
11412 | 1789 |
! |
1790 |
||
602 | 1791 |
asFloatArray |
15939 | 1792 |
"return a new FloatArray with the collection's elements |
12119 | 1793 |
(which must convert to 32bit floats)." |
1794 |
||
15939 | 1795 |
^ self asArrayOfType:FloatArray |
1796 |
! |
|
1797 |
||
602 | 1798 |
asIdentitySet |
15939 | 1799 |
"return a new IdentitySet with the receiver collection's elements" |
602 | 1800 |
|
14991 | 1801 |
^ self addAllTo:(IdentitySet new:self size) |
68 | 1802 |
! |
1803 |
||
12119 | 1804 |
asIntegerArray |
1805 |
"return a new IntegerArray with the collection's elements |
|
1806 |
(which must convert to 32bit integers in the range 0..16rFFFFFFFF)." |
|
1807 |
||
1808 |
^ self asIntegerArray:IntegerArray |
|
1809 |
! |
|
1810 |
||
1811 |
asIntegerArray:arrayClass |
|
1812 |
"return a new Array with the collection's elements" |
|
1813 |
||
1814 |
|anIntegerArray |
|
1815 |
index "{ Class: SmallInteger }" | |
|
1816 |
||
1817 |
anIntegerArray := arrayClass new:(self size). |
|
1818 |
index := 1. |
|
1819 |
self do:[:each | |
|
1820 |
anIntegerArray at:index put:each asInteger. |
|
1821 |
index := index + 1 |
|
1822 |
]. |
|
1823 |
^ anIntegerArray |
|
1824 |
! |
|
1825 |
||
16068 | 1826 |
asKeysAndValues |
1827 |
"return a Dictionary with the receiver's associations as key->value pairs |
|
1828 |
using each element's key as dictionary key and value as dictionary value." |
|
1829 |
||
1830 |
^ Dictionary withAssociations:self |
|
1831 |
||
1832 |
" |
|
1833 |
{ 10->'ten' . 20->'twenty' . 30->'thirty'} asKeysAndValues |
|
1834 |
" |
|
1835 |
! |
|
1836 |
||
8050 | 1837 |
asLongIntegerArray |
15939 | 1838 |
"return a new LongIntegerArray with the collection's elements |
12119 | 1839 |
(which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)." |
1840 |
||
1841 |
^ self asIntegerArray:LongIntegerArray |
|
8050 | 1842 |
! |
1843 |
||
16825 | 1844 |
asMutableCollection |
1845 |
"return myself" |
|
1846 |
||
1847 |
^ self |
|
1848 |
! |
|
1849 |
||
14996 | 1850 |
asNewArray |
15386 | 1851 |
"return a new Array with the receiver collection's elements. |
1852 |
This method ensures that the returned collection is a new one, not |
|
1853 |
the same or shared with the original receiver" |
|
14996 | 1854 |
|
1855 |
^ self asArray |
|
1856 |
! |
|
1857 |
||
1858 |
asNewDictionary |
|
15386 | 1859 |
"return a new Dictionary with the receiver collection's elements. |
1860 |
This method ensures that the returned collection is a new one, not |
|
1861 |
the same or shared with the original receiver" |
|
14996 | 1862 |
|
1863 |
^ self asDictionary |
|
1864 |
! |
|
1865 |
||
1866 |
asNewIdentitySet |
|
15386 | 1867 |
"return a new IdentitySet with the receiver collection's elements. |
1868 |
This method ensures that the returned collection is a new one, not |
|
1869 |
the same or shared with the original receiver" |
|
14996 | 1870 |
|
1871 |
^ self asIdentitySet |
|
1872 |
! |
|
1873 |
||
1874 |
asNewOrderedCollection |
|
15386 | 1875 |
"return a new OrderedCollection with the receiver collection's elements. |
1876 |
This method ensures that the returned collection is a new one, not |
|
1877 |
the same or shared with the original receiver" |
|
14996 | 1878 |
|
1879 |
^ self asOrderedCollection |
|
1880 |
! |
|
1881 |
||
15030 | 1882 |
asNewOrderedSet |
15386 | 1883 |
"return a new OrderedSet with the receiver collection's elements. |
1884 |
This method ensures that the returned collection is a new one, not |
|
1885 |
the same or shared with the original receiver" |
|
15030 | 1886 |
|
1887 |
^ self asOrderedSet |
|
1888 |
! |
|
1889 |
||
14996 | 1890 |
asNewSet |
15386 | 1891 |
"return a new Set with the receiver collection's elements. |
1892 |
This method ensures that the returned collection is a new one, not |
|
1893 |
the same or shared with the original receiver" |
|
14996 | 1894 |
|
1895 |
^ self asSet |
|
1896 |
! |
|
1897 |
||
15080 | 1898 |
asNilIfEmpty |
1899 |
"return mySelf, or nil if I am empty" |
|
1900 |
||
1901 |
self isEmpty ifTrue:[^ nil]. |
|
1902 |
^ self. |
|
1903 |
! |
|
1904 |
||
602 | 1905 |
asOrderedCollection |
15060 | 1906 |
"return an OrderedCollection with the receiver collection's elements. |
1907 |
Notice: this is redefined in OrderedCollection, where it returns the receiver. |
|
1908 |
Use asNewOrderedCollection, if you intent to modify the returned collection." |
|
602 | 1909 |
|
1910 |
^ self addAllTo:(OrderedCollection new:self size) |
|
1911 |
! |
|
1912 |
||
14696 | 1913 |
asOrderedSet |
15386 | 1914 |
"return a new OrderedSet with the receiver collection's elements. |
1915 |
Notice: this is redefined in OrderedSet, where it returns the receiver. |
|
1916 |
Use asNewOrderedSet, if you intent to modify the returned collection." |
|
14696 | 1917 |
|
1918 |
^ self addAllTo:(OrderedSet new:self size) |
|
1919 |
! |
|
1920 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1921 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1922 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1923 |
I am already a Collection, but not sequenceable." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1924 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1925 |
^ self asArray |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1926 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1927 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1928 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1929 |
|
602 | 1930 |
asSet |
15060 | 1931 |
"return a Set with the receiver collection's elements. |
1932 |
Notice: this is redefined in Set, where it returns the receiver. |
|
1933 |
Use asNewSet, if you intent to modify the returned collection." |
|
602 | 1934 |
|
14317
cdf4c073a6bc
changed: #asSet - nil is supported now in set
Stefan Vogel <sv@exept.de>
parents:
14313
diff
changeset
|
1935 |
^ self addAllTo:(Set new:self size) |
602 | 1936 |
! |
1937 |
||
19177 | 1938 |
asSignedByteArray |
1939 |
"return a new ByteArray with the collection's elements |
|
1940 |
(which must convert to 8bit integers in the range -128..127)." |
|
1941 |
||
1942 |
^ self asIntegerArray:SignedByteArray |
|
1943 |
||
1944 |
" |
|
1945 |
#( 1 2 3 4 -128 -5 -6) asSignedByteArray |
|
1946 |
" |
|
1947 |
! |
|
1948 |
||
13773
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1949 |
asSignedIntegerArray |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1950 |
"return a new SignedIntegerArray with the collection's elements |
22265 | 1951 |
(which must convert to 32bit signed integers in the range 16r-80000000..16r7FFFFFFF)." |
13773
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1952 |
|
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1953 |
^ self asIntegerArray:SignedIntegerArray |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1954 |
|
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1955 |
"Created: / 07-10-2011 / 13:14:01 / cg" |
22265 | 1956 |
"Modified (comment): / 19-09-2017 / 16:32:11 / stefan" |
13773
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1957 |
! |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1958 |
|
15941 | 1959 |
asSignedLongIntegerArray |
1960 |
"return a new LongIntegerArray with the collection's elements |
|
22265 | 1961 |
(which must convert to 64bit integers in the range 16r-8000000000000000..16r7FFFFFFFFFFFFFFF)." |
15941 | 1962 |
|
1963 |
^ self asIntegerArray:SignedLongIntegerArray |
|
22265 | 1964 |
|
1965 |
"Modified (comment): / 19-09-2017 / 16:32:17 / stefan" |
|
15941 | 1966 |
! |
1967 |
||
1968 |
asSignedWordArray |
|
1969 |
"return a new WordArray with the collection's elements |
|
1970 |
(which must convert to 16bit integers in the range -0x8000..16r7FFF)." |
|
1971 |
||
1972 |
^ self asIntegerArray:SignedWordArray |
|
1973 |
! |
|
1974 |
||
602 | 1975 |
asSortedCollection |
15939 | 1976 |
"return a new SortedCollection with the receiver collection's elements" |
1 | 1977 |
|
602 | 1978 |
|aSortedCollection| |
1979 |
||
1980 |
aSortedCollection := SortedCollection new:self size. |
|
1981 |
aSortedCollection addAll:self. |
|
1982 |
^ aSortedCollection |
|
1983 |
! |
|
1984 |
||
1985 |
asSortedCollection:sortBlock |
|
15939 | 1986 |
"return a new SortedCollection with the receiver collection's elements, |
602 | 1987 |
using sortBlock for comparing" |
1988 |
||
1989 |
|aSortedCollection| |
|
1990 |
||
1991 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
1992 |
aSortedCollection addAll:self. |
|
1993 |
^ aSortedCollection |
|
1 | 1994 |
! |
1995 |
||
2919 | 1996 |
asSortedStrings |
14614 | 1997 |
"Create & return a SortedCollection that sorts the receiver's |
2919 | 1998 |
elements according to the locales collating policy. |
15030 | 1999 |
This is currently not really supported - strings are sorted |
2919 | 2000 |
without caring for the locale." |
2001 |
||
2002 |
|aSortedCollection| |
|
2003 |
||
2004 |
aSortedCollection := SortedCollection forStrings:self size. |
|
2005 |
aSortedCollection addAll:self. |
|
2006 |
^ aSortedCollection |
|
2007 |
||
2008 |
"Created: 13.9.1997 / 09:36:22 / cg" |
|
2009 |
"Modified: 13.9.1997 / 09:43:00 / cg" |
|
2010 |
! |
|
2011 |
||
2012 |
asSortedStrings:sortBlock |
|
14614 | 2013 |
"Create & return a SortedCollection that sorts the receiver's |
3061 | 2014 |
elements using sortBlock and according to the locales collating policy, |
2015 |
which is passed as first arg to sortBlock. |
|
15030 | 2016 |
This is currently not really supported - strings are sorted |
2919 | 2017 |
without caring for the locale." |
2018 |
||
2019 |
|aSortedCollection| |
|
2020 |
||
2021 |
aSortedCollection := SortedCollection forStrings:self size. |
|
2022 |
aSortedCollection sortBlock:sortBlock. |
|
2023 |
aSortedCollection addAll:self. |
|
2024 |
^ aSortedCollection |
|
2025 |
||
3061 | 2026 |
"Created: / 13.9.1997 / 09:36:45 / cg" |
2027 |
"Modified: / 27.10.1997 / 16:39:48 / cg" |
|
2919 | 2028 |
! |
2029 |
||
2030 |
asSortedStrings:sortBlock with:aCollationPolicy |
|
14614 | 2031 |
"Create & return a SortedCollection that sorts the receiver's |
2919 | 2032 |
elements using sortBlock and according to the specified locales collating policy. |
15030 | 2033 |
This is currently not really supported - strings are sorted |
2919 | 2034 |
without caring for the locale." |
2035 |
||
2036 |
|aSortedCollection| |
|
2037 |
||
2038 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
2039 |
aSortedCollection sortBlock:sortBlock. |
|
2040 |
aSortedCollection addAll:self. |
|
2041 |
^ aSortedCollection |
|
2042 |
||
2043 |
"Created: 13.9.1997 / 09:37:21 / cg" |
|
2044 |
"Modified: 13.9.1997 / 09:45:27 / cg" |
|
2045 |
! |
|
2046 |
||
2047 |
asSortedStringsWith: aCollationPolicy |
|
14614 | 2048 |
"Create & return a SortedCollection that sorts the receiver's |
2919 | 2049 |
elements according to the specified locales collating policy. |
15030 | 2050 |
This is currently not really supported - strings are sorted |
2919 | 2051 |
without caring for the locale." |
2052 |
||
2053 |
|aSortedCollection| |
|
2054 |
||
2055 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
2056 |
aSortedCollection addAll:self. |
|
2057 |
^ aSortedCollection |
|
2058 |
||
2059 |
"Created: 13.9.1997 / 09:37:50 / cg" |
|
2060 |
"Modified: 13.9.1997 / 09:44:08 / cg" |
|
2061 |
! |
|
2062 |
||
602 | 2063 |
asString |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2064 |
"return a String with the collection's elements |
602 | 2065 |
(which must convert to characters)" |
2066 |
||
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2067 |
|string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2068 |
index "{ Class: SmallInteger }" |
18663 | 2069 |
char |
2070 |
stringBitsPerCharacter "{ Class: SmallInteger }" |
|
2071 |
charBitsPerCharacter "{ Class: SmallInteger }" |
|
2072 |
| |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2073 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2074 |
string := String new:(self size). |
18663 | 2075 |
stringBitsPerCharacter := string bitsPerCharacter. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2076 |
|
602 | 2077 |
index := 1. |
2078 |
self do:[:each | |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2079 |
char := each asCharacter. |
18663 | 2080 |
charBitsPerCharacter := char bitsPerCharacter. |
2081 |
charBitsPerCharacter > stringBitsPerCharacter ifTrue:[ |
|
2082 |
charBitsPerCharacter == 16 ifTrue:[ |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2083 |
string := Unicode16String fromString:string. |
18663 | 2084 |
stringBitsPerCharacter := 16. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2085 |
] ifFalse:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2086 |
string := Unicode32String fromString:string. |
18663 | 2087 |
stringBitsPerCharacter := 32. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2088 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2089 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2090 |
string at:index put:char. |
6135 | 2091 |
index := index + 1 |
360 | 2092 |
]. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2093 |
^ string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2094 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2095 |
" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2096 |
#(80 81 82) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2097 |
#(16r8000 16r8001 16r8002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2098 |
#(16r800000 16r800001 16r800002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
2099 |
" |
602 | 2100 |
! |
2101 |
||
2102 |
asStringCollection |
|
1413 | 2103 |
"return a new string collection containing the elements; |
2104 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 2105 |
|
2106 |
^ self addAllTo:(StringCollection new) |
|
1413 | 2107 |
|
2108 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 2109 |
! |
2110 |
||
8160 | 2111 |
asUnicodeString |
15939 | 2112 |
"return a String with the collection's elements |
8160 | 2113 |
(which must convert to characters)" |
2114 |
||
2115 |
|aString |
|
2116 |
index "{ Class: SmallInteger }" | |
|
2117 |
||
2118 |
aString := UnicodeString uninitializedNew:self size. |
|
2119 |
index := 1. |
|
2120 |
self do:[:each | |
|
2121 |
aString at:index put:each asCharacter. |
|
2122 |
index := index + 1 |
|
2123 |
]. |
|
2124 |
^ aString |
|
2125 |
||
2126 |
" |
|
2127 |
#(16r440 16r443 16r441 16r441 16r43A 16r438 16r439 16r20 16r44F 16r437 16r44B 16r43A) asUnicodeString |
|
2128 |
" |
|
2129 |
! |
|
2130 |
||
19177 | 2131 |
asUnsignedByteArray |
2132 |
"return a new ByteArray with the collection's elements |
|
2133 |
(which must convert to 8bit integers in the range 0..255)." |
|
2134 |
||
2135 |
^ self asIntegerArray:ByteArray |
|
2136 |
! |
|
2137 |
||
7885 | 2138 |
asWordArray |
15939 | 2139 |
"return a new WordArray with the collection's elements |
7885 | 2140 |
(which must convert to integers in the range 0..16rFFFF)." |
2141 |
||
15939 | 2142 |
^ self asIntegerArray:WordArray |
7885 | 2143 |
! |
2144 |
||
14928 | 2145 |
associationsAsDictionary |
2146 |
"return a new Dictionary with the receiver collection's elements, |
|
2147 |
which must be associations" |
|
2148 |
||
2149 |
^ Dictionary withAssociations:self. |
|
2150 |
||
2151 |
" |
|
2152 |
{ 'ten' -> 10 . 'twenty' -> 20 . 'thirty' -> 30 } associationsAsDictionary |
|
2153 |
" |
|
2154 |
! |
|
2155 |
||
12291 | 2156 |
copyAs:collectionClass |
2157 |
"return a new instance of collectionClass with the receiver collection's elements. |
|
2158 |
This is similar to copy as:collectionClass, to ensure that we get a new |
|
2159 |
(unshared) collection, but avoids the copy if the receiver is not already an |
|
2160 |
instance of collectionClass." |
|
2161 |
||
2162 |
|newColl| |
|
2163 |
||
2164 |
newColl := self as:collectionClass. |
|
2165 |
newColl == self ifTrue:[ |
|
2166 |
^ self copy |
|
2167 |
]. |
|
2168 |
^ newColl. |
|
2169 |
||
2170 |
" |
|
2171 |
|coll1 coll2| |
|
2172 |
||
2173 |
coll1 := #(1 2 3 4 5). |
|
2174 |
coll2 := coll1 copyAs:Array. '-- will generate a copy'. |
|
2175 |
self assert:(coll1 ~~ coll2). |
|
2176 |
self assert:(coll1 = coll2). |
|
2177 |
||
2178 |
coll1 := #(1 2 3 4 5). |
|
2179 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate an OC'. |
|
2180 |
self assert:(coll1 ~~ coll2). |
|
12292
5d5c2bc66cc0
comment/format in: #copyAs:
Claus Gittinger <cg@exept.de>
parents:
12291
diff
changeset
|
2181 |
self assert:(coll1 isSameSequenceAs: coll2). |
12291 | 2182 |
|
2183 |
coll1 := #(1 2 3 4 5) asOrderedCollection. |
|
2184 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate a copy'. |
|
2185 |
self assert:(coll1 ~~ coll2). |
|
2186 |
self assert:(coll1 = coll2). |
|
2187 |
" |
|
2188 |
! |
|
2189 |
||
12249 | 2190 |
copyAsOrderedCollection |
2191 |
"return a new OrderedCollection with the receiver collection's elements. |
|
2192 |
This is similar to copy asOrderedCollection, to ensure that we get a new |
|
2193 |
(unshared) collection, but avoids the copy if the receiver is not already an |
|
2194 |
OrderedCollection." |
|
2195 |
||
2196 |
|newColl| |
|
2197 |
||
2198 |
newColl := self asOrderedCollection. |
|
2199 |
newColl == self ifTrue:[ |
|
2200 |
^ self copy |
|
2201 |
]. |
|
2202 |
^ newColl. |
|
2203 |
! |
|
2204 |
||
7905 | 2205 |
keysAndValues |
2206 |
|assocs| |
|
2207 |
||
2208 |
assocs := OrderedCollection new. |
|
2209 |
self keysAndValuesDo:[:k :v | |
|
2210 |
assocs add:(k -> v). |
|
2211 |
]. |
|
2212 |
^ assocs. |
|
2213 |
||
2214 |
" |
|
2215 |
#(10 20 30 40 50 60) keysAndValues |
|
2216 |
" |
|
2217 |
! |
|
2218 |
||
9410 | 2219 |
literalArrayEncoding |
2220 |
"encode myself as an array literal, from which a copy of the receiver |
|
2221 |
can be reconstructed with #decodeAsLiteralArray." |
|
2222 |
||
2223 |
|encoding idx| |
|
2224 |
||
2225 |
encoding := Array new:self size + 1. |
|
2226 |
encoding at:1 put:self class name. |
|
2227 |
idx := 2. |
|
2228 |
self do:[:element| |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
2229 |
encoding at:idx put:element literalArrayEncoding. |
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
2230 |
idx := idx + 1. |
9410 | 2231 |
]. |
2232 |
^ encoding |
|
2233 |
||
2234 |
||
2235 |
" |
|
2236 |
(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
|
2237 |
with:(1 @ 2) |
9410 | 2238 |
) literalArrayEncoding decodeAsLiteralArray |
2239 |
" |
|
2240 |
||
2241 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
2242 |
! |
|
2243 |
||
602 | 2244 |
readStream |
2245 |
"return a stream for reading from the receiver" |
|
2246 |
||
2247 |
^ ReadStream on:self |
|
88 | 2248 |
|
2249 |
" |
|
602 | 2250 |
|s| |
2251 |
||
2252 |
s := 'hello world' readStream. |
|
2253 |
s next:5. |
|
2254 |
s next. |
|
2255 |
(s next:5) inspect |
|
88 | 2256 |
" |
1 | 2257 |
! |
2258 |
||
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2259 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2260 |
"return a stream for reading from the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2261 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2262 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2263 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2264 |
^ self readStream |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2265 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2266 |
|
8631 | 2267 |
readWriteStream |
2268 |
"return a stream for reading and writing from/to the receiver" |
|
2269 |
||
2270 |
^ ReadWriteStream with:self |
|
2271 |
||
2272 |
" |
|
11436 | 2273 |
'hello world' readWriteStream |
2274 |
nextPutAll:'+Foo'; |
|
2275 |
contents |
|
2276 |
||
2277 |
'hello world' readWriteStream |
|
2278 |
setToEnd; |
|
2279 |
nextPutAll:'+Foo'; |
|
2280 |
contents |
|
8631 | 2281 |
" |
2282 |
! |
|
2283 |
||
602 | 2284 |
writeStream |
2285 |
"return a stream for writing onto the receiver" |
|
2286 |
||
14608 | 2287 |
^ self class writeStreamClass on:self |
2 | 2288 |
|
602 | 2289 |
" |
2290 |
|s| |
|
2291 |
||
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
2292 |
s := #() writeStream. |
602 | 2293 |
s nextPut:1. |
2294 |
s nextPut:2. |
|
2295 |
s nextPut:3. |
|
2296 |
s contents inspect |
|
2297 |
" |
|
88 | 2298 |
|
2299 |
" |
|
602 | 2300 |
|s| |
2 | 2301 |
|
602 | 2302 |
s := OrderedCollection new writeStream. |
2303 |
s nextPut:1. |
|
2304 |
s nextPut:2. |
|
2305 |
s nextPut:3. |
|
2306 |
s contents inspect |
|
2307 |
" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2308 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2309 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2310 |
writeStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2311 |
"return a stream for writing onto the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2312 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2313 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2314 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
2315 |
^ self writeStream |
602 | 2316 |
! ! |
1 | 2317 |
|
602 | 2318 |
!Collection methodsFor:'copying'! |
2319 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2320 |
copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2321 |
"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
|
2322 |
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
|
2323 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2324 |
^ self shallowCopy postCopyFrom:self |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2325 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2326 |
"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
|
2327 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2328 |
|
602 | 2329 |
copyEmpty |
2330 |
"return a copy of the receiver with no elements. |
|
2331 |
This is used by copying and enumeration methods |
|
2332 |
to get a new instance which is similar to the receiver." |
|
2333 |
||
2334 |
^ self species new |
|
1 | 2335 |
! |
2336 |
||
602 | 2337 |
copyEmpty:size |
2338 |
"return a copy of the receiver with no elements, but space for |
|
2339 |
size elements. This is used by copying and enumeration methods |
|
2340 |
to get a new instance which is similar to the receiver. |
|
2341 |
This method should be redefined in subclasses with instance |
|
2342 |
variables, which should be put into the copy too. |
|
2343 |
For example, SortedCollection has to copy its sortBlock into the |
|
2344 |
new collection." |
|
1 | 2345 |
|
602 | 2346 |
^ self species new:size |
360 | 2347 |
! |
2348 |
||
602 | 2349 |
copyEmptyAndGrow:size |
2350 |
"return a copy of the receiver with size nil elements. |
|
2351 |
This is used by copying and enumeration methods |
|
2352 |
to get a new instance which is similar to the receiver." |
|
155 | 2353 |
|
602 | 2354 |
^ (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
|
2355 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2356 |
|
14955 | 2357 |
copyWith: additionalElement |
2358 |
"Return a copy of the dictionary that is 1 bigger than the receiver and |
|
2359 |
includes the argument, additionalElement, at the end." |
|
2360 |
||
2361 |
| newColl | |
|
2362 |
||
2363 |
newColl := self copy. |
|
2364 |
newColl add: additionalElement. |
|
2365 |
^ newColl |
|
2366 |
! |
|
2367 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2368 |
postCopyFrom:original |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2369 |
"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
|
2370 |
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
|
2371 |
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
|
2372 |
setHash: functionality)." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2373 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2374 |
"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
|
2375 |
^ self postCopy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2376 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
2377 |
"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
|
2378 |
"Modified: / 19.4.1998 / 20:03:57 / cg" |
1 | 2379 |
! ! |
2380 |
||
2381 |
!Collection methodsFor:'enumerating'! |
|
2382 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2383 |
addAllNonNilElementsTo:aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2384 |
"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
|
2385 |
Return aCollection." |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2386 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2387 |
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
|
2388 |
^ aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2389 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2390 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2391 |
#(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
|
2392 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2393 |
! |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
2394 |
|
602 | 2395 |
addAllTo:aCollection |
2396 |
"add all elements of the receiver, to aCollection. |
|
2397 |
Return aCollection." |
|
77 | 2398 |
|
602 | 2399 |
self do:[:each | aCollection add:each]. |
2400 |
^ aCollection |
|
5254 | 2401 |
|
2402 |
" |
|
2403 |
#(1 2 3 4 5 1 2 3 4 5) addAllTo:Set new |
|
2404 |
" |
|
2405 |
||
2406 |
"Modified: / 11.2.2000 / 11:22:14 / cg" |
|
77 | 2407 |
! |
2408 |
||
21647 | 2409 |
and:aSecondCollection and:aThirdCollection do:aBlock |
2410 |
"evaluate the argument, aBlock for each element in the receiver, |
|
2411 |
then for each element in aSecondCollection, then for each in aThirdCollection." |
|
2412 |
||
2413 |
self do:aBlock. |
|
2414 |
aSecondCollection do:aBlock. |
|
2415 |
aThirdCollection do:aBlock. |
|
2416 |
||
2417 |
" |
|
2418 |
#(1 2 3) and: #(a b c) and: #(x y z) do:[:each | Transcript showCR:each] |
|
2419 |
" |
|
2420 |
||
2421 |
"Created: / 15-03-2017 / 18:19:34 / cg" |
|
2422 |
! |
|
2423 |
||
2424 |
and:aSecondCollection do:aBlock |
|
2425 |
"evaluate the argument, aBlock for each element in the receiver, |
|
2426 |
then for each element in aSecondCollection." |
|
2427 |
||
2428 |
self do:aBlock. |
|
2429 |
aSecondCollection do:aBlock. |
|
2430 |
||
2431 |
" |
|
2432 |
#(1 2 3) and: #(a b c) do:[:each | Transcript showCR:each] |
|
2433 |
" |
|
2434 |
||
2435 |
"Created: / 15-03-2017 / 18:19:03 / cg" |
|
2436 |
! |
|
2437 |
||
22274 | 2438 |
collect:aBlockOrSymbol |
1 | 2439 |
"for each element in the receiver, evaluate the argument, aBlock |
2440 |
and return a new collection with the results" |
|
2441 |
||
2442 |
|newCollection| |
|
2443 |
||
19780 | 2444 |
newCollection := self speciesForCollecting new:self size. |
22274 | 2445 |
self do:[:element | newCollection add:(aBlockOrSymbol value:element)]. |
1 | 2446 |
^ newCollection |
68 | 2447 |
|
2448 |
" |
|
20211 | 2449 |
#(1 2 3 4) asSet collect:[:n | n * 2] |
22274 | 2450 |
#(1 2 3 4) asSet collect:#mul2 |
2451 |
" |
|
2452 |
||
2453 |
"Modified (comment): / 20-09-2017 / 19:39:22 / stefan" |
|
2454 |
! |
|
2455 |
||
2456 |
collect:aBlockOrSymbol as:aClass |
|
11871 | 2457 |
"like collect, but use an instance of aClass to collect the results. |
2458 |
Also avoids the need for an extra intermediate collection which is created with |
|
2459 |
the standard coding: 'self asXXXX collect:[...]" |
|
2460 |
||
2461 |
|newCollection |
|
2462 |
idx "{ Class:SmallInteger }" |
|
2463 |
sz "{ Class:SmallInteger }"| |
|
2464 |
||
2465 |
sz := self size. |
|
2466 |
newCollection := (aClass new:sz) grow:sz. |
|
12008 | 2467 |
newCollection isSequenceable ifTrue:[ |
2468 |
idx := 1. |
|
2469 |
self do:[:el | |
|
22274 | 2470 |
newCollection at:idx put:(aBlockOrSymbol value:el). |
12008 | 2471 |
idx := idx + 1. |
2472 |
]. |
|
2473 |
] ifFalse:[ |
|
2474 |
self do:[:el | |
|
22274 | 2475 |
newCollection add:(aBlockOrSymbol value:el). |
12008 | 2476 |
]. |
11871 | 2477 |
]. |
2478 |
^ newCollection |
|
2479 |
||
2480 |
" |
|
2481 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
|
2482 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
|
22274 | 2483 |
|
2484 |
'abcdef' collect:#digitValue as:ByteArray |
|
2485 |
" |
|
2486 |
||
2487 |
"Modified (comment): / 20-09-2017 / 18:02:15 / stefan" |
|
11871 | 2488 |
! |
2489 |
||
16777 | 2490 |
collect:collectBlock thenDetect:detectBlock ifNone:exceptionalValue |
2491 |
"first apply collectBlock to each element, then pass the result to |
|
2492 |
detectBlock. |
|
2493 |
Return the first element from collectBlock for which detectBlock evaluates to true. |
|
2494 |
If none does, return the value of exceptionalValue, which is usually a block. |
|
2495 |
Returns the same as if two separate collect:+detect:ifNone: messages were sent, |
|
2496 |
but avoids the creation of intermediate collections, so this is nicer for |
|
2497 |
big collections." |
|
2498 |
||
2499 |
self do:[:each | |
|
2500 |
|rslt| |
|
2501 |
||
2502 |
rslt := collectBlock value:each. |
|
2503 |
(detectBlock value:rslt) ifTrue:[^ rslt]. |
|
2504 |
]. |
|
2505 |
^ exceptionalValue value |
|
2506 |
||
2507 |
" |
|
2508 |
( #(1 2 3 4) collect:[:e | e squared] ) detect:[:e| e odd] ifNone:0 |
|
2509 |
#(1 2 3 4) collect:[:e | e squared] thenDetect:[:e| e odd] ifNone:0 |
|
22274 | 2510 |
#(1 2 3 4) collect:#squared thenDetect:#odd ifNone:0 |
2511 |
" |
|
2512 |
||
2513 |
"Modified (comment): / 20-09-2017 / 18:18:33 / stefan" |
|
16777 | 2514 |
! |
2515 |
||
14027 | 2516 |
collect:collectBlock thenDo:aBlock |
2517 |
"combination of collect followed by do. |
|
2518 |
Avoids the creation of intermediate garbage" |
|
2519 |
||
2520 |
self do:[:each | aBlock value:(collectBlock value:each)] |
|
2521 |
||
2522 |
" |
|
2523 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenDo:[:i | Transcript showCR:i ] |
|
2524 |
" |
|
2525 |
||
2526 |
"Created: / 28-02-2012 / 21:05:16 / cg" |
|
2527 |
! |
|
2528 |
||
11928 | 2529 |
collect:collectBlock thenReject:rejectBlock |
2530 |
"combination of collect followed by reject. |
|
2531 |
May be redefined by some subclasses for optimal performance |
|
2532 |
(avoiding the creation of intermediate garbage)" |
|
2533 |
||
12943
85c58cb22c32
changed: #collect:thenReject:
Claus Gittinger <cg@exept.de>
parents:
12666
diff
changeset
|
2534 |
^ self collect:collectBlock thenSelect:[:el | (rejectBlock value:el) not] |
11928 | 2535 |
|
2536 |
" |
|
2537 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenReject:[:i | i > 10] |
|
2538 |
" |
|
12943
85c58cb22c32
changed: #collect:thenReject:
Claus Gittinger <cg@exept.de>
parents:
12666
diff
changeset
|
2539 |
|
85c58cb22c32
changed: #collect:thenReject:
Claus Gittinger <cg@exept.de>
parents:
12666
diff
changeset
|
2540 |
"Modified: / 11-07-2010 / 17:03:16 / cg" |
11928 | 2541 |
! |
2542 |
||
22274 | 2543 |
collect:collectBlockOrSymbol thenSelect:selectBlockOrSymbol |
4890 | 2544 |
"combination of collect followed by select. |
2545 |
May be redefined by some subclasses for optimal performance |
|
7164 | 2546 |
(avoiding the creation of intermediate garbage)" |
4890 | 2547 |
|
22274 | 2548 |
^ (self collect:collectBlockOrSymbol) select:selectBlockOrSymbol |
4892 | 2549 |
|
2550 |
" |
|
2551 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10] |
|
22274 | 2552 |
#(1 2 3 4 5 6 7) collect:#mul2 thenSelect:#isPerfectSquare |
2553 |
" |
|
2554 |
||
2555 |
"Modified (format): / 20-09-2017 / 18:07:52 / stefan" |
|
4890 | 2556 |
! |
2557 |
||
15721 | 2558 |
collect:collectBlock thenSelect:selectBlock as:aCollectionClass |
2559 |
"first apply collectBlock to each element, then pass the result to |
|
2560 |
selectBlock. |
|
2561 |
Return a new collection with all elements from the receiver, |
|
2562 |
for which the selectBlock evaluates to true. |
|
15722 | 2563 |
Returns the same as if three separate collect+select+as messages were sent, |
15721 | 2564 |
but avoids the creation of intermediate collections, so this is nicer for |
2565 |
big collections." |
|
2566 |
||
2567 |
|newCollection| |
|
2568 |
||
2569 |
newCollection := aCollectionClass new. |
|
2570 |
self do:[:each | |
|
2571 |
|rslt| |
|
2572 |
||
2573 |
rslt := collectBlock value:each. |
|
2574 |
(selectBlock value:rslt) ifTrue:[newCollection add:rslt]. |
|
2575 |
]. |
|
2576 |
^ newCollection |
|
2577 |
||
2578 |
" |
|
2579 |
#(1 2 3 4) select:[:e | e odd] thenCollect:[:e| e*e] as:OrderedCollection |
|
2580 |
(1 to:10) select:[:e | e even] thenCollect:[:e| e*e] as:IdentitySet |
|
2581 |
" |
|
2582 |
||
2583 |
"Created: / 29-08-2013 / 09:56:20 / cg" |
|
2584 |
! |
|
2585 |
||
6867 | 2586 |
collectAll:aBlock |
2587 |
"for each element in the receiver, evaluate the argument, aBlock. |
|
9315 | 2588 |
The block is supposed to return a collection, whose elements are collected. |
8608 | 2589 |
The species of the returned collection is that of the first returned |
2590 |
partial result." |
|
6867 | 2591 |
|
2592 |
|result| |
|
2593 |
||
2594 |
self do:[:element | |
|
2595 |
|individualResult| |
|
2596 |
||
2597 |
individualResult := aBlock value:element. |
|
2598 |
result isNil ifTrue:[ |
|
20689 | 2599 |
result := individualResult speciesForCollecting new. |
6867 | 2600 |
]. |
2601 |
result addAll:individualResult. |
|
2602 |
]. |
|
2603 |
||
20689 | 2604 |
"do not answer an empty - possibly immutable - Array" |
2605 |
^ result ? self speciesForCollecting new. |
|
6867 | 2606 |
|
2607 |
" |
|
2608 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] |
|
7629 | 2609 |
#(1 2 3 4) collectAll:[:n | Array with:n with:n squared ] |
8607 | 2610 |
#(1 2 3 4) collectAll:[:n | 1 to:n ] |
2611 |
(Array with:Point with:Rectangle) collectAll:[:c | c instVarNames ] |
|
7629 | 2612 |
" |
6867 | 2613 |
! |
2614 |
||
15733 | 2615 |
collectAll:aBlock as:collectionClass |
2616 |
"for each element in the receiver, evaluate the argument, aBlock. |
|
2617 |
The block is supposed to return a collection, whose elements are collected. |
|
2618 |
The returned collection will be an instance of collectionClass" |
|
2619 |
||
2620 |
|result| |
|
2621 |
||
2622 |
result := collectionClass new. |
|
2623 |
self do:[:element | |
|
20689 | 2624 |
result addAll:(aBlock value:element). |
15733 | 2625 |
]. |
2626 |
||
2627 |
^ result |
|
2628 |
||
2629 |
" |
|
2630 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] as:OrderedCollection |
|
2631 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] as:Bag |
|
2632 |
#(1 2 3 4) collectAll:[:n | Array with:n with:n squared ] as: OrderedCollection |
|
2633 |
#(1 2 3 4) collectAll:[:n | 1 to:n ] as: Set |
|
2634 |
(Array with:Point with:Rectangle) collectAll:[:c | c instVarNames ] as:StringCollection |
|
2635 |
" |
|
2636 |
! |
|
2637 |
||
17408 | 2638 |
collectWithIndex:aTwoArgBlock |
2639 |
"for each element in the receiver and a running index, |
|
2640 |
evaluate the argument, aTwoArgBlock. |
|
2641 |
Return a new collection with the results" |
|
2642 |
||
2643 |
|newCollection runIndex| |
|
2644 |
||
19780 | 2645 |
newCollection := self speciesForCollecting new:self size. |
17408 | 2646 |
runIndex := 1. |
2647 |
self do:[:element | |
|
2648 |
newCollection add:(aTwoArgBlock value:element value:runIndex). |
|
2649 |
runIndex := runIndex + 1. |
|
2650 |
]. |
|
2651 |
^ newCollection |
|
2652 |
||
2653 |
" |
|
2654 |
#(1 2 3 4) collectWithIndex:[:n :i | n * 2 + i] |
|
2655 |
#(1 2 3 4) collectWithIndex:[:n :i | i -> (n * 2)] |
|
2656 |
" |
|
2657 |
! |
|
2658 |
||
602 | 2659 |
count:aBlock |
2660 |
"count elements, for which aBlock returns true. |
|
2661 |
Return the sum." |
|
2662 |
||
7336 | 2663 |
|count| |
2664 |
||
2665 |
count := 0. |
|
2666 |
self do:[:element | |
|
2667 |
(aBlock value:element) ifTrue:[count := count + 1] |
|
2668 |
]. |
|
2669 |
^ count |
|
77 | 2670 |
|
2671 |
" |
|
602 | 2672 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
7336 | 2673 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
22274 | 2674 |
#(1 nil nil nil 2 3 nil 4 5) count:#isNil |
2675 |
" |
|
2676 |
||
2677 |
"Modified (comment): / 20-09-2017 / 19:26:48 / stefan" |
|
2678 |
! |
|
2679 |
||
2680 |
detect:aBlockOrSmbol |
|
1 | 2681 |
"evaluate the argument, aBlock for each element in the receiver until |
2682 |
the block returns true; in this case return the element which caused |
|
2683 |
the true evaluation. |
|
345 | 2684 |
If none of the evaluations returns true, report an error" |
1 | 2685 |
|
22274 | 2686 |
^ self detect:aBlockOrSmbol ifNone:[self errorNotFound] |
68 | 2687 |
|
2688 |
" |
|
2689 |
#(1 2 3 4) detect:[:n | n odd] |
|
2690 |
#(2 4 6 8) detect:[:n | n odd] |
|
16215 | 2691 |
|
2692 |
#(1 2 3 4) detect: #odd |
|
2693 |
#(2 4 6 8) detect: #odd |
|
68 | 2694 |
" |
22274 | 2695 |
|
2696 |
"Modified (format): / 20-09-2017 / 18:08:13 / stefan" |
|
1 | 2697 |
! |
2698 |
||
16217 | 2699 |
detect:generatorBlock forWhich:testBlock ifNone:exceptionValue |
8750 | 2700 |
"evaluate generatorBlock for each element in the receiver until |
2701 |
testBlock returns true for it; |
|
2702 |
in this case return the value from generatorBlock, which caused the true evaluation. |
|
16217 | 2703 |
If none of the test evaluations returns true, return the value from exceptionValue" |
8750 | 2704 |
|
2705 |
self do:[:each | |
|
2706 |
|val| |
|
2707 |
||
2708 |
val := generatorBlock value:each. |
|
2709 |
(testBlock value:val) ifTrue:[^ val]. |
|
2710 |
]. |
|
16217 | 2711 |
^ exceptionValue value |
8750 | 2712 |
|
2713 |
" |
|
2714 |
#(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
2715 |
#( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
8751 | 2716 |
|
2717 |
#( 'st' 'c' 'java' ) |
|
2718 |
detect:[:ext | 'Foo' asFilename withSuffix:ext] |
|
2719 |
forWhich:[:fn | fn exists] |
|
2720 |
ifNone:nil |
|
8750 | 2721 |
" |
2722 |
! |
|
2723 |
||
22274 | 2724 |
detect:aOneArgBlockOrSymbol ifNone:exceptionValue |
22250 | 2725 |
"evaluate the argument aOneArgBlock for each element in the receiver until |
2726 |
the block returns true; in this case return the element that caused the |
|
2727 |
true evaluation. |
|
16217 | 2728 |
If none of the evaluations returns true, return the value from exceptionValue" |
1 | 2729 |
|
22250 | 2730 |
self do:[:each | |
22274 | 2731 |
(aOneArgBlockOrSymbol value:each) ifTrue:[^ each]. |
1 | 2732 |
]. |
16217 | 2733 |
^ exceptionValue value |
68 | 2734 |
|
2735 |
" |
|
22250 | 2736 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
2737 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
22274 | 2738 |
#(1 2 3 4) detect:#odd ifNone:['sorry'] |
2739 |
#(2 4 6 8) detect:#odd ifNone:['sorry'] |
|
68 | 2740 |
" |
9842 | 2741 |
|
2742 |
"Modified: / 13-09-2006 / 11:17:42 / cg" |
|
22250 | 2743 |
"Modified (comment): / 13-09-2017 / 11:34:47 / mawalch" |
22274 | 2744 |
"Modified (format): / 20-09-2017 / 18:10:34 / stefan" |
1 | 2745 |
! |
2746 |
||
15408 | 2747 |
detect:checkBlock thenCompute:evalBlock |
2748 |
"evaluate the argument, aBlock for each element in the receiver until |
|
19459 | 2749 |
checkBck returns true; in this case return the value from evalBlock |
15408 | 2750 |
applied to the element which caused the true evaluation. |
2751 |
If none of the evaluations returns true, report an error" |
|
2752 |
||
2753 |
^ self detect:checkBlock thenCompute:evalBlock ifNone:[self errorNotFound] |
|
2754 |
||
2755 |
" |
|
2756 |
#((1 'one') (2 'two') (3 'three') (4 'four')) |
|
22274 | 2757 |
detect:[:pair | pair first odd] thenCompute:[:pair | pair second] |
2758 |
||
2759 |
#((1 'one') (2 'two') (3 'three') (4 'four')) |
|
2760 |
detect:[:pair | pair first odd] thenCompute:#second |
|
19708 | 2761 |
|
2762 |
#( (2 'two') (4 'four')) |
|
22274 | 2763 |
detect:[:pair | pair first odd] thenCompute:[:pair | pair second] |
2764 |
||
2765 |
" |
|
2766 |
||
2767 |
"Modified (comment): / 20-09-2017 / 18:13:15 / stefan" |
|
15408 | 2768 |
! |
2769 |
||
2770 |
detect:checkBlock thenCompute:evalBlock ifNone:exceptionValue |
|
22250 | 2771 |
"evaluate the argument checkBlock for each element in the receiver until |
2772 |
it returns true; in this case return the value from evalBlock applied to |
|
2773 |
the element that caused the true evaluation. |
|
15408 | 2774 |
If none of the evaluations returns true, return the value from exceptionValue." |
2775 |
||
19708 | 2776 |
|foundElement| |
22250 | 2777 |
|
19708 | 2778 |
foundElement := self detect:checkBlock ifNone:[^ exceptionValue value]. |
2779 |
^ evalBlock value:foundElement. |
|
15408 | 2780 |
|
2781 |
" |
|
22250 | 2782 |
#((1 'one') (2 'two') (3 'three') (4 'four')) |
15408 | 2783 |
detect:[:pair | pair first odd] thenCompute:[:pair | pair second] |
2784 |
ifNone:[ nil ]. |
|
2785 |
" |
|
22250 | 2786 |
" |
2787 |
#( (2 'two') (4 'four')) |
|
2788 |
detect:[:pair | pair first odd] thenCompute:[:pair | pair second] |
|
2789 |
ifNone:[ nil ]. |
|
2790 |
" |
|
2791 |
||
2792 |
"Modified (comment): / 13-09-2017 / 11:33:51 / mawalch" |
|
15408 | 2793 |
! |
2794 |
||
421 | 2795 |
detectLast:aBlock |
2796 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2797 |
the block returns true; in this case return the element which caused |
|
2798 |
the true evaluation. The elements are processed in reverse order. |
|
2799 |
If none of the evaluations returns true, report an error" |
|
2800 |
||
2801 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
2802 |
||
2803 |
" |
|
2804 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
22274 | 2805 |
#(1 2 3 4) detectLast:#odd |
421 | 2806 |
#(2 4 6 8) detectLast:[:n | n odd] |
2807 |
" |
|
22274 | 2808 |
|
2809 |
"Modified (comment): / 20-09-2017 / 18:14:33 / stefan" |
|
421 | 2810 |
! |
2811 |
||
16217 | 2812 |
detectLast:aBlock ifNone:exceptionValue |
421 | 2813 |
"evaluate the argument, aBlock for each element in the receiver until |
2814 |
the block returns true; in this case return the element which caused |
|
2815 |
the true evaluation. The elements are processed in reverse order. |
|
16217 | 2816 |
If none of the evaluations returns true, return the value from exceptionValue" |
421 | 2817 |
|
2818 |
self reverseDo:[:each | |
|
6135 | 2819 |
(aBlock value:each) ifTrue:[^ each]. |
421 | 2820 |
]. |
16217 | 2821 |
^ exceptionValue value |
421 | 2822 |
|
2823 |
" |
|
2824 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2825 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2826 |
" |
|
2827 |
! |
|
2828 |
||
22274 | 2829 |
detectMax: aBlockOrSymbol |
19708 | 2830 |
"Evaluate aBlock with each of the receiver's elements as argument. |
19459 | 2831 |
Answer the element for which aBlock evaluates to the highest magnitude. |
2832 |
If the receiver collection is empty, return nil. |
|
2833 |
This method might also be called elect:." |
|
13608 | 2834 |
|
2835 |
| maxElement maxValue | |
|
16215 | 2836 |
|
2837 |
self do: [:each | |
|
2838 |
| val | |
|
2839 |
||
22274 | 2840 |
val := aBlockOrSymbol value: each. |
19459 | 2841 |
"Note that there is no way to get the first element |
2842 |
which works for all kinds of Collections. |
|
2843 |
Must therefore test every one (maxValue is nil for the first element)." |
|
21309 | 2844 |
(maxValue isNil or:[val > maxValue]) ifTrue: [ |
16215 | 2845 |
maxElement := each. |
2846 |
maxValue := val |
|
2847 |
] |
|
2848 |
]. |
|
13608 | 2849 |
^ maxElement |
2850 |
||
16215 | 2851 |
" |
2852 |
#(1 -1 5 -17 10 -8 5) detectMax: #abs |
|
2853 |
" |
|
2854 |
||
13608 | 2855 |
"Created: / 20-08-2011 / 21:34:49 / cg" |
22274 | 2856 |
"Modified (format): / 20-09-2017 / 18:15:35 / stefan" |
2857 |
! |
|
2858 |
||
2859 |
detectMin: aBlockOrSymbol |
|
19708 | 2860 |
"Evaluate aBlock with each of the receiver's elements as argument. |
19459 | 2861 |
Answer the element for which aBlock evaluates to the lowest number. |
2862 |
If the receiver collection is empty, return nil." |
|
13608 | 2863 |
|
2864 |
| minElement minValue | |
|
16215 | 2865 |
|
2866 |
self do: [:each | |
|
2867 |
| val | |
|
2868 |
||
22274 | 2869 |
val := aBlockOrSymbol value: each. |
19459 | 2870 |
"Note that there is no way to get the first element |
2871 |
which works for all kinds of Collections. |
|
2872 |
Must therefore test every one (minValue is nil for the first element)." |
|
21309 | 2873 |
(minValue isNil or:[val < minValue]) ifTrue: [ |
16215 | 2874 |
minElement := each. |
2875 |
minValue := val |
|
2876 |
] |
|
2877 |
]. |
|
13608 | 2878 |
^ minElement |
2879 |
||
16215 | 2880 |
" |
2881 |
#(1 -1 5 -17 10 -8 5) detectMin: #abs |
|
2882 |
" |
|
2883 |
||
13608 | 2884 |
"Created: / 20-08-2011 / 21:35:13 / cg" |
22274 | 2885 |
"Modified (format): / 20-09-2017 / 18:15:28 / stefan" |
13608 | 2886 |
! |
2887 |
||
602 | 2888 |
do:aBlock |
2889 |
"evaluate the argument, aBlock for each element" |
|
2890 |
||
2891 |
^ self subclassResponsibility |
|
2892 |
! |
|
2893 |
||
4394 | 2894 |
do:aBlock inBetweenDo:betweenBlock |
2895 |
"evaluate the argument, aBlock for each element. |
|
2896 |
Between elements (i.e. after each except for the last), |
|
2897 |
evaluate betweenBlock. |
|
5254 | 2898 |
This is a utility helper for collection printers |
2899 |
(for example, to print a space between elements)." |
|
4394 | 2900 |
|
8359 | 2901 |
<resource: #obsolete> |
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2902 |
self obsoleteMethodWarning:'use #do:separatedBy:'. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2903 |
^ self do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2904 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2905 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2906 |
! |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2907 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2908 |
do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2909 |
"evaluate the argument, aBlock for each element. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2910 |
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
|
2911 |
evaluate betweenBlock. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2912 |
This is a utility helper for collection printers |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2913 |
(for example, to print a space between elements)." |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2914 |
|
15443 | 2915 |
"/ could do the more hackish: |
2916 |
"/ |
|
2917 |
"/ |b| |
|
2918 |
"/ |
|
2919 |
"/ b := [ b := betweenBlock ]. |
|
2920 |
"/ self do:[:element | |
|
2921 |
"/ b value. |
|
2922 |
"/ aBlock value:element |
|
2923 |
"/ ]. |
|
2924 |
"/ |
|
2925 |
"/ but that creates a block, whereas the following does not. |
|
2926 |
||
4394 | 2927 |
|first| |
2928 |
||
2929 |
first := true. |
|
2930 |
self do:[:element | |
|
2931 |
first ifTrue:[ |
|
2932 |
first := false |
|
2933 |
] ifFalse:[ |
|
2934 |
betweenBlock value. |
|
2935 |
]. |
|
2936 |
aBlock value:element |
|
2937 |
]. |
|
2938 |
||
2939 |
" |
|
2940 |
#(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
|
2941 |
separatedBy:[ Transcript show:'-'] |
5619 | 2942 |
|
2943 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2944 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2945 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2946 |
|
2947 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2948 |
associations |
|
2949 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2950 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2951 |
|
4394 | 2952 |
" |
5254 | 2953 |
|
2954 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
|
4394 | 2955 |
! |
2956 |
||
18961 | 2957 |
do:aBlock whileTrue:whileBlock |
2958 |
"evaluate the argument, aBlock for each element until whileBlock |
|
2959 |
evaluates to false. |
|
2960 |
Answer the last result from evaluating aBlock." |
|
2961 |
||
2962 |
|result| |
|
2963 |
||
2964 |
self do:[:el | |
|
2965 |
result := aBlock value:el. |
|
2966 |
(whileBlock value:el) ifFalse:[ |
|
2967 |
^ result. |
|
2968 |
] |
|
2969 |
]. |
|
2970 |
^ result |
|
2971 |
||
2972 |
"Example: |
|
2973 |
search for the first element which is >= 99; return it. |
|
2974 |
remaining elements are not processed: |
|
2975 |
||
2976 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2977 |
do:[:element| |
|
2978 |
Transcript showCR:element. |
|
2979 |
element |
|
2980 |
] whileTrue:[:element| element < 99] |
|
2981 |
" |
|
2982 |
! |
|
2983 |
||
14027 | 2984 |
do:aBlock without:anItem |
2985 |
"enumerate all elements except those equal to anItem into aBlock." |
|
2986 |
||
2987 |
self do:[:el | |
|
2988 |
anItem ~= el ifTrue:[ aBlock value:el ] |
|
2989 |
]. |
|
2990 |
^ self |
|
2991 |
||
2992 |
" |
|
2993 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2994 |
do:[:el | Transcript showCR:el ] |
|
2995 |
without:5 |
|
2996 |
" |
|
2997 |
||
2998 |
"Created: / 28-02-2012 / 21:11:41 / cg" |
|
2999 |
! |
|
3000 |
||
18961 | 3001 |
doWhileTrue:aBlock |
3002 |
"evaluate the argument, aBlock for each element, |
|
3003 |
until the block evaluates to false. |
|
3004 |
Answer true, if all the elements have been processed, |
|
3005 |
false otherwise." |
|
3006 |
||
3007 |
self do:[:el | |
|
3008 |
(aBlock value:el) ifFalse:[ |
|
3009 |
^ false. |
|
3010 |
] |
|
3011 |
]. |
|
3012 |
^ true |
|
3013 |
||
3014 |
"Example: |
|
3015 |
search for the first element which is >= 99; return it. |
|
3016 |
remaining elements are not processed: |
|
3017 |
||
3018 |
|lastElement| |
|
3019 |
||
3020 |
#(1 2 3 4 999 5 6 7 8 9) |
|
3021 |
doWhileTrue:[:element| |
|
3022 |
Transcript showCR:element. |
|
3023 |
lastElement := element. |
|
3024 |
element < 99 |
|
3025 |
]. |
|
3026 |
lastElement |
|
3027 |
" |
|
3028 |
! |
|
3029 |
||
1212 | 3030 |
doWithExit:aBlock |
3031 |
"evaluate the argument, aBlock for each element. |
|
3032 |
Passes an additional exit object, which can be used to leave |
|
3033 |
the loop early, by sending it a #value: message. |
|
3034 |
Returns nil or the value passed to the exit>>value: message. |
|
3035 |
||
8662 | 3036 |
Notice, that this is different to a return statement in the block, |
3037 |
which returns from the enclosed method, NOT only from the block." |
|
3038 |
||
3039 |
|exit| |
|
3040 |
||
3041 |
exit := [:exitValue | ^exitValue]. |
|
14028 | 3042 |
|
8662 | 3043 |
self do:[:el | |
3044 |
aBlock value:el value:exit |
|
3045 |
]. |
|
3046 |
^ nil |
|
1212 | 3047 |
|
3048 |
"Example: |
|
3049 |
search for the first element which is >= 99; return it. |
|
3050 |
remaining elements are not processed: |
|
3051 |
||
3052 |
#(1 2 3 4 999 5 6 7 8 9) |
|
3053 |
doWithExit:[:element :exit | |
|
1422 | 3054 |
Transcript showCR:element. |
8662 | 3055 |
element >= 99 ifTrue:[exit value:element]] |
1212 | 3056 |
|
3057 |
#(1 2 3 4 5 6 7 8 9) |
|
3058 |
doWithExit:[:element :exit | |
|
1422 | 3059 |
Transcript showCR:element. |
1212 | 3060 |
element >= 99 ifTrue:[exit value:element]] |
3061 |
" |
|
3062 |
||
3063 |
"to demonstrate the difference to returning from the block: |
|
3064 |
this works: |
|
3065 |
||
3066 |
|el| |
|
3067 |
||
3068 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
3069 |
doWithExit:[:element :exit | |
|
3070 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 3071 |
Transcript showCR:el. |
1212 | 3072 |
|
3073 |
||
1422 | 3074 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 3075 |
|
3076 |
|el| |
|
3077 |
||
3078 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
3079 |
do:[:element | |
|
3080 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 3081 |
Transcript showCR:el. |
1212 | 3082 |
" |
3083 |
||
14028 | 3084 |
"Modified: / 18-04-1996 / 14:16:59 / cg" |
3085 |
"Modified (format): / 29-02-2012 / 11:50:02 / cg" |
|
1212 | 3086 |
! |
3087 |
||
17387 | 3088 |
doWithIndex:aBlock |
3089 |
"Squeak/V'Age compatibility; |
|
3090 |
like keysAndValuesDo:, but passes the index as second argument. |
|
3091 |
Same as withIndexDo:, due to parallel evolution of different Smalltalk dialects" |
|
3092 |
||
3093 |
self keysAndValuesDo:[:index :el | aBlock value:el value:index]. |
|
3094 |
||
3095 |
"Created: / 17-10-1997 / 12:33:10 / cg" |
|
3096 |
! |
|
3097 |
||
16214 | 3098 |
flatDetect:aBlock |
3099 |
"for each element of the collection, if it's a scalar, evaluate aBlock for it; |
|
3100 |
otherwise, recursively invoke flatDetect: on the collection. |
|
3101 |
Return the first element for which aBlock evaluates to true. |
|
3102 |
Thus implementing a depth-first search. |
|
3103 |
Raises an error, if no element is found" |
|
3104 |
||
3105 |
^ self flatDetect:aBlock ifNone:[self errorNotFound] |
|
3106 |
||
3107 |
" |
|
3108 |
#( |
|
3109 |
(1 2 3) |
|
3110 |
4 5 |
|
3111 |
(6) |
|
3112 |
7 |
|
3113 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) flatDetect:[:el | el>5] |
|
3114 |
" |
|
3115 |
! |
|
3116 |
||
3117 |
flatDetect:aBlock ifNone:exceptionValue |
|
3118 |
"for each element of the collection, if it's a scalar, evaluate aBlock for it; |
|
3119 |
otherwise, recursively invoke flatDetect: on the collection. |
|
3120 |
Return the first element for which aBlock evaluates to true. |
|
3121 |
Thus implementing a depth-first search. |
|
3122 |
Return the value from exceptionValue if none found" |
|
3123 |
||
3124 |
self do:[:each | |
|
3125 |
(each isNonByteCollection) ifTrue:[ |
|
3126 |
each flatDo:[:el | (aBlock value:el) ifTrue:[^ el]]. |
|
3127 |
] ifFalse:[ |
|
3128 |
(aBlock value:each) ifTrue:[^ each]. |
|
3129 |
]. |
|
3130 |
]. |
|
3131 |
^ exceptionValue value. |
|
3132 |
||
3133 |
" |
|
3134 |
#( |
|
3135 |
(1 2 3) |
|
3136 |
4 5 |
|
3137 |
(6) |
|
3138 |
7 |
|
3139 |
( 8 |
|
3140 |
(9 10) |
|
3141 |
11 |
|
3142 |
12 |
|
3143 |
( 13 |
|
3144 |
( 14 |
|
3145 |
(15) |
|
3146 |
16) |
|
3147 |
) |
|
3148 |
) |
|
3149 |
) flatDetect:[:el | el<0] ifNone:#none |
|
3150 |
" |
|
3151 |
" |
|
3152 |
#( |
|
3153 |
(1 2 3) |
|
3154 |
4 5 |
|
3155 |
(6) |
|
3156 |
7 |
|
3157 |
( 8 |
|
3158 |
(9 10) |
|
3159 |
11 |
|
3160 |
12 |
|
3161 |
( 13 |
|
3162 |
( 14 |
|
3163 |
(15) |
|
3164 |
16) |
|
3165 |
) |
|
3166 |
) |
|
3167 |
) flatDetect:[:el | el>15] ifNone:#none |
|
3168 |
" |
|
21735 | 3169 |
|
3170 |
"Modified (format): / 28-04-2017 / 13:48:29 / stefan" |
|
16214 | 3171 |
! |
3172 |
||
11412 | 3173 |
flatDo:aBlock |
16214 | 3174 |
"for each element of the collection, if it's a scalar, evaluate aBlock for it; |
3175 |
otherwise, recursively invoke flatDo: on the collection. |
|
13226
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
3176 |
Thus implementing a depth-first enumeration" |
11412 | 3177 |
|
3178 |
self do:[:each | |
|
3179 |
(each isNonByteCollection) ifTrue:[ |
|
3180 |
each flatDo:aBlock |
|
3181 |
] ifFalse:[ |
|
3182 |
aBlock value:each |
|
3183 |
]. |
|
3184 |
]. |
|
3185 |
||
3186 |
" |
|
3187 |
#( |
|
3188 |
(1 2 3) |
|
3189 |
4 5 |
|
3190 |
(6) |
|
3191 |
7 |
|
16214 | 3192 |
(8 (9 10) 11 12 (13 (14 (15) 16))) |
3193 |
) flatDo:[:el | Transcript showCR:el] |
|
11412 | 3194 |
" |
13226
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
3195 |
|
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
3196 |
"Modified: / 22-01-2011 / 09:12:22 / cg" |
11412 | 3197 |
! |
3198 |
||
13698 | 3199 |
fold: binaryBlock |
3200 |
"Evaluate the block with the first two elements of the receiver, |
|
3201 |
then with the result of the first evaluation and the next element, |
|
3202 |
and so on. Answer the result of the final evaluation. If the receiver |
|
3203 |
is empty, raise an error. If the receiver has a single element, answer |
|
14028 | 3204 |
that element. |
14312 | 3205 |
|
3206 |
Here the reduction is done from left to right. |
|
3207 |
||
3208 |
See also: #inject:into: #reduce:" |
|
14028 | 3209 |
|
3210 |
| first nextValue | |
|
3211 |
||
3212 |
first := true. |
|
3213 |
self do:[:each | |
|
14313
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3214 |
first |
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3215 |
ifTrue: [first := false. nextValue := each] |
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3216 |
ifFalse: [nextValue := binaryBlock value:nextValue value:each] |
14312 | 3217 |
]. |
3218 |
first ifTrue:[ |
|
3219 |
^ self emptyCollectionError. |
|
14028 | 3220 |
]. |
3221 |
^ nextValue |
|
13698 | 3222 |
|
3223 |
" |
|
19038 | 3224 |
(1 to:10) fold:[:sum :el| sum + el] |
22274 | 3225 |
(1 to:10) fold:#+ |
14312 | 3226 |
(1 to:15) fold:[:x :y| '(', x printString, '+', y printString, ')'] |
3227 |
(1 to:15) reduce:[:x :y| '(', x printString, '+', y printString, ')'] |
|
13698 | 3228 |
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b] |
14312 | 3229 |
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') reduce: [:a :b | a, ' ', b] |
14538 | 3230 |
#() fold: [:a :b | a, ' ', b] -- raises an error |
13698 | 3231 |
" |
3232 |
||
3233 |
"Created: / 14-09-2011 / 16:29:53 / cg" |
|
22274 | 3234 |
"Modified (comment): / 20-09-2017 / 19:24:41 / stefan" |
13698 | 3235 |
! |
3236 |
||
1 | 3237 |
inject:thisValue into:binaryBlock |
1262 | 3238 |
"starting with thisValue for value, pass this value and each element |
3239 |
to binaryBlock, replacing value with the result returned from the block |
|
14313
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3240 |
in the next iteration. |
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3241 |
|
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3242 |
See also: #fold: #reduce:" |
1262 | 3243 |
|
1 | 3244 |
|nextValue| |
3245 |
||
3246 |
nextValue := thisValue. |
|
3247 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
3248 |
^ nextValue |
|
68 | 3249 |
|
1262 | 3250 |
"sum up the elements of a collection: |
3251 |
||
68 | 3252 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
3253 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
22274 | 3254 |
(1 to:10) inject:0 into:#+ |
1262 | 3255 |
|
3256 |
find the minimum: |
|
3257 |
||
3258 |
|coll| |
|
3259 |
coll := #(1 99 -15 20 100). |
|
3260 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
22274 | 3261 |
|
3262 |
|coll| |
|
3263 |
coll := #(1 99 -15 20 100). |
|
3264 |
coll inject:(coll first) into:#min: |
|
3265 |
" |
|
3266 |
||
3267 |
"Modified: / 23-04-1996 / 13:47:06 / cg" |
|
3268 |
"Modified (comment): / 20-09-2017 / 19:26:06 / stefan" |
|
1 | 3269 |
! |
3270 |
||
16034 | 3271 |
injectAndCollect:thisValue into:binaryBlock |
3272 |
"starting with thisValue for value, pass this value and each element |
|
3273 |
to binaryBlock, replacing value with the result returned from the block |
|
3274 |
in the next iteration. |
|
3275 |
Collect all results and return them all. |
|
3276 |
||
3277 |
See also: #fold: #reduce:" |
|
3278 |
||
3279 |
|coll nextValue| |
|
3280 |
||
3281 |
coll := self species new. |
|
3282 |
nextValue := thisValue. |
|
3283 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each. coll add:nextValue]. |
|
3284 |
^ coll |
|
3285 |
||
3286 |
"sum up the elements of a collection: |
|
3287 |
||
3288 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
|
3289 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
3290 |
||
3291 |
same, getting all partial sums: |
|
3292 |
(1 to:10) injectAndCollect:0 into:[:accu :element | accu + element] |
|
3293 |
" |
|
3294 |
! |
|
3295 |
||
14227
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3296 |
keysAndValuesCollect:aBlock |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3297 |
"for each key-value pair in the receiver, evaluate the argument, aBlock |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3298 |
and return a collection with the results. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3299 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3300 |
See also: |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3301 |
#associationsCollect: (which passes keys->value pairs) |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3302 |
#collect: (which only passes values) |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3303 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3304 |
This is much like #associationsCollect:, but aBlock gets the |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3305 |
key and value as two separate arguments. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3306 |
#associationsCollect: is a bit slower. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3307 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3308 |
WARNING: do not add/remove elements while iterating over the receiver. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3309 |
Iterate over a copy to do this." |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3310 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3311 |
|newCollection| |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3312 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3313 |
newCollection := OrderedCollection new. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3314 |
self keysAndValuesDo:[:key :value | |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3315 |
newCollection add:(aBlock value:key value:value) |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3316 |
]. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3317 |
^ newCollection |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3318 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3319 |
" |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3320 |
|ages| |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3321 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3322 |
ages := Dictionary new. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3323 |
ages at:'cg' put:37. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3324 |
ages at:'ca' put:33. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3325 |
ages at:'sv' put:36. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3326 |
ages at:'tk' put:28. |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3327 |
ages keysAndValuesCollect:[:name :age | |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3328 |
name , '''s age is ' , age printString] |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3329 |
" |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3330 |
|
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3331 |
"Modified: 20.4.1996 / 11:33:50 / cg" |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3332 |
! |
11b9add3e354
moved: #keysAndValuesCollect:
Claus Gittinger <cg@exept.de>
parents:
14070
diff
changeset
|
3333 |
|
22168 | 3334 |
keysAndValuesConform:aTwoArgBlock |
3335 |
"evaluate the argument, aBlock for every element in the collection, |
|
3336 |
passing both index and element as arguments. |
|
3337 |
Return false if any such evaluation returns false, true otherwise." |
|
3338 |
||
3339 |
self keysAndValuesDo:[:index :el | |
|
3340 |
(aTwoArgBlock value:index value:el) ifFalse:[^ false]. |
|
3341 |
]. |
|
3342 |
^ true |
|
3343 |
||
3344 |
" |
|
3345 |
#(10 20 30 40) keysAndValuesConform:[:key :element | element = (key * 10) ]. |
|
3346 |
#(10 20 30 33 40) keysAndValuesConform:[:key :element | element = (key * 10) ]. |
|
3347 |
" |
|
3348 |
! |
|
3349 |
||
19734 | 3350 |
keysAndValuesDetect:aBlock ifNone:exceptionalValue |
3351 |
"for each key-value pair in the receiver, evaluate the argument, aBlock |
|
3352 |
and return the value for which aBlock returns true the very first time. |
|
3353 |
If none of the evaluations returns true, return the result of the |
|
3354 |
evaluation of the exceptionBlock" |
|
3355 |
||
3356 |
self keysAndValuesDo:[:key :value | |
|
3357 |
(aBlock value:key value:value) ifTrue:[^ value]. |
|
3358 |
]. |
|
3359 |
^ exceptionalValue value |
|
3360 |
||
3361 |
" |
|
3362 |
|ages| |
|
3363 |
||
3364 |
ages := Dictionary new. |
|
3365 |
ages at:'cg' put:37. |
|
3366 |
ages at:'ca' put:33. |
|
3367 |
ages at:'sv' put:36. |
|
3368 |
ages at:'tk' put:28. |
|
3369 |
ages keysAndValuesDetect:[:name :age | age = 33]. |
|
3370 |
" |
|
3371 |
! |
|
3372 |
||
19738 | 3373 |
keysAndValuesDetectKey:aBlock ifNone:exceptionalValue |
3374 |
"for each key-value pair in the receiver, evaluate the argument, aBlock |
|
3375 |
and return the key/index for which aBlock returns true the very first time. |
|
3376 |
If none of the evaluations returns true, return the result of the |
|
3377 |
evaluation of the exceptionBlock" |
|
3378 |
||
3379 |
self keysAndValuesDo:[:key :value | |
|
3380 |
(aBlock value:key value:value) ifTrue:[^ key]. |
|
3381 |
]. |
|
3382 |
^ exceptionalValue value |
|
3383 |
||
3384 |
" |
|
3385 |
|ages| |
|
3386 |
||
3387 |
ages := Dictionary new. |
|
3388 |
ages at:'cg' put:37. |
|
3389 |
ages at:'ca' put:33. |
|
3390 |
ages at:'sv' put:36. |
|
3391 |
ages at:'tk' put:28. |
|
3392 |
ages keysAndValuesDetectKey:[:name :age | age = 33] ifNone:nil. |
|
3393 |
" |
|
3394 |
! |
|
3395 |
||
602 | 3396 |
keysAndValuesDo:aTwoArgBlock |
3397 |
"evaluate the argument, aBlock for every element in the collection, |
|
19459 | 3398 |
passing both index and element as arguments. |
3399 |
Blocked here - must be redefined in subclasses which have keyed elements" |
|
602 | 3400 |
|
3401 |
^ self errorNotKeyed |
|
3402 |
! |
|
3403 |
||
1359 | 3404 |
keysAndValuesReverseDo:aTwoArgBlock |
3405 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
19459 | 3406 |
passing both index and element as arguments. |
3407 |
Blocked here - must be redefined in subclasses which have keyed elements" |
|
1359 | 3408 |
|
3409 |
^ self errorNotKeyed |
|
3410 |
||
3411 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
3412 |
! |
|
3413 |
||
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3414 |
keysAndValuesSelect:selectBlockWith2Args thenCollect:collectBlockWith2Args |
19459 | 3415 |
"first call the selectBlockWith2Args, passsing it each key and element, |
3416 |
if that returns true, call the collectBlockWith2Args, also with key and element, |
|
3417 |
and collect the resulting values in an OrderedCollection." |
|
3418 |
||
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3419 |
|collected| |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3420 |
|
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3421 |
collected := OrderedCollection new. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3422 |
self keysAndValuesDo:[:eachKey :eachValue | |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3423 |
(selectBlockWith2Args value:eachKey value:eachValue) ifTrue:[ |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3424 |
collected add:(collectBlockWith2Args value:eachKey value:eachValue) |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3425 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3426 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3427 |
^ collected |
9077
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
3428 |
|
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
3429 |
" |
19459 | 3430 |
#(10 20 30 40) |
3431 |
keysAndValuesSelect:[:idx :val | idx > 2] |
|
3432 |
thenCollect:[:idx :val | idx->val] |
|
9077
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
3433 |
" |
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3434 |
! |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
3435 |
|
13042 | 3436 |
keysDo:aBlock |
3437 |
"evaluate the argument, aBlock for every key in the collection." |
|
3438 |
||
3439 |
self keysAndValuesDo:[:k :v | aBlock value:k] |
|
3440 |
||
3441 |
"Created: / 24-08-2010 / 10:12:14 / cg" |
|
3442 |
! |
|
3443 |
||
18935 | 3444 |
map:selectorOrBlock |
3445 |
"for lisp fans (and also for Javascript) - similar to collect:" |
|
3446 |
||
3447 |
selectorOrBlock isSymbol ifTrue:[ |
|
3448 |
^ self collect:[:eachElement | eachElement perform:selectorOrBlock]. |
|
3449 |
]. |
|
3450 |
^ self collect:selectorOrBlock. |
|
3451 |
||
7454 | 3452 |
" |
22274 | 3453 |
#(1 2 3 4) map:#negated |
3454 |
||
3455 |
Time millisecondsToRun:[ |
|
3456 |
(1 to:10000000) map:#negated |
|
3457 |
] |
|
3458 |
||
3459 |
Time millisecondsToRun:[ |
|
3460 |
(1 to:10000000) collect:#negated |
|
3461 |
] |
|
7454 | 3462 |
" |
14028 | 3463 |
|
3464 |
"Modified (comment): / 29-02-2012 / 11:53:51 / cg" |
|
22274 | 3465 |
"Modified (comment): / 20-09-2017 / 19:38:45 / stefan" |
7454 | 3466 |
! |
3467 |
||
18935 | 3468 |
map:selectorOrBlock with:arg |
14028 | 3469 |
"for lisp fans - similar to collect:" |
7454 | 3470 |
|
18935 | 3471 |
selectorOrBlock isSymbol ifTrue:[ |
3472 |
^ self collect:[:eachElement | eachElement perform:selectorOrBlock with:arg]. |
|
3473 |
]. |
|
3474 |
^ self collect:[:each | selectorOrBlock value:each value:arg]. |
|
7454 | 3475 |
|
3476 |
" |
|
3477 |
#(1 2 3 4) map:#+ with:1 |
|
18935 | 3478 |
#(1 2 3 4) map:[:a :b | a + b] with:1 |
7454 | 3479 |
" |
14028 | 3480 |
|
3481 |
"Modified (comment): / 29-02-2012 / 11:53:57 / cg" |
|
7454 | 3482 |
! |
3483 |
||
8863 | 3484 |
nonNilElementsDo:aBlock |
3485 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
3486 |
||
3487 |
self do:[:each | |
|
3488 |
each notNil ifTrue:[ |
|
3489 |
aBlock value:each. |
|
3490 |
] |
|
3491 |
] |
|
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3492 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3493 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3494 |
#(1 nil 3 nil nil 6 7 nil) |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3495 |
nonNilElementsDo:[:el | Transcript showCR:el] |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3496 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3497 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3498 |
"Modified (comment): / 21-12-2011 / 15:51:39 / cg" |
8863 | 3499 |
! |
3500 |
||
10750 | 3501 |
pairsDo:aTwoArgBlock |
3502 |
"evaluate the argument, aTwoArgBlock for every element in the collection, |
|
602 | 3503 |
which is supposed to consist of 2-element collections. |
12347 | 3504 |
The block is called with 2 arguments for each collection in the receiver. |
3505 |
CONFUSION ATTACK: |
|
3506 |
this is different from pairWiseDo:. |
|
3507 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
|
3508 |
(sigh: but we were first, so they should have adapted...)" |
|
602 | 3509 |
|
3510 |
self do:[:aPair | |
|
10750 | 3511 |
aTwoArgBlock value:(aPair at:1) value:(aPair at:2). |
602 | 3512 |
] |
10750 | 3513 |
|
602 | 3514 |
" |
6555 | 3515 |
#( |
3516 |
(1 one) |
|
3517 |
(2 two) |
|
3518 |
(3 three) |
|
3519 |
(4 four) |
|
3520 |
(5 five) |
|
3521 |
(6 six) |
|
3522 |
) pairsDo: |
|
3523 |
[:num :sym | |
|
3524 |
Transcript show:num; show:' is: '; showCR:sym] |
|
602 | 3525 |
|
3526 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 3527 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 3528 |
" |
10750 | 3529 |
|
3530 |
"Modified: / 20-10-2007 / 17:17:50 / cg" |
|
602 | 3531 |
! |
3532 |
||
19708 | 3533 |
partition:check as:species into:aTwoArgBlock |
3534 |
"enumerate the receiver's elements and partition them into two collections, |
|
3535 |
depending on the outcome of a check block. |
|
3536 |
The type of result collection is passed in via the species argument. |
|
3537 |
Evaluate aTwoArgBlock on the two selected and rejected value collections. |
|
3538 |
Also return the selected values as return value" |
|
13512 | 3539 |
|
3540 |
|selected rejected| |
|
3541 |
||
19708 | 3542 |
selected := species new. |
3543 |
rejected := species new. |
|
13512 | 3544 |
self do:[:each | ((check value:each) ifTrue:[selected] ifFalse:[rejected]) add:each]. |
3545 |
aTwoArgBlock value:selected value:rejected. |
|
3546 |
^ selected |
|
3547 |
||
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3548 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3549 |
#(1 2 3 4 5 6 7 8) |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3550 |
partition:[:el | el even] |
19708 | 3551 |
as:Set |
3552 |
into:[:evenElements :oddElements | |
|
3553 |
Transcript show:'even: '; showCR:evenElements. |
|
3554 |
Transcript show:' odd: '; showCR:oddElements. |
|
3555 |
]. |
|
3556 |
" |
|
3557 |
! |
|
3558 |
||
3559 |
partition:check into:aTwoArgBlock |
|
3560 |
"enumerate the receiver's elements and partition them into two collections, |
|
3561 |
depending on the outcome of a check block. |
|
3562 |
Evaluate aTwoArgBlock on the two selected and rejected value collections. |
|
3563 |
Also return the selected values as return value" |
|
3564 |
||
3565 |
^ self partition:check as:OrderedCollection into:aTwoArgBlock |
|
3566 |
||
3567 |
" |
|
3568 |
#(1 2 3 4 5 6 7 8) |
|
3569 |
partition:[:el | el even] |
|
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3570 |
into:[:evenElements :oddElements | |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3571 |
Transcript show:'even: '; showCR:evenElements. |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3572 |
Transcript show:' odd: '; showCR:oddElements. |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3573 |
]. |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3574 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3575 |
|
13512 | 3576 |
"Created: / 20-07-2011 / 00:54:41 / cg" |
3577 |
! |
|
3578 |
||
14312 | 3579 |
reduce:binaryBlock |
3580 |
"Evaluate the block with the first two elements of the receiver, |
|
3581 |
then with the result of the first evaluation and the next element, |
|
3582 |
and so on. Answer the result of the final evaluation. If the receiver |
|
3583 |
is empty, raise an error. If the receiver has a single element, answer |
|
3584 |
that element. |
|
3585 |
||
3586 |
Here the reduction is done from right to left. |
|
3587 |
||
3588 |
See also: #inject:into: #fold:" |
|
3589 |
||
3590 |
| first nextValue | |
|
3591 |
||
14027 | 3592 |
first := true. |
14312 | 3593 |
self do:[:each | |
14313
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3594 |
first |
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3595 |
ifTrue: [first := false. nextValue := each] |
6c0e007e10af
comment/format in: #inject:into:
Stefan Vogel <sv@exept.de>
parents:
14312
diff
changeset
|
3596 |
ifFalse: [nextValue := binaryBlock value:each value:nextValue] |
14312 | 3597 |
]. |
3598 |
first ifTrue:[ |
|
3599 |
^ self emptyCollectionError. |
|
3600 |
]. |
|
3601 |
^ nextValue |
|
3602 |
||
3603 |
" |
|
3604 |
(1 to:15) reduce:[:x :y| '(', x printString, '+', y printString, ')'] |
|
3605 |
(1 to:15) fold:[:x :y| '(', x printString, '+', y printString, ')'] |
|
3606 |
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') reduce: [:a :b | a, ' ', b] |
|
3607 |
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b] |
|
14027 | 3608 |
#(10 1 2 3) reduce:[:el :diff | diff - el] |
3609 |
#(10 1 2 3) reduce:[:el :diff | diff + el] |
|
22274 | 3610 |
#(10 1 2 3) reduce:#+ |
3611 |
#(10 1 2 3) reduce:#max: |
|
14027 | 3612 |
" |
3613 |
||
3614 |
"Created: / 28-02-2012 / 21:16:33 / cg" |
|
22274 | 3615 |
"Modified (comment): / 20-09-2017 / 19:29:14 / stefan" |
14027 | 3616 |
! |
3617 |
||
13512 | 3618 |
reduceLeft:aTwoArgBlock |
14312 | 3619 |
^ self fold:aTwoArgBlock |
13512 | 3620 |
|
3621 |
" |
|
3622 |
#(1 2 3 4 5) reduceLeft:[:sum :el | sum + el] |
|
22274 | 3623 |
#(1 2 3 4 5) reduceLeft:#+ |
13512 | 3624 |
" |
3625 |
||
3626 |
"Created: / 20-07-2011 / 01:01:03 / cg" |
|
22274 | 3627 |
"Modified (comment): / 20-09-2017 / 19:24:11 / stefan" |
13512 | 3628 |
! |
3629 |
||
1 | 3630 |
reject:aBlock |
3631 |
"return a new collection with all elements from the receiver, for which |
|
3632 |
the argument aBlock evaluates to false" |
|
3633 |
||
11928 | 3634 |
^ self select:[:element | (aBlock value:element) not] |
68 | 3635 |
|
3636 |
" |
|
3637 |
#(1 2 3 4) reject:[:e | e odd] |
|
3638 |
(1 to:10) reject:[:e | e even] |
|
3639 |
" |
|
1 | 3640 |
! |
3641 |
||
11928 | 3642 |
reject:rejectBlock thenCollect:collectBlock |
3643 |
"combination of reject followed by collect. |
|
3644 |
May be redefined by some subclasses for optimal performance |
|
3645 |
(avoiding the creation of intermediate garbage)" |
|
3646 |
||
12944
791a4ec545c2
changed: #reject:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
12943
diff
changeset
|
3647 |
^ self select:[:el | (rejectBlock value:el) not] thenCollect:collectBlock |
11928 | 3648 |
|
3649 |
" |
|
3650 |
#(1 2 3 4 5 6 7) reject:[:i | i even] thenCollect:[:i | i * 2] |
|
3651 |
" |
|
12944
791a4ec545c2
changed: #reject:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
12943
diff
changeset
|
3652 |
|
791a4ec545c2
changed: #reject:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
12943
diff
changeset
|
3653 |
"Modified: / 11-07-2010 / 17:04:07 / cg" |
11928 | 3654 |
! |
3655 |
||
14861 | 3656 |
reject:selectBlock thenDo:doBlock |
3657 |
"combination of reject followed by do |
|
3658 |
Avoids the creation of intermediate garbage" |
|
3659 |
||
3660 |
self do:[:eachElement | |
|
3661 |
(selectBlock value:eachElement) ifFalse:[ |
|
3662 |
doBlock value:eachElement |
|
3663 |
] |
|
3664 |
]. |
|
3665 |
||
3666 |
" |
|
3667 |
#(1 2 3 4 5 6 7) reject:[:i | i even] thenDo:[:i | Transcript showCR:i] |
|
3668 |
" |
|
3669 |
! |
|
3670 |
||
602 | 3671 |
reverseDo:aBlock |
3672 |
"evaluate the argument, aBlock for each element in reverse order." |
|
3673 |
||
3674 |
"it could be defined in terms of do: - but very inefficient. |
|
3675 |
Better force programmer to define a better version ..." |
|
3676 |
||
3677 |
^ self subclassResponsibility |
|
3678 |
! |
|
3679 |
||
1 | 3680 |
select:aBlock |
3681 |
"return a new collection with all elements from the receiver, for which |
|
5265 | 3682 |
the argument aBlock evaluates to true. |
3683 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
1 | 3684 |
|
15033 | 3685 |
|newCollection| |
3686 |
||
21267 | 3687 |
newCollection := self speciesForAdding new. |
15033 | 3688 |
self do:[:each | |
3689 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
|
3690 |
]. |
|
3691 |
^ newCollection |
|
13002 | 3692 |
|
3693 |
" |
|
20212 | 3694 |
#(1 2 3 4) select:[:e | e odd] |
3695 |
(1 to:10) select:[:e | e even] |
|
22274 | 3696 |
(1 to:10) select:#even |
13002 | 3697 |
" |
3698 |
||
3699 |
"Modified: / 07-08-2010 / 16:26:40 / cg" |
|
21267 | 3700 |
"Modified: / 20-01-2017 / 17:42:33 / stefan" |
22274 | 3701 |
"Modified (comment): / 20-09-2017 / 19:22:25 / stefan" |
13002 | 3702 |
! |
3703 |
||
3704 |
select:aBlock as:aCollectionClass |
|
3705 |
"return a new collection with all elements from the receiver, for which |
|
3706 |
the argument aBlock evaluates to true. |
|
3707 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
3708 |
||
15033 | 3709 |
|newCollection idx| |
3710 |
||
3711 |
aCollectionClass growIsCheap ifTrue:[ |
|
3712 |
newCollection := aCollectionClass new. |
|
3713 |
self do:[:each | |
|
3714 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
|
3715 |
]. |
|
3716 |
] ifFalse:[ |
|
3717 |
newCollection := aCollectionClass new:self size. |
|
3718 |
idx := 1. |
|
3719 |
self do:[:eachElement | |
|
3720 |
(aBlock value:eachElement) ifTrue:[ |
|
3721 |
newCollection at:idx put:eachElement. |
|
3722 |
idx := idx + 1. |
|
3723 |
]. |
|
3724 |
]. |
|
3725 |
newCollection := newCollection copyFrom:1 to:idx-1. |
|
1 | 3726 |
]. |
3727 |
^ newCollection |
|
68 | 3728 |
|
3729 |
" |
|
20151 | 3730 |
#(1 2 3 4) select:[:e | e odd] as:OrderedCollection. |
3731 |
(1 to:10) select:[:e | e even] as:OrderedCollection. |
|
3732 |
||
3733 |
#(1 2 3 4) select:[:e | e odd] as:Set. |
|
3734 |
(1 to:10) select:[:e | e even] as:Set. |
|
3735 |
||
3736 |
#(1 2 3 4) select:[:e | e odd] as:ByteArray. |
|
3737 |
(1 to:10) select:[:e | e even] as:ByteArray. |
|
68 | 3738 |
" |
13002 | 3739 |
|
3740 |
"Created: / 07-08-2010 / 16:26:15 / cg" |
|
68 | 3741 |
! |
3742 |
||
16217 | 3743 |
select:aBlock ifNone:exceptionValue |
68 | 3744 |
"try a new collection with all elements from the receiver, for which |
3745 |
the argument aBlock evaluates to true. If none of the elements passes |
|
16217 | 3746 |
the check of aBlock, return the value from exceptionValue. |
5265 | 3747 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
68 | 3748 |
|
3749 |
|newCollection| |
|
3750 |
||
3751 |
newCollection := self select:aBlock. |
|
16217 | 3752 |
newCollection isEmpty ifTrue:[^ exceptionValue value]. |
68 | 3753 |
^ newCollection |
3754 |
||
3755 |
" |
|
3756 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
21143 | 3757 |
#(1 2 3 4) select:[:e | e > 10] |
68 | 3758 |
" |
12 | 3759 |
! |
3760 |
||
4892 | 3761 |
select:selectBlock thenCollect:collectBlock |
3762 |
"combination of select followed by collect. |
|
3763 |
May be redefined by some subclasses for optimal performance |
|
5844 | 3764 |
(avoiding the creation of intermediate garbage)" |
4892 | 3765 |
|
14991 | 3766 |
"/ We do not do this now, since some classes reimplement select or collect.... |
3767 |
"/ ^ self select:selectBlock thenCollect:collectBlock as:self species |
|
4892 | 3768 |
^ (self select:selectBlock) collect:collectBlock |
3769 |
||
3770 |
" |
|
3771 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2] |
|
3772 |
" |
|
3773 |
! |
|
3774 |
||
14991 | 3775 |
select:selectBlock thenCollect:collectBlock as:aCollectionClass |
21319 | 3776 |
"return a new collection with all elements from the receiver, |
3777 |
for which the argument selectBlock evaluates to true. |
|
19010 | 3778 |
Process the elements through collectBlock before adding. |
15722 | 3779 |
Returns the same as if three separate collect+select+as: messages were sent, |
21319 | 3780 |
but avoids the creation of intermediate collections, |
3781 |
so this is nicer for big collections." |
|
14991 | 3782 |
|
3783 |
|newCollection| |
|
3784 |
||
3785 |
newCollection := aCollectionClass new. |
|
3786 |
self do:[:each | |
|
3787 |
(selectBlock value:each) ifTrue:[newCollection add:(collectBlock value:each)]. |
|
3788 |
]. |
|
3789 |
^ newCollection |
|
3790 |
||
3791 |
" |
|
3792 |
#(1 2 3 4) select:[:e | e odd] thenCollect:[:e| e*e] as:OrderedCollection |
|
3793 |
(1 to:10) select:[:e | e even] thenCollect:[:e| e*e] as:IdentitySet |
|
3794 |
" |
|
3795 |
||
3796 |
"Created: / 07-08-2010 / 16:26:15 / cg" |
|
3797 |
! |
|
3798 |
||
6695 | 3799 |
select:selectBlock thenDo:doBlock |
19459 | 3800 |
"combination of select followed by do. |
19010 | 3801 |
The same as if two separate select:+do: messages were sent, |
20212 | 3802 |
but avoids the creation of intermediate collections, |
19010 | 3803 |
so this is nicer for big collections." |
6695 | 3804 |
|
3805 |
self do:[:eachElement | |
|
3806 |
(selectBlock value:eachElement) ifTrue:[ |
|
3807 |
doBlock value:eachElement |
|
3808 |
] |
|
3809 |
]. |
|
3810 |
||
3811 |
" |
|
3812 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i] |
|
3813 |
" |
|
3814 |
! |
|
3815 |
||
19459 | 3816 |
selectWithIndex:aTwoArgBlock |
20212 | 3817 |
"return a new collection with all elements from the receiver, |
19459 | 3818 |
for which the argument aBlock evaluates to true. |
3819 |
aTwoArgBlock is called with value and index as arguments." |
|
3820 |
||
3821 |
|newCollection| |
|
3822 |
||
3823 |
newCollection := self species new. |
|
3824 |
self doWithIndex:[:eachValue :eachKey | |
|
3825 |
(aTwoArgBlock value:eachValue value:eachKey) ifTrue:[newCollection add:eachValue]. |
|
3826 |
]. |
|
3827 |
^ newCollection |
|
3828 |
||
3829 |
" |
|
20212 | 3830 |
#(10 20 30 40) selectWithIndex:[:e :i | i odd] |
3831 |
#(10 20 30 40) selectWithIndex:[:e :i | i even] |
|
19459 | 3832 |
" |
3833 |
! |
|
3834 |
||
14711 | 3835 |
triplesDo:aThreeArgBlock |
3836 |
"evaluate the argument, aThreeArgBlock for every element in the collection, |
|
362 | 3837 |
which is supposed to consist of 3-element collections. |
3838 |
The block is called with 3 arguments for each collection in the receiver." |
|
3839 |
||
3840 |
self do:[:aTriple | |
|
14711 | 3841 |
aThreeArgBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
362 | 3842 |
] |
3843 |
" |
|
20212 | 3844 |
#( |
3845 |
(1 one eins) |
|
3846 |
(2 two zwei) |
|
3847 |
(3 three drei) |
|
3848 |
(4 four vier) |
|
3849 |
(5 five #'fuenf') |
|
6555 | 3850 |
(6 six sechs) |
20212 | 3851 |
) |
3852 |
triplesDo:[:num :sym1 :sym2 | |
|
6555 | 3853 |
Transcript show:num; space; show:sym1; space; showCR:sym2 |
3854 |
] |
|
362 | 3855 |
" |
2639 | 3856 |
|
3857 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
1 | 3858 |
! |
3859 |
||
14711 | 3860 |
tuplesDo:anNArgBlock |
3861 |
"evaluate the argument, anNArgBlock for every element in the collection, |
|
14325 | 3862 |
which is supposed to consist of N-element collections. |
3863 |
The block is called with N arguments for each collection in the receiver." |
|
3864 |
||
3865 |
self do:[:aTuple | |
|
14711 | 3866 |
anNArgBlock valueWithArguments:aTuple |
14325 | 3867 |
] |
3868 |
" |
|
20151 | 3869 |
#( |
3870 |
(1 one eins uno) |
|
3871 |
(2 two zwei due) |
|
3872 |
(3 three drei tre) |
|
3873 |
(4 four vier quattro) |
|
3874 |
(5 five #'fuenf' cinque) |
|
3875 |
) |
|
3876 |
tuplesDo:[:num :sym1 :sym2 :sym3 | |
|
14325 | 3877 |
Transcript show:num; space; show:sym1; space; show:sym2; space; showCR:sym3 |
3878 |
] |
|
3879 |
" |
|
3880 |
||
3881 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
3882 |
! |
|
3883 |
||
20212 | 3884 |
with:aCollection andDefault:defaultElement collect:aTwoArgBlock |
3885 |
"like with:collect:, but use defaultElement for missing elements in aCollection |
|
14912 | 3886 |
(i.e. if the receiver is longer) |
3887 |
The third argument, aBlock must be a two-argument block, which is |
|
3888 |
evaluated for each element-pair. |
|
3889 |
Collect the results and return a collection containing them. |
|
3890 |
This method fails if neither the receiver nor aCollection is |
|
3891 |
a sequenceable collection (i.e. implements numeric key access). |
|
3892 |
Can be used like zip/zipAll in other languages." |
|
3893 |
||
3894 |
|newCollection| |
|
3895 |
||
3896 |
newCollection := self speciesForAdding new. |
|
3897 |
self with:aCollection andDefault:defaultElement do:[:el1 :el2 | |
|
3898 |
newCollection add:(aTwoArgBlock value:el1 value:el2). |
|
3899 |
]. |
|
3900 |
^ newCollection as:self species |
|
3901 |
||
3902 |
" |
|
20212 | 3903 |
(1 to:3) with:#(one two) andDefault:'xxx' collect:[:num :sym | (num->sym)] |
3904 |
#(1 2 3) with:#(10 20) andDefault:99 collect:[:x :y | (x@y)] |
|
14912 | 3905 |
" |
3906 |
! |
|
3907 |
||
21735 | 3908 |
with:aSequenceableCollection andDefault:defaultElement do:aTwoArgBlock |
14912 | 3909 |
"evaluate the argument, aBlock for successive elements from |
3910 |
each the receiver and the argument, aSequenceableCollection. |
|
3911 |
If the receiver has more elements than the argument, use defaultElement |
|
3912 |
for remaining evaluations. |
|
3913 |
The third argument, aBlock must be a two-argument block. |
|
3914 |
This method fails if neither the receiver nor aCollection is |
|
3915 |
a sequenceable collection (i.e. implements numeric key access)" |
|
3916 |
||
3917 |
|index "{ Class: SmallInteger }"| |
|
3918 |
||
21735 | 3919 |
"this method is redefined in SequenceableCollection, so we know, |
3920 |
that we are not sequenceable. Maybe aCollection is..." |
|
3921 |
aSequenceableCollection isSequenceable ifFalse:[ |
|
3922 |
^ self error:'neither collection is sequenceable'. |
|
3923 |
]. |
|
3924 |
||
14912 | 3925 |
index := 1. |
21735 | 3926 |
self do:[:eachElement | |
3927 |
aTwoArgBlock value:eachElement value:(aSequenceableCollection at:index ifAbsent:defaultElement). |
|
3928 |
index := index + 1 |
|
3929 |
]. |
|
14912 | 3930 |
|
3931 |
" |
|
3932 |
(1 to:3) with:#(one two) andDefault:99 do:[:num :sym | |
|
3933 |
Transcript showCR:(num->sym) |
|
3934 |
] |
|
3935 |
" |
|
21735 | 3936 |
|
3937 |
"Modified (format): / 28-04-2017 / 12:53:20 / stefan" |
|
14912 | 3938 |
! |
3939 |
||
22274 | 3940 |
with:aCollection collect:aTwoArgBlockOrSymbol |
602 | 3941 |
"evaluate the argument, aBlock for successive elements from |
3942 |
each the receiver and the argument, aSequenceableCollection; |
|
3943 |
The second argument, aBlock must be a two-argument block, which is |
|
3944 |
evaluated for each element-pair. |
|
3945 |
Collect the results and return a collection containing them. |
|
3946 |
This method fails if neither the receiver nor aCollection is |
|
14912 | 3947 |
a sequenceable collection (i.e. implements numeric key access). |
3948 |
Can be used like zip/zipAll in other languages." |
|
260 | 3949 |
|
12653 | 3950 |
|newCollection| |
1 | 3951 |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3952 |
newCollection := self speciesForAdding new. |
12653 | 3953 |
self with:aCollection do:[:el1 :el2 | |
22274 | 3954 |
newCollection add:(aTwoArgBlockOrSymbol value:el1 value:el2). |
602 | 3955 |
]. |
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3956 |
^ newCollection as:self species |
201 | 3957 |
|
3958 |
" |
|
20212 | 3959 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
3960 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
|
22274 | 3961 |
#(1 2 3) with:#(10 20 30) collect:#@ |
3962 |
" |
|
3963 |
||
3964 |
"Modified (comment): / 20-09-2017 / 18:20:56 / stefan" |
|
12653 | 3965 |
! |
3966 |
||
16075 | 3967 |
with:aCollection conform:aTwoArgBlock |
3968 |
"evaluate the argument, aBlock for successive elements from |
|
3969 |
each the receiver and the argument, aCollection. |
|
3970 |
Return true, if the block returns true for all of these pairs. |
|
3971 |
This method fails if neither the receiver nor aCollection is |
|
3972 |
a sequenceable collection (i.e. implements numeric key access)." |
|
3973 |
||
3974 |
self with:aCollection do:[:a :b | |
|
3975 |
(aTwoArgBlock value:a value:b) ifFalse:[^ false]. |
|
3976 |
]. |
|
3977 |
^ true. |
|
3978 |
||
3979 |
" |
|
3980 |
(1 to:3) with:#(1 2 3 4) conform:[:a :b | a = b] --- raises an error |
|
20212 | 3981 |
(1 to:3) with:#(1 22 3) conform:[:a :b | a = b] |
3982 |
(1 to:3) with:#(1 2 3) conform:[:a :b | a = b] |
|
22274 | 3983 |
(1 to:3) with:#(1 2 3) conform:#= |
3984 |
(1 to:3) with:#(1.0 2.0 3.0) conform:#= |
|
3985 |
(1 to:3) with:#(1.0 2.0 3.0) conform:#== |
|
21490 | 3986 |
(1 to:3) with:#('1' '2' '3') conform:[:a :b | a asString = b asString] |
3987 |
" |
|
3988 |
||
3989 |
"Modified (comment): / 19-02-2017 / 18:30:08 / cg" |
|
22274 | 3990 |
"Modified (comment): / 20-09-2017 / 19:20:05 / stefan" |
16075 | 3991 |
! |
3992 |
||
13464 | 3993 |
with:aCollection contains:aTwoArgBlock |
3994 |
"evaluate the argument, aBlock for successive elements from |
|
3995 |
each the receiver and the argument, aCollection. |
|
3996 |
Return true, if the block returns true for any of these pairs. |
|
3997 |
This method fails if neither the receiver nor aCollection is |
|
3998 |
a sequenceable collection (i.e. implements numeric key access)." |
|
3999 |
||
4000 |
self with:aCollection do:[:a :b | |
|
4001 |
(aTwoArgBlock value:a value:b) ifTrue:[^ true]. |
|
4002 |
]. |
|
4003 |
^ false. |
|
4004 |
||
4005 |
" |
|
4006 |
(1 to:3) with:#(1 2 3 4) contains:[:a :b | a ~= b] --- raises an error |
|
4007 |
(1 to:3) with:#(1 22 3) contains:[:a :b | a ~= b] |
|
4008 |
(1 to:3) with:#(1 2 3) contains:[:a :b | a ~= b] |
|
22274 | 4009 |
(1 to:3) with:#(1 2 4) contains:#~= |
13464 | 4010 |
" |
4011 |
||
4012 |
"Created: / 30-06-2011 / 12:37:41 / cg" |
|
22274 | 4013 |
"Modified (comment): / 20-09-2017 / 19:21:31 / stefan" |
13464 | 4014 |
! |
4015 |
||
12653 | 4016 |
with:aCollection count:aTwoArgBlock |
4017 |
"evaluate the argument, aBlock for successive elements from |
|
4018 |
each the receiver and the argument, aSequenceableCollection. |
|
4019 |
Count, how often the second argument, aTwoArgBlock returns true. |
|
4020 |
This method fails if neither the receiver nor aCollection is |
|
4021 |
a sequenceable collection (i.e. implements numeric key access)." |
|
4022 |
||
4023 |
|count "{ Class: SmallInteger }"| |
|
4024 |
||
4025 |
count := 0. |
|
4026 |
self with:aCollection do:[:el1 :el2 | |
|
4027 |
(aTwoArgBlock value:el1 value:el2) ifTrue:[ |
|
4028 |
count := count + 1 |
|
4029 |
] |
|
4030 |
]. |
|
4031 |
^ count |
|
4032 |
||
4033 |
" |
|
4034 |
(1 to:3) with:#(1 3 3) count:[:n1 :n2 | n1 = n2] |
|
22274 | 4035 |
(1 to:3) with:#(1 3 3) count:#= |
4036 |
" |
|
4037 |
||
4038 |
"Modified (comment): / 20-09-2017 / 19:21:59 / stefan" |
|
2 | 4039 |
! |
4040 |
||
602 | 4041 |
with:aCollection do:aTwoArgBlock |
4042 |
"evaluate the argument, aBlock for successive elements from |
|
4043 |
each the receiver and the argument, aSequenceableCollection. |
|
4044 |
The second argument, aBlock must be a two-argument block. |
|
4045 |
This method fails if neither the receiver nor aCollection is |
|
13468
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
4046 |
a sequenceable collection (i.e. implements numeric key access) |
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
4047 |
or (new!!) if the sizes are different." |
602 | 4048 |
|
4049 |
|index "{ Class: SmallInteger }"| |
|
2 | 4050 |
|
13464 | 4051 |
aCollection size == self size ifFalse:[ |
13468
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
4052 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
13464 | 4053 |
]. |
4054 |
||
602 | 4055 |
index := 1. |
4056 |
aCollection isSequenceable ifFalse:[ |
|
6135 | 4057 |
self isSequenceable ifFalse:[ |
4058 |
^ self error:'neither collection is sequenceable'. |
|
4059 |
]. |
|
4060 |
aCollection do:[:element | |
|
4061 |
aTwoArgBlock value:(self at:index) value:element. |
|
4062 |
index := index + 1 |
|
4063 |
] |
|
602 | 4064 |
] ifTrue:[ |
6135 | 4065 |
self do:[:element | |
4066 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
4067 |
index := index + 1 |
|
4068 |
] |
|
602 | 4069 |
] |
201 | 4070 |
|
4071 |
" |
|
602 | 4072 |
(1 to:3) with:#(one two three) do:[:num :sym | |
6135 | 4073 |
Transcript showCR:(num->sym) |
602 | 4074 |
] |
14066 | 4075 |
|
4076 |
the following fail because sets do not have ordered elements |
|
4077 |
||
602 | 4078 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
6135 | 4079 |
Transcript showCR:(num->sym) |
602 | 4080 |
]. |
4081 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
6135 | 4082 |
Transcript showCR:(num->sym) |
602 | 4083 |
] |
201 | 4084 |
" |
13464 | 4085 |
|
13468
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
4086 |
"Modified: / 30-06-2011 / 17:39:47 / cg" |
14066 | 4087 |
"Modified (comment): / 18-03-2012 / 15:16:50 / cg" |
4088 |
! |
|
4089 |
||
4090 |
with:aCollection reverseDo:aTwoArgBlock |
|
4091 |
"evaluate the argument, aBlock in reverse order for successive elements from |
|
4092 |
each the receiver and the argument, aSequenceableCollection. |
|
4093 |
The second argument, aBlock must be a two-argument block. |
|
4094 |
This method fails if neither the receiver nor aCollection is |
|
4095 |
a sequenceable collection (i.e. implements numeric key access) |
|
4096 |
or (new!!) if the sizes are different." |
|
4097 |
||
4098 |
|index "{ Class: SmallInteger }"| |
|
4099 |
||
4100 |
aCollection size == self size ifFalse:[ |
|
4101 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
|
4102 |
]. |
|
4103 |
||
4104 |
index := self size. |
|
4105 |
aCollection isSequenceable ifFalse:[ |
|
4106 |
self isSequenceable ifFalse:[ |
|
4107 |
^ self error:'neither collection is sequenceable'. |
|
4108 |
]. |
|
4109 |
aCollection reverseDo:[:element | |
|
4110 |
aTwoArgBlock value:(self at:index) value:element. |
|
4111 |
index := index - 1 |
|
4112 |
] |
|
4113 |
] ifTrue:[ |
|
4114 |
self reverseDo:[:element | |
|
4115 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
4116 |
index := index - 1 |
|
4117 |
] |
|
4118 |
] |
|
4119 |
||
4120 |
" |
|
4121 |
(1 to:3) with:#(one two three) reverseDo:[:num :sym | |
|
4122 |
Transcript showCR:(num->sym) |
|
4123 |
] |
|
4124 |
||
4125 |
the following fail because sets do not have ordered elements |
|
4126 |
||
4127 |
(1 to:3) with:#(one two three) asSet reverseDo:[:num :sym | |
|
4128 |
Transcript showCR:(num->sym) |
|
4129 |
]. |
|
4130 |
(1 to:3) asSet with:#(one two three) reverseDo:[:num :sym | |
|
4131 |
Transcript showCR:(num->sym) |
|
4132 |
] |
|
4133 |
" |
|
4134 |
||
4135 |
"Modified: / 30-06-2011 / 17:39:47 / cg" |
|
4136 |
"Created: / 18-03-2012 / 15:15:04 / cg" |
|
17387 | 4137 |
! |
4138 |
||
4139 |
withIndexCollect:aTwoArgBlock |
|
4140 |
"same as keysAndValuesCollect:, but with argument order reversed" |
|
4141 |
||
4142 |
|newCollection| |
|
4143 |
||
18507 | 4144 |
newCollection := self speciesForCollecting new. |
17387 | 4145 |
self keysAndValuesDo:[:key :value | |
4146 |
newCollection add:(aTwoArgBlock value:value value:key) |
|
4147 |
]. |
|
4148 |
^ newCollection |
|
4149 |
||
4150 |
" |
|
4151 |
#(one two three) withIndexCollect:[:sym :num | (num->sym)] |
|
4152 |
#(10 20 30) withIndexCollect:[:n :i | n*i ] |
|
4153 |
" |
|
4154 |
! |
|
4155 |
||
4156 |
withIndexDo:aTwoArgBlock |
|
4157 |
"evaluate the argument, aBlock for every element in the collection, |
|
4158 |
passing both element and index as arguments. |
|
4159 |
Same as doWithIndex:, due to parallel evolution of different Smalltalk dialects" |
|
4160 |
||
4161 |
^ self keysAndValuesDo:[:key :index | |
|
4162 |
aTwoArgBlock value:index value:key |
|
4163 |
]. |
|
1 | 4164 |
! ! |
4165 |
||
5874 | 4166 |
!Collection methodsFor:'enumerating-tests'! |
4167 |
||
4168 |
allSatisfy:aBlock |
|
4169 |
"evaluate aBlock for each of the receiver's elements. |
|
4170 |
Return true, if aBlock returns true for all elements, false otherwise |
|
4171 |
(i.e. false if any element fails to satisfy the block-condition). |
|
4172 |
This is an ANSI renomer of #conform:" |
|
4173 |
||
4174 |
^ self conform:aBlock. |
|
4175 |
||
4176 |
" |
|
4177 |
#(1 2 3 4 5) allSatisfy:[:el | el odd] |
|
4178 |
#(2 4 6 8 10) allSatisfy:[:el | el odd] |
|
4179 |
#(2 4 6 8 10) allSatisfy:[:el | el even] |
|
4180 |
" |
|
4181 |
||
4182 |
! |
|
4183 |
||
4184 |
anySatisfy:aBlock |
|
4185 |
"evaluate aBlock for each of the receiver's elements. |
|
4186 |
Return true, if aBlock ever returns true, false otherwise |
|
4187 |
(i.e. if any element satisfies the block-condition). |
|
18567 | 4188 |
This is an ANSI renomer of #contains: |
4189 |
(which is a better name, because #contains: is often misread as #includes by beginners)" |
|
5874 | 4190 |
|
4191 |
^ self contains:aBlock. |
|
4192 |
||
4193 |
" |
|
4194 |
#(1 2 3 4 5) anySatisfy:[:el | el odd] |
|
4195 |
#(2 4 6 8 10) anySatisfy:[:el | el odd] |
|
4196 |
" |
|
4197 |
! |
|
4198 |
||
9842 | 4199 |
conform:aOneArgBlock |
5874 | 4200 |
"return true, if every element conforms to some condition. |
4201 |
I.e. return false, if aBlock returns false for any element; |
|
15751 | 4202 |
true otherwise. Returns true for empty receivers." |
5874 | 4203 |
|
9842 | 4204 |
self do:[:element | |
4205 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
4206 |
]. |
|
5874 | 4207 |
^ true |
9842 | 4208 |
|
4209 |
" |
|
4210 |
#(1 2 3 4 5) conform:[:el | el even] |
|
4211 |
#(2 4 6 8 10) conform:[:el | el even] |
|
15751 | 4212 |
#() conform:[:el | el even] |
9842 | 4213 |
" |
4214 |
||
4215 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
5874 | 4216 |
! |
4217 |
||
9842 | 4218 |
contains:aOneArgBlock |
15378 | 4219 |
"evaluate aOneArgBlock for each of the receiver's elements |
4220 |
Return true and skip remaining elements, if aBlock ever returns true, |
|
18567 | 4221 |
otherwise return false. |
4222 |
(#anySatisfy: is a better name, because #contains: is often misread as #includes by beginners)" |
|
5874 | 4223 |
|
9842 | 4224 |
self do:[:element | |
4225 |
(aOneArgBlock value:element) ifTrue:[^ true] |
|
4226 |
]. |
|
4227 |
^ false |
|
5874 | 4228 |
|
4229 |
" |
|
4230 |
#(1 2 3 4 5) contains:[:el | el odd] |
|
4231 |
#(2 4 6 8 10) contains:[:el | el odd] |
|
4232 |
" |
|
4233 |
||
9842 | 4234 |
"Modified: / 13-09-2006 / 11:18:36 / cg" |
8577 | 4235 |
! |
4236 |
||
4237 |
noneSatisfy:aBlock |
|
20151 | 4238 |
"evaluate aBlock for each of the receiver's elements. |
8577 | 4239 |
Return true, if aBlock returns false for all elements, false otherwise |
4240 |
(i.e. false if any element satisfies the block-condition)." |
|
4241 |
||
9843 | 4242 |
^ (self contains:aBlock) not |
8577 | 4243 |
|
4244 |
" |
|
20151 | 4245 |
#(1 2 3 4 5) noneSatisfy:[:el | el odd] |
4246 |
#(2 4 6 8 10) noneSatisfy:[:el | el odd] |
|
4247 |
#(2 4 6 8 10) noneSatisfy:[:el | el even] |
|
8577 | 4248 |
" |
9843 | 4249 |
|
4250 |
"Modified: / 13-09-2006 / 11:19:57 / cg" |
|
5874 | 4251 |
! ! |
4252 |
||
602 | 4253 |
!Collection methodsFor:'error handling'! |
4254 |
||
20151 | 4255 |
emptyCheck |
17266 | 4256 |
"check if the receiver is empty; report an error if so" |
602 | 4257 |
|
11475
f7e1a355df17
changed #emptyCheck (is isEmpty instead of size == 0)
Claus Gittinger <cg@exept.de>
parents:
11470
diff
changeset
|
4258 |
self isEmpty ifTrue:[ |
6135 | 4259 |
^ self emptyCollectionError. |
602 | 4260 |
]. |
4261 |
! |
|
4262 |
||
4263 |
emptyCollectionError |
|
14538 | 4264 |
"report an error that the operation is not allowed for empty collections" |
4265 |
||
4266 |
<resource: #skipInDebuggersWalkBack> |
|
602 | 4267 |
|
4268 |
^ EmptyCollectionSignal raise |
|
4269 |
! |
|
4270 |
||
4271 |
errorInvalidKey:aKey |
|
4272 |
"report an error that the given key was invalid" |
|
4273 |
||
14538 | 4274 |
<resource: #skipInDebuggersWalkBack> |
4275 |
||
602 | 4276 |
^ InvalidKeySignal raiseRequestWith:aKey |
4277 |
! |
|
4278 |
||
4279 |
errorNotKeyed |
|
4280 |
"report an error that keyed access methods are not allowed" |
|
4281 |
||
14538 | 4282 |
<resource: #skipInDebuggersWalkBack> |
4283 |
||
602 | 4284 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
4285 |
! |
|
4286 |
||
4287 |
errorValueNotFound:anObject |
|
4288 |
"report an error that an object was not found in the collection" |
|
4289 |
||
14538 | 4290 |
<resource: #skipInDebuggersWalkBack> |
4291 |
||
6757 | 4292 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
3075 | 4293 |
|
4294 |
"Modified: / 30.10.1997 / 15:52:18 / cg" |
|
602 | 4295 |
! |
4296 |
||
14022
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4297 |
noModificationError |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4298 |
"a store is attempted into an immutable collection (typically: a literal). |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4299 |
For our convenience, find the method that contains me, for a nicer error message" |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4300 |
|
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4301 |
|creator msg context| |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4302 |
|
20211 | 4303 |
creator := Method allSubInstances |
4304 |
detect:[:aMethod | (aMethod referencesGlobal:self)] |
|
14538 | 4305 |
ifNone:nil. |
20211 | 4306 |
creator isNil ifTrue:[ |
4307 |
creator := Method allSubInstances |
|
4308 |
detect:[:aMethod | (aMethod literalsDetect:[:l | l == self] ifNone:nil) notNil] |
|
4309 |
ifNone:nil. |
|
16801 | 4310 |
]. |
14022
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4311 |
creator notNil ifTrue:[ |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4312 |
msg := ' (' , creator whoString , ')' |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4313 |
]. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4314 |
context := thisContext sender. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4315 |
" |
20211 | 4316 |
this error is reported on an attempt to store into an immutable collection (typically: a literal). |
14022
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4317 |
The literal was created in creator. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4318 |
If you press continue in the debugger, the store will be performed. |
20211 | 4319 |
If you do not want this, press abort and check your code. |
14022
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4320 |
Storing into literals is VERY VERY bad coding style. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4321 |
" |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4322 |
NoModificationError |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4323 |
raiseRequestWith:self |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4324 |
errorString:msg |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4325 |
in:context. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4326 |
|
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4327 |
"Created: / 03-08-1998 / 14:47:45 / cg" |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4328 |
! |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
4329 |
|
602 | 4330 |
notEnoughElementsError |
20151 | 4331 |
"report an error that the operation is not allowed, |
602 | 4332 |
since not enough elements are in the collection" |
4333 |
||
14538 | 4334 |
<resource: #skipInDebuggersWalkBack> |
4335 |
||
602 | 4336 |
^ NotEnoughElementsSignal raise |
4337 |
! ! |
|
4338 |
||
4339 |
!Collection methodsFor:'growing'! |
|
4340 |
||
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4341 |
changeCapacityTo:newSize |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4342 |
newSize > self capacity ifTrue:[ |
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
4343 |
self grow:newSize |
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4344 |
]. |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4345 |
|
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4346 |
"Created: / 30.10.2001 / 17:56:50 / cg" |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4347 |
! |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
4348 |
|
602 | 4349 |
grow |
4350 |
"make the receiver larger" |
|
4351 |
||
4352 |
self grow:(self size + self growSize) |
|
4353 |
! |
|
4354 |
||
4355 |
grow:howBig |
|
14614 | 4356 |
"change the receiver's size" |
602 | 4357 |
|
4358 |
^ self subclassResponsibility |
|
4359 |
! |
|
4360 |
||
4361 |
growSize |
|
4362 |
"return a suitable size increment for growing. |
|
4363 |
The default returned here may be (and is) redefined in subclasses." |
|
4364 |
||
4365 |
^ self size max:2 |
|
4366 |
! ! |
|
4367 |
||
14401
bdfdcb8d0386
added: #addAll:withOccurrences:
Claus Gittinger <cg@exept.de>
parents:
14325
diff
changeset
|
4368 |
|
1 | 4369 |
!Collection methodsFor:'printing & storing'! |
4370 |
||
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4371 |
displayOn:aGCOrStream |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4372 |
"print a representation of the receiver on aGCOrStream for display in inspectors etc." |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4373 |
|
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4374 |
|noneYet total limit| |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4375 |
|
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4376 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21542 | 4377 |
"/ old ST80 means: draw-yourself on a GC. |
16743 | 4378 |
(aGCOrStream isStream) ifFalse:[ |
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4379 |
^ super displayOn:aGCOrStream |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4380 |
]. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4381 |
|
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4382 |
aGCOrStream nextPutAll:self displayStringName; nextPut:$(. |
602 | 4383 |
|
4384 |
thisContext isRecursive ifTrue:[ |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4385 |
'Collection [error]: displayString of self referencing collection.' errorPrintCR. |
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4386 |
aGCOrStream nextPutAll:'"recursive")'. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4387 |
^ self. |
602 | 4388 |
]. |
4389 |
||
4390 |
noneYet := true. |
|
4391 |
total := 0. |
|
4392 |
limit := self maxPrint. |
|
4393 |
||
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4394 |
self printElementsDo: [:element | |
1413 | 4395 |
noneYet ifTrue:[ |
4396 |
noneYet := false. |
|
4397 |
] ifFalse:[ |
|
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4398 |
aGCOrStream space |
1413 | 4399 |
]. |
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4400 |
element displayOn:aGCOrStream. |
1413 | 4401 |
total := total + 1. |
4402 |
(total > limit) ifTrue:[ |
|
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4403 |
aGCOrStream nextPutAll:'... )'. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4404 |
^ self |
1413 | 4405 |
] |
602 | 4406 |
]. |
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
4407 |
aGCOrStream nextPutAll:')'. |
1413 | 4408 |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4409 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4410 |
#(1 2 3 'hello' $a) asOrderedCollection displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4411 |
|
20151 | 4412 |
(Dictionary new at:#hello put:'world'; |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4413 |
at:#foo put:'bar'; yourself) displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4414 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4415 |
|
21542 | 4416 |
"Modified: / 20-01-1998 / 14:11:03 / stefan" |
4417 |
"Modified: / 02-02-1999 / 22:39:44 / cg" |
|
4418 |
"Modified (format): / 22-02-2017 / 16:46:32 / cg" |
|
3964
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4419 |
! |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4420 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4421 |
displayStringName |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4422 |
"redefinable helper for displayString" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4423 |
|
14291
32d444cbde13
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
14227
diff
changeset
|
4424 |
^ self class name |
3964
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4425 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
4426 |
"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
|
4427 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4428 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4429 |
maxPrint |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4430 |
"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
|
4431 |
than the limit defined here." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4432 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4433 |
^ 5000 |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4434 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4435 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4436 |
printElementsDo:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4437 |
"perform aBlock (1 arg) for all elements. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4438 |
Used in #printOn:. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4439 |
Subclasses (e.g. Dictionary) may redefine this." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4440 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4441 |
^ self do:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4442 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4443 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4444 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4445 |
|
11599 | 4446 |
printElementsOn:aStream |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4447 |
"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
|
4448 |
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
|
4449 |
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
|
4450 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4451 |
|
3402 | 4452 |
|limit firstOne s| |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4453 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4454 |
thisContext isRecursive ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4455 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4456 |
aStream nextPutAll:'#("recursive")'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4457 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4458 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4459 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4460 |
aStream nextPut:$(. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4461 |
firstOne := true. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4462 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4463 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4464 |
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
|
4465 |
(needed for limit calculation) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4466 |
" |
18211 | 4467 |
limit := self maxPrint. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4468 |
aStream isPositionable ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4469 |
s := aStream. |
18211 | 4470 |
limit := s position + limit. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4471 |
] ifFalse:[ |
18270 | 4472 |
s := CharacterWriteStream new:50. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4473 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4474 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4475 |
self printElementsDo:[:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4476 |
firstOne ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4477 |
s space |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4478 |
] ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4479 |
firstOne := false |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4480 |
]. |
15612 | 4481 |
(s position >= limit) ifTrue:[ |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4482 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4483 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4484 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4485 |
aStream nextPutAll:'...etc...)'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4486 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4487 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4488 |
element printOn:s. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4489 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4490 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4491 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4492 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4493 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4494 |
aStream nextPut:$) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4495 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4496 |
" |
22321 | 4497 |
#(1 2 3 'hello' $a $ü) printOn:Transcript |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4498 |
(Array new:100000) printOn:Transcript |
20151 | 4499 |
(Array new:100000) printOn:Stdout |
4500 |
(Array new:100000) printString size |
|
4501 |
(Dictionary new at:#hello put:'world'; |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4502 |
at:#foo put:'bar'; yourself) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4503 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4504 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4505 |
|a| |
20151 | 4506 |
a := Array new:3. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4507 |
a at:2 put:a. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4508 |
a printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4509 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4510 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4511 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
4512 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
602 | 4513 |
! |
4514 |
||
11599 | 4515 |
printOn:aStream |
4516 |
"append a user readable representation of the receiver to aStream. |
|
4517 |
The text appended is not meant to be read back for reconstruction of |
|
4518 |
the receiver. Also, this method limits the size of generated string. |
|
4519 |
" |
|
4520 |
||
4521 |
thisContext isRecursive ifTrue:[ |
|
4522 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
|
4523 |
aStream nextPutAll:'#("recursive")'. |
|
4524 |
^ self |
|
4525 |
]. |
|
4526 |
||
4527 |
aStream nextPutAll:self class name. |
|
4528 |
self printElementsOn:aStream. |
|
4529 |
||
4530 |
" |
|
4531 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
4532 |
(Array new:100000) printOn:Transcript |
|
20211 | 4533 |
(Array new:100000) printOn:Stdout |
4534 |
(Array new:100000) printString size |
|
4535 |
(Dictionary new at:#hello put:'world'; |
|
11599 | 4536 |
at:#foo put:'bar'; yourself) printOn:Transcript |
4537 |
" |
|
4538 |
" |
|
20211 | 4539 |
|a| |
4540 |
a := Array new:3. |
|
11599 | 4541 |
a at:2 put:a. |
4542 |
a printOn:Transcript |
|
4543 |
" |
|
4544 |
||
4545 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
|
4546 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
|
4547 |
! |
|
4548 |
||
1 | 4549 |
storeOn:aStream |
88 | 4550 |
"output a printed representation onto the argument, aStream. |
4551 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
4552 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
4553 |
(use storeBinaryOn: to handle those)." |
|
1 | 4554 |
|
5254 | 4555 |
|notEmpty| |
1 | 4556 |
|
44 | 4557 |
thisContext isRecursive ifTrue:[ |
15856 | 4558 |
RecursiveStoreError raiseRequestWith:self. |
2289 | 4559 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 4560 |
aStream nextPutAll:'#recursive'. |
4561 |
^ self |
|
44 | 4562 |
]. |
4563 |
||
4564 |
aStream nextPutAll:'('. |
|
1 | 4565 |
aStream nextPutAll:(self class name). |
44 | 4566 |
aStream nextPutAll:' new'. |
5254 | 4567 |
notEmpty := false. |
1 | 4568 |
self do:[:element | |
1413 | 4569 |
aStream nextPutAll:' add:'. |
4570 |
element storeOn:aStream. |
|
4571 |
aStream nextPutAll:';'. |
|
5254 | 4572 |
notEmpty := true |
1 | 4573 |
]. |
5254 | 4574 |
notEmpty ifTrue:[aStream nextPutAll:' yourself']. |
1 | 4575 |
aStream nextPut:$) |
44 | 4576 |
|
88 | 4577 |
" |
4578 |
OrderedCollection new storeOn:Transcript |
|
4579 |
(1 to:10) storeOn:Transcript |
|
4580 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
4581 |
" |
|
293 | 4582 |
" |
4583 |
|s| |
|
4584 |
||
4585 |
s := Set new. |
|
4586 |
s add:1; add:'hello'; add:s. |
|
4587 |
s storeOn:Transcript |
|
4588 |
" |
|
1413 | 4589 |
|
5254 | 4590 |
"Modified: / 11.2.2000 / 11:24:56 / cg" |
1 | 4591 |
! ! |
602 | 4592 |
|
4593 |
!Collection methodsFor:'queries'! |
|
4594 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
4595 |
defaultElement |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
4596 |
^ nil |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
4597 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
4598 |
|
16778 | 4599 |
isReadOnly |
19459 | 4600 |
"true if this is a readOnly (immutable) collection. |
4601 |
Q1: should this be called isImmutable? |
|
4602 |
Q2: who uses this?" |
|
4603 |
||
16778 | 4604 |
^ false |
4605 |
! |
|
4606 |
||
20302 | 4607 |
isValidElement:anObject |
4608 |
"return true, if I can hold this kind of object" |
|
4609 |
||
4610 |
"/ here, true is returned for any. |
|
4611 |
"/ nust be redefined in subclasses which do nt allow some (i.e. ByteArray) |
|
4612 |
^ true |
|
4613 |
! |
|
4614 |
||
16778 | 4615 |
isWritable |
19459 | 4616 |
"true if this is not a readOnly (immutable) collection. |
4617 |
Q1: should this be called isMutable? |
|
4618 |
Q2: who uses this?" |
|
4619 |
||
16778 | 4620 |
^ self isReadOnly not |
4621 |
! |
|
4622 |
||
15999 | 4623 |
size |
4624 |
"return the number of elements in the receiver. |
|
4625 |
This is usually redefined in subclasses for more performance." |
|
4626 |
||
4627 |
|count "{ Class: SmallInteger }" | |
|
4628 |
||
4629 |
count := 0. |
|
4630 |
self do:[:element | |
|
4631 |
count := count + 1 |
|
4632 |
]. |
|
4633 |
^ count |
|
4634 |
! |
|
4635 |
||
4636 |
speciesForAdding |
|
4637 |
"like species, but redefined for collections which cannot grow easily. |
|
4638 |
Used by functions which create a growing collection (see collect:with:, for example)" |
|
4639 |
||
4640 |
^ self species |
|
18507 | 4641 |
! |
4642 |
||
4643 |
speciesForCollecting |
|
4644 |
"like species, but used when doing collect operations. |
|
4645 |
Redefined for collections which return a different classes object when doing collect." |
|
4646 |
||
4647 |
^ self speciesForAdding |
|
15999 | 4648 |
! ! |
4649 |
||
4650 |
!Collection methodsFor:'searching'! |
|
4651 |
||
4652 |
findFirst:aBlock |
|
4653 |
"find the index of the first element, for which evaluation of the argument, aBlock |
|
4654 |
returns true; return its index or 0 if none detected. |
|
4655 |
This is much like #detect, however, here an INDEX is returned, |
|
4656 |
while #detect returns the element." |
|
4657 |
||
4658 |
^ self findFirst:aBlock ifNone:0 |
|
4659 |
||
4660 |
" |
|
4661 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
|
4662 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
4663 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
4664 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
4665 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
|
4666 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
4667 |
" |
|
4668 |
||
4669 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
4670 |
! |
|
4671 |
||
4672 |
findFirst:aBlock ifNone:exceptionValue |
|
4673 |
"find the index of the first element, for which evaluation of the argument, aBlock returns true; |
|
4674 |
return its index or the value from exceptionValue if none detected. |
|
4675 |
This is much like #detect:ifNone:, however, here an INDEX is returned, |
|
4676 |
while #detect:ifNone: returns the element." |
|
4677 |
||
4678 |
self subclassResponsibility |
|
4679 |
||
4680 |
" |
|
4681 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:99 |
|
4682 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 9)] ifNone:99 |
|
4683 |
" |
|
4684 |
! |
|
4685 |
||
17216 | 4686 |
findLast:aBlock |
4687 |
"find the last element, for which evaluation of the argument, aBlock returns true. |
|
4688 |
Return its index or 0 if none detected." |
|
4689 |
||
4690 |
^ self findLast:aBlock ifNone:0 |
|
4691 |
||
4692 |
" |
|
4693 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
|
4694 |
'one.two.three' findLast:[:c | (c == $.)] |
|
4695 |
" |
|
4696 |
! |
|
4697 |
||
4698 |
findLast:aBlock ifNone:exceptionValue |
|
4699 |
"find the index of the last element, for which evaluation of the argument, aBlock returns true. |
|
4700 |
Return its index or the value from exceptionValue if none detected." |
|
4701 |
||
4702 |
self subclassResponsibility |
|
4703 |
||
4704 |
" |
|
4705 |
#(1 2 3 4 5 6) findLast:[:x | (x >= 3)] ifNone:99 |
|
4706 |
#(1 2 3 4 5 6) findLast:[:x | (x >= 9)] ifNone:99 |
|
4707 |
" |
|
4708 |
! |
|
4709 |
||
15999 | 4710 |
keysOfLargest:n |
4711 |
"return the keys (aka indices) of the n largest elements, key of largest last. |
|
4712 |
Raises an exception, if the receiver does not contain at least n elements" |
|
4713 |
||
4714 |
|mySize loopAction actionForFirstN nLargest minInLargest | |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4715 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4716 |
mySize := self size. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4717 |
n > mySize ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4718 |
self notEnoughElementsError |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4719 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4720 |
|
15999 | 4721 |
"/ for big collections, it seems to be better to sort only once |
4722 |
"/ (many individual insert operations into a sortedCollection are expensive) |
|
4723 |
"/ Consider using a tree-oriented collection if this becomes a problem |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4724 |
(n < 50) ifTrue:[ |
15999 | 4725 |
n == 1 ifTrue:[ |
4726 |
|max kMax| |
|
4727 |
||
4728 |
loopAction := [:k :el | |
|
4729 |
max := el. kMax := k. |
|
4730 |
loopAction := |
|
4731 |
[:k :el | |
|
4732 |
el > max ifTrue:[ |
|
4733 |
max := el. kMax := k |
|
4734 |
]. |
|
4735 |
] |
|
4736 |
]. |
|
4737 |
self keysAndValuesDo:[:k :el | loopAction value:k value:el ]. |
|
4738 |
kMax isNil ifTrue:[ self notEnoughElementsError ]. |
|
4739 |
^ Array with:kMax. |
|
4740 |
]. |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4741 |
n == 2 ifTrue:[ |
15999 | 4742 |
|max1 kMax1 max2 kMax2| |
4743 |
||
4744 |
loopAction := [:k :el | |
|
4745 |
max1 := el. kMax1 := k. |
|
4746 |
loopAction := |
|
4747 |
[:k :el | |
|
4748 |
el > max1 ifTrue:[ |
|
4749 |
max2 := max1. kMax2 := kMax1. |
|
4750 |
max1 := el. kMax1 := k |
|
4751 |
] ifFalse:[ |
|
4752 |
max2 := el. kMax2 := k |
|
4753 |
]. |
|
4754 |
loopAction := |
|
4755 |
[:k :el | |
|
4756 |
el > max2 ifTrue:[ |
|
4757 |
el > max1 ifTrue:[ |
|
4758 |
max2 := max1. max1 := el. |
|
4759 |
kMax2 := kMax1. kMax1 := k. |
|
4760 |
] ifFalse:[ |
|
4761 |
max2 := el. |
|
4762 |
kMax2 := k. |
|
4763 |
]. |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4764 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4765 |
] |
15999 | 4766 |
] |
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4767 |
]. |
15999 | 4768 |
|
4769 |
self keysAndValuesDo:[:k :el | loopAction value:k value:el ]. |
|
4770 |
kMax2 isNil ifTrue:[ self notEnoughElementsError ]. |
|
4771 |
^ Array with:kMax2 with:kMax1 |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4772 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4773 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4774 |
nLargest := SortedCollection new:n. |
15999 | 4775 |
nLargest setSortBlock:[:a :b | a key < b key]. |
4776 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4777 |
actionForFirstN := |
15999 | 4778 |
[:k :el | |
4779 |
nLargest add:(el -> k). |
|
4780 |
nLargest size == n ifTrue:[ |
|
4781 |
minInLargest := nLargest min key. |
|
4782 |
loopAction := |
|
4783 |
[:k :el | |
|
4784 |
el > minInLargest ifTrue:[ |
|
4785 |
nLargest removeFirst. |
|
4786 |
nLargest add:(el -> k). |
|
4787 |
minInLargest := nLargest min key |
|
4788 |
]. |
|
4789 |
] |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4790 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4791 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4792 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4793 |
loopAction := actionForFirstN. |
15999 | 4794 |
self keysAndValuesDo:[:k :el | loopAction value:k value:el ]. |
4795 |
^ nLargest collect:[:a | a value]. |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4796 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4797 |
|
15999 | 4798 |
^ (self keys asNewOrderedCollection |
4799 |
sortWith:self asNewOrderedCollection) |
|
4800 |
copyFrom:(self size-n+1) |
|
4801 |
||
4802 |
" |
|
4803 |
#(10 35 20 45 30 5) keysOfLargest:1 |
|
4804 |
#(10 35 20 45 30 5) keysOfLargest:2 |
|
4805 |
#(10 35 20 45 30 5) largest:2 |
|
4806 |
#(10 35 20 45 30 5) keysOfLargest:3 |
|
4807 |
#(10 35 20 45 30 5) keysOfLargest:5 |
|
4808 |
#(10 35 20 45 30 5) keysOfLargest:6 |
|
4809 |
#(10 35 20 45 30 5) keysOfLargest:8 |
|
4810 |
(1 to: 1000) asArray shuffled keysOfLargest:51 |
|
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4811 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4812 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4813 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4814 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4815 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4816 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4817 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4818 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4819 |
data asSortedCollection at:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4820 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4821 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4822 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4823 |
40 timesRepeat:[ |
15999 | 4824 |
data keysOfLargest:3 |
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4825 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4826 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4827 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4828 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4829 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4830 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
4831 |
|
15999 | 4832 |
keysOfSmallest:n |
4833 |
"return the keys (aka indices) of the n smallest elements, key of largest last. |
|
4834 |
Raises an exception, if the receiver does not contain at least n elements" |
|
4835 |
||
4836 |
|mySize loopAction actionForFirstN nSmallest maxInSmallest | |
|
4837 |
||
4838 |
mySize := self size. |
|
4839 |
n > mySize ifTrue:[ |
|
4840 |
self notEnoughElementsError |
|
4841 |
]. |
|
4842 |
||
4843 |
"/ for big collections, it seems to be better to sort only once |
|
4844 |
"/ (many individual insert operations into a sortedCollection are expensive) |
|
4845 |
"/ Consider using a tree-oriented collection if this becomes a problem |
|
4846 |
(n < 50) ifTrue:[ |
|
4847 |
n == 1 ifTrue:[ |
|
4848 |
|min kMin| |
|
4849 |
||
4850 |
loopAction := [:k :el | |
|
4851 |
min := el. kMin := k. |
|
4852 |
loopAction := |
|
4853 |
[:k :el | |
|
4854 |
el < min ifTrue:[ |
|
4855 |
min := el. kMin := k |
|
4856 |
]. |
|
4857 |
] |
|
4858 |
]. |
|
4859 |
self keysAndValuesDo:[:k :el | loopAction value:k value:el ]. |
|
4860 |
kMin isNil ifTrue:[ self notEnoughElementsError ]. |
|
4861 |
^ Array with:kMin. |
|
4862 |
]. |
|
4863 |
n == 2 ifTrue:[ |
|
4864 |
|min1 kMin1 min2 kMin2| |
|
4865 |
||
4866 |
loopAction := [:k :el | |
|
4867 |
min1 := el. kMin1 := k. |
|
4868 |
loopAction := |
|
4869 |
[:k :el | |
|
4870 |
el < min1 ifTrue:[ |
|
4871 |
min2 := min1. kMin2 := kMin1. |
|
4872 |
min1 := el. kMin1 := k |
|
4873 |
] ifFalse:[ |
|
4874 |
min2 := el. kMin2 := k |
|
4875 |
]. |
|
4876 |
loopAction := |
|
4877 |
[:k :el | |
|
4878 |
el < min2 ifTrue:[ |
|
4879 |
el < min1 ifTrue:[ |
|
4880 |
min2 := min1. min1 := el. |
|
4881 |
kMin2 := kMin1. kMin1 := k. |
|
4882 |
] ifFalse:[ |
|
4883 |
min2 := el. |
|
4884 |
kMin2 := k. |
|
4885 |
]. |
|
4886 |
]. |
|
4887 |
] |
|
4888 |
] |
|
4889 |
]. |
|
4890 |
||
4891 |
self keysAndValuesDo:[:k :el | loopAction value:k value:el ]. |
|
4892 |
kMin2 isNil ifTrue:[ self notEnoughElementsError ]. |
|
4893 |
^ Array with:kMin1 with:kMin2 |
|
4894 |
]. |
|
4895 |
||
4896 |
nSmallest := SortedCollection new:n. |
|
4897 |
nSmallest setSortBlock:[:a :b | a key < b key]. |
|
4898 |
||
4899 |
actionForFirstN := |
|
4900 |
[:k :el | |
|
4901 |
nSmallest add:(el -> k). |
|
4902 |
nSmallest size == n ifTrue:[ |
|
16007 | 4903 |
maxInSmallest := nSmallest max key. |
15999 | 4904 |
loopAction := |
4905 |
[:k :el | |
|
4906 |
el < maxInSmallest ifTrue:[ |
|
4907 |
nSmallest removeLast. |
|
4908 |
nSmallest add:(el -> k). |
|
16007 | 4909 |
maxInSmallest := nSmallest max key |
15999 | 4910 |
]. |
4911 |
] |
|
4912 |
]. |
|
4913 |
]. |
|
4914 |
||
4915 |
loopAction := actionForFirstN. |
|
4916 |
self keysAndValuesDo:[:k :el | loopAction value:k value:el ]. |
|
4917 |
^ nSmallest collect:[:a | a value]. |
|
4918 |
]. |
|
4919 |
||
4920 |
^ (self keys asNewOrderedCollection |
|
4921 |
sortWith:self asNewOrderedCollection) |
|
4922 |
copyTo:n |
|
4923 |
||
4924 |
" |
|
4925 |
#(10 35 20 45 30 5) keysOfSmallest:1 |
|
4926 |
#(10 35 20 45 30 5) keysOfSmallest:2 |
|
4927 |
#(10 35 20 45 30 5) smallest:2 |
|
4928 |
#(10 35 20 45 30 5) keysOfSmallest:3 |
|
4929 |
#(10 35 20 45 30 5) keysOfSmallest:5 |
|
4930 |
#(10 35 20 45 30 5) keysOfSmallest:6 |
|
4931 |
#(10 35 20 45 30 5) keysOfSmallest:8 |
|
4932 |
(1 to: 1000) asArray shuffled keysOfSmallest:51 |
|
4933 |
" |
|
4934 |
||
4935 |
" |
|
4936 |
|t1 t2 data| |
|
4937 |
||
4938 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
|
4939 |
t1 := Time millisecondsToRun:[ |
|
4940 |
40 timesRepeat:[ |
|
4941 |
data asSortedCollection at:3 |
|
4942 |
] |
|
4943 |
]. |
|
4944 |
t2 := Time millisecondsToRun:[ |
|
4945 |
40 timesRepeat:[ |
|
4946 |
data keysOfSmallest:3 |
|
4947 |
]. |
|
4948 |
]. |
|
4949 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
|
4950 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
|
4951 |
" |
|
4952 |
! |
|
4953 |
||
4954 |
largest:n |
|
4955 |
"return a collection containing the n largest elements, largest last. |
|
4956 |
Raises an exception, if the receiver does not contain at least n elements" |
|
4957 |
||
4958 |
|mySize loopAction nLargest sz_nLargest minInLargest| |
|
4959 |
||
4960 |
mySize := self size. |
|
4961 |
n > mySize ifTrue:[ |
|
4962 |
self notEnoughElementsError |
|
4963 |
]. |
|
4964 |
||
4965 |
"/ for big collections, it seems to be better to sort only once |
|
4966 |
"/ (many individual insert operations into a sortedCollection are expensive) |
|
4967 |
"/ Consider using a tree-oriented collection if this becomes a problem |
|
4968 |
(n < 50) ifTrue:[ |
|
4969 |
n == 1 ifTrue:[ ^ Array with:self max ]. |
|
4970 |
n == 2 ifTrue:[ |
|
4971 |
|l1 l2| |
|
4972 |
||
4973 |
loopAction := [:el | |
|
4974 |
l1 := el. |
|
4975 |
loopAction := |
|
4976 |
[:el | |
|
4977 |
el > l1 ifTrue:[ |
|
4978 |
l2 := l1. l1 := el |
|
4979 |
] ifFalse:[ |
|
4980 |
l2 := el |
|
4981 |
]. |
|
4982 |
loopAction := |
|
4983 |
[:el | |
|
4984 |
el > l2 ifTrue:[ |
|
4985 |
el > l1 ifTrue:[ |
|
4986 |
l2 := l1. l1 := el |
|
4987 |
] ifFalse:[ |
|
4988 |
l2 := el |
|
4989 |
]. |
|
4990 |
]. |
|
4991 |
]. |
|
4992 |
] |
|
4993 |
]. |
|
4994 |
||
4995 |
self do:[:el | loopAction value:el ]. |
|
4996 |
^ Array with:l2 with:l1 |
|
4997 |
]. |
|
4998 |
||
4999 |
nLargest := SortedCollection new:n. |
|
5000 |
sz_nLargest := 0. |
|
5001 |
loopAction := |
|
5002 |
[:el | |
|
5003 |
nLargest add:el. |
|
5004 |
sz_nLargest := sz_nLargest + 1. |
|
5005 |
sz_nLargest == n ifTrue:[ |
|
5006 |
loopAction := |
|
5007 |
[:el | |
|
5008 |
el > minInLargest ifTrue:[ |
|
5009 |
nLargest removeFirst. |
|
5010 |
nLargest add:el. |
|
5011 |
minInLargest := nLargest min |
|
5012 |
]. |
|
5013 |
]. |
|
5014 |
minInLargest := nLargest min. |
|
5015 |
]. |
|
5016 |
]. |
|
5017 |
||
5018 |
self do:[:el | loopAction value:el ]. |
|
5019 |
^ nLargest |
|
5020 |
]. |
|
5021 |
||
5022 |
^ self asSortedCollection copyTo:n |
|
5023 |
||
5024 |
" |
|
5025 |
#(10 35 20 45 30 5) largest:1 |
|
5026 |
#(10 35 20 45 30 5) largest:2 |
|
5027 |
#(10 35 20 45 30 5) largest:3 |
|
5028 |
#(10 35 20 45 30 5) largest:5 |
|
5029 |
#(10 35 20 45 30 5) largest:6 |
|
5030 |
#(10 35 20 45 30 5) largest:8 |
|
5031 |
" |
|
5032 |
||
5033 |
" |
|
5034 |
|t1 t2 data| |
|
5035 |
||
5036 |
data := (1 to:100000) collect:[:i | Random nextInteger ]. |
|
5037 |
t1 := Time millisecondsToRun:[ |
|
5038 |
40 timesRepeat:[ |
|
5039 |
data asSortedCollection copyLast:2 |
|
5040 |
] |
|
5041 |
]. |
|
5042 |
t2 := Time millisecondsToRun:[ |
|
5043 |
40 timesRepeat:[ |
|
5044 |
data largest:2 |
|
5045 |
]. |
|
5046 |
]. |
|
5047 |
Transcript show:'asSorted -> '; show:t1; showCR:'ms'. |
|
5048 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
|
5049 |
" |
|
5050 |
! |
|
5051 |
||
602 | 5052 |
longestCommonPrefix |
1246 | 5053 |
"return the longest common prefix of my elements. |
5054 |
Typically used with string collections." |
|
5055 |
||
15736 | 5056 |
^ self longestCommonPrefixCaseSensitive:true |
5532 | 5057 |
|
5058 |
" |
|
5059 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
5060 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
5061 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
5062 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
5063 |
" |
|
5064 |
||
5065 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
|
5066 |
! |
|
5067 |
||
15736 | 5068 |
longestCommonPrefixCaseSensitive:caseSensitive |
5069 |
"return the longest common prefix of all of my elements (which must be sequenceable collections). |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5070 |
Typically used with string collections, |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5071 |
especially with completion of selectors or filenames." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5072 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5073 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5074 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5075 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5076 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5077 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5078 |
] ifFalse:[ |
15736 | 5079 |
longest := longest commonPrefixWith:eachCollection caseSensitive:caseSensitive |
1246 | 5080 |
] |
602 | 5081 |
]. |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5082 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5083 |
|
15736 | 5084 |
" |
5085 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixCaseSensitive:false |
|
5086 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixCaseSensitive:true |
|
5087 |
#('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixCaseSensitive:true |
|
5088 |
#('AAA' 'A11' 'AA2') longestCommonPrefixCaseSensitive:true |
|
5089 |
#('AAA' 'BBB' 'CCC') longestCommonPrefixCaseSensitive:true |
|
5090 |
" |
|
5091 |
! |
|
5092 |
||
5093 |
longestCommonPrefixIgnoreCase:ignoreCase |
|
5094 |
<resource: #obsolete> |
|
5095 |
"return the longest common prefix of my elements (which must be sequenceableCollections). |
|
5096 |
Typically used with string collections, |
|
5097 |
especially with completion of selectors or filenames." |
|
5098 |
||
5099 |
^ self longestCommonPrefixCaseSensitive:ignoreCase not |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5100 |
! |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5101 |
|
6506 | 5102 |
longestCommonSuffix |
5103 |
"return the longest common suffix (tail) of my elements. |
|
5104 |
Typically used with string collections." |
|
5105 |
||
15736 | 5106 |
^ self longestCommonSuffixCaseSensitive:true |
13521
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
5107 |
|
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
5108 |
" |
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
5109 |
#('abcdefg' '1234cdefg' 'aaaaaadefg') longestCommonSuffix |
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
5110 |
" |
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
5111 |
|
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
5112 |
"Modified (comment): / 24-07-2011 / 10:32:15 / cg" |
6506 | 5113 |
! |
5114 |
||
15736 | 5115 |
longestCommonSuffixCaseSensitive:caseSensitive |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5116 |
"return the longest common suffix (tail) of my elements |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5117 |
(which must be sequenceableCollections)." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5118 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5119 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5120 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5121 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5122 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5123 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5124 |
] ifFalse:[ |
15736 | 5125 |
longest := longest commonSuffixWith:eachCollection caseSensitive:caseSensitive |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5126 |
] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5127 |
]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5128 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5129 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
5130 |
" |
20211 | 5131 |
#('Array' 'ByteArray' 'BigArray') longestCommonSuffixCaseSensitive:false |
5132 |
#('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixCaseSensitive:true |
|
5133 |
#('AAA' 'BBB' 'CCC') longestCommonSuffixCaseSensitive:true |
|
15736 | 5134 |
" |
5135 |
! |
|
5136 |
||
5137 |
longestCommonSuffixIgnoreCase:ignoreCase |
|
5138 |
<resource: #obsolete> |
|
5139 |
"return the longest common suffix (tail) of my elements |
|
5140 |
(which must be sequenceableCollections)." |
|
5141 |
||
5142 |
^ self longestCommonSuffixCaseSensitive:ignoreCase not |
|
5557 | 5143 |
! |
5144 |
||
7821 | 5145 |
max |
5146 |
"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
|
5147 |
using #< to compare elements. |
7821 | 5148 |
Raises an error, if the receiver is empty." |
5149 |
||
12945 | 5150 |
^ self |
14312 | 5151 |
fold:[:maxSoFar :each | |
5152 |
maxSoFar < each |
|
5153 |
ifTrue:[each] |
|
12945 | 5154 |
ifFalse:[maxSoFar] |
5155 |
] |
|
7821 | 5156 |
|
5157 |
" |
|
5158 |
#(15 1 -9 10 5) max |
|
5159 |
(1 to:15) max |
|
5160 |
(-1 to:-15 by:-1) max |
|
5161 |
(-1 to:-15 by:-4) max |
|
5162 |
(0 to:15 by:4) max |
|
5163 |
" |
|
5164 |
||
12945 | 5165 |
"Modified: / 11-07-2010 / 17:05:25 / cg" |
7821 | 5166 |
! |
5167 |
||
13040 | 5168 |
maxApplying:aBlock |
5169 |
"return the maximum value from applying aBlock to each element in the receiver collection, |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
5170 |
using aBlock to compare elements. |
13040 | 5171 |
Raises an error, if the receiver is empty." |
5172 |
||
14312 | 5173 |
|ret| |
5174 |
||
5175 |
ret := self |
|
13040 | 5176 |
inject:nil |
5177 |
into:[:maxSoFar :this | |
|
5178 |
|v| |
|
5179 |
||
5180 |
v := aBlock value:this. |
|
5181 |
(maxSoFar isNil or:[maxSoFar < v]) |
|
5182 |
ifTrue:[v] |
|
5183 |
ifFalse:[maxSoFar] |
|
14312 | 5184 |
]. |
5185 |
||
5186 |
ret isNil ifTrue:[ |
|
5187 |
^ self emptyCollectionError. |
|
5188 |
]. |
|
5189 |
^ ret. |
|
5190 |
||
5191 |
" |
|
5192 |
#() max -> Error |
|
13040 | 5193 |
#(15 1 -9 -20 10 5) max -> 15 |
5194 |
#(15 1 -9 -20 10 5) maxApplying:[:el | el abs] -> 20 |
|
5195 |
" |
|
5196 |
||
5197 |
"Created: / 23-08-2010 / 11:02:50 / cg" |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
5198 |
"Modified: / 18-03-2011 / 10:32:29 / cg" |
13040 | 5199 |
! |
5200 |
||
7821 | 5201 |
min |
5202 |
"return the minimum value in the receiver collection, |
|
5203 |
using < to compare elements. |
|
20211 | 5204 |
Raises an error if the receiver is empty." |
5205 |
||
5206 |
^ self |
|
5207 |
fold:[:minSoFar :each | |
|
14312 | 5208 |
each < minSoFar |
5209 |
ifTrue:[each] |
|
12945 | 5210 |
ifFalse:[minSoFar] |
5211 |
] |
|
7821 | 5212 |
|
5213 |
" |
|
20211 | 5214 |
#(15 1 -9 10 5) min |
5215 |
(1 to:15) min |
|
5216 |
(-1 to:-15 by:-1) min |
|
5217 |
(-1 to:-15 by:-4) min |
|
7821 | 5218 |
" |
5219 |
||
12945 | 5220 |
"Modified: / 11-07-2010 / 17:06:38 / cg" |
7821 | 5221 |
! |
5222 |
||
13040 | 5223 |
minApplying:aBlock |
5224 |
"return the minimum value from applying aBlock to each element in the receiver collection, |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
5225 |
using aBlock to compare elements. |
13040 | 5226 |
Raises an error, if the receiver is empty." |
5227 |
||
14312 | 5228 |
|ret| |
5229 |
||
5230 |
ret := self |
|
13040 | 5231 |
inject:nil |
5232 |
into:[:minSoFar :this | |
|
5233 |
|v| |
|
5234 |
||
5235 |
v := aBlock value:this. |
|
5236 |
(minSoFar isNil or:[v < minSoFar]) |
|
5237 |
ifTrue:[v] |
|
5238 |
ifFalse:[minSoFar] |
|
14312 | 5239 |
]. |
5240 |
ret isNil ifTrue:[ |
|
5241 |
^ self emptyCollectionError. |
|
5242 |
]. |
|
5243 |
^ ret. |
|
13040 | 5244 |
|
5245 |
" |
|
5246 |
#(15 -1 -9 10 5) min -> -9 |
|
5247 |
#(15 -1 -9 10 5) minApplying:[:el | el abs] -> 1 |
|
5248 |
" |
|
5249 |
||
5250 |
"Created: / 23-08-2010 / 11:01:42 / cg" |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
5251 |
"Modified: / 18-03-2011 / 10:32:33 / cg" |
13040 | 5252 |
! |
5253 |
||
7821 | 5254 |
minMax |
5255 |
"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
|
5256 |
as a two element array, using #< to compare elements. |
7821 | 5257 |
Raises an error, if the receiver is empty." |
5258 |
||
5259 |
|min max| |
|
5260 |
||
5261 |
self do:[:each | |
|
5262 |
min isNil ifTrue:[ |
|
5263 |
min := max := each |
|
5264 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
5265 |
max < each ifTrue:[ |
7821 | 5266 |
max := each |
5267 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
5268 |
each < min ifTrue:[ |
7821 | 5269 |
min := each |
5270 |
] |
|
5271 |
] |
|
5272 |
] |
|
5273 |
]. |
|
14312 | 5274 |
min isNil ifTrue:[ |
5275 |
^ self emptyCollectionError. |
|
5276 |
]. |
|
5277 |
||
7821 | 5278 |
^ Array with:min with:max |
5279 |
||
5280 |
" |
|
5281 |
#(15 1 -9 10 5) minMax |
|
5282 |
(1 to:15) minMax |
|
5283 |
(-1 to:-15 by:-1) minMax |
|
5284 |
(-1 to:-15 by:-4) minMax |
|
5285 |
(0 to:15 by:4) minMax |
|
5286 |
" |
|
21735 | 5287 |
|
5288 |
"Modified: / 28-04-2017 / 14:13:11 / stefan" |
|
7821 | 5289 |
! |
5290 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5291 |
nthLargest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5292 |
"return the n-largest element" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5293 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5294 |
^ (self largest:n) first |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5295 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5296 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5297 |
#(10 35 20 45 30 5) nthLargest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5298 |
#(10 35 20 45 30 5) nthLargest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5299 |
#(10 35 20 45 30 5) nthLargest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5300 |
#(10 35 20 45 30 5) nthLargest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5301 |
#(10 35 20 45 30 5) nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5302 |
#(10 35 20 45 30 5) nthLargest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5303 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5304 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5305 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5306 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5307 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5308 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5309 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5310 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5311 |
data asSortedCollection at:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5312 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5313 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5314 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5315 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5316 |
data nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5317 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5318 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5319 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5320 |
Transcript show:'nthMost -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5321 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5322 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
5323 |
|
15999 | 5324 |
smallest:n |
5325 |
"return the n smallest elements" |
|
5326 |
||
20211 | 5327 |
|mySize loopAction actionForFirstN actionForRest |
15999 | 5328 |
nSmallest sz_nSmallest maxInSmallest| |
5329 |
||
5330 |
mySize := self size. |
|
5331 |
n > mySize ifTrue:[ |
|
5332 |
self notEnoughElementsError |
|
5557 | 5333 |
]. |
15999 | 5334 |
|
5335 |
"/ for big collections, it seems to be better to sort only once |
|
5336 |
"/ (many individual insert operations into a sortedCollection are expensive) |
|
5337 |
"/ Consider using a tree-oriented collection if this becomes a problem |
|
5338 |
(n < 50) ifTrue:[ |
|
5339 |
n == 1 ifTrue:[ ^ Array with:self min ]. |
|
5340 |
n == 2 ifTrue:[ |
|
5341 |
|l1 l2| |
|
5342 |
||
20211 | 5343 |
loopAction := [:el | l1 := el. |
5344 |
loopAction := |
|
5345 |
[:el | |
|
15999 | 5346 |
el < l1 ifTrue:[ |
5347 |
l2 := l1. l1 := el |
|
5348 |
] ifFalse:[ |
|
5349 |
l2 := el |
|
5350 |
]. |
|
5351 |
loopAction := actionForRest |
|
5352 |
] |
|
5353 |
]. |
|
20211 | 5354 |
actionForRest := [:el | |
15999 | 5355 |
el < l2 ifTrue:[ |
5356 |
el < l1 ifTrue:[ |
|
5357 |
l2 := l1. l1 := el |
|
5358 |
] ifFalse:[ |
|
5359 |
l2 := el |
|
5360 |
]. |
|
5361 |
]. |
|
5362 |
]. |
|
5363 |
||
5364 |
self do:[:el | loopAction value:el ]. |
|
5365 |
^ Array with:l1 with:l2 |
|
5366 |
]. |
|
5367 |
||
5368 |
nSmallest := SortedCollection new:n. |
|
5369 |
sz_nSmallest := 0. |
|
20211 | 5370 |
actionForFirstN := |
5371 |
[:el | |
|
15999 | 5372 |
nSmallest add:el. |
5373 |
sz_nSmallest := sz_nSmallest + 1. |
|
5374 |
sz_nSmallest == n ifTrue:[ |
|
5375 |
loopAction := actionForRest. |
|
5376 |
maxInSmallest := nSmallest max. |
|
5377 |
]. |
|
5378 |
]. |
|
20211 | 5379 |
actionForRest := |
15999 | 5380 |
[:el | |
5381 |
el < maxInSmallest ifTrue:[ |
|
5382 |
nSmallest removeLast. |
|
5383 |
nSmallest add:el. |
|
5384 |
maxInSmallest := nSmallest max |
|
5385 |
]. |
|
5386 |
]. |
|
5387 |
||
5388 |
loopAction := actionForFirstN. |
|
5389 |
self do:[:el | loopAction value:el ]. |
|
5390 |
^ nSmallest |
|
5391 |
]. |
|
5392 |
||
20211 | 5393 |
^ self asSortedCollection copyFrom:(self size - n + 1) |
5394 |
||
5395 |
" |
|
5396 |
#(10 35 20 45 30 5) smallest:1 |
|
5397 |
#(10 35 20 45 30 5) smallest:2 |
|
5398 |
#(10 35 20 45 30 5) smallest:3 |
|
5399 |
#(10 35 20 45 30 5) smallest:5 |
|
5400 |
#(10 35 20 45 30 5) smallest:6 |
|
15999 | 5401 |
(1 to:10000) asArray shuffled smallest:10 |
20211 | 5402 |
(1 to:10000) asArray shuffled largest:10 |
15999 | 5403 |
#(10 35 20 45 30 5) smallest:8 |
5404 |
" |
|
5405 |
||
5406 |
" |
|
5407 |
|t1 t2 data| |
|
5408 |
||
5409 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
|
5410 |
t1 := Time millisecondsToRun:[ |
|
5411 |
40 timesRepeat:[ |
|
20211 | 5412 |
data asSortedCollection at:3 |
15999 | 5413 |
] |
5414 |
]. |
|
5415 |
t2 := Time millisecondsToRun:[ |
|
5416 |
40 timesRepeat:[ |
|
5417 |
data smallest:3 |
|
5418 |
]. |
|
5419 |
]. |
|
5420 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
|
5421 |
Transcript show:'smallest -> '; show:t2; showCR:'ms'. |
|
5422 |
" |
|
11706 | 5423 |
! ! |
5424 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5425 |
!Collection methodsFor:'set operations'! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5426 |
|
7169 | 5427 |
\ aCollection |
20226 | 5428 |
"return a new set containing all elements of the receiver, |
8606 | 5429 |
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
|
5430 |
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
|
5431 |
|
21267 | 5432 |
^ self select:[:eachElement | (aCollection includes:eachElement) not]. |
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5433 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5434 |
" |
20226 | 5435 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) asSet |
7169 | 5436 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) |
21267 | 5437 |
('hello' \ 'l') |
5438 |
||
5439 |
(Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro')) |
|
5440 |
\ (Dictionary withKeysAndValues:#(1 'uno' 4 'quatro')) |
|
5441 |
" |
|
5442 |
||
5443 |
"Modified (comment): / 20-01-2017 / 19:28:00 / stefan" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5444 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5445 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5446 |
intersect:aCollection |
20226 | 5447 |
"return a new set containing all elements of the receiver, |
8606 | 5448 |
which are also contained in the argument collection. |
21267 | 5449 |
For large collections you better use a Set for aCollection" |
5450 |
||
5451 |
^ aCollection select:[:eachElement | self includes:eachElement]. |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5452 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5453 |
" |
20226 | 5454 |
#(0 1 2 3 4 5 6 7 8 9) asSet intersect:#(1 2 3 11) |
5455 |
#(0 1 2 3 4 5 6 7 8 9) intersect:#(1 2 3 11) |
|
21267 | 5456 |
|
5457 |
(Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro')) |
|
5458 |
intersect:(Dictionary withKeysAndValues:#(1 'uno' 4 'quatro' 5 'cinque')) |
|
5459 |
" |
|
5460 |
||
5461 |
"Modified: / 20-01-2017 / 19:33:14 / stefan" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5462 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5463 |
|
7169 | 5464 |
union:aCollection |
20226 | 5465 |
"return a new set containing all elements of the receiver |
7169 | 5466 |
plus those of the aCollection" |
5467 |
||
5468 |
|newCollection| |
|
5469 |
||
5470 |
newCollection := self speciesForAdding new. |
|
5471 |
newCollection addAll:self. |
|
5472 |
newCollection addAll:aCollection. |
|
5473 |
^ newCollection |
|
5474 |
||
5475 |
" |
|
20226 | 5476 |
#(0 2 4 6 8) union:#(1 3 5 7) |
5477 |
#(0 2 4 6 8) union:#(0 1 3 5 7) |
|
21267 | 5478 |
|
5479 |
(Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro')) |
|
5480 |
union:(Dictionary withKeysAndValues:#(1 'uno' 4 'quatro' 5 'cinque')) |
|
5481 |
" |
|
5482 |
||
5483 |
"Modified (comment): / 20-01-2017 / 19:35:23 / stefan" |
|
7169 | 5484 |
! |
5485 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5486 |
xor:aCollection |
20211 | 5487 |
"return a new set containing all elements, |
8606 | 5488 |
which are contained in either the receiver or aCollection, but not in both. |
5489 |
||
5490 |
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
|
5491 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5492 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5493 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5494 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5495 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5496 |
(self includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5497 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5498 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5499 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5500 |
self do:[:element| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5501 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5502 |
newCollection add:element. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5503 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5504 |
]. |
20211 | 5505 |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5506 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5507 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5508 |
" |
20211 | 5509 |
#(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11) |
21267 | 5510 |
(Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro')) |
5511 |
xor:(Dictionary withKeysAndValues:#(1 'uno' 4 'quatro' 5 'cinque')) |
|
5512 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5513 |
" |
12430 | 5514 |
|
5515 |
" |
|
5516 |
|c1 c2| |
|
5517 |
||
5518 |
c1 := #( foo bar baz baloo ). |
|
5519 |
c2 := #( foe bar banana baloo ). |
|
20211 | 5520 |
c1 symmetricDifference:c2. |
12430 | 5521 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
5522 |
" |
|
21267 | 5523 |
|
5524 |
"Modified (comment): / 20-01-2017 / 19:37:58 / stefan" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5525 |
! ! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
5526 |
|
10811 | 5527 |
!Collection methodsFor:'sorting & reordering'! |
5528 |
||
19780 | 5529 |
sortedBy:aTwoArgBlock |
16178 | 5530 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
5531 |
When the block returns true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
5532 |
||
19780 | 5533 |
|newCollection| |
5534 |
||
5535 |
newCollection := self speciesForAdding new:self size. |
|
5536 |
self addAllTo:newCollection. |
|
5537 |
newCollection sort:aTwoArgBlock. |
|
5538 |
||
5539 |
^ newCollection |
|
16178 | 5540 |
! |
5541 |
||
5542 |
sortedBySelector:aSelector |
|
5543 |
"return a new collection containing my elements sorted based on the value of what aSelector returns when sent to my |
|
5544 |
elements. Sorting by a selector is so common, that it's worth a separate utility" |
|
5545 |
||
5546 |
^ self sortedBy:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
5547 |
||
5548 |
" |
|
5549 |
|a b| |
|
5550 |
||
5551 |
a := #(123 25235 12 13423423 234234). |
|
5552 |
b := a sortedBySelector:#abs |
|
5553 |
" |
|
5554 |
! |
|
5555 |
||
10811 | 5556 |
topologicalSort |
5557 |
"Sort a partial ordered collection. |
|
5558 |
The receiver consists of tupels defining a partial order. |
|
5559 |
Use the algorithm by R. E. Tarjan from 1972. |
|
5560 |
Answer an OrderedCollection containing the sorted items" |
|
5561 |
||
16539 | 5562 |
^ self topologicalSortStable: false |
5563 |
||
5564 |
"Modified: / 05-06-2014 / 12:22:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5565 |
! |
|
5566 |
||
5567 |
topologicalSortStable: sortStable |
|
5568 |
"Sort a partial ordered collection. |
|
5569 |
The receiver consists of tupels defining a partial order. |
|
5570 |
Use the algorithm by R. E. Tarjan from 1972. |
|
5571 |
Answer an OrderedCollection containing the sorted items. |
|
5572 |
||
5573 |
If sortStable is true, try to make order stable among |
|
16811 | 5574 |
multiple invocations. If false, stability is not guaranteed. |
16539 | 5575 |
" |
5576 |
||
10811 | 5577 |
|graph roots sorted count| |
5578 |
||
5579 |
"create a graph. |
|
5580 |
For each node there is an entry containing the number of references |
|
5581 |
and a list of arcs to other nodes" |
|
5582 |
||
16539 | 5583 |
graph := sortStable ifTrue:[ OrderedDictionary new ] ifFalse:[ Dictionary new ]. |
10811 | 5584 |
|
20226 | 5585 |
self do:[:eachTuple| |
10811 | 5586 |
|node1| |
5587 |
node1 := eachTuple first. |
|
5588 |
graph at:node1 ifAbsentPut:[OrderedCollection with:0]. |
|
20226 | 5589 |
2 to:(eachTuple size) do:[:i| |
10811 | 5590 |
|node2 n2| |
5591 |
||
14880 | 5592 |
node2 := eachTuple at:i. |
10811 | 5593 |
"add an arc from node1 to node2" |
5594 |
(graph at:node1) add:node2. |
|
14880 | 5595 |
"add the node" |
5596 |
n2 := graph at:node2 ifAbsentPut:[OrderedCollection with:0]. |
|
10811 | 5597 |
"increment count on incoming arcs of node2" |
5598 |
n2 at:1 put:(n2 first + 1). |
|
14880 | 5599 |
node1 := node2. |
10811 | 5600 |
]. |
20226 | 5601 |
]. |
10811 | 5602 |
|
5603 |
"now find the root nodes (having zero incoming arcs)" |
|
5604 |
roots := OrderedCollection new. |
|
5605 |
graph keysAndValuesDo:[:eachKey :eachValue| |
|
5606 |
eachValue first = 0 ifTrue:[ |
|
5607 |
roots add:eachKey |
|
5608 |
]. |
|
5609 |
]. |
|
5610 |
||
5611 |
"for each root, out it to the sorted list and remove it from the graph. |
|
5612 |
This may cause referenced nodes to be moved to the root" |
|
5613 |
||
5614 |
sorted := OrderedCollection new:graph size. |
|
20226 | 5615 |
[roots notEmpty] whileTrue:[ |
10811 | 5616 |
|root eachEntry| |
5617 |
||
5618 |
root := roots removeFirst. |
|
5619 |
sorted add:root. |
|
5620 |
eachEntry := graph removeKey:root. |
|
5621 |
2 to:eachEntry size do:[:i| |
|
5622 |
|eachChild eachChildGraph| |
|
5623 |
eachChild := eachEntry at:i. |
|
5624 |
eachChildGraph := graph at:eachChild. |
|
14880 | 5625 |
count := (eachChildGraph at:1) - 1. |
5626 |
eachChildGraph at:1 put:count. |
|
5627 |
count = 0 ifTrue:[ |
|
10811 | 5628 |
roots add:eachChild. |
5629 |
]. |
|
5630 |
]. |
|
5631 |
]. |
|
5632 |
||
14880 | 5633 |
"if graph is empty, we are done. Otherwise there is a cycle" |
10811 | 5634 |
graph notEmpty ifTrue:[ |
14840 | 5635 |
"search for cycle" |
14880 | 5636 |
|checkBlock| |
14840 | 5637 |
|
5638 |
checkBlock := [:graphEntry :backTrace| |
|
5639 |
2 to:graphEntry size do:[:i| |
|
5640 |
|eachChild eachChildGraph| |
|
5641 |
eachChild := graphEntry at:i. |
|
5642 |
(backTrace includesIdentical:eachChild) ifTrue:[ |
|
5643 |
backTrace add:eachChild. |
|
19586 | 5644 |
ProceedableError |
5645 |
raiseRequestWith:backTrace reversed |
|
5646 |
errorString:('cycle in ordering: %1' bindWith:(backTrace reversed printStringWithSeparator:' -> ')). |
|
14840 | 5647 |
]. |
5648 |
eachChildGraph := graph at:eachChild. |
|
5649 |
checkBlock value:eachChildGraph value:(backTrace copyWith:eachChild). |
|
5650 |
]. |
|
5651 |
]. |
|
5652 |
graph keysAndValuesDo:[:eachNode :eachGraph| |
|
14880 | 5653 |
checkBlock value:eachGraph value:(OrderedCollection with:eachNode). |
14840 | 5654 |
]. |
14880 | 5655 |
"should not be reached" |
10811 | 5656 |
self error:'cycle in ordering' mayProceed:true. |
5657 |
]. |
|
5658 |
||
5659 |
^ sorted |
|
5660 |
||
5661 |
" |
|
5662 |
#((1 2) (1 3) (3 2)) topologicalSort |
|
11384 | 5663 |
#((1 2) (1 3) (3 2) (4)) topologicalSort |
5664 |
||
5665 |
with cycles: |
|
10811 | 5666 |
#((1 2) (1 3) (3 1)) topologicalSort |
11384 | 5667 |
#((1 2) (2 3) (3 1)) topologicalSort |
5668 |
#((1 2) (2 3) (3 4) (4 1)) topologicalSort |
|
11385
c3d908e81ec7
changed #topologicalSort - comment
Stefan Vogel <sv@exept.de>
parents:
11384
diff
changeset
|
5669 |
#((1 1) (1 2) (1 3) (3 2) (4)) topologicalSort |
10811 | 5670 |
|
20226 | 5671 |
(Smalltalk allClasses collect:[:eachClass| |
10811 | 5672 |
Array with:eachClass superclass with:eachClass |
14880 | 5673 |
]) topologicalSort. |
5674 |
||
20226 | 5675 |
((Smalltalk allClasses asSortedCollection:[:a :b| a name < b name])collect:[:eachClass| |
14880 | 5676 |
Array with:eachClass superclass with:eachClass |
10811 | 5677 |
]) topologicalSort |
5678 |
" |
|
16539 | 5679 |
|
16811 | 5680 |
"Modified: / 05-06-2014 / 12:21:55 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10811 | 5681 |
! ! |
5682 |
||
15458 | 5683 |
!Collection methodsFor:'statistical functions'! |
5684 |
||
15999 | 5685 |
arithmeticMean |
5686 |
"arithmetic mean value of all elements in the collection" |
|
5687 |
||
5688 |
^ self sum / self size |
|
5689 |
||
5690 |
" |
|
5691 |
TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5). |
|
5692 |
TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4). |
|
5693 |
TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431). |
|
5694 |
" |
|
5695 |
||
5696 |
"Created: / 13-04-2011 / 16:52:32 / cg" |
|
5697 |
! |
|
5698 |
||
5699 |
average |
|
5700 |
"average value of all elements in the collection" |
|
5701 |
||
5702 |
^ self arithmeticMean |
|
5703 |
||
5704 |
" |
|
5705 |
TestCase assert:( { 1. 2. 3. 4 } average = 2.5). |
|
5706 |
" |
|
5707 |
||
5708 |
"Created: / 18-03-2011 / 10:31:04 / cg" |
|
5709 |
"Modified: / 13-04-2011 / 17:09:21 / cg" |
|
5710 |
! |
|
5711 |
||
5712 |
geometricMean |
|
5713 |
"geometric mean value of all elements in the collection" |
|
5714 |
||
5715 |
^ self product raisedTo:(1/self size) |
|
5716 |
||
5717 |
" |
|
5718 |
TestCase assert:( { 1. 2. 3. 4. } geometricMean closeTo: 2.21336). |
|
5719 |
TestCase assert:( { 1. 2. 3. 4. 5 } geometricMean closeTo: 2.60517). |
|
5720 |
TestCase assert:( { 13. 23. 12. 44. 55 } geometricMean closeTo: 24.41932). |
|
5721 |
" |
|
5722 |
||
5723 |
"Created: / 13-04-2011 / 16:53:44 / cg" |
|
5724 |
! |
|
5725 |
||
16001 | 5726 |
median |
5727 |
"Return the middle element, or as close as we can get." |
|
5728 |
||
5729 |
^ self asSortedCollection median |
|
5730 |
||
5731 |
" |
|
5732 |
#(10 35 20 45 30 5) median |
|
5733 |
" |
|
5734 |
! |
|
5735 |
||
15458 | 5736 |
standardDeviation |
5737 |
"standard deviation value of all elements in the collection, |
|
5738 |
which is the complete set and not a sample." |
|
5739 |
||
5740 |
^ self variance sqrt |
|
5741 |
||
5742 |
" |
|
5743 |
TestCase assert:( #( 1 2 3 4) arithmeticMean = 2.5). |
|
5744 |
TestCase assert:( #(13 23 12 44 55) arithmeticMean closeTo: 29.4). |
|
5745 |
TestCase assert:( #(13 23 12 44 55) standardDeviation closeTo: 17.2116). |
|
5746 |
TestCase assert:( (1 to: 100) arithmeticMean = ((100 + 1)/2)). |
|
5747 |
TestCase assert:( (1 to: 100) standardDeviation = ((100 squared - 1)/12) sqrt). |
|
5748 |
TestCase assert:( (1 to: 6) standardDeviation = ((6 squared - 1)/12) sqrt). |
|
5749 |
" |
|
5750 |
! |
|
5751 |
||
5752 |
variance |
|
5753 |
"compute the variance over a complete data set (and not of a sample)" |
|
5754 |
||
5755 |
|mean sumDeltaSquares| |
|
5756 |
||
5757 |
mean := self arithmeticMean. |
|
5758 |
sumDeltaSquares := 0. |
|
5759 |
self do:[:each| |
|
5760 |
sumDeltaSquares := sumDeltaSquares + ((each - mean) squared). |
|
5761 |
]. |
|
5762 |
^ sumDeltaSquares / self size. |
|
5763 |
||
5764 |
" |
|
5765 |
#(1 1 1 1 1 1 1 1 1 1) variance |
|
5766 |
#(1 2 3 4 5 6 7 8 9 0) variance |
|
5767 |
" |
|
5768 |
! ! |
|
5769 |
||
602 | 5770 |
!Collection methodsFor:'testing'! |
5771 |
||
14991 | 5772 |
allElementsHaveTheIdenticalValue |
5773 |
"true if all elements of the receiver have the same value" |
|
5774 |
||
5775 |
^ self identicalValuesComputedBy:[:el | el] |
|
5776 |
||
5777 |
" |
|
5778 |
#(1 2 3 5 6 7 8 9) allElementsHaveTheIdenticalValue |
|
5779 |
#(1 1 1 1 1 1) allElementsHaveTheIdenticalValue |
|
5780 |
#(1 1 1.0 1.0 1) allElementsHaveTheIdenticalValue |
|
5781 |
#(1 1 1.0 1.0 1) allElementsHaveTheSameValue |
|
5782 |
" |
|
5783 |
! |
|
5784 |
||
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5785 |
allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5786 |
"true if all elements of the receiver have the same value" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5787 |
|
13872
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
5788 |
^ self sameValuesComputedBy:[:el | el] |
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5789 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5790 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5791 |
#(1 2 3 5 6 7 8 9) allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5792 |
#(1 1 1 1 1 1) allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5793 |
#(1 1 1.0 1.0 1) allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5794 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5795 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5796 |
"Created: / 21-12-2011 / 15:54:08 / cg" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5797 |
! |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
5798 |
|
602 | 5799 |
capacity |
20355 | 5800 |
"return the number of elements, that the receiver is prepared to take. |
5801 |
For most collections, this is the actual size. |
|
5802 |
However, some have more space preallocated to allow |
|
21657 | 5803 |
for faster adding of elements (i.e. there are logical vs. physical sizes)." |
602 | 5804 |
|
5805 |
^ self size |
|
21657 | 5806 |
|
5807 |
"Modified (comment): / 17-03-2017 / 11:50:34 / stefan" |
|
602 | 5808 |
! |
5809 |
||
14991 | 5810 |
identicalValuesComputedBy:aBlock |
18322 | 5811 |
"true if aBlock answers identical values for all elements of the receiver" |
14991 | 5812 |
|
5813 |
|first valueForFirstElement| |
|
5814 |
||
5815 |
first := true. |
|
5816 |
self do:[:each | |
|
5817 |
first ifTrue:[ |
|
5818 |
first := false. |
|
5819 |
valueForFirstElement := aBlock value:each. |
|
5820 |
] ifFalse:[ |
|
5821 |
valueForFirstElement == (aBlock value:each) ifFalse:[ |
|
5822 |
^ false. |
|
5823 |
]. |
|
5824 |
]. |
|
5825 |
]. |
|
5826 |
^ true |
|
5827 |
||
5828 |
" |
|
5829 |
#(1 2 3 5 6 7 8 9) sameValuesComputedBy:[:el | el even] |
|
5830 |
#(1 1 1 1 1 1) sameValuesComputedBy:[:el | el even] |
|
5831 |
#(1 1 1.0 1.0 1) sameValuesComputedBy:[:el | el even] |
|
5832 |
#(1 3 3 15 1) sameValuesComputedBy:[:el | el even] |
|
5833 |
" |
|
5834 |
||
5835 |
"Created: / 21-12-2011 / 15:59:19 / cg" |
|
5836 |
! |
|
5837 |
||
19680 | 5838 |
includes:searchedElement |
5839 |
"return true, if an element equal to the argument, searchedElement is in the collection. |
|
602 | 5840 |
This compares using #= (i.e. it does not look for the object itself, |
5841 |
instead, one that compares equal). |
|
7807 | 5842 |
See #includesIdentical: when identity is asked for. |
5843 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 5844 |
|
19680 | 5845 |
self do:[:eachElement | |
5846 |
(searchedElement = eachElement) ifTrue:[^ true]. |
|
602 | 5847 |
]. |
5848 |
^ false |
|
19680 | 5849 |
|
5850 |
"Modified (format): / 30-04-2016 / 17:10:22 / cg" |
|
602 | 5851 |
! |
5852 |
||
5853 |
includesAll:aCollection |
|
20211 | 5854 |
"return true if the receiver includes all elements of |
602 | 5855 |
the argument, aCollection; false if any is missing. |
22321 | 5856 |
Notice: this method has O² runtime behavior and may be |
20211 | 5857 |
slow for big receivers/args. |
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
5858 |
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
|
5859 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
5860 |
^ aCollection conform:[:element | (self includes:element)] |
602 | 5861 |
|
5862 |
" |
|
5863 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
5864 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
5865 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
5866 |
" |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
5867 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
5868 |
"Modified: / 13-10-2006 / 12:54:50 / cg" |
602 | 5869 |
! |
5870 |
||
19680 | 5871 |
includesAny:searchedElementsCollection |
20211 | 5872 |
"return true if the receiver includes any from the argument, aCollection. |
19680 | 5873 |
Return false if it includes none. |
5874 |
Uses #= (value compare) |
|
20211 | 5875 |
Notice: |
22321 | 5876 |
this method has O² runtime behavior for some subclasses and may be slow for big receivers/args. |
20211 | 5877 |
Think about using a Set or Dictionary. |
17572 | 5878 |
Some speedup is also possible, by arranging highly |
19680 | 5879 |
probable elements towards the beginning of aCollection, to avoid useless searches. |
17572 | 5880 |
|
5881 |
Also: I am not sure, if (and if so, at which breakeven), |
|
5882 |
it is better to reverse the loops, and walk over the receiver's |
|
5883 |
elements once, walking over the searched elements in the inner loop. |
|
21404 | 5884 |
If the receiver is large, caching effects will definitely favour this, as |
5885 |
the smaller collection might fit into the cache. |
|
17572 | 5886 |
" |
5887 |
||
19749 | 5888 |
|mySize searchedSize| |
20226 | 5889 |
|
19749 | 5890 |
mySize := self size. |
5891 |
searchedSize := searchedElementsCollection size. |
|
5892 |
"/ avoid this only for big receivers, due to caching effects |
|
5893 |
(mySize < searchedSize) ifTrue:[ |
|
17572 | 5894 |
self do:[:existingElement | |
19680 | 5895 |
(searchedElementsCollection includes:existingElement) ifTrue:[^ true]. |
17572 | 5896 |
]. |
5897 |
] ifFalse:[ |
|
19680 | 5898 |
searchedElementsCollection do:[:searchedElement | |
17572 | 5899 |
(self includes:searchedElement) ifTrue:[^ true]. |
5900 |
]. |
|
602 | 5901 |
]. |
5902 |
^ false |
|
5903 |
||
5904 |
" |
|
5905 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
5906 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
5907 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
5908 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
17572 | 5909 |
|
5910 |
|coll| |
|
5911 |
coll := (1 to:10000) asOrderedCollection. |
|
5912 |
Time millisecondsToRun:[ |
|
19749 | 5913 |
1000000 timesRepeat:[ coll includesAny:#(500 600) ] |
20226 | 5914 |
]. |
17572 | 5915 |
|
5916 |
|coll| |
|
21404 | 5917 |
coll := (1 to:10000). |
5918 |
Time millisecondsToRun:[ |
|
5919 |
1000000 timesRepeat:[ coll includesAny:#(500 600) ] |
|
5920 |
]. |
|
5921 |
||
5922 |
|coll| |
|
17572 | 5923 |
coll := (1 to:10000) asOrderedCollection. |
5924 |
Time millisecondsToRun:[ |
|
5925 |
100000 timesRepeat:[ coll includesAny:#(-1 -10) ] |
|
5926 |
]. |
|
5927 |
||
5928 |
Notice: it is redefined for string search in a subclass: |
|
5929 |
||
5930 |
|coll| |
|
5931 |
coll := String new:10000 withAll:$a. |
|
5932 |
coll at:500 put:$b. |
|
5933 |
Time millisecondsToRun:[ |
|
5934 |
100000 timesRepeat:[ coll includesAny:'bc' ] |
|
20226 | 5935 |
]. |
17572 | 5936 |
|
5937 |
|coll| |
|
5938 |
coll := String new:10000 withAll:$a. |
|
5939 |
Time millisecondsToRun:[ |
|
5940 |
100000 timesRepeat:[ coll includesAny:'bc' ] |
|
20226 | 5941 |
]. |
17572 | 5942 |
|
602 | 5943 |
" |
19680 | 5944 |
|
21404 | 5945 |
"Modified (comment): / 12-02-2017 / 11:47:42 / cg" |
19680 | 5946 |
! |
5947 |
||
5948 |
includesAnyIdentical:searchedElementsCollection |
|
20226 | 5949 |
"return true, if the receiver includes any from the argument, aCollection. |
19680 | 5950 |
Return false if it includes none. |
10504 | 5951 |
Use identity compare for comparing. |
20226 | 5952 |
Notice: |
22321 | 5953 |
this method has O² runtime behavior for some subclasses and may be slow for big receivers/args. |
20226 | 5954 |
Think about using a Set or Dictionary. |
19680 | 5955 |
Some speedup is also possible, by arranging highly |
5956 |
probable elements towards the beginning of aCollection, to avoid useless searches." |
|
5957 |
||
5958 |
searchedElementsCollection do:[:element | |
|
10504 | 5959 |
(self includesIdentical:element) ifTrue:[^ true]. |
5960 |
]. |
|
5961 |
^ false |
|
5962 |
||
5963 |
" |
|
5964 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(1 2 3) |
|
5965 |
#('hello' 'there' 'world') includesAnyIdentical:#('hello' 'world') |
|
5966 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(7 8 9) |
|
5967 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(8 9 10) |
|
5968 |
" |
|
19680 | 5969 |
|
5970 |
"Modified (comment): / 30-04-2016 / 17:13:38 / cg" |
|
5971 |
! |
|
5972 |
||
5973 |
includesIdentical:searchedElement |
|
5974 |
"return true, if the argument, searchedElement is in the collection. |
|
602 | 5975 |
This compares using #== (i.e. object identity). |
7807 | 5976 |
See #includes: when equality is asked for. |
5977 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 5978 |
|
19680 | 5979 |
self do:[:eachElement | |
5980 |
(searchedElement == eachElement) ifTrue:[^ true]. |
|
602 | 5981 |
]. |
5982 |
^ false |
|
19680 | 5983 |
|
5984 |
"Modified (comment): / 30-04-2016 / 17:10:34 / cg" |
|
602 | 5985 |
! |
5986 |
||
9005 | 5987 |
isCollection |
5988 |
"return true, if the receiver is some kind of collection; |
|
5989 |
true is returned here - the method is redefined from Object." |
|
5990 |
||
5991 |
^ true |
|
5992 |
! |
|
5993 |
||
602 | 5994 |
isEmpty |
5995 |
"return true, if the receiver is empty" |
|
5996 |
||
5997 |
^ self size == 0 |
|
5998 |
! |
|
5999 |
||
6181 | 6000 |
isEmptyOrNil |
14614 | 6001 |
"return true if I am nil or an empty collection - true here, if the receiver's size is 0, |
6002 |
(from Squeak)" |
|
6181 | 6003 |
|
6004 |
^ self isEmpty |
|
6005 |
||
6006 |
"Created: / 13.11.2001 / 13:17:12 / cg" |
|
6182 | 6007 |
"Modified: / 13.11.2001 / 13:28:35 / cg" |
6181 | 6008 |
! |
6009 |
||
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
6010 |
isNilOrEmptyCollection |
8833 | 6011 |
"return true if I am nil or an empty collection - false here. |
6012 |
Obsolete, use isEmptyOrNil." |
|
6013 |
||
6014 |
<resource:#obsolete> |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
6015 |
|
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
6016 |
^ self isEmpty |
6182 | 6017 |
|
6018 |
"Modified: / 13.11.2001 / 13:27:55 / cg" |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
6019 |
! |
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
6020 |
|
9005 | 6021 |
isNonByteCollection |
6022 |
"return true, if the receiver is some kind of collection, but not a String, ByteArray etc.; |
|
6023 |
true is returned here - the method is redefined from Object." |
|
6024 |
||
6025 |
^ true |
|
6026 |
! |
|
6027 |
||
18467 | 6028 |
isOrdered |
6029 |
"return true, if the receiver's elements are ordered. |
|
6030 |
This defaults to true here, and is to be redefined by collections which use |
|
6031 |
hashing, and the order of keys and values is therefore not guaranteed to remain |
|
18469 | 6032 |
the same, as objects are added. |
6033 |
Notice, that this query might be useless/false for some collections; |
|
6034 |
for example, a file directory may change its order even though smalltalk does not touch it; |
|
6035 |
or a collection which is based on computed block values may return completely differently |
|
6036 |
ordered elements (also random value collections, etc.). |
|
20211 | 6037 |
Therefore, use this only as a hint |
6038 |
(e.g. when showing values, to avoid sorting and destroying |
|
18469 | 6039 |
any previous order in the visual representation)" |
18467 | 6040 |
|
6041 |
^ true |
|
6042 |
! |
|
6043 |
||
9005 | 6044 |
isSorted |
6045 |
"return true, if the receiver is sorted. |
|
6046 |
Collections which hold their elements in sorted order |
|
20211 | 6047 |
should return true. Some algorithms (quicksort) degenerate when |
9005 | 6048 |
operating on sorted collections and can be avoided if this information |
6049 |
is given. The default returned here (false) should not hurt. |
|
6050 |
I.e. you should NEVER depend on that in your application." |
|
6051 |
||
6052 |
^ false |
|
6053 |
||
6054 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
6055 |
! |
|
6056 |
||
6057 |
isSortedBy:aBlock |
|
6058 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock). |
|
6059 |
Collections which hold their elements in sorted order |
|
20211 | 6060 |
should return true. Some algorithms (quicksort) degenerate when |
9005 | 6061 |
operating on sorted collections and can be avoided if this information |
6062 |
is given. The default returned here (false) should not hurt. |
|
6063 |
I.e. you should NEVER depend on that in your application." |
|
6064 |
||
6065 |
^ false |
|
6066 |
! |
|
6067 |
||
6068 |
isSortedCollection |
|
6069 |
"return true, if the receiver is a sortedCollection." |
|
6070 |
||
6071 |
^ false |
|
6072 |
||
6073 |
! |
|
6074 |
||
602 | 6075 |
notEmpty |
6076 |
"return true, if the receiver is not empty" |
|
6077 |
||
6078 |
^ self isEmpty not |
|
6079 |
! |
|
6080 |
||
6932 | 6081 |
notEmptyOrNil |
6082 |
"Squeak compatibility: |
|
6083 |
return true if I am neither nil nor an empty collection." |
|
6084 |
||
6085 |
^ self notEmpty |
|
6086 |
! |
|
6087 |
||
602 | 6088 |
occurrencesOf:anElement |
6089 |
"return the number of occurrences of the argument, anElement in |
|
5048 | 6090 |
the receiver. Uses #= (i.e. equality) compare." |
602 | 6091 |
|
6092 |
|count "{ Class: SmallInteger }" | |
|
6093 |
||
6094 |
count := 0. |
|
6095 |
self do:[:element | |
|
5048 | 6096 |
(anElement = element) ifTrue:[ |
6097 |
count := count + 1 |
|
6098 |
]. |
|
602 | 6099 |
]. |
6100 |
^ count |
|
8274 | 6101 |
! |
6102 |
||
6103 |
occurrencesOfAny:aCollectionOfElements |
|
20211 | 6104 |
"return the number of occurrences of any in aCollectionOfElements in the receiver. |
8274 | 6105 |
Uses #= (i.e. equality) compare. |
6106 |
Should be redefined in subclass(es) if ever used heavily." |
|
6107 |
||
6108 |
|count "{ Class: SmallInteger }" | |
|
6109 |
||
6110 |
count := 0. |
|
8275 | 6111 |
aCollectionOfElements do:[:element | |
6112 |
count := count + (self occurrencesOf:element) |
|
8274 | 6113 |
]. |
6114 |
^ count |
|
6115 |
||
6116 |
" |
|
20211 | 6117 |
#(1 4 6 8 4 1) occurrencesOfAny:#(1 4) |
6118 |
#(1 4 6 8 4 1) occurrencesOfAny:#(2 5) |
|
6119 |
'hello world' occurrencesOfAny:'hel' |
|
8274 | 6120 |
" |
13871 | 6121 |
! |
6122 |
||
13872
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6123 |
sameValuesComputedBy:aBlock |
18321 | 6124 |
"true if aBlock answers equal values for all elements of the receiver" |
13871 | 6125 |
|
6126 |
|first valueForFirstElement| |
|
6127 |
||
6128 |
first := true. |
|
6129 |
self do:[:each | |
|
6130 |
first ifTrue:[ |
|
6131 |
first := false. |
|
6132 |
valueForFirstElement := aBlock value:each. |
|
6133 |
] ifFalse:[ |
|
6134 |
valueForFirstElement = (aBlock value:each) ifFalse:[ |
|
6135 |
^ false. |
|
6136 |
]. |
|
6137 |
]. |
|
6138 |
]. |
|
6139 |
^ true |
|
6140 |
||
6141 |
" |
|
13872
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6142 |
#(1 2 3 5 6 7 8 9) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6143 |
#(1 1 1 1 1 1) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6144 |
#(1 1 1.0 1.0 1) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6145 |
#(1 3 3 15 1) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6146 |
" |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6147 |
|
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
6148 |
"Created: / 21-12-2011 / 15:59:19 / cg" |
602 | 6149 |
! ! |
6150 |
||
4680 | 6151 |
!Collection methodsFor:'tracing'! |
6152 |
||
6153 |
traceInto:aRequestor level:level from:referrer |
|
6154 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
6155 |
||
6156 |
^ aRequestor traceCollection:self level:level from:referrer |
|
6157 |
||
6158 |
||
6159 |
! ! |
|
6160 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6161 |
!Collection methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6162 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6163 |
acceptVisitor:aVisitor with:aParameter |
16724 | 6164 |
"dispatch for visitor pattern; send #visitCollection:with: to aVisitor" |
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6165 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6166 |
^ aVisitor visitCollection:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6167 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
6168 |
|
22045 | 6169 |
|
2097 | 6170 |
!Collection class methodsFor:'documentation'! |
628 | 6171 |
|
13699 | 6172 |
version |
18507 | 6173 |
^ '$Header$' |
13699 | 6174 |
! |
6175 |
||
12090 | 6176 |
version_CVS |
18507 | 6177 |
^ '$Header$' |
628 | 6178 |
! ! |
6867 | 6179 |
|
14608 | 6180 |
|
602 | 6181 |
Collection initialize! |