author | Claus Gittinger <cg@exept.de> |
Tue, 28 Feb 2012 21:22:53 +0100 | |
changeset 14027 | a23e237f2fad |
parent 14022 | 28ecd3e2d8c9 |
child 14028 | 0a7d1b52002c |
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 |
|
1 | 14 |
Object subclass:#Collection |
11331 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
|
17 |
NotEnoughElementsSignal RecursiveCollectionStoreStringSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Abstract' |
|
1 | 20 |
! |
21 |
||
2097 | 22 |
!Collection class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
6135 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
Abstract superclass for all collections. |
|
41 |
This abstract class provides functionality common to all collections, |
|
42 |
without knowing how the concrete class implements things. Thus, all |
|
43 |
methods found here depend on some basic mechanisms to be defined in the |
|
44 |
concrete class. |
|
45 |
These basic methods are usually defined as #subclassResponsibility here. |
|
46 |
Some methods are also redefined for better performance. |
|
1289 | 47 |
|
1325 | 48 |
Subclasses should at least implement: |
49 |
do: - enumerate elements |
|
50 |
||
51 |
they should implement one of the following set of access messages: |
|
52 |
keyed collections: |
|
53 |
at:ifAbsent: - fetching an element |
|
54 |
at: - fetching an element |
|
55 |
at:put: - storing an element |
|
56 |
||
57 |
unkeyed collections: |
|
58 |
add: - add an element |
|
59 |
remove:ifAbsent: - remove an element |
|
60 |
||
61 |
||
1289 | 62 |
[author:] |
63 |
Claus Gittinger |
|
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
2097 | 67 |
!Collection class methodsFor:'initialization'! |
10 | 68 |
|
69 |
initialize |
|
70 |
"setup the signal" |
|
71 |
||
54 | 72 |
InvalidKeySignal isNil ifTrue:[ |
7092
630807cd320f
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
7054
diff
changeset
|
73 |
InvalidKeySignal := Error newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
74 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
75 |
InvalidKeySignal notifierString:'invalid key:'. |
345 | 76 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
77 |
ValueNotFoundSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
78 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
79 |
ValueNotFoundSignal notifierString:'value not found:'. |
155 | 80 |
|
8510
af60b80bf50e
refer to exception classes instead of signal class vars
Stefan Vogel <sv@exept.de>
parents:
8395
diff
changeset
|
81 |
NotEnoughElementsSignal := NotFoundError newSignalMayProceed:true. |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
82 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
83 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
348 | 84 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
85 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
86 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
87 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
54 | 88 |
] |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
89 |
|
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
90 |
"Modified: / 8.11.1997 / 19:18:17 / cg" |
10 | 91 |
! ! |
92 |
||
2097 | 93 |
!Collection class methodsFor:'instance creation'! |
155 | 94 |
|
13049
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
95 |
combiningEach:collection1 withEach:collection2 using:aTwoArgBlock |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
96 |
"evaluate aTwoArgBlock for each combination of elements from collection1 |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
97 |
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
|
98 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
99 |
|newColl idx useIndex newEl| |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
100 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
101 |
newColl := self new:(collection1 size * collection2 size). |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
102 |
useIndex := newColl isFixedSize. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
103 |
idx := 1. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
104 |
collection1 do:[:e1 | |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
105 |
collection2 do:[:e2 | |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
106 |
newEl := aTwoArgBlock value:e1 value:e2. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
107 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
108 |
useIndex ifTrue:[ |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
109 |
newColl at:idx put:(aTwoArgBlock value:e1 value:e2). |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
110 |
idx := idx + 1. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
111 |
] ifFalse:[ |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
112 |
newColl add:(aTwoArgBlock value:e1 value:e2) |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
113 |
]. |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
114 |
] |
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 |
^ newColl |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
117 |
|
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
118 |
" |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
119 |
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
|
120 |
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
|
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 |
"Created: / 25-08-2010 / 17:21:47 / cg" |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
124 |
! |
5fb3970c8dd9
added: #combiningEach:withEach:using:
Claus Gittinger <cg@exept.de>
parents:
13046
diff
changeset
|
125 |
|
602 | 126 |
new:size withAll:element |
5626 | 127 |
"return a new Collection of size, where all elements are |
602 | 128 |
initialized to element" |
348 | 129 |
|
602 | 130 |
|newCollection| |
92 | 131 |
|
602 | 132 |
newCollection := self new:size. |
133 |
size timesRepeat:[newCollection add:element] |
|
134 |
! |
|
1 | 135 |
|
5626 | 136 |
newFrom:aCollection |
137 |
"Return an instance of me containing the same elements as aCollection." |
|
138 |
||
139 |
^ self withAll:aCollection |
|
140 |
||
141 |
" |
|
142 |
Bag newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
143 |
Set newFrom:#(1 2 3 4 4 5 6 7 7 7 ) |
|
144 |
" |
|
145 |
! |
|
146 |
||
1 | 147 |
with:anObject |
148 |
"return a new Collection with one element:anObject" |
|
149 |
||
150 |
|newCollection| |
|
151 |
||
152 |
newCollection := self new. |
|
153 |
newCollection add:anObject. |
|
154 |
^ newCollection |
|
155 |
! |
|
156 |
||
157 |
with:firstObject with:secondObject |
|
158 |
"return a new Collection with two elements:firstObject and secondObject" |
|
159 |
||
160 |
|newCollection| |
|
161 |
||
162 |
newCollection := self new. |
|
1112 | 163 |
newCollection add:firstObject; add:secondObject. |
1 | 164 |
^ newCollection |
165 |
! |
|
166 |
||
167 |
with:firstObject with:secondObject with:thirdObject |
|
168 |
"return a new Collection with three elements" |
|
169 |
||
170 |
|newCollection| |
|
171 |
||
172 |
newCollection := self new. |
|
1112 | 173 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 174 |
^ newCollection |
175 |
! |
|
176 |
||
177 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
178 |
"return a new Collection with four elements" |
|
179 |
||
180 |
|newCollection| |
|
181 |
||
182 |
newCollection := self new. |
|
1112 | 183 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 184 |
^ newCollection |
185 |
! |
|
186 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
187 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
345 | 188 |
"return a new Collection with five elements" |
189 |
||
190 |
|newCollection| |
|
191 |
||
192 |
newCollection := self new. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
193 |
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
|
194 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
195 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
196 |
"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
|
197 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
198 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
199 |
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
|
200 |
"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
|
201 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
202 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
203 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
204 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
205 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6. |
345 | 206 |
^ newCollection |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
207 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
208 |
"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
|
209 |
! |
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 |
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
|
212 |
"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
|
213 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
214 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
215 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
216 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
217 |
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
|
218 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
219 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
220 |
"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
|
221 |
! |
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 |
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
|
224 |
"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
|
225 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
226 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
227 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
228 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
229 |
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
|
230 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
231 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
232 |
"Created: 22.1.1997 / 19:34:34 / cg" |
345 | 233 |
! |
234 |
||
1 | 235 |
withAll:aCollection |
236 |
"return a new Collection with all elements taken from the argument, |
|
237 |
aCollection" |
|
238 |
||
239 |
|newCollection| |
|
240 |
||
241 |
newCollection := self new. |
|
242 |
newCollection addAll:aCollection. |
|
243 |
^newCollection |
|
9159 | 244 |
! |
245 |
||
246 |
withSize:n |
|
247 |
"return a new collection which really provides space for n elements. |
|
248 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
249 |
||
250 |
^ self new:n |
|
251 |
! ! |
|
252 |
||
253 |
!Collection class methodsFor:'Compatibility-Squeak'! |
|
254 |
||
255 |
ofSize:n |
|
256 |
"return a new collection which really provides space for n elements. |
|
257 |
Kludges around the stupid definition of OrderedCollection>>new:" |
|
258 |
||
259 |
^ self withSize:n |
|
1 | 260 |
! ! |
261 |
||
13678 | 262 |
|
2097 | 263 |
!Collection class methodsFor:'Signal constants'! |
10 | 264 |
|
602 | 265 |
emptyCollectionSignal |
266 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 267 |
|
602 | 268 |
^ EmptyCollectionSignal |
308 | 269 |
! |
270 |
||
602 | 271 |
invalidKeySignal |
272 |
"return the signal used to report bad key usage" |
|
308 | 273 |
|
602 | 274 |
^ InvalidKeySignal |
201 | 275 |
! |
276 |
||
602 | 277 |
notEnoughElementsSignal |
278 |
"return the signal used to report attempts for an operation, for which |
|
279 |
there are not enough elements in the collection" |
|
249 | 280 |
|
602 | 281 |
^ NotEnoughElementsSignal |
404 | 282 |
! |
283 |
||
602 | 284 |
valueNotFoundSignal |
285 |
"return the signal used to report a nonexisting element." |
|
404 | 286 |
|
602 | 287 |
^ ValueNotFoundSignal |
288 |
! ! |
|
404 | 289 |
|
12090 | 290 |
!Collection class methodsFor:'misc ui support'! |
291 |
||
292 |
iconInBrowserSymbol |
|
293 |
<resource: #programImage> |
|
294 |
||
295 |
^ #containerClassBrowserIcon |
|
296 |
! ! |
|
297 |
||
2097 | 298 |
!Collection class methodsFor:'queries'! |
404 | 299 |
|
602 | 300 |
growIsCheap |
301 |
"return true, if this collection can easily grow |
|
302 |
(i.e. without a need for become:). |
|
303 |
Returns true here; this method is redefined in fix-size |
|
304 |
collections" |
|
404 | 305 |
|
602 | 306 |
^ true |
8893 | 307 |
! |
308 |
||
309 |
isAbstract |
|
11231 | 310 |
"Return if this class is an abstract class. |
311 |
True is returned for Collection here; false for subclasses. |
|
312 |
Abstract subclasses must redefine again." |
|
313 |
||
8893 | 314 |
^ self == Collection |
257 | 315 |
! ! |
316 |
||
7261 | 317 |
!Collection methodsFor:'Compatibility-Dolphin'! |
6411 | 318 |
|
319 |
identityIncludes:anObject |
|
7807 | 320 |
"return true, if the argument, anObject is in the collection. |
321 |
Same as #includesIdentical for Dolphin compatibility." |
|
322 |
||
6411 | 323 |
^ self includesIdentical:anObject. |
6869 | 324 |
! |
325 |
||
326 |
includesAnyOf:aCollection |
|
327 |
"same as #includesAny for Dolphin compatibility." |
|
328 |
||
329 |
^ self includesAny:aCollection |
|
330 |
||
331 |
" |
|
332 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
333 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
334 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
335 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
336 |
" |
|
12430 | 337 |
! |
338 |
||
339 |
symmetricDifference:aCollection |
|
340 |
"return a new set containing all elements, |
|
341 |
which are contained in either the receiver or aCollection, but not in both. |
|
342 |
Same as xor: - for compatibility" |
|
343 |
||
344 |
^ self xor:aCollection |
|
345 |
||
346 |
" |
|
347 |
|c1 c2| |
|
348 |
||
349 |
c1 := #( foo bar baz baloo ). |
|
350 |
c2 := #( foe bar banana baloo ). |
|
351 |
c1 symmetricDifference:c2. |
|
352 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
353 |
" |
|
6411 | 354 |
! ! |
355 |
||
7261 | 356 |
!Collection methodsFor:'Compatibility-Squeak'! |
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
357 |
|
13699 | 358 |
, aCollection |
359 |
^ self copy addAll: aCollection; yourself |
|
360 |
||
361 |
"Created: / 14-09-2011 / 16:32:06 / cg" |
|
362 |
! |
|
363 |
||
9354 | 364 |
addIfNotPresent:anObject |
7011 | 365 |
"Include anObject as one of the receiver's elements, but only if there |
366 |
is no such element already. Anwser anObject." |
|
367 |
||
368 |
(self includes: anObject) ifFalse: [self add: anObject]. |
|
369 |
^ anObject |
|
370 |
! |
|
371 |
||
6149 | 372 |
anyOne |
8858 | 373 |
"return any element from the collection. |
374 |
Report an error if there is none. |
|
6149 | 375 |
Same as #anElement - for Squeak compatibility" |
376 |
||
377 |
^ self anElement |
|
378 |
! |
|
379 |
||
13678 | 380 |
associationsDo: aBlock |
381 |
"cg: I think this is bad, but - well..." |
|
382 |
||
383 |
self do: aBlock |
|
384 |
||
385 |
"Created: / 12-09-2011 / 09:21:58 / cg" |
|
386 |
! |
|
387 |
||
12249 | 388 |
difference: aCollection |
389 |
"Answer the set-theoretic difference of two collections." |
|
390 |
||
391 |
^ self reject:[:each | aCollection includes: each] |
|
392 |
||
393 |
" |
|
394 |
#(0 2 4 6 8) difference:#(2 4) |
|
395 |
" |
|
396 |
! |
|
397 |
||
12345 | 398 |
gather:aBlock |
399 |
"return an Array, |
|
400 |
containing all elements as returned from applying aBlock to each element if the receiver, |
|
401 |
where the block returns a collection of to-be-added elements. |
|
402 |
This could also be called: collectAllAsArray:" |
|
403 |
||
404 |
^ self gather:aBlock as:Array |
|
405 |
||
406 |
" |
|
407 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] |
|
408 |
" |
|
409 |
! |
|
410 |
||
411 |
gather:aBlock as:aClass |
|
412 |
"return an instance of the collection-class aClass, |
|
413 |
containing all elements as returned from applying aBlock to each element if the receiver. |
|
414 |
where the block returns a collection of to-be-added elements. |
|
415 |
This could also be called: collectAll:as:" |
|
416 |
||
417 |
^ aClass streamContents:[:s | |
|
418 |
self do: [:ea | s nextPutAll: (aBlock value: ea)] |
|
419 |
] |
|
420 |
||
421 |
" |
|
422 |
(Set withAll:#(10 20 30 10 20 40)) gather:[:el | Array with:el with:el * 2] as:OrderedCollection |
|
423 |
(Set withAll:#(10 20 30 10 20 40)) collectAll:[:el | Array with:el with:el * 2] |
|
424 |
" |
|
425 |
! |
|
426 |
||
9354 | 427 |
groupBy:keyBlock having:selectBlock |
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
428 |
"Like in SQL operation - Split the receiver's contents into collections of |
9354 | 429 |
elements for which keyBlock returns the same results, and return those |
430 |
collections allowed by selectBlock. " |
|
431 |
||
9319 | 432 |
|result key| |
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
433 |
|
9319 | 434 |
result := OrderedDictionary new. |
12593
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
435 |
self do:[:e | |
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
436 |
key := keyBlock value: e. |
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
437 |
(result includesKey: key) ifFalse: [ |
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
438 |
result at: key put: OrderedCollection new |
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
439 |
]. |
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
440 |
(result at: key) add: e |
664991bb352e
comment/format in: #groupBy:having:
Claus Gittinger <cg@exept.de>
parents:
12430
diff
changeset
|
441 |
]. |
9319 | 442 |
^ result := result select: selectBlock |
443 |
||
9354 | 444 |
" |
445 |
#(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a |true] |
|
446 |
" |
|
9319 | 447 |
! |
448 |
||
9065 | 449 |
ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
450 |
|action| |
|
451 |
||
452 |
action := self isEmpty ifTrue:[ ifEmptyValue ] ifFalse:[ ifNotEmptyValue ]. |
|
453 |
||
454 |
action isBlock ifTrue:[ |
|
455 |
action numArgs == 1 ifTrue:[ |
|
456 |
^ action value:self |
|
457 |
] |
|
458 |
]. |
|
459 |
^ action value |
|
460 |
! |
|
461 |
||
462 |
ifEmpty:ifEmptyValue ifNotEmptyDo:ifNotEmptyValue |
|
463 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
464 |
! |
|
465 |
||
466 |
ifEmptyDo:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
467 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
468 |
! |
|
469 |
||
470 |
ifNotEmpty:ifNotEmptyValue |
|
471 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
472 |
! |
|
473 |
||
474 |
ifNotEmptyDo:ifNotEmptyValue |
|
475 |
^ self ifEmpty:nil ifNotEmpty:ifNotEmptyValue |
|
476 |
! |
|
477 |
||
478 |
ifNotEmptyDo:ifNotEmptyValue ifEmpty:ifEmptyValue |
|
479 |
^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue |
|
480 |
! |
|
481 |
||
11280 | 482 |
intersection:aCollection |
483 |
^ self intersect:aCollection |
|
484 |
||
485 |
"Created: / 22-10-2008 / 21:29:27 / cg" |
|
486 |
! |
|
487 |
||
5752
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
488 |
withIndexDo:aTwoArgBlock |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
489 |
"evaluate the argument, aBlock for every element in the collection, |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
490 |
passing both element and index as arguments." |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
491 |
|
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
492 |
^ self keysAndValuesDo:[:key :index | |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
493 |
aTwoArgBlock value:index value:key |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
494 |
]. |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
495 |
! ! |
66031c1d493a
added #withIndexDo: for Squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
5651
diff
changeset
|
496 |
|
13678 | 497 |
|
1 | 498 |
!Collection methodsFor:'accessing'! |
499 |
||
602 | 500 |
anElement |
6966 | 501 |
"return any element from the collection, |
502 |
report an error if there is none" |
|
602 | 503 |
|
504 |
self do: [:each | ^ each]. |
|
6511
0ca4f36b16ca
signal an error in #anElement if there is none;
Claus Gittinger <cg@exept.de>
parents:
6506
diff
changeset
|
505 |
self emptyCollectionError. |
602 | 506 |
! |
507 |
||
13046
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
508 |
at:aKey ifNilOrAbsentPut:valueBlock |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
509 |
"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
|
510 |
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
|
511 |
from the valueBlock and return it. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
512 |
Useful for lazy initialization of collections." |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
513 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
514 |
|val| |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
515 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
516 |
val := self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
517 |
val isNil ifTrue:[ |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
518 |
self at:aKey put:(val := valueBlock value). |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
519 |
]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
520 |
^ val |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
521 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
522 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
523 |
|d| |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
524 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
525 |
d := Dictionary new. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
526 |
d at:#foo ifNilOrAbsentPut:[ 'hello' ]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
527 |
d |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
528 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
529 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
530 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
531 |
|a| |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
532 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
533 |
a := Array new:10. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
534 |
a at:1 ifNilOrAbsentPut:[ 'hello' ]. |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
535 |
a |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
536 |
" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
537 |
|
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
538 |
"Created: / 24-08-2010 / 17:07:21 / cg" |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
539 |
! |
705f04952af4
added: #at:ifNilOrAbsentPut:
Claus Gittinger <cg@exept.de>
parents:
13042
diff
changeset
|
540 |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
541 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
542 |
"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
|
543 |
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
|
544 |
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
|
545 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
546 |
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
|
547 |
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
|
548 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
549 |
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
|
550 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
551 |
" |
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
552 |
args: indexCollection : <Collection of keys<object> > |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
553 |
returns: self |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
554 |
" |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
555 |
|
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
556 |
" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
557 |
(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
|
558 |
(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
|
559 |
(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
|
560 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
561 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
562 |
(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
|
563 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
564 |
|
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
565 |
"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
|
566 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
567 |
|
4985 | 568 |
fifth |
569 |
"return the fifth element of the collection. |
|
570 |
For unordered collections, this simply returns the fifth |
|
571 |
element when enumerating them. |
|
572 |
This should be redefined in subclasses." |
|
573 |
||
574 |
^ self nth:5 |
|
575 |
||
576 |
" |
|
577 |
#(1 2 3 4 5) fifth |
|
578 |
" |
|
579 |
! |
|
580 |
||
68 | 581 |
first |
582 |
"return the first element of the collection. |
|
4855 | 583 |
For unordered collections, this simply returns the first |
584 |
element when enumerating them. |
|
68 | 585 |
This should be redefined in subclasses." |
586 |
||
587 |
self do:[:e | ^ e]. |
|
588 |
||
589 |
"error if collection is empty" |
|
155 | 590 |
^ self emptyCollectionError |
68 | 591 |
! |
592 |
||
5887 | 593 |
first:n |
594 |
"return the n first elements of the collection. |
|
595 |
Raises an error if there are not enough elements in the receiver. |
|
596 |
For unordered collections, this simply returns the first |
|
597 |
n elements when enumerating them |
|
598 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
599 |
This should be redefined in subclasses." |
|
600 |
||
601 |
|coll remain| |
|
602 |
||
603 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
604 |
||
605 |
coll := OrderedCollection new. |
|
606 |
remain := n. |
|
607 |
self do:[:e | |
|
13741 | 608 |
coll add:e. |
609 |
remain := remain - 1. |
|
610 |
remain == 0 ifTrue:[ |
|
611 |
^ coll |
|
612 |
]. |
|
613 |
]. |
|
5887 | 614 |
|
615 |
"error if collection has not enough elements" |
|
616 |
^ self notEnoughElementsError |
|
617 |
||
618 |
" |
|
619 |
#(1 2 3 4 5) first:3 |
|
620 |
#(1 2 3 4 5) asSet first:3 |
|
621 |
" |
|
13741 | 622 |
|
623 |
"Modified (format): / 29-09-2011 / 10:16:49 / cg" |
|
5887 | 624 |
! |
625 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
626 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
627 |
"return the first element of the collection. |
10593 | 628 |
If its empty, return the exceptionValue. |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
629 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
630 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
631 |
self isEmpty ifTrue:[^ exceptionValue value]. |
10593 | 632 |
^ self first |
633 |
||
634 |
"Modified: / 04-06-2007 / 22:36:14 / cg" |
|
635 |
! |
|
636 |
||
637 |
firstOrNil |
|
638 |
"return the first element of the collection. |
|
639 |
If its empty, return nil. |
|
640 |
(i.e. dont trigger an error as done in #first)" |
|
641 |
||
642 |
^ self firstIfEmpty:nil |
|
643 |
||
644 |
"Created: / 04-06-2007 / 22:36:07 / cg" |
|
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
645 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
646 |
|
4985 | 647 |
fourth |
648 |
"return the fourth element of the collection. |
|
649 |
For unordered collections, this simply returns the fourth |
|
650 |
element when enumerating them. |
|
651 |
This should be redefined in subclasses." |
|
652 |
||
653 |
^ self nth:4 |
|
654 |
||
655 |
" |
|
656 |
#(1 2 3 4) fourth |
|
657 |
" |
|
658 |
! |
|
659 |
||
68 | 660 |
last |
661 |
"return the last element of the collection. |
|
662 |
This should be redefined in subclasses." |
|
663 |
||
664 |
|theLastOne any| |
|
665 |
||
666 |
any := false. |
|
667 |
self do:[:e | any := true. theLastOne := e]. |
|
668 |
any ifTrue:[ |
|
6135 | 669 |
^ theLastOne |
68 | 670 |
]. |
671 |
||
672 |
"error if collection is empty" |
|
155 | 673 |
^ self emptyCollectionError |
68 | 674 |
! |
675 |
||
5887 | 676 |
last:n |
677 |
"return the n last elements of the collection. |
|
678 |
Raises an error if there are not enough elements in the receiver. |
|
679 |
For unordered collections, this simply returns the last |
|
680 |
n elements when enumerating them |
|
681 |
(Warning: the contents of the returned collection is not deterministic in this case). |
|
682 |
This should be redefined in subclasses since the implementation here is VERY inefficient." |
|
683 |
||
684 |
|coll remain| |
|
685 |
||
686 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
687 |
||
10798
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
688 |
coll := OrderedCollection new:n. |
5887 | 689 |
remain := n. |
690 |
self do:[:e | |
|
13741 | 691 |
remain > 0 ifTrue:[ |
692 |
remain := remain - 1. |
|
693 |
] ifFalse:[ |
|
694 |
coll removeFirst. |
|
695 |
]. |
|
696 |
coll add:e. |
|
697 |
]. |
|
5887 | 698 |
|
699 |
remain ~~ 0 ifTrue:[ |
|
700 |
"error if collection has not enough elements" |
|
701 |
^ self notEnoughElementsError |
|
702 |
]. |
|
703 |
^ coll |
|
704 |
||
705 |
" |
|
706 |
#(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
|
707 |
#(1 2 3 4 5 6 7 8 9 0) asSet last:3 |
5887 | 708 |
'hello world' last:5 |
709 |
" |
|
13741 | 710 |
|
711 |
"Modified (format): / 29-09-2011 / 10:16:22 / cg" |
|
5887 | 712 |
! |
713 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
714 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
715 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
716 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
717 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
718 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
719 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
720 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
721 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
722 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
723 |
"Created: 6.2.1996 / 15:27:49 / cg" |
4855 | 724 |
! |
725 |
||
9073 | 726 |
median |
727 |
"Return the middle element, or as close as we can get." |
|
728 |
||
729 |
^ self asSortedCollection median |
|
730 |
||
731 |
" |
|
732 |
#(10 35 20 45 30 5) median |
|
733 |
" |
|
734 |
! |
|
735 |
||
4985 | 736 |
nth:n |
737 |
"return the nth element of the collection. |
|
738 |
For unordered collections, this simply returns the nth |
|
739 |
element when enumerating them. |
|
740 |
This should be redefined in subclasses." |
|
741 |
||
742 |
|count| |
|
743 |
||
744 |
count := 1. |
|
745 |
self do:[:e | |
|
746 |
count == n ifTrue:[^ e]. |
|
747 |
count := count + 1 |
|
748 |
]. |
|
749 |
||
5266 | 750 |
"error if collection is smaller" |
751 |
^ self notEnoughElementsError |
|
4985 | 752 |
|
753 |
" |
|
754 |
#(1 2 3 4) nth:3 |
|
5266 | 755 |
#(1 2 3 4) nth:5 |
756 |
||
757 |
#(1 2 3 4) asSet nth:3 |
|
758 |
#(1 2 3 4) asSet nth:5 |
|
4985 | 759 |
" |
760 |
! |
|
761 |
||
11916 | 762 |
order |
763 |
"report an error that only OrderedXXX's have an order" |
|
764 |
||
765 |
self shouldNotImplement |
|
766 |
! |
|
767 |
||
4855 | 768 |
second |
769 |
"return the second element of the collection. |
|
770 |
For unordered collections, this simply returns the second |
|
771 |
element when enumerating them. |
|
7129 | 772 |
This should be redefined in subclasses." |
773 |
||
774 |
^ self nth:2 |
|
4855 | 775 |
|
4856 | 776 |
" |
777 |
#(1 2 3) second |
|
778 |
" |
|
6135 | 779 |
! |
780 |
||
781 |
seventh |
|
782 |
"return the seventh element of the collection. |
|
783 |
For unordered collections, this simply returns the sixth |
|
784 |
element when enumerating them. |
|
785 |
This should be redefined in subclasses." |
|
786 |
||
787 |
^ self nth:7 |
|
788 |
||
789 |
" |
|
790 |
#(1 2 3 4 5 6 7) seventh |
|
791 |
" |
|
792 |
||
793 |
"Created: / 1.11.2001 / 16:43:29 / cg" |
|
4856 | 794 |
! |
795 |
||
4985 | 796 |
sixth |
797 |
"return the sixth element of the collection. |
|
798 |
For unordered collections, this simply returns the sixth |
|
799 |
element when enumerating them. |
|
800 |
This should be redefined in subclasses." |
|
801 |
||
802 |
^ self nth:6 |
|
803 |
||
804 |
" |
|
805 |
#(1 2 3 4 5 6 7) sixth |
|
806 |
" |
|
807 |
! |
|
808 |
||
4856 | 809 |
third |
810 |
"return the third element of the collection. |
|
811 |
For unordered collections, this simply returns the third |
|
812 |
element when enumerating them. |
|
813 |
This should be redefined in subclasses." |
|
814 |
||
4985 | 815 |
^ self nth:3 |
4856 | 816 |
|
817 |
" |
|
818 |
#(1 2 3) third |
|
819 |
" |
|
13741 | 820 |
|
821 |
"Modified (format): / 29-09-2011 / 10:17:25 / cg" |
|
12666 | 822 |
! |
823 |
||
824 |
values |
|
825 |
"return a collection containing all values of the receiver. |
|
826 |
This is to make value access to an OrderedDictionary compatible with any-Collection" |
|
827 |
||
828 |
|aCollection| |
|
829 |
||
830 |
aCollection := OrderedCollection new. |
|
831 |
self do:[:value| aCollection add:value]. |
|
832 |
^ aCollection |
|
1 | 833 |
! ! |
834 |
||
835 |
!Collection methodsFor:'adding & removing'! |
|
836 |
||
837 |
add:anObject |
|
88 | 838 |
"add the argument, anObject to the receiver. |
839 |
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
|
840 |
(i.e. don't depend on where it will be put). |
2351 | 841 |
An error is raised here - it is to be implemented by a concrete subclass." |
1 | 842 |
|
843 |
^ self subclassResponsibility |
|
2351 | 844 |
|
845 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 846 |
! |
847 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
848 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
849 |
"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
|
850 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
851 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
852 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
853 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
854 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
855 |
"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
|
856 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
857 |
|
1 | 858 |
addAll:aCollection |
1163 | 859 |
"add all elements of the argument, aCollection to the receiver. |
7752 | 860 |
Returns the argument, aCollection (sigh)." |
1 | 861 |
|
862 |
aCollection do:[:element | |
|
1163 | 863 |
self add:element |
1 | 864 |
]. |
865 |
^ aCollection |
|
88 | 866 |
|
867 |
" |
|
868 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
869 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
870 |
" |
|
1163 | 871 |
|
872 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 873 |
! |
874 |
||
12 | 875 |
addAllFirst:aCollection |
876 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 877 |
of the receiver. Returns the argument, aCollection." |
12 | 878 |
|
879 |
aCollection reverseDo:[:element | |
|
1163 | 880 |
self addFirst:element |
12 | 881 |
]. |
882 |
^ aCollection |
|
883 |
||
884 |
" |
|
885 |
|c| |
|
886 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 887 |
c addAllFirst:#(9 8 7 6 5). |
888 |
c |
|
12 | 889 |
" |
1163 | 890 |
|
891 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 892 |
! |
893 |
||
602 | 894 |
addAllLast:aCollection |
1163 | 895 |
"add all elements of the argument, aCollection to the receiver. |
896 |
Returns the argument, aCollection." |
|
602 | 897 |
|
898 |
aCollection do:[:element | |
|
1163 | 899 |
self addLast:element |
602 | 900 |
]. |
901 |
^ aCollection |
|
902 |
||
903 |
" |
|
904 |
|c| |
|
905 |
c := #(4 3 2 1) asOrderedCollection. |
|
906 |
c addAllLast:#(9 8 7 6 5) |
|
907 |
" |
|
1163 | 908 |
|
909 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 910 |
! |
911 |
||
912 |
addFirst:anObject |
|
913 |
"add the argument, anObject to the receiver. |
|
2351 | 914 |
If the receiver is ordered, the new element will be added at the beginning. |
915 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 916 |
|
917 |
^ self subclassResponsibility |
|
2351 | 918 |
|
919 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 920 |
! |
921 |
||
602 | 922 |
addLast:anObject |
923 |
"add the argument, anObject to the receiver. |
|
1163 | 924 |
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
|
925 |
Return the argument, anObject. |
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
926 |
|
18d87b8fce50
Clean up code and document differences between #add: and #addLast:
Stefan Vogel <sv@exept.de>
parents:
10795
diff
changeset
|
927 |
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
|
928 |
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
|
929 |
the end, even if it is already present in the collection." |
602 | 930 |
|
931 |
^ self add:anObject |
|
1163 | 932 |
|
933 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 934 |
! |
935 |
||
3124 | 936 |
contents:aCollection |
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
937 |
"set my contents from aCollection |
5265 | 938 |
- this may be redefined in a concrete subclass for more performance" |
3124 | 939 |
|
6042
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
940 |
aCollection == self ifTrue:[ |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
941 |
"/ self error:'should not happen'. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
942 |
^ self |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
943 |
]. |
04414166010e
check for bad arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
5887
diff
changeset
|
944 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
945 |
self removeAll. |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
946 |
aCollection notNil ifTrue:[ |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
947 |
self addAll:aCollection |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
948 |
] |
3124 | 949 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
950 |
"Modified: / 17.8.1998 / 10:18:43 / cg" |
3124 | 951 |
! |
952 |
||
1 | 953 |
remove:anObject |
2351 | 954 |
"search for the first element, which is equal to anObject; |
955 |
if found, remove and return it. |
|
956 |
If not found, report a 'value not found'-error. |
|
957 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 958 |
|
1163 | 959 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
960 |
||
2351 | 961 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 962 |
! |
963 |
||
602 | 964 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 965 |
"search for the first element, which is equal to anObject; |
966 |
if found, remove and return it. |
|
967 |
If not found, return the the value of the exceptionBlock. |
|
968 |
Uses equality compare (=) to search for the occurrence. |
|
969 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 970 |
|
971 |
^ self subclassResponsibility |
|
2351 | 972 |
|
973 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 974 |
! |
975 |
||
134 | 976 |
removeAll |
1163 | 977 |
"remove all elements from the receiver. Returns the receiver. |
134 | 978 |
This should be reimplemented in subclasses for better |
979 |
performance." |
|
980 |
||
2383 | 981 |
[self isEmpty] whileFalse:[ |
1163 | 982 |
self removeFirst |
134 | 983 |
]. |
1163 | 984 |
|
2383 | 985 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 986 |
! |
987 |
||
1 | 988 |
removeAll:aCollection |
12993 | 989 |
"remove all elements from the receiver which are equal to any in aCollection. |
1163 | 990 |
Return the argument, aCollection. |
5267 | 991 |
Raises an error, if some element-to-remove is not in the receiver. |
992 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1163 | 993 |
|
360 | 994 |
Notice: for some collections (those not tuned for |
1163 | 995 |
resizing themself) this may be very slow. |
996 |
If the number of removed elements is big compared to to |
|
997 |
the receivers size, it may be better to copy the |
|
998 |
ones which are not to be removed into a new collection." |
|
1 | 999 |
|
1000 |
aCollection do:[:element | self remove:element]. |
|
1001 |
^ aCollection |
|
1163 | 1002 |
|
5268 | 1003 |
" |
1004 |
|coll| |
|
1005 |
||
1006 |
coll := #(1 2 3 4 5 6) asSet. |
|
1007 |
coll removeAll:#(4 5 6). |
|
1008 |
coll |
|
1009 |
" |
|
1010 |
||
1011 |
"raises an error: |
|
1012 |
|coll| |
|
1013 |
||
1014 |
coll := #(1 2 3 4 5 6) asSet. |
|
1015 |
coll removeAll:#(4 5 6 7 8). |
|
1016 |
coll |
|
1017 |
" |
|
1018 |
||
1019 |
"no error raised: |
|
1020 |
|coll| |
|
1021 |
||
1022 |
coll := #(1 2 3 4 5 6) asSet. |
|
1023 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1024 |
coll |
|
1025 |
" |
|
1026 |
||
12993 | 1027 |
"Modified: / 05-08-2010 / 13:50:33 / cg" |
348 | 1028 |
! |
1029 |
||
5265 | 1030 |
removeAllFoundIn:aCollection |
12993 | 1031 |
"remove all elements from the receiver which are equal to any in aCollection. |
5267 | 1032 |
No error is raised, if some element-to-remove is not in the receiver. |
1033 |
(see also: #removeAll:, which does raise an error)." |
|
4980 | 1034 |
|
1035 |
aCollection do:[:each | self remove:each ifAbsent:[]]. |
|
5265 | 1036 |
^ aCollection |
5268 | 1037 |
|
1038 |
" |
|
1039 |
|coll| |
|
1040 |
||
1041 |
coll := #(1 2 3 4 5 6) asSet. |
|
1042 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1043 |
coll |
|
1044 |
" |
|
1045 |
||
1046 |
"raises an error: |
|
1047 |
|coll| |
|
1048 |
||
1049 |
coll := #(1 2 3 4 5 6) asSet. |
|
1050 |
coll removeAll:#(4 5 6 7 8). |
|
1051 |
coll |
|
1052 |
" |
|
1053 |
||
12993 | 1054 |
"Modified: / 05-08-2010 / 13:51:05 / cg" |
1055 |
! |
|
1056 |
||
1057 |
removeAllIdentical:aCollection |
|
1058 |
"remove all elements from the receiver which are in aCollection. |
|
1059 |
Return the argument, aCollection. |
|
1060 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1061 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1062 |
||
1063 |
Notice: for some collections (those not tuned for |
|
1064 |
resizing themself) this may be very slow. |
|
1065 |
If the number of removed elements is big compared to to |
|
1066 |
the receivers size, it may be better to copy the |
|
1067 |
ones which are not to be removed into a new collection." |
|
1068 |
||
1069 |
aCollection do:[:element | self removeIdentical:element]. |
|
1070 |
^ aCollection |
|
1071 |
||
1072 |
" |
|
1073 |
|coll| |
|
1074 |
||
1075 |
coll := #(1 2 3 4 5 6) asSet. |
|
1076 |
coll removeAll:#(4 5 6). |
|
1077 |
coll |
|
1078 |
" |
|
1079 |
||
1080 |
"raises an error: |
|
1081 |
|coll| |
|
1082 |
||
1083 |
coll := #(1 2 3 4 5 6) asSet. |
|
1084 |
coll removeAll:#(4 5 6 7 8). |
|
1085 |
coll |
|
1086 |
" |
|
1087 |
||
1088 |
"no error raised: |
|
1089 |
|coll| |
|
1090 |
||
1091 |
coll := #(1 2 3 4 5 6) asSet. |
|
1092 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1093 |
coll |
|
1094 |
" |
|
1095 |
||
1096 |
"Created: / 05-08-2010 / 13:51:51 / cg" |
|
1097 |
! |
|
1098 |
||
1099 |
removeAllIdenticalFoundIn:aCollection |
|
1100 |
"remove all elements from the receiver which are in aCollection. |
|
1101 |
No error is raised, if some element-to-remove is not in the receiver. |
|
1102 |
(see also: #removeAll:, which does raise an error)." |
|
1103 |
||
1104 |
aCollection do:[:each | self removeIdentical:each ifAbsent:[]]. |
|
1105 |
^ aCollection |
|
1106 |
||
1107 |
" |
|
1108 |
|coll| |
|
1109 |
||
1110 |
coll := #(1 2 3 4 5 6) asSet. |
|
1111 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
1112 |
coll |
|
1113 |
" |
|
1114 |
||
1115 |
"raises an error: |
|
1116 |
|coll| |
|
1117 |
||
1118 |
coll := #(1 2 3 4 5 6) asSet. |
|
1119 |
coll removeAll:#(4 5 6 7 8). |
|
1120 |
coll |
|
1121 |
" |
|
1122 |
||
1123 |
"Created: / 05-08-2010 / 13:52:21 / cg" |
|
4980 | 1124 |
! |
1125 |
||
12636 | 1126 |
removeAllKeys:aCollection |
1127 |
"remove all keys of the argument, aCollection from the receiver. |
|
1128 |
Raises an error, if some element-to-remove is not in the receiver. |
|
1129 |
Notice: only works for keyed collections, such as dictionaries." |
|
1130 |
||
1131 |
aCollection do:[:element | self removeKey:element]. |
|
1132 |
! |
|
1133 |
||
5265 | 1134 |
removeAllSuchThat:aBlock |
4980 | 1135 |
"Apply the condition to each element and remove it if the condition is true. |
5265 | 1136 |
Return a collection of removed elements. |
5267 | 1137 |
First elements-to-remove are collected, then removed in one operation." |
4980 | 1138 |
|
5267 | 1139 |
|removedElements| |
4980 | 1140 |
|
5267 | 1141 |
removedElements := OrderedCollection new. |
1142 |
self do:[:element | |
|
4980 | 1143 |
(aBlock value:element) ifTrue: [ |
5267 | 1144 |
removedElements add:element |
4980 | 1145 |
] |
1146 |
]. |
|
5267 | 1147 |
self removeAll:removedElements. |
1148 |
^ removedElements |
|
1149 |
||
1150 |
" |
|
1151 |
|coll| |
|
1152 |
||
1153 |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection. |
|
1154 |
coll removeAllSuchThat:[:el | el even]. |
|
1155 |
coll |
|
1156 |
" |
|
1157 |
" |
|
1158 |
|coll| |
|
1159 |
||
1160 |
coll := #(1 2 3 4 5 6 7 8 9 10) asSet. |
|
1161 |
coll removeAllSuchThat:[:el | el even]. |
|
1162 |
coll |
|
1163 |
" |
|
1164 |
||
4980 | 1165 |
! |
1166 |
||
348 | 1167 |
removeFirst |
1168 |
"remove the first element from the receiver. |
|
1169 |
Return the removed element." |
|
1170 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1171 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1172 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1173 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1174 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1175 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1176 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1177 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1178 |
(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
|
1179 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1180 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1181 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 1182 |
! |
1183 |
||
1184 |
removeFirst:n |
|
1185 |
"remove the first n elements from the receiver. |
|
5265 | 1186 |
Return a collection of removed elements. |
360 | 1187 |
Notice: for some collections (those not tuned for |
5265 | 1188 |
resizing themself) this may be very slow." |
348 | 1189 |
|
1190 |
|ret| |
|
1191 |
||
1192 |
self size < n ifTrue:[ |
|
5265 | 1193 |
^ self notEnoughElementsError |
348 | 1194 |
]. |
1195 |
ret := Array new:n. |
|
1196 |
1 to:n do:[:i | |
|
5265 | 1197 |
ret at:i put:(self removeFirst). |
348 | 1198 |
]. |
1199 |
^ ret |
|
1200 |
! |
|
1201 |
||
2351 | 1202 |
removeIdentical:anObject |
1203 |
"search for the first element, which is identical to anObject; |
|
1204 |
if found, remove and return it. |
|
1205 |
If not found, report a 'value not found'-error. |
|
1206 |
Uses identity compare (==) to search for the occurrence." |
|
1207 |
||
1208 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
1209 |
||
1210 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
1211 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
1212 |
! |
|
1213 |
||
1214 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
1215 |
"search for the first element, which is identical to anObject; |
|
1216 |
if found, remove and return it. |
|
1217 |
If not found, return the the value of the exceptionBlock. |
|
1218 |
Uses identity compare (==) to search for the occurrence. |
|
1219 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1220 |
||
1221 |
^ self subclassResponsibility |
|
1222 |
||
1223 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
1224 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
1225 |
! |
|
1226 |
||
602 | 1227 |
removeLast |
1228 |
"remove the last element from the receiver. |
|
2351 | 1229 |
Return the removed element. |
1230 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 1231 |
|
1232 |
^ self subclassResponsibility |
|
2351 | 1233 |
|
1234 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 1235 |
! |
1236 |
||
348 | 1237 |
removeLast:n |
11331 | 1238 |
"remove the last n elements from the receiver collection. |
5265 | 1239 |
Return a collection of removed elements. |
11331 | 1240 |
Notice: for some collections this may be very slow |
1241 |
(those not tuned for resizing themself)." |
|
348 | 1242 |
|
1243 |
|ret| |
|
1244 |
||
1245 |
self size < n ifTrue:[ |
|
5265 | 1246 |
^ self notEnoughElementsError |
348 | 1247 |
]. |
1248 |
ret := Array new:n. |
|
1249 |
n to:1 by:-1 do:[:i | |
|
5265 | 1250 |
ret at:i put:(self removeLast). |
348 | 1251 |
]. |
1252 |
^ ret |
|
1 | 1253 |
! ! |
1254 |
||
8858 | 1255 |
!Collection methodsFor:'bulk operations'! |
1256 |
||
12116 | 1257 |
abs |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1258 |
"absolute value of all elements in the collection" |
12116 | 1259 |
|
1260 |
^ self collect: [:a | a abs] |
|
1261 |
||
1262 |
" |
|
1263 |
TestCase assert:( { 1. -2. -3. 4 } abs = { 1. 2. 3. 4 }). |
|
1264 |
" |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1265 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1266 |
"Modified: / 18-03-2011 / 10:33:50 / cg" |
13316 | 1267 |
! |
1268 |
||
12115 | 1269 |
negated |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1270 |
"negated value of all elements in the collection" |
12115 | 1271 |
|
1272 |
^ self collect:[:a | a negated] |
|
1273 |
||
1274 |
" |
|
1275 |
TestCase assert:( { 1. -2. -3. 4 } negated = { -1. 2. 3. -4 }). |
|
1276 |
" |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1277 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
1278 |
"Modified: / 18-03-2011 / 10:33:53 / cg" |
12115 | 1279 |
! |
1280 |
||
11468 | 1281 |
product |
11498 | 1282 |
"multiply up all elements." |
8858 | 1283 |
|
11468 | 1284 |
self emptyCheck. |
1285 |
^ self |
|
11469 | 1286 |
inject:(self first class unity) |
1287 |
into:[:accum :each | accum * each]. |
|
8858 | 1288 |
|
1289 |
" |
|
11468 | 1290 |
TestCase should:[ Array new sum ] raise:Error. |
1291 |
||
1292 |
TestCase assert:( { 1 } product == 1). |
|
1293 |
TestCase assert:( { 6 } product == 6). |
|
1294 |
TestCase assert:( { 1. 2. 3. 4. 5. } product = 5 factorial ) |
|
1295 |
" |
|
1296 |
! |
|
1297 |
||
1298 |
sum |
|
11498 | 1299 |
"sum up all elements; return 0 for an empty collection." |
11468 | 1300 |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1301 |
self isEmpty ifTrue:[^ 0]. |
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1302 |
|
11468 | 1303 |
^ self |
1304 |
inject:(self first class zero) |
|
1305 |
into:[:accum :each | accum + each]. |
|
1306 |
||
1307 |
||
1308 |
" |
|
11489
6009fd847761
sum returns 0 when applied to an empty receiver
Claus Gittinger <cg@exept.de>
parents:
11475
diff
changeset
|
1309 |
TestCase assert: ( { } sum = 0 ). |
11468 | 1310 |
TestCase assert: ( { 1 } sum = 1 ). |
1311 |
TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ). |
|
1312 |
TestCase assert: ( (1 to:10) sum = 55 ). |
|
1313 |
TestCase assert: ( 'abc' asByteArray sum = 294 ). |
|
1314 |
TestCase assert: ( { 10 +/- 2. |
|
1315 |
20 +/- 4. |
|
1316 |
100 +/- 10 } sum = (130 +/- 16) ). |
|
1317 |
||
1318 |
TestCase assert: ( { (1 / 9). |
|
1319 |
(1 / 7). |
|
1320 |
} sum = (16 / 63) ). |
|
8858 | 1321 |
" |
11514 | 1322 |
! |
1323 |
||
1324 |
sum:aBlock |
|
13041 | 1325 |
"for each element in the receiver, evaluate the argument, aBlock and sum up the results. |
1326 |
Return the total sum or 0 for an empty collection. |
|
11514 | 1327 |
Similar to (self collect...) sum, but avoids creation of an intermediate collection." |
1328 |
||
1329 |
|sum| |
|
1330 |
||
1331 |
self do:[:element | |
|
1332 |
|thisValue| |
|
1333 |
||
1334 |
thisValue := aBlock value:element. |
|
1335 |
sum isNil ifTrue:[ |
|
1336 |
sum := thisValue |
|
1337 |
] ifFalse:[ |
|
1338 |
sum := sum + thisValue |
|
1339 |
]. |
|
1340 |
]. |
|
1341 |
^ sum ? 0 |
|
1342 |
||
1343 |
" |
|
1344 |
((1 to:10) collect:[:n | n squared]) sum |
|
1345 |
((1 to:10) sum:[:n | n squared]) |
|
1346 |
" |
|
13041 | 1347 |
|
1348 |
"Modified: / 23-08-2010 / 18:19:42 / cg" |
|
8858 | 1349 |
! ! |
1350 |
||
602 | 1351 |
!Collection methodsFor:'converting'! |
1352 |
||
1353 |
asArray |
|
1354 |
"return a new Array with the collections elements" |
|
1355 |
||
1356 |
|anArray |
|
1357 |
index "{ Class: SmallInteger }" | |
|
1 | 1358 |
|
602 | 1359 |
anArray := Array new:(self size). |
1360 |
index := 1. |
|
1361 |
self do:[:each | |
|
6135 | 1362 |
anArray at:index put:each. |
1363 |
index := index + 1 |
|
602 | 1364 |
]. |
1365 |
^ anArray |
|
1 | 1366 |
! |
1367 |
||
602 | 1368 |
asBag |
1369 |
"return a new Bag with the receiver collections elements" |
|
1 | 1370 |
|
602 | 1371 |
^ self addAllTo:(Bag new) |
1 | 1372 |
! |
1373 |
||
602 | 1374 |
asByteArray |
1375 |
"return a new ByteArray with the collections elements |
|
12119 | 1376 |
(which must convert to 8bit integers in the range 0..255)." |
1377 |
||
1378 |
^ self asIntegerArray:ByteArray |
|
1 | 1379 |
! |
1380 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1381 |
asCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1382 |
"return myself as a Collection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1383 |
I am already a Collection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1384 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1385 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1386 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1387 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1388 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1389 |
|
7906 | 1390 |
asDictionary |
1391 |
"return a new Dictionary with the receiver collections elements" |
|
1392 |
||
1393 |
|d| |
|
1394 |
||
1395 |
d := Dictionary new. |
|
1396 |
self keysAndValuesDo:[:k :v | d at:k put:v]. |
|
1397 |
^ d |
|
1398 |
||
1399 |
" |
|
1400 |
#(10 20 30 40 50 60 70 80 90) asDictionary |
|
1401 |
" |
|
1402 |
! |
|
1403 |
||
602 | 1404 |
asDoubleArray |
1405 |
"return a new DoubleArray with the collections elements |
|
12119 | 1406 |
(which must convert to 64bit floats)." |
1407 |
||
1408 |
^ self asIntegerArray:DoubleArray |
|
602 | 1409 |
! |
68 | 1410 |
|
11412 | 1411 |
asFlatOrderedCollection |
13319
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1412 |
"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
|
1413 |
and recursively of all collectionelements from the receiver" |
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1414 |
|
11412 | 1415 |
|coll| |
1416 |
||
1417 |
coll := OrderedCollection new. |
|
1418 |
self flatDo:[:el | coll add:el]. |
|
1419 |
^ coll. |
|
1420 |
||
1421 |
" |
|
1422 |
#( |
|
1423 |
(1 2 3) |
|
1424 |
4 5 |
|
1425 |
(6) |
|
1426 |
7 |
|
13319
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1427 |
(8 (9 10) 11 12 (13 (14 (15) 16))) |
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1428 |
) asFlatOrderedCollection |
11412 | 1429 |
" |
13319
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1430 |
|
08126c4bdb61
comment/format in: #asFlatOrderedCollection
Claus Gittinger <cg@exept.de>
parents:
13317
diff
changeset
|
1431 |
"Modified: / 20-03-2011 / 22:02:36 / cg" |
11412 | 1432 |
! |
1433 |
||
602 | 1434 |
asFloatArray |
1435 |
"return a new FloatArray with the collections elements |
|
12119 | 1436 |
(which must convert to 32bit floats)." |
1437 |
||
1438 |
^ self asIntegerArray:FloatArray |
|
602 | 1439 |
! |
1440 |
||
1441 |
asIdentitySet |
|
1442 |
"return a new IdentitySet with the receiver collections elements" |
|
1443 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1444 |
^ self addAllNonNilElementsTo:(IdentitySet new:self size) |
68 | 1445 |
! |
1446 |
||
12119 | 1447 |
asIntegerArray |
1448 |
"return a new IntegerArray with the collection's elements |
|
1449 |
(which must convert to 32bit integers in the range 0..16rFFFFFFFF)." |
|
1450 |
||
1451 |
^ self asIntegerArray:IntegerArray |
|
1452 |
! |
|
1453 |
||
1454 |
asIntegerArray:arrayClass |
|
1455 |
"return a new Array with the collection's elements" |
|
1456 |
||
1457 |
|anIntegerArray |
|
1458 |
index "{ Class: SmallInteger }" | |
|
1459 |
||
1460 |
anIntegerArray := arrayClass new:(self size). |
|
1461 |
index := 1. |
|
1462 |
self do:[:each | |
|
1463 |
anIntegerArray at:index put:each asInteger. |
|
1464 |
index := index + 1 |
|
1465 |
]. |
|
1466 |
^ anIntegerArray |
|
1467 |
! |
|
1468 |
||
2167 | 1469 |
asList |
1470 |
"return a new List with the receiver collections elements" |
|
1471 |
||
1472 |
^ self addAllTo:(List new:self size) |
|
1473 |
||
1474 |
"Created: 14.2.1997 / 16:25:23 / cg" |
|
1475 |
! |
|
1476 |
||
8050 | 1477 |
asLongIntegerArray |
1478 |
"return a new LongIntegerArray with the collections elements |
|
12119 | 1479 |
(which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)." |
1480 |
||
1481 |
^ self asIntegerArray:LongIntegerArray |
|
8050 | 1482 |
! |
1483 |
||
602 | 1484 |
asOrderedCollection |
1485 |
"return a new OrderedCollection with the receiver collections elements" |
|
1486 |
||
1487 |
^ self addAllTo:(OrderedCollection new:self size) |
|
1488 |
! |
|
1489 |
||
11469 | 1490 |
asRunArray |
1491 |
"return a new RunArray with the collections elements" |
|
1492 |
||
1493 |
^ RunArray from:self. |
|
1494 |
||
1495 |
"/ |runs lastElement occurrences| |
|
1496 |
"/ |
|
1497 |
"/ runs := RunArray new. |
|
1498 |
"/ occurrences := 0. |
|
1499 |
"/ self do:[:each | |
|
1500 |
"/ each == lastElement ifTrue:[ |
|
1501 |
"/ occurrences := occurrences + 1 |
|
1502 |
"/ ] ifFalse:[ |
|
1503 |
"/ runs add:lastElement withOccurrences:occurrences. |
|
1504 |
"/ occurrences := 1. |
|
1505 |
"/ lastElement := each |
|
1506 |
"/ ]. |
|
1507 |
"/ ]. |
|
1508 |
"/ occurrences ~~ 0 ifTrue:[ |
|
1509 |
"/ runs add:lastElement withOccurrences:occurrences |
|
1510 |
"/ ]. |
|
1511 |
"/ ^ runs |
|
1394 | 1512 |
|
1513 |
" |
|
11469 | 1514 |
#(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray |
1394 | 1515 |
" |
11469 | 1516 |
|
1517 |
"Modified: / 7.4.1998 / 09:50:54 / cg" |
|
1394 | 1518 |
! |
1519 |
||
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1520 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1521 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1522 |
I am already a Collection, but not sequenceable." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1523 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1524 |
^ self asArray |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1525 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1526 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1527 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5557
diff
changeset
|
1528 |
|
602 | 1529 |
asSet |
1530 |
"return a new Set with the receiver collections elements" |
|
1531 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1532 |
^ self addAllNonNilElementsTo:(Set new:self size) |
602 | 1533 |
! |
1534 |
||
9353 | 1535 |
asSharedCollection |
1536 |
"return a shared collection on the receiver. |
|
1537 |
This implements synchronized access to me." |
|
1538 |
||
1539 |
^ SharedCollection for:self. |
|
1540 |
! |
|
1541 |
||
13773
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1542 |
asSignedIntegerArray |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1543 |
"return a new SignedIntegerArray with the collection's elements |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1544 |
(which must convert to 32bit signed integers in the range -16r80000000..16r7FFFFFFF)." |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1545 |
|
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1546 |
^ self asIntegerArray:SignedIntegerArray |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1547 |
|
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1548 |
"Created: / 07-10-2011 / 13:14:01 / cg" |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1549 |
! |
8de2745d8a24
added: #asSignedIntegerArray
Claus Gittinger <cg@exept.de>
parents:
13741
diff
changeset
|
1550 |
|
602 | 1551 |
asSortedCollection |
1552 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 1553 |
|
602 | 1554 |
|aSortedCollection| |
1555 |
||
1556 |
aSortedCollection := SortedCollection new:self size. |
|
1557 |
aSortedCollection addAll:self. |
|
1558 |
^ aSortedCollection |
|
1559 |
! |
|
1560 |
||
1561 |
asSortedCollection:sortBlock |
|
1562 |
"return a new SortedCollection with the receiver collections elements, |
|
1563 |
using sortBlock for comparing" |
|
1564 |
||
1565 |
|aSortedCollection| |
|
1566 |
||
1567 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
1568 |
aSortedCollection addAll:self. |
|
1569 |
^ aSortedCollection |
|
1 | 1570 |
! |
1571 |
||
2919 | 1572 |
asSortedStrings |
1573 |
"Create & return a SortedCollection that sorts the receivers |
|
1574 |
elements according to the locales collating policy. |
|
1575 |
This is currently not really support - strings are sorted |
|
1576 |
without caring for the locale." |
|
1577 |
||
1578 |
|aSortedCollection| |
|
1579 |
||
1580 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1581 |
aSortedCollection addAll:self. |
|
1582 |
^ aSortedCollection |
|
1583 |
||
1584 |
"Created: 13.9.1997 / 09:36:22 / cg" |
|
1585 |
"Modified: 13.9.1997 / 09:43:00 / cg" |
|
1586 |
! |
|
1587 |
||
1588 |
asSortedStrings:sortBlock |
|
1589 |
"Create & return a SortedCollection that sorts the receivers |
|
3061 | 1590 |
elements using sortBlock and according to the locales collating policy, |
1591 |
which is passed as first arg to sortBlock. |
|
2919 | 1592 |
This is currently not really support - strings are sorted |
1593 |
without caring for the locale." |
|
1594 |
||
1595 |
|aSortedCollection| |
|
1596 |
||
1597 |
aSortedCollection := SortedCollection forStrings:self size. |
|
1598 |
aSortedCollection sortBlock:sortBlock. |
|
1599 |
aSortedCollection addAll:self. |
|
1600 |
^ aSortedCollection |
|
1601 |
||
3061 | 1602 |
"Created: / 13.9.1997 / 09:36:45 / cg" |
1603 |
"Modified: / 27.10.1997 / 16:39:48 / cg" |
|
2919 | 1604 |
! |
1605 |
||
1606 |
asSortedStrings:sortBlock with:aCollationPolicy |
|
1607 |
"Create & return a SortedCollection that sorts the receivers |
|
1608 |
elements using sortBlock and according to the specified locales collating policy. |
|
1609 |
This is currently not really support - strings are sorted |
|
1610 |
without caring for the locale." |
|
1611 |
||
1612 |
|aSortedCollection| |
|
1613 |
||
1614 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1615 |
aSortedCollection sortBlock:sortBlock. |
|
1616 |
aSortedCollection addAll:self. |
|
1617 |
^ aSortedCollection |
|
1618 |
||
1619 |
"Created: 13.9.1997 / 09:37:21 / cg" |
|
1620 |
"Modified: 13.9.1997 / 09:45:27 / cg" |
|
1621 |
! |
|
1622 |
||
1623 |
asSortedStringsWith: aCollationPolicy |
|
1624 |
"Create & return a SortedCollection that sorts the receivers |
|
1625 |
elements according to the specified locales collating policy. |
|
1626 |
This is currently not really support - strings are sorted |
|
1627 |
without caring for the locale." |
|
1628 |
||
1629 |
|aSortedCollection| |
|
1630 |
||
1631 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1632 |
aSortedCollection addAll:self. |
|
1633 |
^ aSortedCollection |
|
1634 |
||
1635 |
"Created: 13.9.1997 / 09:37:50 / cg" |
|
1636 |
"Modified: 13.9.1997 / 09:44:08 / cg" |
|
1637 |
! |
|
1638 |
||
602 | 1639 |
asString |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1640 |
"return a String with the collection's elements |
602 | 1641 |
(which must convert to characters)" |
1642 |
||
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1643 |
|string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1644 |
index "{ Class: SmallInteger }" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1645 |
char bitsPerCharacter| |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1646 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1647 |
string := String new:(self size). |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1648 |
bitsPerCharacter := string bitsPerCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1649 |
|
602 | 1650 |
index := 1. |
1651 |
self do:[:each | |
|
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1652 |
char := each asCharacter. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1653 |
char bitsPerCharacter > bitsPerCharacter ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1654 |
char bitsPerCharacter == 16 ifTrue:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1655 |
string := Unicode16String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1656 |
] ifFalse:[ |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1657 |
string := Unicode32String fromString:string. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1658 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1659 |
]. |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1660 |
string at:index put:char. |
6135 | 1661 |
index := index + 1 |
360 | 1662 |
]. |
11708
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1663 |
^ string |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1664 |
|
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1665 |
" |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1666 |
#(80 81 82) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1667 |
#(16r8000 16r8001 16r8002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1668 |
#(16r800000 16r800001 16r800002) asString |
43d1d6cf1ba3
fixed asString for non-8-bit characters
Claus Gittinger <cg@exept.de>
parents:
11706
diff
changeset
|
1669 |
" |
602 | 1670 |
! |
1671 |
||
1672 |
asStringCollection |
|
1413 | 1673 |
"return a new string collection containing the elements; |
1674 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 1675 |
|
1676 |
^ self addAllTo:(StringCollection new) |
|
1413 | 1677 |
|
1678 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 1679 |
! |
1680 |
||
8160 | 1681 |
asUnicodeString |
1682 |
"return a String with the collections elements |
|
1683 |
(which must convert to characters)" |
|
1684 |
||
1685 |
|aString |
|
1686 |
index "{ Class: SmallInteger }" | |
|
1687 |
||
1688 |
aString := UnicodeString uninitializedNew:self size. |
|
1689 |
index := 1. |
|
1690 |
self do:[:each | |
|
1691 |
aString at:index put:each asCharacter. |
|
1692 |
index := index + 1 |
|
1693 |
]. |
|
1694 |
^ aString |
|
1695 |
||
1696 |
" |
|
1697 |
#(16r440 16r443 16r441 16r441 16r43A 16r438 16r439 16r20 16r44F 16r437 16r44B 16r43A) asUnicodeString |
|
1698 |
" |
|
1699 |
! |
|
1700 |
||
7885 | 1701 |
asWordArray |
1702 |
"return a new WordArray with the collections elements |
|
1703 |
(which must convert to integers in the range 0..16rFFFF)." |
|
1704 |
||
1705 |
|aWordArray |
|
1706 |
index "{ Class: SmallInteger }" | |
|
1707 |
||
1708 |
aWordArray := WordArray new:(self size). |
|
1709 |
index := 1. |
|
1710 |
self do:[:each | |
|
1711 |
aWordArray at:index put:each asInteger. |
|
1712 |
index := index + 1 |
|
1713 |
]. |
|
1714 |
^ aWordArray |
|
1715 |
! |
|
1716 |
||
12291 | 1717 |
copyAs:collectionClass |
1718 |
"return a new instance of collectionClass with the receiver collection's elements. |
|
1719 |
This is similar to copy as:collectionClass, to ensure that we get a new |
|
1720 |
(unshared) collection, but avoids the copy if the receiver is not already an |
|
1721 |
instance of collectionClass." |
|
1722 |
||
1723 |
|newColl| |
|
1724 |
||
1725 |
newColl := self as:collectionClass. |
|
1726 |
newColl == self ifTrue:[ |
|
1727 |
^ self copy |
|
1728 |
]. |
|
1729 |
^ newColl. |
|
1730 |
||
1731 |
" |
|
1732 |
|coll1 coll2| |
|
1733 |
||
1734 |
coll1 := #(1 2 3 4 5). |
|
1735 |
coll2 := coll1 copyAs:Array. '-- will generate a copy'. |
|
1736 |
self assert:(coll1 ~~ coll2). |
|
1737 |
self assert:(coll1 = coll2). |
|
1738 |
||
1739 |
coll1 := #(1 2 3 4 5). |
|
1740 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate an OC'. |
|
1741 |
self assert:(coll1 ~~ coll2). |
|
12292
5d5c2bc66cc0
comment/format in: #copyAs:
Claus Gittinger <cg@exept.de>
parents:
12291
diff
changeset
|
1742 |
self assert:(coll1 isSameSequenceAs: coll2). |
12291 | 1743 |
|
1744 |
coll1 := #(1 2 3 4 5) asOrderedCollection. |
|
1745 |
coll2 := coll1 copyAs:OrderedCollection. '-- will generate a copy'. |
|
1746 |
self assert:(coll1 ~~ coll2). |
|
1747 |
self assert:(coll1 = coll2). |
|
1748 |
" |
|
1749 |
! |
|
1750 |
||
12249 | 1751 |
copyAsOrderedCollection |
1752 |
"return a new OrderedCollection with the receiver collection's elements. |
|
1753 |
This is similar to copy asOrderedCollection, to ensure that we get a new |
|
1754 |
(unshared) collection, but avoids the copy if the receiver is not already an |
|
1755 |
OrderedCollection." |
|
1756 |
||
1757 |
|newColl| |
|
1758 |
||
1759 |
newColl := self asOrderedCollection. |
|
1760 |
newColl == self ifTrue:[ |
|
1761 |
^ self copy |
|
1762 |
]. |
|
1763 |
^ newColl. |
|
1764 |
! |
|
1765 |
||
7905 | 1766 |
keysAndValues |
1767 |
|assocs| |
|
1768 |
||
1769 |
assocs := OrderedCollection new. |
|
1770 |
self keysAndValuesDo:[:k :v | |
|
1771 |
assocs add:(k -> v). |
|
1772 |
]. |
|
1773 |
^ assocs. |
|
1774 |
||
1775 |
" |
|
1776 |
#(10 20 30 40 50 60) keysAndValues |
|
1777 |
" |
|
1778 |
! |
|
1779 |
||
9410 | 1780 |
literalArrayEncoding |
1781 |
"encode myself as an array literal, from which a copy of the receiver |
|
1782 |
can be reconstructed with #decodeAsLiteralArray." |
|
1783 |
||
1784 |
|encoding idx| |
|
1785 |
||
1786 |
encoding := Array new:self size + 1. |
|
1787 |
encoding at:1 put:self class name. |
|
1788 |
idx := 2. |
|
1789 |
self do:[:element| |
|
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1790 |
encoding at:idx put:element literalArrayEncoding. |
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1791 |
idx := idx + 1. |
9410 | 1792 |
]. |
1793 |
^ encoding |
|
1794 |
||
1795 |
||
1796 |
" |
|
1797 |
(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
|
1798 |
with:(1 @ 2) |
9410 | 1799 |
) literalArrayEncoding decodeAsLiteralArray |
1800 |
" |
|
1801 |
||
1802 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
1803 |
! |
|
1804 |
||
602 | 1805 |
readStream |
1806 |
"return a stream for reading from the receiver" |
|
1807 |
||
1808 |
^ ReadStream on:self |
|
88 | 1809 |
|
1810 |
" |
|
602 | 1811 |
|s| |
1812 |
||
1813 |
s := 'hello world' readStream. |
|
1814 |
s next:5. |
|
1815 |
s next. |
|
1816 |
(s next:5) inspect |
|
88 | 1817 |
" |
1 | 1818 |
! |
1819 |
||
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1820 |
readStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1821 |
"return a stream for reading from the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1822 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1823 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1824 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1825 |
^ self readStream |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1826 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1827 |
|
8631 | 1828 |
readWriteStream |
1829 |
"return a stream for reading and writing from/to the receiver" |
|
1830 |
||
1831 |
^ ReadWriteStream with:self |
|
1832 |
||
1833 |
" |
|
11436 | 1834 |
'hello world' readWriteStream |
1835 |
nextPutAll:'+Foo'; |
|
1836 |
contents |
|
1837 |
||
1838 |
'hello world' readWriteStream |
|
1839 |
setToEnd; |
|
1840 |
nextPutAll:'+Foo'; |
|
1841 |
contents |
|
8631 | 1842 |
" |
1843 |
! |
|
1844 |
||
602 | 1845 |
writeStream |
1846 |
"return a stream for writing onto the receiver" |
|
1847 |
||
1848 |
^ WriteStream on:self |
|
2 | 1849 |
|
602 | 1850 |
" |
1851 |
|s| |
|
1852 |
||
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
1853 |
s := #() writeStream. |
602 | 1854 |
s nextPut:1. |
1855 |
s nextPut:2. |
|
1856 |
s nextPut:3. |
|
1857 |
s contents inspect |
|
1858 |
" |
|
88 | 1859 |
|
1860 |
" |
|
602 | 1861 |
|s| |
2 | 1862 |
|
602 | 1863 |
s := OrderedCollection new writeStream. |
1864 |
s nextPut:1. |
|
1865 |
s nextPut:2. |
|
1866 |
s nextPut:3. |
|
1867 |
s contents inspect |
|
1868 |
" |
|
8140
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1869 |
! |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1870 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1871 |
writeStreamOrNil |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1872 |
"return a stream for writing onto the receiver. |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1873 |
This has been defined for protocol compatibility with FileName, |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1874 |
but nil is never returned here" |
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1875 |
|
7cc7ac5f29ca
Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents:
8050
diff
changeset
|
1876 |
^ self writeStream |
602 | 1877 |
! ! |
1 | 1878 |
|
602 | 1879 |
!Collection methodsFor:'copying'! |
1880 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1881 |
copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1882 |
"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
|
1883 |
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
|
1884 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1885 |
^ self shallowCopy postCopyFrom:self |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1886 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1887 |
"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
|
1888 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1889 |
|
602 | 1890 |
copyEmpty |
1891 |
"return a copy of the receiver with no elements. |
|
1892 |
This is used by copying and enumeration methods |
|
1893 |
to get a new instance which is similar to the receiver." |
|
1894 |
||
1895 |
^ self species new |
|
1 | 1896 |
! |
1897 |
||
602 | 1898 |
copyEmpty:size |
1899 |
"return a copy of the receiver with no elements, but space for |
|
1900 |
size elements. This is used by copying and enumeration methods |
|
1901 |
to get a new instance which is similar to the receiver. |
|
1902 |
This method should be redefined in subclasses with instance |
|
1903 |
variables, which should be put into the copy too. |
|
1904 |
For example, SortedCollection has to copy its sortBlock into the |
|
1905 |
new collection." |
|
1 | 1906 |
|
602 | 1907 |
^ self species new:size |
360 | 1908 |
! |
1909 |
||
602 | 1910 |
copyEmptyAndGrow:size |
1911 |
"return a copy of the receiver with size nil elements. |
|
1912 |
This is used by copying and enumeration methods |
|
1913 |
to get a new instance which is similar to the receiver." |
|
155 | 1914 |
|
602 | 1915 |
^ (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
|
1916 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1917 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1918 |
postCopyFrom:original |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1919 |
"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
|
1920 |
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
|
1921 |
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
|
1922 |
setHash: functionality)." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1923 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1924 |
"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
|
1925 |
^ self postCopy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1926 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1927 |
"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
|
1928 |
"Modified: / 19.4.1998 / 20:03:57 / cg" |
1 | 1929 |
! ! |
1930 |
||
1931 |
!Collection methodsFor:'enumerating'! |
|
1932 |
||
10795
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1933 |
addAllNonNilElementsTo:aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1934 |
"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
|
1935 |
Return aCollection." |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1936 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1937 |
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
|
1938 |
^ aCollection |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1939 |
|
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1940 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1941 |
#(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
|
1942 |
" |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1943 |
! |
3797cbc3fcb4
Do not add nil elements to a Set in #asSet and #asIdentitySet
Stefan Vogel <sv@exept.de>
parents:
10750
diff
changeset
|
1944 |
|
602 | 1945 |
addAllTo:aCollection |
1946 |
"add all elements of the receiver, to aCollection. |
|
1947 |
Return aCollection." |
|
77 | 1948 |
|
602 | 1949 |
self do:[:each | aCollection add:each]. |
1950 |
^ aCollection |
|
5254 | 1951 |
|
1952 |
" |
|
1953 |
#(1 2 3 4 5 1 2 3 4 5) addAllTo:Set new |
|
1954 |
" |
|
1955 |
||
1956 |
"Modified: / 11.2.2000 / 11:22:14 / cg" |
|
77 | 1957 |
! |
1958 |
||
1 | 1959 |
collect:aBlock |
1960 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1961 |
and return a new collection with the results" |
|
1962 |
||
1963 |
|newCollection| |
|
1964 |
||
201 | 1965 |
newCollection := self species new. |
1966 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 1967 |
^ newCollection |
68 | 1968 |
|
1969 |
" |
|
1970 |
#(1 2 3 4) collect:[:n | n * 2] |
|
1971 |
" |
|
1 | 1972 |
! |
1973 |
||
11871 | 1974 |
collect:aBlock as:aClass |
1975 |
"like collect, but use an instance of aClass to collect the results. |
|
1976 |
Also avoids the need for an extra intermediate collection which is created with |
|
1977 |
the standard coding: 'self asXXXX collect:[...]" |
|
1978 |
||
1979 |
|newCollection |
|
1980 |
idx "{ Class:SmallInteger }" |
|
1981 |
sz "{ Class:SmallInteger }"| |
|
1982 |
||
1983 |
sz := self size. |
|
1984 |
newCollection := (aClass new:sz) grow:sz. |
|
12008 | 1985 |
newCollection isSequenceable ifTrue:[ |
1986 |
idx := 1. |
|
1987 |
self do:[:el | |
|
1988 |
newCollection at:idx put:(aBlock value:el). |
|
1989 |
idx := idx + 1. |
|
1990 |
]. |
|
1991 |
] ifFalse:[ |
|
1992 |
self do:[:el | |
|
1993 |
newCollection add:(aBlock value:el). |
|
1994 |
]. |
|
11871 | 1995 |
]. |
1996 |
^ newCollection |
|
1997 |
||
1998 |
" |
|
1999 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
|
2000 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
|
2001 |
" |
|
2002 |
! |
|
2003 |
||
14027 | 2004 |
collect:collectBlock thenDo:aBlock |
2005 |
"combination of collect followed by do. |
|
2006 |
Avoids the creation of intermediate garbage" |
|
2007 |
||
2008 |
self do:[:each | aBlock value:(collectBlock value:each)] |
|
2009 |
||
2010 |
" |
|
2011 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenDo:[:i | Transcript showCR:i ] |
|
2012 |
" |
|
2013 |
||
2014 |
"Created: / 28-02-2012 / 21:05:16 / cg" |
|
2015 |
! |
|
2016 |
||
11928 | 2017 |
collect:collectBlock thenReject:rejectBlock |
2018 |
"combination of collect followed by reject. |
|
2019 |
May be redefined by some subclasses for optimal performance |
|
2020 |
(avoiding the creation of intermediate garbage)" |
|
2021 |
||
12943
85c58cb22c32
changed: #collect:thenReject:
Claus Gittinger <cg@exept.de>
parents:
12666
diff
changeset
|
2022 |
^ self collect:collectBlock thenSelect:[:el | (rejectBlock value:el) not] |
11928 | 2023 |
|
2024 |
" |
|
2025 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenReject:[:i | i > 10] |
|
2026 |
" |
|
12943
85c58cb22c32
changed: #collect:thenReject:
Claus Gittinger <cg@exept.de>
parents:
12666
diff
changeset
|
2027 |
|
85c58cb22c32
changed: #collect:thenReject:
Claus Gittinger <cg@exept.de>
parents:
12666
diff
changeset
|
2028 |
"Modified: / 11-07-2010 / 17:03:16 / cg" |
11928 | 2029 |
! |
2030 |
||
4890 | 2031 |
collect:collectBlock thenSelect:selectBlock |
2032 |
"combination of collect followed by select. |
|
2033 |
May be redefined by some subclasses for optimal performance |
|
7164 | 2034 |
(avoiding the creation of intermediate garbage)" |
4890 | 2035 |
|
4892 | 2036 |
^ (self collect:collectBlock) select:selectBlock |
2037 |
||
2038 |
" |
|
2039 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10] |
|
2040 |
" |
|
4890 | 2041 |
! |
2042 |
||
6867 | 2043 |
collectAll:aBlock |
2044 |
"for each element in the receiver, evaluate the argument, aBlock. |
|
9315 | 2045 |
The block is supposed to return a collection, whose elements are collected. |
8608 | 2046 |
The species of the returned collection is that of the first returned |
2047 |
partial result." |
|
6867 | 2048 |
|
2049 |
|result| |
|
2050 |
||
2051 |
self do:[:element | |
|
2052 |
|individualResult| |
|
2053 |
||
2054 |
individualResult := aBlock value:element. |
|
2055 |
result isNil ifTrue:[ |
|
2056 |
result := individualResult speciesForAdding new. |
|
2057 |
]. |
|
2058 |
result addAll:individualResult. |
|
2059 |
]. |
|
2060 |
||
8609
daa0d6682135
collectAll: now returns an empty collection if
Claus Gittinger <cg@exept.de>
parents:
8608
diff
changeset
|
2061 |
^ result ? #() |
6867 | 2062 |
|
2063 |
" |
|
2064 |
#(1 2 3 4) collectAll:[:n | Array new:n withAll:n ] |
|
7629 | 2065 |
#(1 2 3 4) collectAll:[:n | Array with:n with:n squared ] |
8607 | 2066 |
#(1 2 3 4) collectAll:[:n | 1 to:n ] |
2067 |
(Array with:Point with:Rectangle) collectAll:[:c | c instVarNames ] |
|
7629 | 2068 |
" |
6867 | 2069 |
! |
2070 |
||
602 | 2071 |
count:aBlock |
2072 |
"count elements, for which aBlock returns true. |
|
2073 |
Return the sum." |
|
2074 |
||
7336 | 2075 |
|count| |
2076 |
||
2077 |
count := 0. |
|
2078 |
self do:[:element | |
|
2079 |
(aBlock value:element) ifTrue:[count := count + 1] |
|
2080 |
]. |
|
2081 |
^ count |
|
77 | 2082 |
|
2083 |
" |
|
602 | 2084 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
7336 | 2085 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
77 | 2086 |
" |
2087 |
! |
|
2088 |
||
1 | 2089 |
detect:aBlock |
2090 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2091 |
the block returns true; in this case return the element which caused |
|
2092 |
the true evaluation. |
|
345 | 2093 |
If none of the evaluations returns true, report an error" |
1 | 2094 |
|
2095 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 2096 |
|
2097 |
" |
|
2098 |
#(1 2 3 4) detect:[:n | n odd] |
|
2099 |
#(2 4 6 8) detect:[:n | n odd] |
|
2100 |
" |
|
1 | 2101 |
! |
2102 |
||
8750 | 2103 |
detect:generatorBlock forWhich:testBlock ifNone:exceptionBlock |
2104 |
"evaluate generatorBlock for each element in the receiver until |
|
2105 |
testBlock returns true for it; |
|
2106 |
in this case return the value from generatorBlock, which caused the true evaluation. |
|
2107 |
If none of the test evaluations returns true, return the result of the |
|
2108 |
evaluation of the exceptionBlock" |
|
2109 |
||
2110 |
self do:[:each | |
|
2111 |
|val| |
|
2112 |
||
2113 |
val := generatorBlock value:each. |
|
2114 |
(testBlock value:val) ifTrue:[^ val]. |
|
2115 |
]. |
|
2116 |
^ exceptionBlock value |
|
2117 |
||
2118 |
" |
|
2119 |
#(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
2120 |
#( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry'] |
|
8751 | 2121 |
|
2122 |
#( 'st' 'c' 'java' ) |
|
2123 |
detect:[:ext | 'Foo' asFilename withSuffix:ext] |
|
2124 |
forWhich:[:fn | fn exists] |
|
2125 |
ifNone:nil |
|
8750 | 2126 |
" |
2127 |
! |
|
2128 |
||
9842 | 2129 |
detect:aOneArgBlock ifNone:exceptionBlock |
1 | 2130 |
"evaluate the argument, aBlock for each element in the receiver until |
2131 |
the block returns true; in this case return the element which caused |
|
2132 |
the true evaluation. |
|
2133 |
If none of the evaluations returns true, return the result of the |
|
2134 |
evaluation of the exceptionBlock" |
|
2135 |
||
2136 |
self do:[:each | |
|
9842 | 2137 |
(aOneArgBlock value:each) ifTrue:[^ each]. |
1 | 2138 |
]. |
2139 |
^ exceptionBlock value |
|
68 | 2140 |
|
2141 |
" |
|
2142 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
2143 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
2144 |
" |
|
9842 | 2145 |
|
2146 |
"Modified: / 13-09-2006 / 11:17:42 / cg" |
|
1 | 2147 |
! |
2148 |
||
421 | 2149 |
detectLast:aBlock |
2150 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2151 |
the block returns true; in this case return the element which caused |
|
2152 |
the true evaluation. The elements are processed in reverse order. |
|
2153 |
If none of the evaluations returns true, report an error" |
|
2154 |
||
2155 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
2156 |
||
2157 |
" |
|
2158 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
2159 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
2160 |
" |
|
2161 |
! |
|
2162 |
||
2163 |
detectLast:aBlock ifNone:exceptionBlock |
|
2164 |
"evaluate the argument, aBlock for each element in the receiver until |
|
2165 |
the block returns true; in this case return the element which caused |
|
2166 |
the true evaluation. The elements are processed in reverse order. |
|
2167 |
If none of the evaluations returns true, return the result of the |
|
2168 |
evaluation of the exceptionBlock" |
|
2169 |
||
2170 |
self reverseDo:[:each | |
|
6135 | 2171 |
(aBlock value:each) ifTrue:[^ each]. |
421 | 2172 |
]. |
2173 |
^ exceptionBlock value |
|
2174 |
||
2175 |
" |
|
2176 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2177 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
2178 |
" |
|
2179 |
! |
|
2180 |
||
13608 | 2181 |
detectMax: aBlock |
2182 |
"Evaluate aBlock with each of the receiver's elements as the argument. |
|
2183 |
Answer the element for which aBlock evaluates to the highest magnitude. |
|
2184 |
If collection empty, return nil. This method might also be called elect:." |
|
2185 |
||
2186 |
| maxElement maxValue | |
|
2187 |
self do: [:each | | val | |
|
2188 |
maxValue == nil |
|
2189 |
ifFalse: [ |
|
2190 |
(val := aBlock value: each) > maxValue ifTrue: [ |
|
2191 |
maxElement := each. |
|
2192 |
maxValue := val]] |
|
2193 |
ifTrue: ["first element" |
|
2194 |
maxElement := each. |
|
2195 |
maxValue := aBlock value: each]. |
|
2196 |
"Note that there is no way to get the first element that works |
|
2197 |
for all kinds of Collections. Must test every one."]. |
|
2198 |
^ maxElement |
|
2199 |
||
2200 |
"Created: / 20-08-2011 / 21:34:49 / cg" |
|
2201 |
! |
|
2202 |
||
2203 |
detectMin: aBlock |
|
2204 |
"Evaluate aBlock with each of the receiver's elements as the argument. |
|
2205 |
Answer the element for which aBlock evaluates to the lowest number. |
|
2206 |
If collection empty, return nil." |
|
2207 |
||
2208 |
| minElement minValue | |
|
2209 |
self do: [:each | | val | |
|
2210 |
minValue == nil |
|
2211 |
ifFalse: [ |
|
2212 |
(val := aBlock value: each) < minValue ifTrue: [ |
|
2213 |
minElement := each. |
|
2214 |
minValue := val]] |
|
2215 |
ifTrue: ["first element" |
|
2216 |
minElement := each. |
|
2217 |
minValue := aBlock value: each]. |
|
2218 |
"Note that there is no way to get the first element that works |
|
2219 |
for all kinds of Collections. Must test every one."]. |
|
2220 |
^ minElement |
|
2221 |
||
2222 |
"Created: / 20-08-2011 / 21:35:13 / cg" |
|
2223 |
! |
|
2224 |
||
602 | 2225 |
do:aBlock |
2226 |
"evaluate the argument, aBlock for each element" |
|
2227 |
||
2228 |
^ self subclassResponsibility |
|
2229 |
! |
|
2230 |
||
4394 | 2231 |
do:aBlock inBetweenDo:betweenBlock |
2232 |
"evaluate the argument, aBlock for each element. |
|
2233 |
Between elements (i.e. after each except for the last), |
|
2234 |
evaluate betweenBlock. |
|
5254 | 2235 |
This is a utility helper for collection printers |
2236 |
(for example, to print a space between elements)." |
|
4394 | 2237 |
|
8359 | 2238 |
<resource: #obsolete> |
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2239 |
self obsoleteMethodWarning:'use #do:separatedBy:'. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2240 |
^ self do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2241 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2242 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2243 |
#(1 2 3 4) do:[:el | Transcript show:el] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2244 |
inBetweenDo:[ Transcript show:'-'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2245 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2246 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2247 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2248 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2249 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2250 |
(Dictionary with:(1->'one') with:(2->'two')) |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2251 |
associations |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2252 |
do:[:el | Transcript showCR:el printString] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2253 |
inBetweenDo:[ Transcript showCR:'----'] |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2254 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2255 |
" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2256 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2257 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2258 |
! |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2259 |
|
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2260 |
do:aBlock separatedBy:betweenBlock |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2261 |
"evaluate the argument, aBlock for each element. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2262 |
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
|
2263 |
evaluate betweenBlock. |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2264 |
This is a utility helper for collection printers |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2265 |
(for example, to print a space between elements)." |
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2266 |
|
4394 | 2267 |
|first| |
2268 |
||
2269 |
first := true. |
|
2270 |
self do:[:element | |
|
2271 |
first ifTrue:[ |
|
2272 |
first := false |
|
2273 |
] ifFalse:[ |
|
2274 |
betweenBlock value. |
|
2275 |
]. |
|
2276 |
aBlock value:element |
|
2277 |
]. |
|
2278 |
||
2279 |
" |
|
2280 |
#(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
|
2281 |
separatedBy:[ Transcript show:'-'] |
5619 | 2282 |
|
2283 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2284 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2285 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2286 |
|
2287 |
(Dictionary with:(1->'one') with:(2->'two')) |
|
2288 |
associations |
|
2289 |
do:[:el | Transcript showCR:el printString] |
|
6052
a7c0dda5e0bc
do:inBetweenDo: -> do:separatedBy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6042
diff
changeset
|
2290 |
separatedBy:[ Transcript showCR:'----'] |
5619 | 2291 |
|
4394 | 2292 |
" |
5254 | 2293 |
|
2294 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
|
4394 | 2295 |
! |
2296 |
||
14027 | 2297 |
do:aBlock without:anItem |
2298 |
"enumerate all elements except those equal to anItem into aBlock." |
|
2299 |
||
2300 |
self do:[:el | |
|
2301 |
anItem ~= el ifTrue:[ aBlock value:el ] |
|
2302 |
]. |
|
2303 |
^ self |
|
2304 |
||
2305 |
" |
|
2306 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2307 |
do:[:el | Transcript showCR:el ] |
|
2308 |
without:5 |
|
2309 |
" |
|
2310 |
||
2311 |
"Created: / 28-02-2012 / 21:11:41 / cg" |
|
2312 |
! |
|
2313 |
||
1212 | 2314 |
doWithExit:aBlock |
2315 |
"evaluate the argument, aBlock for each element. |
|
2316 |
Passes an additional exit object, which can be used to leave |
|
2317 |
the loop early, by sending it a #value: message. |
|
2318 |
Returns nil or the value passed to the exit>>value: message. |
|
2319 |
||
8662 | 2320 |
Notice, that this is different to a return statement in the block, |
2321 |
which returns from the enclosed method, NOT only from the block." |
|
2322 |
||
2323 |
|exit| |
|
2324 |
||
2325 |
exit := [:exitValue | ^exitValue]. |
|
2326 |
self do:[:el | |
|
2327 |
aBlock value:el value:exit |
|
2328 |
]. |
|
2329 |
^ nil |
|
1212 | 2330 |
|
2331 |
"Example: |
|
2332 |
search for the first element which is >= 99; return it. |
|
2333 |
remaining elements are not processed: |
|
2334 |
||
2335 |
#(1 2 3 4 999 5 6 7 8 9) |
|
2336 |
doWithExit:[:element :exit | |
|
1422 | 2337 |
Transcript showCR:element. |
8662 | 2338 |
element >= 99 ifTrue:[exit value:element]] |
1212 | 2339 |
|
2340 |
#(1 2 3 4 5 6 7 8 9) |
|
2341 |
doWithExit:[:element :exit | |
|
1422 | 2342 |
Transcript showCR:element. |
1212 | 2343 |
element >= 99 ifTrue:[exit value:element]] |
2344 |
" |
|
2345 |
||
2346 |
"to demonstrate the difference to returning from the block: |
|
2347 |
this works: |
|
2348 |
||
2349 |
|el| |
|
2350 |
||
2351 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2352 |
doWithExit:[:element :exit | |
|
2353 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 2354 |
Transcript showCR:el. |
1212 | 2355 |
|
2356 |
||
1422 | 2357 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 2358 |
|
2359 |
|el| |
|
2360 |
||
2361 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
2362 |
do:[:element | |
|
2363 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 2364 |
Transcript showCR:el. |
1212 | 2365 |
" |
2366 |
||
2367 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
2368 |
! |
|
2369 |
||
11412 | 2370 |
flatDo:aBlock |
2371 |
"for each element of the collection, if its a scalar, evaluate aBlock for it; |
|
13226
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
2372 |
otherwise, recursively invoke flatDo on the collection. |
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
2373 |
Thus implementing a depth-first enumeration" |
11412 | 2374 |
|
2375 |
self do:[:each | |
|
2376 |
(each isNonByteCollection) ifTrue:[ |
|
2377 |
each flatDo:aBlock |
|
2378 |
] ifFalse:[ |
|
2379 |
aBlock value:each |
|
2380 |
]. |
|
2381 |
]. |
|
2382 |
||
2383 |
" |
|
2384 |
#( |
|
2385 |
(1 2 3) |
|
2386 |
4 5 |
|
2387 |
(6) |
|
2388 |
7 |
|
2389 |
(8 (9 10) 11 12 (13 (14 (15) 16)))) flatDo:[:el | Transcript showCR:el] |
|
2390 |
" |
|
13226
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
2391 |
|
8bc46aab41bf
comment/format in: #flatDo:
Claus Gittinger <cg@exept.de>
parents:
13080
diff
changeset
|
2392 |
"Modified: / 22-01-2011 / 09:12:22 / cg" |
11412 | 2393 |
! |
2394 |
||
13698 | 2395 |
fold: binaryBlock |
2396 |
"Evaluate the block with the first two elements of the receiver, |
|
2397 |
then with the result of the first evaluation and the next element, |
|
2398 |
and so on. Answer the result of the final evaluation. If the receiver |
|
2399 |
is empty, raise an error. If the receiver has a single element, answer |
|
2400 |
that element." |
|
2401 |
||
2402 |
| firstValue nextValue | |
|
2403 |
firstValue := nextValue := Object new. "something that can't be in the receiver" |
|
2404 |
self do: |
|
2405 |
[:each | |
|
2406 |
nextValue := firstValue == nextValue |
|
2407 |
ifTrue: [each] |
|
2408 |
ifFalse: [binaryBlock value: nextValue value: each]]. |
|
2409 |
^nextValue == firstValue |
|
2410 |
ifTrue: [self emptyCollectionError] |
|
2411 |
ifFalse: [nextValue] |
|
2412 |
||
2413 |
" |
|
2414 |
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b] |
|
2415 |
" |
|
2416 |
||
2417 |
"Created: / 14-09-2011 / 16:29:53 / cg" |
|
2418 |
! |
|
2419 |
||
1 | 2420 |
inject:thisValue into:binaryBlock |
1262 | 2421 |
"starting with thisValue for value, pass this value and each element |
2422 |
to binaryBlock, replacing value with the result returned from the block |
|
2423 |
in the next iteration." |
|
2424 |
||
1 | 2425 |
|nextValue| |
2426 |
||
2427 |
nextValue := thisValue. |
|
2428 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
2429 |
^ nextValue |
|
68 | 2430 |
|
1262 | 2431 |
"sum up the elements of a collection: |
2432 |
||
68 | 2433 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
2434 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 2435 |
|
2436 |
find the minimum: |
|
2437 |
||
2438 |
|coll| |
|
2439 |
coll := #(1 99 -15 20 100). |
|
2440 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 2441 |
" |
1262 | 2442 |
|
2443 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 2444 |
! |
2445 |
||
602 | 2446 |
keysAndValuesDo:aTwoArgBlock |
2447 |
"evaluate the argument, aBlock for every element in the collection, |
|
2448 |
passing both index and element as arguments." |
|
2449 |
||
2450 |
^ self errorNotKeyed |
|
2451 |
! |
|
2452 |
||
1359 | 2453 |
keysAndValuesReverseDo:aTwoArgBlock |
2454 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
2455 |
passing both index and element as arguments." |
|
2456 |
||
2457 |
^ self errorNotKeyed |
|
2458 |
||
2459 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
2460 |
! |
|
2461 |
||
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2462 |
keysAndValuesSelect:selectBlockWith2Args thenCollect:collectBlockWith2Args |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2463 |
|collected| |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2464 |
|
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2465 |
collected := OrderedCollection new. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2466 |
self keysAndValuesDo:[:eachKey :eachValue | |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2467 |
(selectBlockWith2Args value:eachKey value:eachValue) ifTrue:[ |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2468 |
collected add:(collectBlockWith2Args value:eachKey value:eachValue) |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2469 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2470 |
]. |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2471 |
^ collected |
9077
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2472 |
|
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2473 |
" |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2474 |
#(10 20 30 40) keysAndValuesSelect:[:idx :val | idx > 2] thenCollect:[:idx :val | idx->val] |
327b64542a83
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9076
diff
changeset
|
2475 |
" |
9076
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2476 |
! |
e7957fd220c8
+keysAndValuesSelect:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
9073
diff
changeset
|
2477 |
|
13042 | 2478 |
keysDo:aBlock |
2479 |
"evaluate the argument, aBlock for every key in the collection." |
|
2480 |
||
2481 |
self keysAndValuesDo:[:k :v | aBlock value:k] |
|
2482 |
||
2483 |
"Created: / 24-08-2010 / 10:12:14 / cg" |
|
2484 |
! |
|
2485 |
||
7454 | 2486 |
map:selector |
2487 |
"for lisp fans - similar to collect" |
|
2488 |
||
2489 |
^ self collect:[:eachElement | eachElement perform:selector]. |
|
2490 |
||
2491 |
" |
|
2492 |
#(1 2 3 4) map:#negated |
|
2493 |
" |
|
2494 |
! |
|
2495 |
||
2496 |
map:selector with:arg |
|
2497 |
"for lisp fans - similar to collect" |
|
2498 |
||
2499 |
^ self collect:[:eachElement | eachElement perform:selector with:arg]. |
|
2500 |
||
2501 |
" |
|
2502 |
#(1 2 3 4) map:#+ with:1 |
|
2503 |
" |
|
2504 |
! |
|
2505 |
||
8863 | 2506 |
nonNilElementsDo:aBlock |
2507 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
2508 |
||
2509 |
self do:[:each | |
|
2510 |
each notNil ifTrue:[ |
|
2511 |
aBlock value:each. |
|
2512 |
] |
|
2513 |
] |
|
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2514 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2515 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2516 |
#(1 nil 3 nil nil 6 7 nil) |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2517 |
nonNilElementsDo:[:el | Transcript showCR:el] |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2518 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2519 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2520 |
"Modified (comment): / 21-12-2011 / 15:51:39 / cg" |
8863 | 2521 |
! |
2522 |
||
10750 | 2523 |
pairsDo:aTwoArgBlock |
2524 |
"evaluate the argument, aTwoArgBlock for every element in the collection, |
|
602 | 2525 |
which is supposed to consist of 2-element collections. |
12347 | 2526 |
The block is called with 2 arguments for each collection in the receiver. |
2527 |
CONFUSION ATTACK: |
|
2528 |
this is different from pairWiseDo:. |
|
2529 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
|
2530 |
(sigh: but we were first, so they should have adapted...)" |
|
602 | 2531 |
|
2532 |
self do:[:aPair | |
|
10750 | 2533 |
aTwoArgBlock value:(aPair at:1) value:(aPair at:2). |
602 | 2534 |
] |
10750 | 2535 |
|
602 | 2536 |
" |
6555 | 2537 |
#( |
2538 |
(1 one) |
|
2539 |
(2 two) |
|
2540 |
(3 three) |
|
2541 |
(4 four) |
|
2542 |
(5 five) |
|
2543 |
(6 six) |
|
2544 |
) pairsDo: |
|
2545 |
[:num :sym | |
|
2546 |
Transcript show:num; show:' is: '; showCR:sym] |
|
602 | 2547 |
|
2548 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 2549 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 2550 |
" |
10750 | 2551 |
|
2552 |
"Modified: / 20-10-2007 / 17:17:50 / cg" |
|
602 | 2553 |
! |
2554 |
||
13512 | 2555 |
partition:check into:aTwoArgBlock |
2556 |
"partition the receiver elements into two sets, depending on the outcome |
|
2557 |
of a check block. Evaluate aTwoArgBlock on the two selected and rejected value |
|
2558 |
collections. Also return the selected values as return value" |
|
2559 |
||
2560 |
|selected rejected| |
|
2561 |
||
2562 |
selected := OrderedCollection new. |
|
2563 |
rejected := OrderedCollection new. |
|
2564 |
self do:[:each | ((check value:each) ifTrue:[selected] ifFalse:[rejected]) add:each]. |
|
2565 |
aTwoArgBlock value:selected value:rejected. |
|
2566 |
^ selected |
|
2567 |
||
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2568 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2569 |
#(1 2 3 4 5 6 7 8) |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2570 |
partition:[:el | el even] |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2571 |
into:[:evenElements :oddElements | |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2572 |
Transcript show:'even: '; showCR:evenElements. |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2573 |
Transcript show:' odd: '; showCR:oddElements. |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2574 |
]. |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2575 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
2576 |
|
13512 | 2577 |
"Created: / 20-07-2011 / 00:54:41 / cg" |
2578 |
! |
|
2579 |
||
14027 | 2580 |
reduce:aTwoArgBlock |
2581 |
"reduce by iteratively applying aTwoArgBlock to the result from the |
|
2582 |
previous reduce and an element. |
|
2583 |
The first evaluation is on the first two elements." |
|
2584 |
||
2585 |
|first| |
|
2586 |
||
2587 |
self emptyCheck. |
|
2588 |
first := true. |
|
2589 |
^ self |
|
2590 |
inject:nil |
|
2591 |
into:[:accu :el | |
|
2592 |
first ifTrue:[ |
|
2593 |
first := false. |
|
2594 |
el |
|
2595 |
] ifFalse:[ |
|
2596 |
aTwoArgBlock value:el value:accu |
|
2597 |
] |
|
2598 |
] |
|
2599 |
||
2600 |
" |
|
2601 |
#(10 1 2 3) reduce:[:el :diff | diff - el] |
|
2602 |
#(10 1 2 3) reduce:[:el :diff | diff + el] |
|
2603 |
" |
|
2604 |
||
2605 |
"Created: / 28-02-2012 / 21:16:33 / cg" |
|
2606 |
! |
|
2607 |
||
13512 | 2608 |
reduceLeft:aTwoArgBlock |
2609 |
"reduce by iteratively applying aTwoArgBlock to an element and the result from the |
|
2610 |
previous reduce. The first evaluation is on the first two elements." |
|
2611 |
||
2612 |
|first| |
|
2613 |
||
14027 | 2614 |
self emptyCheck. |
13512 | 2615 |
first := true. |
2616 |
^ self |
|
2617 |
inject:nil |
|
2618 |
into:[:accu :el | |
|
2619 |
first ifTrue:[ |
|
2620 |
first := false. |
|
2621 |
el |
|
2622 |
] ifFalse:[ |
|
2623 |
aTwoArgBlock value:accu value:el |
|
2624 |
] |
|
2625 |
] |
|
2626 |
||
2627 |
" |
|
2628 |
#(1 2 3 4 5) reduceLeft:[:sum :el | sum + el] |
|
2629 |
" |
|
2630 |
||
2631 |
"Created: / 20-07-2011 / 01:01:03 / cg" |
|
2632 |
! |
|
2633 |
||
1 | 2634 |
reject:aBlock |
2635 |
"return a new collection with all elements from the receiver, for which |
|
2636 |
the argument aBlock evaluates to false" |
|
2637 |
||
11928 | 2638 |
^ self select:[:element | (aBlock value:element) not] |
68 | 2639 |
|
2640 |
" |
|
2641 |
#(1 2 3 4) reject:[:e | e odd] |
|
2642 |
(1 to:10) reject:[:e | e even] |
|
2643 |
" |
|
1 | 2644 |
! |
2645 |
||
11928 | 2646 |
reject:rejectBlock thenCollect:collectBlock |
2647 |
"combination of reject followed by collect. |
|
2648 |
May be redefined by some subclasses for optimal performance |
|
2649 |
(avoiding the creation of intermediate garbage)" |
|
2650 |
||
12944
791a4ec545c2
changed: #reject:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
12943
diff
changeset
|
2651 |
^ self select:[:el | (rejectBlock value:el) not] thenCollect:collectBlock |
11928 | 2652 |
|
2653 |
" |
|
2654 |
#(1 2 3 4 5 6 7) reject:[:i | i even] thenCollect:[:i | i * 2] |
|
2655 |
" |
|
12944
791a4ec545c2
changed: #reject:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
12943
diff
changeset
|
2656 |
|
791a4ec545c2
changed: #reject:thenCollect:
Claus Gittinger <cg@exept.de>
parents:
12943
diff
changeset
|
2657 |
"Modified: / 11-07-2010 / 17:04:07 / cg" |
11928 | 2658 |
! |
2659 |
||
602 | 2660 |
reverseDo:aBlock |
2661 |
"evaluate the argument, aBlock for each element in reverse order." |
|
2662 |
||
2663 |
"it could be defined in terms of do: - but very inefficient. |
|
2664 |
Better force programmer to define a better version ..." |
|
2665 |
||
2666 |
^ self subclassResponsibility |
|
2667 |
! |
|
2668 |
||
1 | 2669 |
select:aBlock |
2670 |
"return a new collection with all elements from the receiver, for which |
|
5265 | 2671 |
the argument aBlock evaluates to true. |
2672 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
1 | 2673 |
|
13002 | 2674 |
^ self select:aBlock as:(self species) |
2675 |
||
2676 |
" |
|
2677 |
#(1 2 3 4) select:[:e | e odd] |
|
2678 |
(1 to:10) select:[:e | e even] |
|
2679 |
" |
|
2680 |
||
2681 |
"Modified: / 07-08-2010 / 16:26:40 / cg" |
|
2682 |
! |
|
2683 |
||
2684 |
select:aBlock as:aCollectionClass |
|
2685 |
"return a new collection with all elements from the receiver, for which |
|
2686 |
the argument aBlock evaluates to true. |
|
2687 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
2688 |
||
1 | 2689 |
|newCollection| |
2690 |
||
13002 | 2691 |
newCollection := aCollectionClass new. |
1 | 2692 |
self do:[:each | |
5265 | 2693 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 2694 |
]. |
2695 |
^ newCollection |
|
68 | 2696 |
|
2697 |
" |
|
13002 | 2698 |
#(1 2 3 4) select:[:e | e odd] as:OrderedCollection |
2699 |
(1 to:10) select:[:e | e even] as:OrderedCollection |
|
68 | 2700 |
" |
13002 | 2701 |
|
2702 |
"Created: / 07-08-2010 / 16:26:15 / cg" |
|
68 | 2703 |
! |
2704 |
||
2705 |
select:aBlock ifNone:exceptionBlock |
|
2706 |
"try a new collection with all elements from the receiver, for which |
|
2707 |
the argument aBlock evaluates to true. If none of the elements passes |
|
5265 | 2708 |
the check of aBlock, return the result of evaluating exceptionBlock. |
2709 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
68 | 2710 |
|
2711 |
|newCollection| |
|
2712 |
||
2713 |
newCollection := self select:aBlock. |
|
2714 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
2715 |
^ newCollection |
|
2716 |
||
2717 |
" |
|
2718 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
2719 |
" |
|
12 | 2720 |
! |
2721 |
||
4892 | 2722 |
select:selectBlock thenCollect:collectBlock |
2723 |
"combination of select followed by collect. |
|
2724 |
May be redefined by some subclasses for optimal performance |
|
5844 | 2725 |
(avoiding the creation of intermediate garbage)" |
4892 | 2726 |
|
2727 |
^ (self select:selectBlock) collect:collectBlock |
|
2728 |
||
2729 |
" |
|
2730 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2] |
|
2731 |
" |
|
2732 |
! |
|
2733 |
||
6695 | 2734 |
select:selectBlock thenDo:doBlock |
2735 |
"combination of select followed by do |
|
2736 |
Avoids the creation of intermediate garbage" |
|
2737 |
||
2738 |
self do:[:eachElement | |
|
2739 |
(selectBlock value:eachElement) ifTrue:[ |
|
2740 |
doBlock value:eachElement |
|
2741 |
] |
|
2742 |
]. |
|
2743 |
||
2744 |
" |
|
2745 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i] |
|
2746 |
" |
|
2747 |
! |
|
2748 |
||
362 | 2749 |
triplesDo:aBlock |
2750 |
"evaluate the argument, aBlock for every element in the collection, |
|
2751 |
which is supposed to consist of 3-element collections. |
|
2752 |
The block is called with 3 arguments for each collection in the receiver." |
|
2753 |
||
2754 |
self do:[:aTriple | |
|
2639 | 2755 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
362 | 2756 |
] |
2757 |
" |
|
6555 | 2758 |
#( |
2759 |
(1 one eins) |
|
2760 |
(2 two zwei) |
|
2761 |
(3 three drei) |
|
2762 |
(4 four vier) |
|
2763 |
(5 five #'fuenf') |
|
2764 |
(6 six sechs) |
|
2765 |
) |
|
2766 |
triplesDo:[:num :sym1 :sym2 | |
|
2767 |
Transcript show:num; space; show:sym1; space; showCR:sym2 |
|
2768 |
] |
|
362 | 2769 |
" |
2639 | 2770 |
|
2771 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
1 | 2772 |
! |
2773 |
||
602 | 2774 |
with:aCollection collect:aTwoArgBlock |
2775 |
"evaluate the argument, aBlock for successive elements from |
|
2776 |
each the receiver and the argument, aSequenceableCollection; |
|
2777 |
The second argument, aBlock must be a two-argument block, which is |
|
2778 |
evaluated for each element-pair. |
|
2779 |
Collect the results and return a collection containing them. |
|
2780 |
This method fails if neither the receiver nor aCollection is |
|
2781 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 2782 |
|
12653 | 2783 |
|newCollection| |
1 | 2784 |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2785 |
newCollection := self speciesForAdding new. |
12653 | 2786 |
self with:aCollection do:[:el1 :el2 | |
2787 |
newCollection add:(aTwoArgBlock value:el1 value:el2). |
|
602 | 2788 |
]. |
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2789 |
^ newCollection as:self species |
201 | 2790 |
|
2791 |
" |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
2792 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
12653 | 2793 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
2794 |
" |
|
2795 |
! |
|
2796 |
||
13464 | 2797 |
with:aCollection contains:aTwoArgBlock |
2798 |
"evaluate the argument, aBlock for successive elements from |
|
2799 |
each the receiver and the argument, aCollection. |
|
2800 |
Return true, if the block returns true for any of these pairs. |
|
2801 |
This method fails if neither the receiver nor aCollection is |
|
2802 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2803 |
||
2804 |
self with:aCollection do:[:a :b | |
|
2805 |
(aTwoArgBlock value:a value:b) ifTrue:[^ true]. |
|
2806 |
]. |
|
2807 |
^ false. |
|
2808 |
||
2809 |
" |
|
2810 |
(1 to:3) with:#(1 2 3 4) contains:[:a :b | a ~= b] --- raises an error |
|
2811 |
(1 to:3) with:#(1 22 3) contains:[:a :b | a ~= b] |
|
2812 |
(1 to:3) with:#(1 2 3) contains:[:a :b | a ~= b] |
|
2813 |
" |
|
2814 |
||
2815 |
"Created: / 30-06-2011 / 12:37:41 / cg" |
|
2816 |
"Modified (Format): / 30-06-2011 / 12:40:38 / cg" |
|
2817 |
! |
|
2818 |
||
12653 | 2819 |
with:aCollection count:aTwoArgBlock |
2820 |
"evaluate the argument, aBlock for successive elements from |
|
2821 |
each the receiver and the argument, aSequenceableCollection. |
|
2822 |
Count, how often the second argument, aTwoArgBlock returns true. |
|
2823 |
This method fails if neither the receiver nor aCollection is |
|
2824 |
a sequenceable collection (i.e. implements numeric key access)." |
|
2825 |
||
2826 |
|count "{ Class: SmallInteger }"| |
|
2827 |
||
2828 |
count := 0. |
|
2829 |
self with:aCollection do:[:el1 :el2 | |
|
2830 |
(aTwoArgBlock value:el1 value:el2) ifTrue:[ |
|
2831 |
count := count + 1 |
|
2832 |
] |
|
2833 |
]. |
|
2834 |
^ count |
|
2835 |
||
2836 |
" |
|
2837 |
(1 to:3) with:#(1 3 3) count:[:n1 :n2 | n1 = n2] |
|
201 | 2838 |
" |
2 | 2839 |
! |
2840 |
||
602 | 2841 |
with:aCollection do:aTwoArgBlock |
2842 |
"evaluate the argument, aBlock for successive elements from |
|
2843 |
each the receiver and the argument, aSequenceableCollection. |
|
2844 |
The second argument, aBlock must be a two-argument block. |
|
2845 |
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
|
2846 |
a sequenceable collection (i.e. implements numeric key access) |
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
2847 |
or (new!!) if the sizes are different." |
602 | 2848 |
|
2849 |
|index "{ Class: SmallInteger }"| |
|
2 | 2850 |
|
13464 | 2851 |
aCollection size == self size ifFalse:[ |
13468
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
2852 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
13464 | 2853 |
]. |
2854 |
||
602 | 2855 |
index := 1. |
2856 |
aCollection isSequenceable ifFalse:[ |
|
6135 | 2857 |
self isSequenceable ifFalse:[ |
2858 |
^ self error:'neither collection is sequenceable'. |
|
2859 |
]. |
|
2860 |
aCollection do:[:element | |
|
2861 |
aTwoArgBlock value:(self at:index) value:element. |
|
2862 |
index := index + 1 |
|
2863 |
] |
|
602 | 2864 |
] ifTrue:[ |
6135 | 2865 |
self do:[:element | |
2866 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
2867 |
index := index + 1 |
|
2868 |
] |
|
602 | 2869 |
] |
201 | 2870 |
|
2871 |
" |
|
602 | 2872 |
(1 to:3) with:#(one two three) do:[:num :sym | |
6135 | 2873 |
Transcript showCR:(num->sym) |
602 | 2874 |
] |
2875 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
6135 | 2876 |
Transcript showCR:(num->sym) |
602 | 2877 |
]. |
2878 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
6135 | 2879 |
Transcript showCR:(num->sym) |
602 | 2880 |
] |
201 | 2881 |
" |
13464 | 2882 |
|
13468
7a4e15f44afe
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13464
diff
changeset
|
2883 |
"Modified: / 30-06-2011 / 17:39:47 / cg" |
1 | 2884 |
! ! |
2885 |
||
5874 | 2886 |
!Collection methodsFor:'enumerating-tests'! |
2887 |
||
2888 |
allSatisfy:aBlock |
|
2889 |
"evaluate aBlock for each of the receiver's elements. |
|
2890 |
Return true, if aBlock returns true for all elements, false otherwise |
|
2891 |
(i.e. false if any element fails to satisfy the block-condition). |
|
2892 |
This is an ANSI renomer of #conform:" |
|
2893 |
||
2894 |
^ self conform:aBlock. |
|
2895 |
||
2896 |
" |
|
2897 |
#(1 2 3 4 5) allSatisfy:[:el | el odd] |
|
2898 |
#(2 4 6 8 10) allSatisfy:[:el | el odd] |
|
2899 |
#(2 4 6 8 10) allSatisfy:[:el | el even] |
|
2900 |
" |
|
2901 |
||
2902 |
! |
|
2903 |
||
2904 |
anySatisfy:aBlock |
|
2905 |
"evaluate aBlock for each of the receiver's elements. |
|
2906 |
Return true, if aBlock ever returns true, false otherwise |
|
2907 |
(i.e. if any element satisfies the block-condition). |
|
2908 |
This is an ANSI renomer of #contains:" |
|
2909 |
||
2910 |
^ self contains:aBlock. |
|
2911 |
||
2912 |
" |
|
2913 |
#(1 2 3 4 5) anySatisfy:[:el | el odd] |
|
2914 |
#(2 4 6 8 10) anySatisfy:[:el | el odd] |
|
2915 |
" |
|
2916 |
||
2917 |
! |
|
2918 |
||
9842 | 2919 |
conform:aOneArgBlock |
5874 | 2920 |
"return true, if every element conforms to some condition. |
2921 |
I.e. return false, if aBlock returns false for any element; |
|
2922 |
true otherwise." |
|
2923 |
||
9842 | 2924 |
self do:[:element | |
2925 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
2926 |
]. |
|
5874 | 2927 |
^ true |
9842 | 2928 |
|
2929 |
" |
|
2930 |
#(1 2 3 4 5) conform:[:el | el even] |
|
2931 |
#(2 4 6 8 10) conform:[:el | el even] |
|
2932 |
" |
|
2933 |
||
2934 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
5874 | 2935 |
! |
2936 |
||
9842 | 2937 |
contains:aOneArgBlock |
2938 |
"evaluate aOneArgBlock for each of the receiver's elements. |
|
5874 | 2939 |
Return true, if aBlock ever returns true, false otherwise." |
2940 |
||
9842 | 2941 |
self do:[:element | |
2942 |
(aOneArgBlock value:element) ifTrue:[^ true] |
|
2943 |
]. |
|
2944 |
^ false |
|
5874 | 2945 |
|
2946 |
" |
|
2947 |
#(1 2 3 4 5) contains:[:el | el odd] |
|
2948 |
#(2 4 6 8 10) contains:[:el | el odd] |
|
2949 |
" |
|
2950 |
||
9842 | 2951 |
"Modified: / 13-09-2006 / 11:18:36 / cg" |
8577 | 2952 |
! |
2953 |
||
2954 |
noneSatisfy:aBlock |
|
2955 |
"evaluate aBlock for each of the receiver's elements. |
|
2956 |
Return true, if aBlock returns false for all elements, false otherwise |
|
2957 |
(i.e. false if any element satisfies the block-condition)." |
|
2958 |
||
9843 | 2959 |
^ (self contains:aBlock) not |
8577 | 2960 |
|
2961 |
" |
|
2962 |
#(1 2 3 4 5) noneSatisfy:[:el | el odd] |
|
2963 |
#(2 4 6 8 10) noneSatisfy:[:el | el odd] |
|
2964 |
#(2 4 6 8 10) noneSatisfy:[:el | el even] |
|
2965 |
" |
|
9843 | 2966 |
|
2967 |
"Modified: / 13-09-2006 / 11:19:57 / cg" |
|
5874 | 2968 |
! ! |
2969 |
||
602 | 2970 |
!Collection methodsFor:'error handling'! |
2971 |
||
2972 |
emptyCheck |
|
2973 |
"check if the receiver is empty; report an error if so" |
|
2974 |
||
11475
f7e1a355df17
changed #emptyCheck (is isEmpty instead of size == 0)
Claus Gittinger <cg@exept.de>
parents:
11470
diff
changeset
|
2975 |
self isEmpty ifTrue:[ |
6135 | 2976 |
^ self emptyCollectionError. |
602 | 2977 |
]. |
2978 |
! |
|
2979 |
||
2980 |
emptyCollectionError |
|
2981 |
"report an error that the operation is not allowed for |
|
2982 |
empty collections" |
|
2983 |
||
2984 |
^ EmptyCollectionSignal raise |
|
2985 |
! |
|
2986 |
||
2987 |
errorInvalidKey:aKey |
|
2988 |
"report an error that the given key was invalid" |
|
2989 |
||
2990 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
2991 |
! |
|
2992 |
||
2993 |
errorNotKeyed |
|
2994 |
"report an error that keyed access methods are not allowed" |
|
2995 |
||
2996 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
2997 |
! |
|
2998 |
||
2999 |
errorValueNotFound:anObject |
|
3000 |
"report an error that an object was not found in the collection" |
|
3001 |
||
6757 | 3002 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
3075 | 3003 |
|
3004 |
"Modified: / 30.10.1997 / 15:52:18 / cg" |
|
602 | 3005 |
! |
3006 |
||
14022
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3007 |
noModificationError |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3008 |
"a store is attempted into an immutable collection (typically: a literal). |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3009 |
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
|
3010 |
|
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3011 |
|creator msg context| |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3012 |
|
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3013 |
creator := Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3014 |
creator notNil ifTrue:[ |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3015 |
msg := ' (' , creator whoString , ')' |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3016 |
]. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3017 |
context := thisContext sender. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3018 |
" |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3019 |
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
|
3020 |
The literal was created in creator. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3021 |
If you press continue in the debugger, the store will be performed. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3022 |
If you don't want this, press abort and check your code. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3023 |
Storing into literals is VERY VERY bad coding style. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3024 |
" |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3025 |
NoModificationError |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3026 |
raiseRequestWith:self |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3027 |
errorString:msg |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3028 |
in:context. |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3029 |
|
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3030 |
"Created: / 03-08-1998 / 14:47:45 / cg" |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3031 |
! |
28ecd3e2d8c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
13872
diff
changeset
|
3032 |
|
602 | 3033 |
notEnoughElementsError |
3034 |
"report an error that the operation is not allowed, |
|
3035 |
since not enough elements are in the collection" |
|
3036 |
||
3037 |
^ NotEnoughElementsSignal raise |
|
3038 |
! ! |
|
3039 |
||
3040 |
!Collection methodsFor:'growing'! |
|
3041 |
||
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3042 |
changeCapacityTo:newSize |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3043 |
newSize > self capacity ifTrue:[ |
11315
9a6ca3147b69
#showWhereWeCameFrom - show more stack backtrace
Stefan Vogel <sv@exept.de>
parents:
11309
diff
changeset
|
3044 |
self grow:newSize |
9309
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3045 |
]. |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3046 |
|
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3047 |
"Created: / 30.10.2001 / 17:56:50 / cg" |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3048 |
! |
d1fe8655bb97
move #changeCapacityTo: from Set to Collection
Stefan Vogel <sv@exept.de>
parents:
9247
diff
changeset
|
3049 |
|
602 | 3050 |
grow |
3051 |
"make the receiver larger" |
|
3052 |
||
3053 |
self grow:(self size + self growSize) |
|
3054 |
! |
|
3055 |
||
3056 |
grow:howBig |
|
3057 |
"change the receivers size" |
|
3058 |
||
3059 |
^ self subclassResponsibility |
|
3060 |
! |
|
3061 |
||
3062 |
growSize |
|
3063 |
"return a suitable size increment for growing. |
|
3064 |
The default returned here may be (and is) redefined in subclasses." |
|
3065 |
||
3066 |
^ self size max:2 |
|
3067 |
! ! |
|
3068 |
||
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
3069 |
|
11412 | 3070 |
!Collection methodsFor:'operations'! |
3071 |
||
3072 |
decrementAt:aKey |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3073 |
"decrement the value at aKey by one" |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3074 |
|
11412 | 3075 |
self at:aKey put:(self at:aKey) - 1. |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3076 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3077 |
"Modified: / 18-03-2011 / 10:32:04 / cg" |
11412 | 3078 |
! |
3079 |
||
3080 |
incrementAt:aKey |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3081 |
"increment the value at aKey by one" |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3082 |
|
11412 | 3083 |
self at:aKey put:(self at:aKey) + 1. |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3084 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3085 |
"Modified: / 18-03-2011 / 10:31:57 / cg" |
11412 | 3086 |
! ! |
3087 |
||
1 | 3088 |
!Collection methodsFor:'printing & storing'! |
3089 |
||
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3090 |
displayOn:aGCOrStream |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3091 |
"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
|
3092 |
|
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3093 |
|noneYet total limit| |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3094 |
|
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3095 |
"/ 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
|
3096 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3097 |
(aGCOrStream isStream and:[aGCOrStream ~~ Transcript]) ifFalse:[ |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3098 |
^ super displayOn:aGCOrStream |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3099 |
]. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3100 |
|
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3101 |
aGCOrStream nextPutAll:self displayStringName; nextPut:$(. |
602 | 3102 |
|
3103 |
thisContext isRecursive ifTrue:[ |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3104 |
'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
|
3105 |
aGCOrStream nextPutAll:'"recursive")'. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3106 |
^ self. |
602 | 3107 |
]. |
3108 |
||
3109 |
noneYet := true. |
|
3110 |
total := 0. |
|
3111 |
limit := self maxPrint. |
|
3112 |
||
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3113 |
self printElementsDo: [:element | |
1413 | 3114 |
noneYet ifTrue:[ |
3115 |
noneYet := false. |
|
3116 |
] ifFalse:[ |
|
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3117 |
aGCOrStream space |
1413 | 3118 |
]. |
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3119 |
element displayOn:aGCOrStream. |
1413 | 3120 |
total := total + 1. |
3121 |
(total > limit) ifTrue:[ |
|
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3122 |
aGCOrStream nextPutAll:'... )'. |
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3123 |
^ self |
1413 | 3124 |
] |
602 | 3125 |
]. |
13080
f692f6c62c02
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13049
diff
changeset
|
3126 |
aGCOrStream nextPutAll:')'. |
1413 | 3127 |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3128 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3129 |
#(1 2 3 'hello' $a) asOrderedCollection displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3130 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3131 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3132 |
at:#foo put:'bar'; yourself) displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3133 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3134 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3135 |
"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
|
3136 |
"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
|
3137 |
! |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3138 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3139 |
displayStringName |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3140 |
"redefinable helper for displayString" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3141 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3142 |
^ (self class name) |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3143 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
3144 |
"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
|
3145 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3146 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3147 |
maxPrint |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3148 |
"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
|
3149 |
than the limit defined here." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3150 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3151 |
^ 5000 |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3152 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3153 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3154 |
printElementsDo:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3155 |
"perform aBlock (1 arg) for all elements. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3156 |
Used in #printOn:. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3157 |
Subclasses (e.g. Dictionary) may redefine this." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3158 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3159 |
^ self do:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3160 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3161 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3162 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3163 |
|
11599 | 3164 |
printElementsOn:aStream |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3165 |
"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
|
3166 |
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
|
3167 |
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
|
3168 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3169 |
|
3402 | 3170 |
|limit firstOne s| |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3171 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3172 |
thisContext isRecursive ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3173 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3174 |
aStream nextPutAll:'#("recursive")'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3175 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3176 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3177 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3178 |
aStream nextPut:$(. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3179 |
firstOne := true. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3180 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3181 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3182 |
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
|
3183 |
(needed for limit calculation) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3184 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3185 |
aStream isPositionable ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3186 |
s := aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3187 |
] ifFalse:[ |
7685
535a69a7cd69
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7629
diff
changeset
|
3188 |
s := WriteStream on:(String uninitializedNew:50). |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3189 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
3190 |
limit := s position1Based + self maxPrint. |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3191 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3192 |
self printElementsDo:[:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3193 |
firstOne ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3194 |
s space |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3195 |
] ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3196 |
firstOne := false |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3197 |
]. |
7054
685d359f9847
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
7011
diff
changeset
|
3198 |
(s position1Based >= limit) ifTrue:[ |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3199 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3200 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3201 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3202 |
aStream nextPutAll:'...etc...)'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3203 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3204 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3205 |
element printOn:s. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3206 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3207 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3208 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3209 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3210 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3211 |
aStream nextPut:$) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3212 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3213 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3214 |
#(1 2 3 'hello' $a) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3215 |
(Array new:100000) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3216 |
(Array new:100000) printOn:Stdout |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3217 |
(Array new:100000) printString size |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3218 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3219 |
at:#foo put:'bar'; yourself) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3220 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3221 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3222 |
|a| |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3223 |
a := Array new:3. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3224 |
a at:2 put:a. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3225 |
a printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3226 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3227 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3228 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
3229 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
602 | 3230 |
! |
3231 |
||
11599 | 3232 |
printOn:aStream |
3233 |
"append a user readable representation of the receiver to aStream. |
|
3234 |
The text appended is not meant to be read back for reconstruction of |
|
3235 |
the receiver. Also, this method limits the size of generated string. |
|
3236 |
" |
|
3237 |
||
3238 |
thisContext isRecursive ifTrue:[ |
|
3239 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
|
3240 |
aStream nextPutAll:'#("recursive")'. |
|
3241 |
^ self |
|
3242 |
]. |
|
3243 |
||
3244 |
aStream nextPutAll:self class name. |
|
3245 |
self printElementsOn:aStream. |
|
3246 |
||
3247 |
" |
|
3248 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
3249 |
(Array new:100000) printOn:Transcript |
|
3250 |
(Array new:100000) printOn:Stdout |
|
3251 |
(Array new:100000) printString size |
|
3252 |
(Dictionary new at:#hello put:'world'; |
|
3253 |
at:#foo put:'bar'; yourself) printOn:Transcript |
|
3254 |
" |
|
3255 |
" |
|
3256 |
|a| |
|
3257 |
a := Array new:3. |
|
3258 |
a at:2 put:a. |
|
3259 |
a printOn:Transcript |
|
3260 |
" |
|
3261 |
||
3262 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
|
3263 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
|
3264 |
! |
|
3265 |
||
1 | 3266 |
storeOn:aStream |
88 | 3267 |
"output a printed representation onto the argument, aStream. |
3268 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
3269 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
3270 |
(use storeBinaryOn: to handle those)." |
|
1 | 3271 |
|
5254 | 3272 |
|notEmpty| |
1 | 3273 |
|
44 | 3274 |
thisContext isRecursive ifTrue:[ |
5498 | 3275 |
Object recursiveStoreStringSignal raiseRequestWith:self. |
2289 | 3276 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 3277 |
aStream nextPutAll:'#recursive'. |
3278 |
^ self |
|
44 | 3279 |
]. |
3280 |
||
3281 |
aStream nextPutAll:'('. |
|
1 | 3282 |
aStream nextPutAll:(self class name). |
44 | 3283 |
aStream nextPutAll:' new'. |
5254 | 3284 |
notEmpty := false. |
1 | 3285 |
self do:[:element | |
1413 | 3286 |
aStream nextPutAll:' add:'. |
3287 |
element storeOn:aStream. |
|
3288 |
aStream nextPutAll:';'. |
|
5254 | 3289 |
notEmpty := true |
1 | 3290 |
]. |
5254 | 3291 |
notEmpty ifTrue:[aStream nextPutAll:' yourself']. |
1 | 3292 |
aStream nextPut:$) |
44 | 3293 |
|
88 | 3294 |
" |
3295 |
OrderedCollection new storeOn:Transcript |
|
3296 |
(1 to:10) storeOn:Transcript |
|
3297 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
3298 |
" |
|
293 | 3299 |
" |
3300 |
|s| |
|
3301 |
||
3302 |
s := Set new. |
|
3303 |
s add:1; add:'hello'; add:s. |
|
3304 |
s storeOn:Transcript |
|
3305 |
" |
|
1413 | 3306 |
|
5254 | 3307 |
"Modified: / 11.2.2000 / 11:24:56 / cg" |
1 | 3308 |
! ! |
602 | 3309 |
|
3310 |
!Collection methodsFor:'queries'! |
|
3311 |
||
13338 | 3312 |
arithmeticMean |
3313 |
"arithmetic mean value of all elements in the collection" |
|
3314 |
||
3315 |
^ self sum / self size |
|
3316 |
||
3317 |
" |
|
3318 |
TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5). |
|
3319 |
TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4). |
|
3320 |
TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431). |
|
3321 |
" |
|
3322 |
||
3323 |
"Created: / 13-04-2011 / 16:52:32 / cg" |
|
3324 |
! |
|
3325 |
||
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3326 |
average |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3327 |
"average value of all elements in the collection" |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3328 |
|
13338 | 3329 |
^ self arithmeticMean |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3330 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3331 |
" |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3332 |
TestCase assert:( { 1. 2. 3. 4 } average = 2.5). |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3333 |
" |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3334 |
|
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3335 |
"Created: / 18-03-2011 / 10:31:04 / cg" |
13338 | 3336 |
"Modified: / 13-04-2011 / 17:09:21 / cg" |
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3337 |
! |
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3338 |
|
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3339 |
defaultElement |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3340 |
^ nil |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3341 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
3342 |
|
13338 | 3343 |
geometricMean |
3344 |
"geometric mean value of all elements in the collection" |
|
3345 |
||
3346 |
^ self product raisedTo:(1/self size) |
|
3347 |
||
3348 |
" |
|
3349 |
TestCase assert:( { 1. 2. 3. 4. } geometricMean closeTo: 2.21336). |
|
3350 |
TestCase assert:( { 1. 2. 3. 4. 5 } geometricMean closeTo: 2.60517). |
|
3351 |
TestCase assert:( { 13. 23. 12. 44. 55 } geometricMean closeTo: 24.41932). |
|
3352 |
" |
|
3353 |
||
3354 |
"Created: / 13-04-2011 / 16:53:44 / cg" |
|
3355 |
! |
|
3356 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3357 |
largest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3358 |
"return the n largest elements" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3359 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3360 |
|mySize loopAction actionForFirstN actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3361 |
nLargest sz_nLargest minInLargest| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3362 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3363 |
mySize := self size. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3364 |
n > mySize ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3365 |
self notEnoughElementsError |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3366 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3367 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3368 |
(n < 50) ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3369 |
n == 1 ifTrue:[ ^ Array with:self max ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3370 |
n == 2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3371 |
|l1 l2| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3372 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3373 |
loopAction := [:el | l1 := el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3374 |
loopAction := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3375 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3376 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3377 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3378 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3379 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3380 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3381 |
loopAction := actionForRest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3382 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3383 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3384 |
actionForRest := [:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3385 |
el > l2 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3386 |
el > l1 ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3387 |
l2 := l1. l1 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3388 |
] ifFalse:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3389 |
l2 := el |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3390 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3391 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3392 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3393 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3394 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3395 |
^ Array with:l2 with:l1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3396 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3397 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3398 |
nLargest := SortedCollection new:n. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3399 |
sz_nLargest := 0. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3400 |
actionForFirstN := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3401 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3402 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3403 |
sz_nLargest := sz_nLargest + 1. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3404 |
sz_nLargest == n ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3405 |
loopAction := actionForRest. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3406 |
minInLargest := nLargest min. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3407 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3408 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3409 |
actionForRest := |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3410 |
[:el | |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3411 |
el > minInLargest ifTrue:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3412 |
nLargest removeFirst. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3413 |
nLargest add:el. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3414 |
minInLargest := nLargest min |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3415 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3416 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3417 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3418 |
loopAction := actionForFirstN. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3419 |
self do:[:el | loopAction value:el ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3420 |
^ nLargest |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3421 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3422 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3423 |
^ self asSortedCollection copyTo:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3424 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3425 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3426 |
#(10 35 20 45 30 5) largest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3427 |
#(10 35 20 45 30 5) largest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3428 |
#(10 35 20 45 30 5) largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3429 |
#(10 35 20 45 30 5) largest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3430 |
#(10 35 20 45 30 5) largest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3431 |
#(10 35 20 45 30 5) largest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3432 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3433 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3434 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3435 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3436 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3437 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3438 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3439 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3440 |
data asSortedCollection at:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3441 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3442 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3443 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3444 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3445 |
data largest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3446 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3447 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3448 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3449 |
Transcript show:'largest -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3450 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3451 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3452 |
|
602 | 3453 |
longestCommonPrefix |
1246 | 3454 |
"return the longest common prefix of my elements. |
3455 |
Typically used with string collections." |
|
3456 |
||
5532 | 3457 |
^ self longestCommonPrefixIgnoreCase:false |
3458 |
||
3459 |
" |
|
3460 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
3461 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
3462 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
3463 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
3464 |
" |
|
3465 |
||
3466 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
|
3467 |
! |
|
3468 |
||
3469 |
longestCommonPrefixIgnoreCase:ignoreCase |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3470 |
"return the longest common prefix of my elements (which must be sequenceableCollections). |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3471 |
Typically used with string collections, |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3472 |
especially with completion of selectors or filenames." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3473 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3474 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3475 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3476 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3477 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3478 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3479 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3480 |
longest := longest commonPrefixWith:eachCollection ignoreCase:ignoreCase |
1246 | 3481 |
] |
602 | 3482 |
]. |
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3483 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3484 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3485 |
"/ |longest try allMatching matchLen| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3486 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3487 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3488 |
"/ find the longest common prefix |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3489 |
"/ " |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3490 |
"/ matchLen := 0. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3491 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3492 |
"/ aName size > matchLen ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3493 |
"/ matchLen := aName size. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3494 |
"/ try := aName |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3495 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3496 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3497 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3498 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3499 |
"/ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3500 |
"/ [true] whileTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3501 |
"/ allMatching := true. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3502 |
"/ self do:[:aName | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3503 |
"/ ((ignoreCase not and:[aName startsWith:try]) |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3504 |
"/ or:[ignoreCase and:[aName asLowercase startsWith:try asLowercase]]) ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3505 |
"/ allMatching := false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3506 |
"/ ] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3507 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3508 |
"/ allMatching ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3509 |
"/ ^ try |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3510 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3511 |
"/ matchLen := matchLen - 1. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3512 |
"/ matchLen == 0 ifTrue:[^ '']. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3513 |
"/ try := try copyTo:matchLen. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3514 |
"/ ]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3515 |
"/ ^ try |
602 | 3516 |
|
3517 |
" |
|
5532 | 3518 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:true |
3519 |
#('Array' 'arrayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
3520 |
#('Array' 'ArayedCollection' 'ARRAYOfFoo') longestCommonPrefixIgnoreCase:false |
|
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3521 |
#('AAA' 'A11' 'AA2') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3522 |
#('AAA' 'BBB' 'CCC') longestCommonPrefixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3523 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3524 |
! |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3525 |
|
6506 | 3526 |
longestCommonSuffix |
3527 |
"return the longest common suffix (tail) of my elements. |
|
3528 |
Typically used with string collections." |
|
3529 |
||
3530 |
^ self longestCommonSuffixIgnoreCase:false |
|
13521
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
3531 |
|
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
3532 |
" |
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
3533 |
#('abcdefg' '1234cdefg' 'aaaaaadefg') longestCommonSuffix |
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
3534 |
" |
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
3535 |
|
3faf9238433e
comment/format in: #longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
13512
diff
changeset
|
3536 |
"Modified (comment): / 24-07-2011 / 10:32:15 / cg" |
6506 | 3537 |
! |
3538 |
||
6505
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3539 |
longestCommonSuffixIgnoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3540 |
"return the longest common suffix (tail) of my elements |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3541 |
(which must be sequenceableCollections)." |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3542 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3543 |
|longest| |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3544 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3545 |
self do:[:eachCollection | |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3546 |
longest isNil ifTrue:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3547 |
longest := eachCollection |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3548 |
] ifFalse:[ |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3549 |
longest := longest commonSuffixWith:eachCollection ignoreCase:ignoreCase |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3550 |
] |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3551 |
]. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3552 |
^ longest. |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3553 |
|
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3554 |
" |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3555 |
#('Array' 'ByteArray' 'BigArray') longestCommonSuffixIgnoreCase:true |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3556 |
#('AAA' 'BBBAA' 'CCCAAAA') longestCommonSuffixIgnoreCase:false |
a86bdc3d8e66
better longestCommonPrefix; added longestCommonSuffix
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
3557 |
#('AAA' 'BBB' 'CCC') longestCommonSuffixIgnoreCase:false |
602 | 3558 |
" |
5557 | 3559 |
! |
3560 |
||
7821 | 3561 |
max |
3562 |
"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
|
3563 |
using #< to compare elements. |
7821 | 3564 |
Raises an error, if the receiver is empty." |
3565 |
||
3566 |
self emptyCheck. |
|
3567 |
||
12945 | 3568 |
^ self |
3569 |
inject:nil |
|
3570 |
into:[:maxSoFar :this | |
|
3571 |
(maxSoFar isNil or:[maxSoFar < this]) |
|
3572 |
ifTrue:[this] |
|
3573 |
ifFalse:[maxSoFar] |
|
3574 |
] |
|
7821 | 3575 |
|
3576 |
" |
|
3577 |
#(15 1 -9 10 5) max |
|
3578 |
(1 to:15) max |
|
3579 |
(-1 to:-15 by:-1) max |
|
3580 |
(-1 to:-15 by:-4) max |
|
3581 |
(0 to:15 by:4) max |
|
3582 |
" |
|
3583 |
||
12945 | 3584 |
"Modified: / 11-07-2010 / 17:05:25 / cg" |
7821 | 3585 |
! |
3586 |
||
13040 | 3587 |
maxApplying:aBlock |
3588 |
"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
|
3589 |
using aBlock to compare elements. |
13040 | 3590 |
Raises an error, if the receiver is empty." |
3591 |
||
3592 |
self emptyCheck. |
|
3593 |
||
3594 |
^ self |
|
3595 |
inject:nil |
|
3596 |
into:[:maxSoFar :this | |
|
3597 |
|v| |
|
3598 |
||
3599 |
v := aBlock value:this. |
|
3600 |
(maxSoFar isNil or:[maxSoFar < v]) |
|
3601 |
ifTrue:[v] |
|
3602 |
ifFalse:[maxSoFar] |
|
3603 |
] |
|
3604 |
||
3605 |
" |
|
3606 |
#(15 1 -9 -20 10 5) max -> 15 |
|
3607 |
#(15 1 -9 -20 10 5) maxApplying:[:el | el abs] -> 20 |
|
3608 |
" |
|
3609 |
||
3610 |
"Created: / 23-08-2010 / 11:02:50 / cg" |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3611 |
"Modified: / 18-03-2011 / 10:32:29 / cg" |
13040 | 3612 |
! |
3613 |
||
7821 | 3614 |
min |
3615 |
"return the minimum value in the receiver collection, |
|
3616 |
using < to compare elements. |
|
3617 |
Raises an error, if the receiver is empty." |
|
3618 |
||
3619 |
self emptyCheck. |
|
3620 |
||
12945 | 3621 |
^ self |
3622 |
inject:nil |
|
3623 |
into:[:minSoFar :this | |
|
3624 |
(minSoFar isNil or:[this < minSoFar]) |
|
3625 |
ifTrue:[this] |
|
3626 |
ifFalse:[minSoFar] |
|
3627 |
] |
|
7821 | 3628 |
|
3629 |
" |
|
3630 |
#(15 1 -9 10 5) min |
|
3631 |
(1 to:15) min |
|
3632 |
(-1 to:-15 by:-1) min |
|
3633 |
(-1 to:-15 by:-4) min |
|
3634 |
" |
|
3635 |
||
12945 | 3636 |
"Modified: / 11-07-2010 / 17:06:38 / cg" |
7821 | 3637 |
! |
3638 |
||
13040 | 3639 |
minApplying:aBlock |
3640 |
"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
|
3641 |
using aBlock to compare elements. |
13040 | 3642 |
Raises an error, if the receiver is empty." |
3643 |
||
3644 |
self emptyCheck. |
|
3645 |
||
3646 |
^ self |
|
3647 |
inject:nil |
|
3648 |
into:[:minSoFar :this | |
|
3649 |
|v| |
|
3650 |
||
3651 |
v := aBlock value:this. |
|
3652 |
(minSoFar isNil or:[v < minSoFar]) |
|
3653 |
ifTrue:[v] |
|
3654 |
ifFalse:[minSoFar] |
|
3655 |
] |
|
3656 |
||
3657 |
" |
|
3658 |
#(15 -1 -9 10 5) min -> -9 |
|
3659 |
#(15 -1 -9 10 5) minApplying:[:el | el abs] -> 1 |
|
3660 |
" |
|
3661 |
||
3662 |
"Created: / 23-08-2010 / 11:01:42 / cg" |
|
13317
1230e1aa7042
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13316
diff
changeset
|
3663 |
"Modified: / 18-03-2011 / 10:32:33 / cg" |
13040 | 3664 |
! |
3665 |
||
7821 | 3666 |
minMax |
3667 |
"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
|
3668 |
as a two element array, using #< to compare elements. |
7821 | 3669 |
Raises an error, if the receiver is empty." |
3670 |
||
3671 |
|min max| |
|
3672 |
||
3673 |
self emptyCheck. |
|
3674 |
||
3675 |
min := max := nil. |
|
3676 |
self do:[:each | |
|
3677 |
min isNil ifTrue:[ |
|
3678 |
min := max := each |
|
3679 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3680 |
max < each ifTrue:[ |
7821 | 3681 |
max := each |
3682 |
] ifFalse:[ |
|
8182
e99d0f95c925
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8178
diff
changeset
|
3683 |
each < min ifTrue:[ |
7821 | 3684 |
min := each |
3685 |
] |
|
3686 |
] |
|
3687 |
] |
|
3688 |
]. |
|
3689 |
^ Array with:min with:max |
|
3690 |
||
3691 |
" |
|
3692 |
#(15 1 -9 10 5) minMax |
|
3693 |
(1 to:15) minMax |
|
3694 |
(-1 to:-15 by:-1) minMax |
|
3695 |
(-1 to:-15 by:-4) minMax |
|
3696 |
(0 to:15 by:4) minMax |
|
3697 |
" |
|
3698 |
! |
|
3699 |
||
9919
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3700 |
nthLargest:n |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3701 |
"return the n-largest element" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3702 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3703 |
^ (self largest:n) first |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3704 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3705 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3706 |
#(10 35 20 45 30 5) nthLargest:1 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3707 |
#(10 35 20 45 30 5) nthLargest:2 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3708 |
#(10 35 20 45 30 5) nthLargest:3 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3709 |
#(10 35 20 45 30 5) nthLargest:5 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3710 |
#(10 35 20 45 30 5) nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3711 |
#(10 35 20 45 30 5) nthLargest:8 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3712 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3713 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3714 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3715 |
|t1 t2 data| |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3716 |
|
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3717 |
data := (1 to:10000) collect:[:i | Random nextInteger ]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3718 |
t1 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3719 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3720 |
data asSortedCollection at:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3721 |
] |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3722 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3723 |
t2 := Time millisecondsToRun:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3724 |
40 timesRepeat:[ |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3725 |
data nthLargest:6 |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3726 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3727 |
]. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3728 |
Transcript show:'asSorted-at -> '; show:t1; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3729 |
Transcript show:'nthMost -> '; show:t2; showCR:'ms'. |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3730 |
" |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3731 |
! |
dd9d3f24c6d0
optimized code for n-largest
Claus Gittinger <cg@exept.de>
parents:
9843
diff
changeset
|
3732 |
|
5557 | 3733 |
size |
3734 |
"return the number of elements in the receiver. |
|
3735 |
This is usually redefined in subclasses for more performance." |
|
3736 |
||
3737 |
|count "{ Class: SmallInteger }" | |
|
3738 |
||
3739 |
count := 0. |
|
3740 |
self do:[:element | |
|
6135 | 3741 |
count := count + 1 |
5557 | 3742 |
]. |
3743 |
^ count |
|
6547
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3744 |
! |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3745 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3746 |
speciesForAdding |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3747 |
"like species, but redefined for collections which cannot grow easily. |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3748 |
Used by functions which create a growing collection (see collect:with:, for example)" |
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3749 |
|
86e092527dd1
+#speciesForAdding; better #collect:with: for fixed size collections
Claus Gittinger <cg@exept.de>
parents:
6511
diff
changeset
|
3750 |
^ self species |
13338 | 3751 |
! |
3752 |
||
3753 |
standardDeviation |
|
3754 |
"standard deviation value of all elements in the collection" |
|
3755 |
||
3756 |
|m sumDeltaSquares| |
|
3757 |
||
3758 |
m := self arithmeticMean. |
|
3759 |
sumDeltaSquares := self inject:0 into:[:sum :this | sum + ((this - m) squared)]. |
|
3760 |
^ (sumDeltaSquares / (self size - 1)) sqrt |
|
3761 |
||
3762 |
" |
|
3763 |
TestCase assert:( { 1. 2. 3. 4 } arithmeticMean = 2.5). |
|
3764 |
TestCase assert:( { 13. 23. 12. 44. 55 } arithmeticMean closeTo: 29.4). |
|
3765 |
TestCase assert:( { 13. 23. 12. 44. 55 } standardDeviation closeTo: 19.2431). |
|
3766 |
" |
|
3767 |
||
3768 |
"Created: / 13-04-2011 / 17:58:47 / cg" |
|
602 | 3769 |
! ! |
3770 |
||
11706 | 3771 |
!Collection methodsFor:'searching'! |
3772 |
||
3773 |
findFirst:aBlock |
|
3774 |
"find the index of the first element, for which evaluation of the argument, aBlock |
|
3775 |
returns true; return its index or 0 if none detected. |
|
3776 |
This is much like #detect, however, here an INDEX is returned, |
|
3777 |
while #detect returns the element." |
|
3778 |
||
3779 |
^ self findFirst:aBlock ifNone:0 |
|
3780 |
||
3781 |
" |
|
3782 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
|
3783 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
3784 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
3785 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
3786 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
|
3787 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
3788 |
" |
|
3789 |
||
3790 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
3791 |
! ! |
|
3792 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3793 |
!Collection methodsFor:'set operations'! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3794 |
|
7169 | 3795 |
\ aCollection |
8606 | 3796 |
"return a new set containing all elements of the receiver, |
3797 |
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
|
3798 |
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
|
3799 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3800 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3801 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3802 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3803 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3804 |
self do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3805 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3806 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3807 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3808 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3809 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3810 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3811 |
" |
7169 | 3812 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) asSet |
3813 |
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) |
|
3814 |
('hello' \ 'l') asString |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3815 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3816 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3817 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3818 |
intersect:aCollection |
8606 | 3819 |
"return a new set containing all elements of the receiver, |
3820 |
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
|
3821 |
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
|
3822 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3823 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3824 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3825 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3826 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3827 |
(self includes:element) ifTrue:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3828 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3829 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3830 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3831 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3832 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3833 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3834 |
#(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
|
3835 |
#(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
|
3836 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3837 |
! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3838 |
|
7169 | 3839 |
union:aCollection |
3840 |
"return a new set containing all elements of the receiver |
|
3841 |
plus those of the aCollection" |
|
3842 |
||
3843 |
|newCollection| |
|
3844 |
||
3845 |
newCollection := self speciesForAdding new. |
|
3846 |
newCollection addAll:self. |
|
3847 |
newCollection addAll:aCollection. |
|
3848 |
^ newCollection |
|
3849 |
||
3850 |
" |
|
3851 |
#(0 2 4 6 8) union:#(1 3 5 7) |
|
11384 | 3852 |
#(0 2 4 6 8) union:#(0 1 3 5 7) |
7169 | 3853 |
" |
3854 |
! |
|
3855 |
||
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3856 |
xor:aCollection |
8606 | 3857 |
"return a new set containing all elements, |
3858 |
which are contained in either the receiver or aCollection, but not in both. |
|
3859 |
||
3860 |
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
|
3861 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3862 |
|newCollection| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3863 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3864 |
newCollection := self speciesForAdding new. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3865 |
aCollection do:[:element | |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3866 |
(self includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3867 |
newCollection add:element |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3868 |
] |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3869 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3870 |
self do:[:element| |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3871 |
(aCollection includes:element) ifFalse:[ |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3872 |
newCollection add:element. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3873 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3874 |
]. |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3875 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3876 |
^ newCollection |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3877 |
|
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3878 |
" |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3879 |
#(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
|
3880 |
" |
12430 | 3881 |
|
3882 |
" |
|
3883 |
|c1 c2| |
|
3884 |
||
3885 |
c1 := #( foo bar baz baloo ). |
|
3886 |
c2 := #( foe bar banana baloo ). |
|
3887 |
c1 symmetricDifference:c2. |
|
3888 |
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet |
|
3889 |
" |
|
7168
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3890 |
! ! |
e5400f46d437
Move set operations (#intersect: et al) to Collection
Stefan Vogel <sv@exept.de>
parents:
7164
diff
changeset
|
3891 |
|
10811 | 3892 |
!Collection methodsFor:'sorting & reordering'! |
3893 |
||
3894 |
topologicalSort |
|
3895 |
"Sort a partial ordered collection. |
|
3896 |
The receiver consists of tupels defining a partial order. |
|
3897 |
Use the algorithm by R. E. Tarjan from 1972. |
|
3898 |
Answer an OrderedCollection containing the sorted items" |
|
3899 |
||
3900 |
|graph roots sorted count| |
|
3901 |
||
3902 |
"create a graph. |
|
3903 |
For each node there is an entry containing the number of references |
|
3904 |
and a list of arcs to other nodes" |
|
3905 |
||
3906 |
graph := Dictionary new. |
|
3907 |
||
3908 |
self do:[:eachTuple| |
|
3909 |
|node1| |
|
3910 |
node1 := eachTuple first. |
|
3911 |
graph at:node1 ifAbsentPut:[OrderedCollection with:0]. |
|
3912 |
1 to:(eachTuple size-1) do:[:i| |
|
3913 |
|node2 n2| |
|
3914 |
||
3915 |
node1 := eachTuple at:i. |
|
3916 |
node2 := eachTuple at:i+1. |
|
3917 |
"add the node" |
|
3918 |
graph at:node2 ifAbsentPut:[OrderedCollection with:0]. |
|
3919 |
"add an arc from node1 to node2" |
|
3920 |
(graph at:node1) add:node2. |
|
3921 |
"increment count on incoming arcs of node2" |
|
3922 |
n2 := graph at:node2. |
|
3923 |
n2 at:1 put:(n2 first + 1). |
|
3924 |
]. |
|
3925 |
]. |
|
3926 |
||
3927 |
"now find the root nodes (having zero incoming arcs)" |
|
3928 |
roots := OrderedCollection new. |
|
3929 |
graph keysAndValuesDo:[:eachKey :eachValue| |
|
3930 |
eachValue first = 0 ifTrue:[ |
|
3931 |
roots add:eachKey |
|
3932 |
]. |
|
3933 |
]. |
|
3934 |
||
3935 |
"for each root, out it to the sorted list and remove it from the graph. |
|
3936 |
This may cause referenced nodes to be moved to the root" |
|
3937 |
||
3938 |
sorted := OrderedCollection new:graph size. |
|
3939 |
[roots notEmpty] whileTrue:[ |
|
3940 |
|root eachEntry| |
|
3941 |
||
3942 |
root := roots removeFirst. |
|
3943 |
sorted add:root. |
|
3944 |
eachEntry := graph removeKey:root. |
|
3945 |
2 to:eachEntry size do:[:i| |
|
3946 |
|eachChild eachChildGraph| |
|
3947 |
eachChild := eachEntry at:i. |
|
3948 |
eachChildGraph := graph at:eachChild. |
|
3949 |
count := eachChildGraph at:1. |
|
3950 |
eachChildGraph at:1 put:(count - 1). |
|
3951 |
count = 1 ifTrue:[ |
|
3952 |
roots add:eachChild. |
|
3953 |
]. |
|
3954 |
]. |
|
3955 |
]. |
|
3956 |
||
3957 |
graph notEmpty ifTrue:[ |
|
3958 |
self error:'cycle in ordering' mayProceed:true. |
|
3959 |
]. |
|
3960 |
||
3961 |
^ sorted |
|
3962 |
||
3963 |
" |
|
3964 |
#((1 2) (1 3) (3 2)) topologicalSort |
|
11384 | 3965 |
#((1 2) (1 3) (3 2) (4)) topologicalSort |
3966 |
||
3967 |
with cycles: |
|
10811 | 3968 |
#((1 2) (1 3) (3 1)) topologicalSort |
11384 | 3969 |
#((1 2) (2 3) (3 1)) topologicalSort |
3970 |
#((1 2) (2 3) (3 4) (4 1)) topologicalSort |
|
11385
c3d908e81ec7
changed #topologicalSort - comment
Stefan Vogel <sv@exept.de>
parents:
11384
diff
changeset
|
3971 |
#((1 1) (1 2) (1 3) (3 2) (4)) topologicalSort |
10811 | 3972 |
|
3973 |
(Smalltalk allClasses collect:[:eachClass| |
|
3974 |
Array with:eachClass superclass with:eachClass |
|
3975 |
]) topologicalSort |
|
3976 |
" |
|
3977 |
! ! |
|
3978 |
||
602 | 3979 |
!Collection methodsFor:'testing'! |
3980 |
||
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3981 |
allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3982 |
"true if all elements of the receiver have the same value" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3983 |
|
13872
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
3984 |
^ self sameValuesComputedBy:[:el | el] |
13870
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3985 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3986 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3987 |
#(1 2 3 5 6 7 8 9) allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3988 |
#(1 1 1 1 1 1) allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3989 |
#(1 1 1.0 1.0 1) allElementsHaveTheSameValue |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3990 |
" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3991 |
|
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3992 |
"Created: / 21-12-2011 / 15:54:08 / cg" |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3993 |
! |
cee65ca9b95d
added: #allElementsHaveTheSameValue
Claus Gittinger <cg@exept.de>
parents:
13773
diff
changeset
|
3994 |
|
602 | 3995 |
capacity |
3996 |
"return the number of elements, that the receiver is |
|
3997 |
prepared to take. For most collections, this is the actual |
|
3998 |
size. However, some have more space preallocated to allow |
|
3999 |
for faster adding of elements. |
|
4000 |
Not used by the system; added for ST-80 compatibility." |
|
4001 |
||
4002 |
^ self size |
|
4003 |
! |
|
4004 |
||
4005 |
includes:anElement |
|
4006 |
"return true, if an object equal to the argument, anObject is in the list. |
|
4007 |
This compares using #= (i.e. it does not look for the object itself, |
|
4008 |
instead, one that compares equal). |
|
7807 | 4009 |
See #includesIdentical: when identity is asked for. |
4010 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 4011 |
|
4012 |
self do:[:element | |
|
6135 | 4013 |
(anElement = element) ifTrue:[^ true]. |
602 | 4014 |
]. |
4015 |
^ false |
|
4016 |
! |
|
4017 |
||
4018 |
includesAll:aCollection |
|
4019 |
"return true, if the the receiver includes all elements of |
|
4020 |
the argument, aCollection; false if any is missing. |
|
4021 |
Notice: this method has O-square 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
|
4022 |
slow for big receivers/args. |
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
4023 |
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
|
4024 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
4025 |
^ aCollection conform:[:element | (self includes:element)] |
602 | 4026 |
|
4027 |
" |
|
4028 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
4029 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
4030 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
4031 |
" |
|
10092
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
4032 |
|
70a5cb4e5df0
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
9919
diff
changeset
|
4033 |
"Modified: / 13-10-2006 / 12:54:50 / cg" |
602 | 4034 |
! |
4035 |
||
4036 |
includesAny:aCollection |
|
4037 |
"return true, if the the receiver includes any elements of |
|
4038 |
the argument, aCollection; false if it includes none. |
|
7807 | 4039 |
Notice: this method has O^2(N) runtime behavior and may be |
4040 |
slow for big receivers/args. |
|
4041 |
Think about using a Set or Dictionary. |
|
4042 |
Some speedup is also possible, by arranging highly |
|
6135 | 4043 |
probable elements towards the beginning of aCollection, |
4044 |
to avoid useless searches." |
|
602 | 4045 |
|
4046 |
aCollection do:[:element | |
|
6135 | 4047 |
(self includes:element) ifTrue:[^ true]. |
602 | 4048 |
]. |
4049 |
^ false |
|
4050 |
||
4051 |
" |
|
4052 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
4053 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
4054 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
4055 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
4056 |
" |
|
4057 |
! |
|
4058 |
||
10504 | 4059 |
includesAnyIdentical:aCollection |
4060 |
"return true, if the the receiver includes any elements of |
|
4061 |
the argument, aCollection; false if it includes none. |
|
4062 |
Use identity compare for comparing. |
|
4063 |
Notice: this method has O^2(N) runtime behavior and may be |
|
4064 |
slow for big receivers/args. |
|
4065 |
Think about using a Set or Dictionary. |
|
4066 |
Some speedup is also possible, by arranging highly |
|
4067 |
probable elements towards the beginning of aCollection, |
|
4068 |
to avoid useless searches." |
|
4069 |
||
4070 |
aCollection do:[:element | |
|
4071 |
(self includesIdentical:element) ifTrue:[^ true]. |
|
4072 |
]. |
|
4073 |
^ false |
|
4074 |
||
4075 |
" |
|
4076 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(1 2 3) |
|
4077 |
#('hello' 'there' 'world') includesAnyIdentical:#('hello' 'world') |
|
4078 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(7 8 9) |
|
4079 |
#(1 2 3 4 5 6 7) includesAnyIdentical:#(8 9 10) |
|
4080 |
" |
|
4081 |
! |
|
4082 |
||
602 | 4083 |
includesIdentical:anElement |
7807 | 4084 |
"return true, if the argument, anObject is in the collection. |
602 | 4085 |
This compares using #== (i.e. object identity). |
7807 | 4086 |
See #includes: when equality is asked for. |
4087 |
This is a *very* slow fallback - many subclasses redefine this for performance." |
|
602 | 4088 |
|
4089 |
self do:[:element | |
|
6135 | 4090 |
(anElement == element) ifTrue:[^ true]. |
602 | 4091 |
]. |
4092 |
^ false |
|
4093 |
! |
|
4094 |
||
9005 | 4095 |
isCollection |
4096 |
"return true, if the receiver is some kind of collection; |
|
4097 |
true is returned here - the method is redefined from Object." |
|
4098 |
||
4099 |
^ true |
|
4100 |
! |
|
4101 |
||
602 | 4102 |
isEmpty |
4103 |
"return true, if the receiver is empty" |
|
4104 |
||
4105 |
^ self size == 0 |
|
4106 |
! |
|
4107 |
||
6181 | 4108 |
isEmptyOrNil |
8833 | 4109 |
"return true if I am nil or an empty collection - true here, if the receivers size is 0, |
4110 |
(from Sqeak)" |
|
6181 | 4111 |
|
4112 |
^ self isEmpty |
|
4113 |
||
4114 |
"Created: / 13.11.2001 / 13:17:12 / cg" |
|
6182 | 4115 |
"Modified: / 13.11.2001 / 13:28:35 / cg" |
6181 | 4116 |
! |
4117 |
||
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4118 |
isNilOrEmptyCollection |
8833 | 4119 |
"return true if I am nil or an empty collection - false here. |
4120 |
Obsolete, use isEmptyOrNil." |
|
4121 |
||
4122 |
<resource:#obsolete> |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4123 |
|
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4124 |
^ self isEmpty |
6182 | 4125 |
|
4126 |
"Modified: / 13.11.2001 / 13:27:55 / cg" |
|
6068
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4127 |
! |
1d5ca1175f3f
added #isNilOrEmptyCollection
Claus Gittinger <cg@exept.de>
parents:
6052
diff
changeset
|
4128 |
|
9005 | 4129 |
isNonByteCollection |
4130 |
"return true, if the receiver is some kind of collection, but not a String, ByteArray etc.; |
|
4131 |
true is returned here - the method is redefined from Object." |
|
4132 |
||
4133 |
^ true |
|
4134 |
! |
|
4135 |
||
4136 |
isSorted |
|
4137 |
"return true, if the receiver is sorted. |
|
4138 |
Collections which hold their elements in sorted order |
|
4139 |
should return true. Some algorithms (quicksort) degenerate when |
|
4140 |
operating on sorted collections and can be avoided if this information |
|
4141 |
is given. The default returned here (false) should not hurt. |
|
4142 |
I.e. you should NEVER depend on that in your application." |
|
4143 |
||
4144 |
^ false |
|
4145 |
||
4146 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
4147 |
! |
|
4148 |
||
4149 |
isSortedBy:aBlock |
|
4150 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock). |
|
4151 |
Collections which hold their elements in sorted order |
|
4152 |
should return true. Some algorithms (quicksort) degenerate when |
|
4153 |
operating on sorted collections and can be avoided if this information |
|
4154 |
is given. The default returned here (false) should not hurt. |
|
4155 |
I.e. you should NEVER depend on that in your application." |
|
4156 |
||
4157 |
^ false |
|
4158 |
||
4159 |
! |
|
4160 |
||
4161 |
isSortedCollection |
|
4162 |
"return true, if the receiver is a sortedCollection." |
|
4163 |
||
4164 |
^ false |
|
4165 |
||
4166 |
! |
|
4167 |
||
9247 | 4168 |
isWeakCollection |
4169 |
"return true, if the receiver has weak references to its elements." |
|
4170 |
||
4171 |
^ false |
|
4172 |
! |
|
4173 |
||
602 | 4174 |
notEmpty |
4175 |
"return true, if the receiver is not empty" |
|
4176 |
||
4177 |
^ self isEmpty not |
|
4178 |
! |
|
4179 |
||
6932 | 4180 |
notEmptyOrNil |
4181 |
"Squeak compatibility: |
|
4182 |
return true if I am neither nil nor an empty collection." |
|
4183 |
||
4184 |
^ self notEmpty |
|
4185 |
! |
|
4186 |
||
602 | 4187 |
occurrencesOf:anElement |
4188 |
"return the number of occurrences of the argument, anElement in |
|
5048 | 4189 |
the receiver. Uses #= (i.e. equality) compare." |
602 | 4190 |
|
4191 |
|count "{ Class: SmallInteger }" | |
|
4192 |
||
4193 |
count := 0. |
|
4194 |
self do:[:element | |
|
5048 | 4195 |
(anElement = element) ifTrue:[ |
4196 |
count := count + 1 |
|
4197 |
]. |
|
602 | 4198 |
]. |
4199 |
^ count |
|
8274 | 4200 |
! |
4201 |
||
4202 |
occurrencesOfAny:aCollectionOfElements |
|
4203 |
"return the number of occurrences of any in aCollectionOfElements in the receiver. |
|
4204 |
Uses #= (i.e. equality) compare. |
|
4205 |
Should be redefined in subclass(es) if ever used heavily." |
|
4206 |
||
4207 |
|count "{ Class: SmallInteger }" | |
|
4208 |
||
4209 |
count := 0. |
|
8275 | 4210 |
aCollectionOfElements do:[:element | |
4211 |
count := count + (self occurrencesOf:element) |
|
8274 | 4212 |
]. |
4213 |
^ count |
|
4214 |
||
4215 |
" |
|
4216 |
#(1 4 6 8 4 1) occurrencesOfAny:#(1 4) |
|
4217 |
#(1 4 6 8 4 1) occurrencesOfAny:#(2 5) |
|
4218 |
'hello world' occurrencesOfAny:'hel' |
|
4219 |
" |
|
13871 | 4220 |
! |
4221 |
||
13872
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4222 |
sameValuesComputedBy:aBlock |
13871 | 4223 |
"true if aBlock answers the same value for all elements of the receiver" |
4224 |
||
4225 |
|first valueForFirstElement| |
|
4226 |
||
4227 |
first := true. |
|
4228 |
self do:[:each | |
|
4229 |
first ifTrue:[ |
|
4230 |
first := false. |
|
4231 |
valueForFirstElement := aBlock value:each. |
|
4232 |
] ifFalse:[ |
|
4233 |
valueForFirstElement = (aBlock value:each) ifFalse:[ |
|
4234 |
^ false. |
|
4235 |
]. |
|
4236 |
]. |
|
4237 |
]. |
|
4238 |
^ true |
|
4239 |
||
4240 |
" |
|
13872
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4241 |
#(1 2 3 5 6 7 8 9) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4242 |
#(1 1 1 1 1 1) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4243 |
#(1 1 1.0 1.0 1) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4244 |
#(1 3 3 15 1) sameValuesComputedBy:[:el | el even] |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4245 |
" |
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4246 |
|
073df93a253b
added: #sameValuesComputedBy:
Claus Gittinger <cg@exept.de>
parents:
13871
diff
changeset
|
4247 |
"Created: / 21-12-2011 / 15:59:19 / cg" |
602 | 4248 |
! ! |
4249 |
||
4680 | 4250 |
!Collection methodsFor:'tracing'! |
4251 |
||
4252 |
traceInto:aRequestor level:level from:referrer |
|
4253 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
4254 |
||
4255 |
^ aRequestor traceCollection:self level:level from:referrer |
|
4256 |
||
4257 |
||
4258 |
! ! |
|
4259 |
||
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4260 |
!Collection methodsFor:'visiting'! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4261 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4262 |
acceptVisitor:aVisitor with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4263 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4264 |
^ aVisitor visitCollection:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4265 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8359
diff
changeset
|
4266 |
|
2097 | 4267 |
!Collection class methodsFor:'documentation'! |
628 | 4268 |
|
13699 | 4269 |
version |
14027 | 4270 |
^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.272 2012-02-28 20:22:53 cg Exp $' |
13699 | 4271 |
! |
4272 |
||
12090 | 4273 |
version_CVS |
14027 | 4274 |
^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.272 2012-02-28 20:22:53 cg Exp $' |
628 | 4275 |
! ! |
6867 | 4276 |
|
602 | 4277 |
Collection initialize! |