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