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