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