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