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