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