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