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