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