author | tm |
Fri, 04 Aug 2000 18:44:15 +0200 | |
changeset 5497 | 92bb94a53929 |
parent 5359 | 396b77b6f1d2 |
child 5498 | 489dc2e6bd4f |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 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 |
" |
|
12 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
14 |
|
1 | 15 |
Object subclass:#Collection |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
16 |
instanceVariableNames:'' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
17 |
classVariableNames:'InvalidKeySignal EmptyCollectionSignal ValueNotFoundSignal |
5497 | 18 |
NotEnoughElementsSignal RecursiveCollectionStoreStringSignal' |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
19 |
poolDictionaries:'' |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
20 |
category:'Collections-Abstract' |
1 | 21 |
! |
22 |
||
2097 | 23 |
!Collection class methodsFor:'documentation'! |
88 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 28 |
All Rights Reserved |
1 | 29 |
|
88 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
88 | 39 |
documentation |
40 |
" |
|
41 |
Abstract superclass for all collections. |
|
42 |
This abstract class provides functionality common to all collections, |
|
43 |
without knowing how the concrete class implements things. Thus, all |
|
44 |
methods found here depend on some basic mechanisms to be defined in the |
|
45 |
concrete class. |
|
46 |
These basic methods are usually defined as #subclassResponsibility here. |
|
47 |
Some methods are also redefined for better performance. |
|
1289 | 48 |
|
1325 | 49 |
Subclasses should at least implement: |
50 |
do: - enumerate elements |
|
51 |
||
52 |
they should implement one of the following set of access messages: |
|
53 |
keyed collections: |
|
54 |
at:ifAbsent: - fetching an element |
|
55 |
at: - fetching an element |
|
56 |
at:put: - storing an element |
|
57 |
||
58 |
unkeyed collections: |
|
59 |
add: - add an element |
|
60 |
remove:ifAbsent: - remove an element |
|
61 |
||
62 |
||
1289 | 63 |
[author:] |
64 |
Claus Gittinger |
|
88 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
2097 | 68 |
!Collection class methodsFor:'initialization'! |
10 | 69 |
|
70 |
initialize |
|
71 |
"setup the signal" |
|
72 |
||
54 | 73 |
InvalidKeySignal isNil ifTrue:[ |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
74 |
InvalidKeySignal := ErrorSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
75 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
76 |
InvalidKeySignal notifierString:'invalid key:'. |
345 | 77 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
78 |
ValueNotFoundSignal := NotFoundSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
79 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
80 |
ValueNotFoundSignal notifierString:'value not found:'. |
155 | 81 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
82 |
NotEnoughElementsSignal := NotFoundSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
83 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
84 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
348 | 85 |
|
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
86 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
87 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
88 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
5497 | 89 |
|
90 |
RecursiveCollectionStoreStringSignal := QuerySignal new. |
|
91 |
RecursiveCollectionStoreStringSignal nameClass:self message:#recursiveCollectionStoreStringSignal. |
|
92 |
RecursiveCollectionStoreStringSignal notifierString:'attempt to generate storeString of recursive collection'. |
|
54 | 93 |
] |
3102
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
94 |
|
2bc81588db6c
sigs are children of NotFoundSignal (ST80 compat)
Claus Gittinger <cg@exept.de>
parents:
3075
diff
changeset
|
95 |
"Modified: / 8.11.1997 / 19:18:17 / cg" |
10 | 96 |
! ! |
97 |
||
2097 | 98 |
!Collection class methodsFor:'instance creation'! |
155 | 99 |
|
602 | 100 |
new:size withAll:element |
101 |
"return a new COllection of size, where all elements are |
|
102 |
initialized to element" |
|
348 | 103 |
|
602 | 104 |
|newCollection| |
92 | 105 |
|
602 | 106 |
newCollection := self new:size. |
107 |
size timesRepeat:[newCollection add:element] |
|
108 |
! |
|
1 | 109 |
|
110 |
with:anObject |
|
111 |
"return a new Collection with one element:anObject" |
|
112 |
||
113 |
|newCollection| |
|
114 |
||
115 |
newCollection := self new. |
|
116 |
newCollection add:anObject. |
|
117 |
^ newCollection |
|
118 |
! |
|
119 |
||
120 |
with:firstObject with:secondObject |
|
121 |
"return a new Collection with two elements:firstObject and secondObject" |
|
122 |
||
123 |
|newCollection| |
|
124 |
||
125 |
newCollection := self new. |
|
1112 | 126 |
newCollection add:firstObject; add:secondObject. |
1 | 127 |
^ newCollection |
128 |
! |
|
129 |
||
130 |
with:firstObject with:secondObject with:thirdObject |
|
131 |
"return a new Collection with three elements" |
|
132 |
||
133 |
|newCollection| |
|
134 |
||
135 |
newCollection := self new. |
|
1112 | 136 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 137 |
^ newCollection |
138 |
! |
|
139 |
||
140 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
141 |
"return a new Collection with four elements" |
|
142 |
||
143 |
|newCollection| |
|
144 |
||
145 |
newCollection := self new. |
|
1112 | 146 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 147 |
^ newCollection |
148 |
! |
|
149 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
150 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
345 | 151 |
"return a new Collection with five elements" |
152 |
||
153 |
|newCollection| |
|
154 |
||
155 |
newCollection := self new. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
156 |
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
|
157 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
158 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
159 |
"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
|
160 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
161 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
162 |
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
|
163 |
"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
|
164 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
165 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
166 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
167 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
168 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6. |
345 | 169 |
^ newCollection |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
170 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
171 |
"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
|
172 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
173 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
174 |
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
|
175 |
"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
|
176 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
177 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
178 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
179 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
180 |
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
|
181 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
182 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
183 |
"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
|
184 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
185 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
186 |
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
|
187 |
"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
|
188 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
189 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
190 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
191 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
192 |
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
|
193 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
194 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
195 |
"Created: 22.1.1997 / 19:34:34 / cg" |
345 | 196 |
! |
197 |
||
1 | 198 |
withAll:aCollection |
199 |
"return a new Collection with all elements taken from the argument, |
|
200 |
aCollection" |
|
201 |
||
202 |
|newCollection| |
|
203 |
||
204 |
newCollection := self new. |
|
205 |
newCollection addAll:aCollection. |
|
206 |
^newCollection |
|
207 |
! ! |
|
208 |
||
2097 | 209 |
!Collection class methodsFor:'Signal constants'! |
10 | 210 |
|
602 | 211 |
emptyCollectionSignal |
212 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 213 |
|
602 | 214 |
^ EmptyCollectionSignal |
308 | 215 |
! |
216 |
||
602 | 217 |
invalidKeySignal |
218 |
"return the signal used to report bad key usage" |
|
308 | 219 |
|
602 | 220 |
^ InvalidKeySignal |
201 | 221 |
! |
222 |
||
602 | 223 |
notEnoughElementsSignal |
224 |
"return the signal used to report attempts for an operation, for which |
|
225 |
there are not enough elements in the collection" |
|
249 | 226 |
|
602 | 227 |
^ NotEnoughElementsSignal |
404 | 228 |
! |
229 |
||
5497 | 230 |
recursiveCollectionStoreStringSignal |
231 |
"return the query signal used to report storeString generation of recursive collections" |
|
232 |
||
233 |
^ RecursiveCollectionStoreStringSignal |
|
234 |
||
235 |
" |
|
236 |
Collection recursiveCollectionStoreStringSignal handle:[:ex | |
|
237 |
self halt |
|
238 |
] do:[ |
|
239 |
|a| |
|
240 |
||
241 |
a := Array new:1. |
|
242 |
a at:1 put:a. |
|
243 |
a storeOn:Transcript |
|
244 |
] |
|
245 |
" |
|
246 |
||
247 |
" |
|
248 |
|a| |
|
249 |
||
250 |
a := Array new:1. |
|
251 |
a at:1 put:a. |
|
252 |
a storeOn:Transcript |
|
253 |
" |
|
254 |
||
255 |
! |
|
256 |
||
602 | 257 |
valueNotFoundSignal |
258 |
"return the signal used to report a nonexisting element." |
|
404 | 259 |
|
602 | 260 |
^ ValueNotFoundSignal |
261 |
! ! |
|
404 | 262 |
|
2097 | 263 |
!Collection class methodsFor:'queries'! |
404 | 264 |
|
602 | 265 |
growIsCheap |
266 |
"return true, if this collection can easily grow |
|
267 |
(i.e. without a need for become:). |
|
268 |
Returns true here; this method is redefined in fix-size |
|
269 |
collections" |
|
404 | 270 |
|
602 | 271 |
^ true |
257 | 272 |
! ! |
273 |
||
1 | 274 |
!Collection methodsFor:'accessing'! |
275 |
||
602 | 276 |
anElement |
277 |
"return any element from the collection, or nil if there is none" |
|
278 |
||
279 |
self do: [:each | ^ each]. |
|
280 |
^ nil |
|
281 |
! |
|
282 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
283 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
284 |
"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
|
285 |
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
|
286 |
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
|
287 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
288 |
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
|
289 |
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
|
290 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
291 |
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
|
292 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
293 |
" |
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
294 |
args: indexCollection : <Collection of keys<object> > |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
295 |
returns: self |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
296 |
" |
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
297 |
|
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
298 |
" |
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
299 |
(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
|
300 |
(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
|
301 |
(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
|
302 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
303 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
304 |
(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
|
305 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
306 |
|
3469
2c7f6fc4685a
started args / returns comments.
Claus Gittinger <cg@exept.de>
parents:
3402
diff
changeset
|
307 |
"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
|
308 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
309 |
|
4985 | 310 |
fifth |
311 |
"return the fifth element of the collection. |
|
312 |
For unordered collections, this simply returns the fifth |
|
313 |
element when enumerating them. |
|
314 |
This should be redefined in subclasses." |
|
315 |
||
316 |
^ self nth:5 |
|
317 |
||
318 |
" |
|
319 |
#(1 2 3 4 5) fifth |
|
320 |
" |
|
321 |
! |
|
322 |
||
68 | 323 |
first |
324 |
"return the first element of the collection. |
|
4855 | 325 |
For unordered collections, this simply returns the first |
326 |
element when enumerating them. |
|
68 | 327 |
This should be redefined in subclasses." |
328 |
||
329 |
self do:[:e | ^ e]. |
|
330 |
||
331 |
"error if collection is empty" |
|
155 | 332 |
^ self emptyCollectionError |
68 | 333 |
! |
334 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
335 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
336 |
"return the first element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
337 |
If its empty, return the excveptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
338 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
339 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
340 |
self isEmpty ifTrue:[^ exceptionValue value]. |
3124 | 341 |
^ self first |
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
342 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
343 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
344 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
345 |
|
4985 | 346 |
fourth |
347 |
"return the fourth element of the collection. |
|
348 |
For unordered collections, this simply returns the fourth |
|
349 |
element when enumerating them. |
|
350 |
This should be redefined in subclasses." |
|
351 |
||
352 |
^ self nth:4 |
|
353 |
||
354 |
" |
|
355 |
#(1 2 3 4) fourth |
|
356 |
" |
|
357 |
! |
|
358 |
||
68 | 359 |
last |
360 |
"return the last element of the collection. |
|
361 |
This should be redefined in subclasses." |
|
362 |
||
363 |
|theLastOne any| |
|
364 |
||
365 |
any := false. |
|
366 |
self do:[:e | any := true. theLastOne := e]. |
|
367 |
any ifTrue:[ |
|
155 | 368 |
^ theLastOne |
68 | 369 |
]. |
370 |
||
371 |
"error if collection is empty" |
|
155 | 372 |
^ self emptyCollectionError |
68 | 373 |
! |
374 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
375 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
376 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
377 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
378 |
(i.e. dont trigger an error as done in #last)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
379 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
380 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
381 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
382 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
383 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
384 |
"Created: 6.2.1996 / 15:27:49 / cg" |
4855 | 385 |
! |
386 |
||
4985 | 387 |
nth:n |
388 |
"return the nth element of the collection. |
|
389 |
For unordered collections, this simply returns the nth |
|
390 |
element when enumerating them. |
|
391 |
This should be redefined in subclasses." |
|
392 |
||
393 |
|count| |
|
394 |
||
395 |
count := 1. |
|
396 |
self do:[:e | |
|
397 |
count == n ifTrue:[^ e]. |
|
398 |
count := count + 1 |
|
399 |
]. |
|
400 |
||
5266 | 401 |
"error if collection is smaller" |
402 |
^ self notEnoughElementsError |
|
4985 | 403 |
|
404 |
" |
|
405 |
#(1 2 3 4) nth:3 |
|
5266 | 406 |
#(1 2 3 4) nth:5 |
407 |
||
408 |
#(1 2 3 4) asSet nth:3 |
|
409 |
#(1 2 3 4) asSet nth:5 |
|
4985 | 410 |
" |
411 |
! |
|
412 |
||
4855 | 413 |
second |
414 |
"return the second element of the collection. |
|
415 |
For unordered collections, this simply returns the second |
|
416 |
element when enumerating them. |
|
417 |
This should be redefined in subclasses." |
|
418 |
||
4856 | 419 |
|count| |
4855 | 420 |
|
4856 | 421 |
count := 1. |
4855 | 422 |
self do:[:e | |
4856 | 423 |
count == 2 ifTrue:[^ e]. |
424 |
count := count + 1 |
|
4855 | 425 |
]. |
426 |
||
5266 | 427 |
"error if collection is smaller" |
428 |
^ self notEnoughElementsError |
|
4855 | 429 |
|
4856 | 430 |
" |
431 |
#(1 2 3) second |
|
432 |
" |
|
433 |
||
434 |
! |
|
435 |
||
4985 | 436 |
sixth |
437 |
"return the sixth element of the collection. |
|
438 |
For unordered collections, this simply returns the sixth |
|
439 |
element when enumerating them. |
|
440 |
This should be redefined in subclasses." |
|
441 |
||
442 |
^ self nth:6 |
|
443 |
||
444 |
" |
|
445 |
#(1 2 3 4 5 6 7) sixth |
|
446 |
" |
|
447 |
! |
|
448 |
||
4856 | 449 |
third |
450 |
"return the third element of the collection. |
|
451 |
For unordered collections, this simply returns the third |
|
452 |
element when enumerating them. |
|
453 |
This should be redefined in subclasses." |
|
454 |
||
4985 | 455 |
^ self nth:3 |
4856 | 456 |
|
457 |
||
458 |
" |
|
459 |
#(1 2 3) third |
|
460 |
" |
|
1 | 461 |
! ! |
462 |
||
463 |
!Collection methodsFor:'adding & removing'! |
|
464 |
||
465 |
add:anObject |
|
88 | 466 |
"add the argument, anObject to the receiver. |
467 |
If the receiver is ordered, the position of the new element is undefined |
|
2351 | 468 |
(i.e. dont depend on where it will be put). |
469 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 470 |
|
471 |
^ self subclassResponsibility |
|
2351 | 472 |
|
473 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 474 |
! |
475 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
476 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
477 |
"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
|
478 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
479 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
480 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
481 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
482 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
483 |
"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
|
484 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
485 |
|
1 | 486 |
addAll:aCollection |
1163 | 487 |
"add all elements of the argument, aCollection to the receiver. |
488 |
Returns the argument, aCollection." |
|
1 | 489 |
|
490 |
aCollection do:[:element | |
|
1163 | 491 |
self add:element |
1 | 492 |
]. |
493 |
^ aCollection |
|
88 | 494 |
|
495 |
" |
|
496 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
497 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
498 |
" |
|
1163 | 499 |
|
500 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 501 |
! |
502 |
||
12 | 503 |
addAllFirst:aCollection |
504 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 505 |
of the receiver. Returns the argument, aCollection." |
12 | 506 |
|
507 |
aCollection reverseDo:[:element | |
|
1163 | 508 |
self addFirst:element |
12 | 509 |
]. |
510 |
^ aCollection |
|
511 |
||
512 |
" |
|
513 |
|c| |
|
514 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 515 |
c addAllFirst:#(9 8 7 6 5). |
516 |
c |
|
12 | 517 |
" |
1163 | 518 |
|
519 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 520 |
! |
521 |
||
602 | 522 |
addAllLast:aCollection |
1163 | 523 |
"add all elements of the argument, aCollection to the receiver. |
524 |
Returns the argument, aCollection." |
|
602 | 525 |
|
526 |
aCollection do:[:element | |
|
1163 | 527 |
self addLast:element |
602 | 528 |
]. |
529 |
^ aCollection |
|
530 |
||
531 |
" |
|
532 |
|c| |
|
533 |
c := #(4 3 2 1) asOrderedCollection. |
|
534 |
c addAllLast:#(9 8 7 6 5) |
|
535 |
" |
|
1163 | 536 |
|
537 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 538 |
! |
539 |
||
540 |
addFirst:anObject |
|
541 |
"add the argument, anObject to the receiver. |
|
2351 | 542 |
If the receiver is ordered, the new element will be added at the beginning. |
543 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 544 |
|
545 |
^ self subclassResponsibility |
|
2351 | 546 |
|
547 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 548 |
! |
549 |
||
602 | 550 |
addLast:anObject |
551 |
"add the argument, anObject to the receiver. |
|
1163 | 552 |
If the receiver is ordered, the new element will be added at the end. |
553 |
Return the argument, anObject." |
|
602 | 554 |
|
555 |
^ self add:anObject |
|
1163 | 556 |
|
557 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 558 |
! |
559 |
||
3124 | 560 |
contents:aCollection |
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
561 |
"set my contents from aCollection |
5265 | 562 |
- this may be redefined in a concrete subclass for more performance" |
3124 | 563 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
564 |
self removeAll. |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
565 |
aCollection notNil ifTrue:[ |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
566 |
self addAll:aCollection |
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
567 |
] |
3124 | 568 |
|
3767
bbf560999d90
care for nil arg in #contents:
Claus Gittinger <cg@exept.de>
parents:
3469
diff
changeset
|
569 |
"Modified: / 17.8.1998 / 10:18:43 / cg" |
3124 | 570 |
! |
571 |
||
1 | 572 |
remove:anObject |
2351 | 573 |
"search for the first element, which is equal to anObject; |
574 |
if found, remove and return it. |
|
575 |
If not found, report a 'value not found'-error. |
|
576 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 577 |
|
1163 | 578 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
579 |
||
2351 | 580 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 581 |
! |
582 |
||
602 | 583 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 584 |
"search for the first element, which is equal to anObject; |
585 |
if found, remove and return it. |
|
586 |
If not found, return the the value of the exceptionBlock. |
|
587 |
Uses equality compare (=) to search for the occurrence. |
|
588 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 589 |
|
590 |
^ self subclassResponsibility |
|
2351 | 591 |
|
592 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 593 |
! |
594 |
||
134 | 595 |
removeAll |
1163 | 596 |
"remove all elements from the receiver. Returns the receiver. |
134 | 597 |
This should be reimplemented in subclasses for better |
598 |
performance." |
|
599 |
||
2383 | 600 |
[self isEmpty] whileFalse:[ |
1163 | 601 |
self removeFirst |
134 | 602 |
]. |
1163 | 603 |
|
2383 | 604 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 605 |
! |
606 |
||
1 | 607 |
removeAll:aCollection |
360 | 608 |
"remove all elements of the argument, aCollection from the receiver. |
1163 | 609 |
Return the argument, aCollection. |
5267 | 610 |
Raises an error, if some element-to-remove is not in the receiver. |
611 |
(see also: #removeAllFoundIn:, which does not raise an error). |
|
1163 | 612 |
|
360 | 613 |
Notice: for some collections (those not tuned for |
1163 | 614 |
resizing themself) this may be very slow. |
615 |
If the number of removed elements is big compared to to |
|
616 |
the receivers size, it may be better to copy the |
|
617 |
ones which are not to be removed into a new collection." |
|
1 | 618 |
|
619 |
aCollection do:[:element | self remove:element]. |
|
620 |
^ aCollection |
|
1163 | 621 |
|
5268 | 622 |
" |
623 |
|coll| |
|
624 |
||
625 |
coll := #(1 2 3 4 5 6) asSet. |
|
626 |
coll removeAll:#(4 5 6). |
|
627 |
coll |
|
628 |
" |
|
629 |
||
630 |
"raises an error: |
|
631 |
|coll| |
|
632 |
||
633 |
coll := #(1 2 3 4 5 6) asSet. |
|
634 |
coll removeAll:#(4 5 6 7 8). |
|
635 |
coll |
|
636 |
" |
|
637 |
||
638 |
"no error raised: |
|
639 |
|coll| |
|
640 |
||
641 |
coll := #(1 2 3 4 5 6) asSet. |
|
642 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
643 |
coll |
|
644 |
" |
|
645 |
||
646 |
||
348 | 647 |
! |
648 |
||
5265 | 649 |
removeAllFoundIn:aCollection |
5267 | 650 |
"Remove each element of aCollection, which is present in the receiver |
651 |
from the receiver. |
|
652 |
No error is raised, if some element-to-remove is not in the receiver. |
|
653 |
(see also: #removeAll:, which does raise an error)." |
|
4980 | 654 |
|
655 |
aCollection do:[:each | self remove:each ifAbsent:[]]. |
|
5265 | 656 |
^ aCollection |
5268 | 657 |
|
658 |
" |
|
659 |
|coll| |
|
660 |
||
661 |
coll := #(1 2 3 4 5 6) asSet. |
|
662 |
coll removeAllFoundIn:#(4 5 6 7 8). |
|
663 |
coll |
|
664 |
" |
|
665 |
||
666 |
"raises an error: |
|
667 |
|coll| |
|
668 |
||
669 |
coll := #(1 2 3 4 5 6) asSet. |
|
670 |
coll removeAll:#(4 5 6 7 8). |
|
671 |
coll |
|
672 |
" |
|
673 |
||
4980 | 674 |
! |
675 |
||
5265 | 676 |
removeAllSuchThat:aBlock |
4980 | 677 |
"Apply the condition to each element and remove it if the condition is true. |
5265 | 678 |
Return a collection of removed elements. |
5267 | 679 |
First elements-to-remove are collected, then removed in one operation." |
4980 | 680 |
|
5267 | 681 |
|removedElements| |
4980 | 682 |
|
5267 | 683 |
removedElements := OrderedCollection new. |
684 |
self do:[:element | |
|
4980 | 685 |
(aBlock value:element) ifTrue: [ |
5267 | 686 |
removedElements add:element |
4980 | 687 |
] |
688 |
]. |
|
5267 | 689 |
self removeAll:removedElements. |
690 |
^ removedElements |
|
691 |
||
692 |
" |
|
693 |
|coll| |
|
694 |
||
695 |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection. |
|
696 |
coll removeAllSuchThat:[:el | el even]. |
|
697 |
coll |
|
698 |
" |
|
699 |
" |
|
700 |
|coll| |
|
701 |
||
702 |
coll := #(1 2 3 4 5 6 7 8 9 10) asSet. |
|
703 |
coll removeAllSuchThat:[:el | el even]. |
|
704 |
coll |
|
705 |
" |
|
706 |
||
4980 | 707 |
! |
708 |
||
348 | 709 |
removeFirst |
710 |
"remove the first element from the receiver. |
|
711 |
Return the removed element." |
|
712 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
713 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
714 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
715 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
716 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
717 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
718 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
719 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
720 |
(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
|
721 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
722 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
723 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 724 |
! |
725 |
||
726 |
removeFirst:n |
|
727 |
"remove the first n elements from the receiver. |
|
5265 | 728 |
Return a collection of removed elements. |
360 | 729 |
Notice: for some collections (those not tuned for |
5265 | 730 |
resizing themself) this may be very slow." |
348 | 731 |
|
732 |
|ret| |
|
733 |
||
734 |
self size < n ifTrue:[ |
|
5265 | 735 |
^ self notEnoughElementsError |
348 | 736 |
]. |
737 |
ret := Array new:n. |
|
738 |
1 to:n do:[:i | |
|
5265 | 739 |
ret at:i put:(self removeFirst). |
348 | 740 |
]. |
741 |
^ ret |
|
742 |
! |
|
743 |
||
2351 | 744 |
removeIdentical:anObject |
745 |
"search for the first element, which is identical to anObject; |
|
746 |
if found, remove and return it. |
|
747 |
If not found, report a 'value not found'-error. |
|
748 |
Uses identity compare (==) to search for the occurrence." |
|
749 |
||
750 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
751 |
||
752 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
753 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
754 |
! |
|
755 |
||
756 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
757 |
"search for the first element, which is identical to anObject; |
|
758 |
if found, remove and return it. |
|
759 |
If not found, return the the value of the exceptionBlock. |
|
760 |
Uses identity compare (==) to search for the occurrence. |
|
761 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
762 |
||
763 |
^ self subclassResponsibility |
|
764 |
||
765 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
766 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
767 |
! |
|
768 |
||
602 | 769 |
removeLast |
770 |
"remove the last element from the receiver. |
|
2351 | 771 |
Return the removed element. |
772 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 773 |
|
774 |
^ self subclassResponsibility |
|
2351 | 775 |
|
776 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 777 |
! |
778 |
||
348 | 779 |
removeLast:n |
780 |
"remove the last n elements from the receiver. |
|
5265 | 781 |
Return a collection of removed elements. |
360 | 782 |
Notice: for some collections (those not tuned for |
5265 | 783 |
resizing themself) this may be very slow." |
348 | 784 |
|
785 |
|ret| |
|
786 |
||
787 |
self size < n ifTrue:[ |
|
5265 | 788 |
^ self notEnoughElementsError |
348 | 789 |
]. |
790 |
ret := Array new:n. |
|
791 |
n to:1 by:-1 do:[:i | |
|
5265 | 792 |
ret at:i put:(self removeLast). |
348 | 793 |
]. |
794 |
^ ret |
|
1 | 795 |
! ! |
796 |
||
602 | 797 |
!Collection methodsFor:'converting'! |
798 |
||
799 |
asArray |
|
800 |
"return a new Array with the collections elements" |
|
801 |
||
802 |
|anArray |
|
803 |
index "{ Class: SmallInteger }" | |
|
1 | 804 |
|
602 | 805 |
anArray := Array new:(self size). |
806 |
index := 1. |
|
807 |
self do:[:each | |
|
808 |
anArray at:index put:each. |
|
809 |
index := index + 1 |
|
810 |
]. |
|
811 |
^ anArray |
|
1 | 812 |
! |
813 |
||
602 | 814 |
asBag |
815 |
"return a new Bag with the receiver collections elements" |
|
1 | 816 |
|
602 | 817 |
^ self addAllTo:(Bag new) |
1 | 818 |
! |
819 |
||
602 | 820 |
asByteArray |
821 |
"return a new ByteArray with the collections elements |
|
822 |
(which must convert to integers in the range 0..255)." |
|
1 | 823 |
|
602 | 824 |
|aByteArray |
825 |
index "{ Class: SmallInteger }" | |
|
1 | 826 |
|
602 | 827 |
aByteArray := ByteArray new:(self size). |
828 |
index := 1. |
|
829 |
self do:[:each | |
|
830 |
aByteArray at:index put:each asInteger. |
|
831 |
index := index + 1 |
|
832 |
]. |
|
833 |
^ aByteArray |
|
1 | 834 |
! |
835 |
||
602 | 836 |
asDoubleArray |
837 |
"return a new DoubleArray with the collections elements |
|
838 |
(which must convert to floats)." |
|
839 |
||
840 |
|aDoubleArray |
|
841 |
index "{ Class: SmallInteger }" | |
|
842 |
||
843 |
aDoubleArray := DoubleArray new:(self size). |
|
844 |
index := 1. |
|
845 |
self do:[:each | |
|
846 |
aDoubleArray at:index put:each asFloat. |
|
847 |
index := index + 1 |
|
848 |
]. |
|
849 |
^ aDoubleArray |
|
850 |
! |
|
68 | 851 |
|
602 | 852 |
asFloatArray |
853 |
"return a new FloatArray with the collections elements |
|
854 |
(which must convert to floats)." |
|
855 |
||
856 |
|aFloatArray |
|
857 |
index "{ Class: SmallInteger }" | |
|
858 |
||
859 |
aFloatArray := FloatArray new:(self size). |
|
860 |
index := 1. |
|
861 |
self do:[:each | |
|
862 |
aFloatArray at:index put:each asFloat. |
|
863 |
index := index + 1 |
|
864 |
]. |
|
865 |
^ aFloatArray |
|
866 |
! |
|
867 |
||
868 |
asIdentitySet |
|
869 |
"return a new IdentitySet with the receiver collections elements" |
|
870 |
||
871 |
^ self addAllTo:(IdentitySet new:self size) |
|
68 | 872 |
! |
873 |
||
2167 | 874 |
asList |
875 |
"return a new List with the receiver collections elements" |
|
876 |
||
877 |
^ self addAllTo:(List new:self size) |
|
878 |
||
879 |
"Created: 14.2.1997 / 16:25:23 / cg" |
|
880 |
! |
|
881 |
||
602 | 882 |
asOrderedCollection |
883 |
"return a new OrderedCollection with the receiver collections elements" |
|
884 |
||
885 |
^ self addAllTo:(OrderedCollection new:self size) |
|
886 |
! |
|
887 |
||
1394 | 888 |
asRunArray |
889 |
"return a new RunArray with the collections elements" |
|
890 |
||
3367
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
891 |
^ RunArray from:self. |
1394 | 892 |
|
3367
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
893 |
"/ |runs lastElement occurrences| |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
894 |
"/ |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
895 |
"/ runs := RunArray new. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
896 |
"/ occurrences := 0. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
897 |
"/ self do:[:each | |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
898 |
"/ each == lastElement ifTrue:[ |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
899 |
"/ occurrences := occurrences + 1 |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
900 |
"/ ] ifFalse:[ |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
901 |
"/ runs add:lastElement withOccurrences:occurrences. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
902 |
"/ occurrences := 1. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
903 |
"/ lastElement := each |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
904 |
"/ ]. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
905 |
"/ ]. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
906 |
"/ occurrences ~~ 0 ifTrue:[ |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
907 |
"/ runs add:lastElement withOccurrences:occurrences |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
908 |
"/ ]. |
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
909 |
"/ ^ runs |
1394 | 910 |
|
911 |
" |
|
912 |
#(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray |
|
913 |
" |
|
914 |
||
3367
9510bc82d4d6
use #from: instance creation method in #asRunArray.
Claus Gittinger <cg@exept.de>
parents:
3191
diff
changeset
|
915 |
"Modified: / 7.4.1998 / 09:50:54 / cg" |
1394 | 916 |
! |
917 |
||
602 | 918 |
asSet |
919 |
"return a new Set with the receiver collections elements" |
|
920 |
||
921 |
^ self addAllTo:(Set new:self size) |
|
922 |
! |
|
923 |
||
924 |
asSortedCollection |
|
925 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 926 |
|
602 | 927 |
|aSortedCollection| |
928 |
||
929 |
aSortedCollection := SortedCollection new:self size. |
|
930 |
aSortedCollection addAll:self. |
|
931 |
^ aSortedCollection |
|
932 |
! |
|
933 |
||
934 |
asSortedCollection:sortBlock |
|
935 |
"return a new SortedCollection with the receiver collections elements, |
|
936 |
using sortBlock for comparing" |
|
937 |
||
938 |
|aSortedCollection| |
|
939 |
||
940 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
941 |
aSortedCollection addAll:self. |
|
942 |
^ aSortedCollection |
|
1 | 943 |
! |
944 |
||
2919 | 945 |
asSortedStrings |
946 |
"Create & return a SortedCollection that sorts the receivers |
|
947 |
elements according to the locales collating policy. |
|
948 |
This is currently not really support - strings are sorted |
|
949 |
without caring for the locale." |
|
950 |
||
951 |
|aSortedCollection| |
|
952 |
||
953 |
aSortedCollection := SortedCollection forStrings:self size. |
|
954 |
aSortedCollection addAll:self. |
|
955 |
^ aSortedCollection |
|
956 |
||
957 |
"Created: 13.9.1997 / 09:36:22 / cg" |
|
958 |
"Modified: 13.9.1997 / 09:43:00 / cg" |
|
959 |
! |
|
960 |
||
961 |
asSortedStrings:sortBlock |
|
962 |
"Create & return a SortedCollection that sorts the receivers |
|
3061 | 963 |
elements using sortBlock and according to the locales collating policy, |
964 |
which is passed as first arg to sortBlock. |
|
2919 | 965 |
This is currently not really support - strings are sorted |
966 |
without caring for the locale." |
|
967 |
||
968 |
|aSortedCollection| |
|
969 |
||
970 |
aSortedCollection := SortedCollection forStrings:self size. |
|
971 |
aSortedCollection sortBlock:sortBlock. |
|
972 |
aSortedCollection addAll:self. |
|
973 |
^ aSortedCollection |
|
974 |
||
3061 | 975 |
"Created: / 13.9.1997 / 09:36:45 / cg" |
976 |
"Modified: / 27.10.1997 / 16:39:48 / cg" |
|
2919 | 977 |
! |
978 |
||
979 |
asSortedStrings:sortBlock with:aCollationPolicy |
|
980 |
"Create & return a SortedCollection that sorts the receivers |
|
981 |
elements using sortBlock and according to the specified locales collating policy. |
|
982 |
This is currently not really support - strings are sorted |
|
983 |
without caring for the locale." |
|
984 |
||
985 |
|aSortedCollection| |
|
986 |
||
987 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
988 |
aSortedCollection sortBlock:sortBlock. |
|
989 |
aSortedCollection addAll:self. |
|
990 |
^ aSortedCollection |
|
991 |
||
992 |
"Created: 13.9.1997 / 09:37:21 / cg" |
|
993 |
"Modified: 13.9.1997 / 09:45:27 / cg" |
|
994 |
! |
|
995 |
||
996 |
asSortedStringsWith: aCollationPolicy |
|
997 |
"Create & return a SortedCollection that sorts the receivers |
|
998 |
elements according to the specified locales collating policy. |
|
999 |
This is currently not really support - strings are sorted |
|
1000 |
without caring for the locale." |
|
1001 |
||
1002 |
|aSortedCollection| |
|
1003 |
||
1004 |
aSortedCollection := SortedCollection forStrings:self size collatedBy:aCollationPolicy. |
|
1005 |
aSortedCollection addAll:self. |
|
1006 |
^ aSortedCollection |
|
1007 |
||
1008 |
"Created: 13.9.1997 / 09:37:50 / cg" |
|
1009 |
"Modified: 13.9.1997 / 09:44:08 / cg" |
|
1010 |
! |
|
1011 |
||
602 | 1012 |
asString |
1013 |
"return a String with the collections elements |
|
1014 |
(which must convert to characters)" |
|
1015 |
||
1016 |
|aString |
|
1017 |
index "{ Class: SmallInteger }" | |
|
360 | 1018 |
|
602 | 1019 |
aString := String new:(self size). |
1020 |
index := 1. |
|
1021 |
self do:[:each | |
|
1022 |
aString at:index put:each asCharacter. |
|
1023 |
index := index + 1 |
|
360 | 1024 |
]. |
602 | 1025 |
^ aString |
1026 |
! |
|
1027 |
||
1028 |
asStringCollection |
|
1413 | 1029 |
"return a new string collection containing the elements; |
1030 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 1031 |
|
1032 |
^ self addAllTo:(StringCollection new) |
|
1413 | 1033 |
|
1034 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 1035 |
! |
1036 |
||
602 | 1037 |
asText |
1038 |
"return a Text-object (collection of lines) from myself. |
|
1039 |
BIG warning: asText is totally misnamed here |
|
1040 |
- ST/X's asText has nothing to do with PP's asText. |
|
1041 |
Therefore it will be removed/renamed soon. |
|
1042 |
Please use #asStringCollection." |
|
1 | 1043 |
|
602 | 1044 |
self obsoleteMethodWarning:'use #asStringCollection; #asText will change its meaning'. |
1045 |
||
1046 |
^ self asStringCollection |
|
1047 |
! |
|
1048 |
||
1049 |
readStream |
|
1050 |
"return a stream for reading from the receiver" |
|
1051 |
||
1052 |
^ ReadStream on:self |
|
88 | 1053 |
|
1054 |
" |
|
602 | 1055 |
|s| |
1056 |
||
1057 |
s := 'hello world' readStream. |
|
1058 |
s next:5. |
|
1059 |
s next. |
|
1060 |
(s next:5) inspect |
|
88 | 1061 |
" |
1 | 1062 |
! |
1063 |
||
602 | 1064 |
writeStream |
1065 |
"return a stream for writing onto the receiver" |
|
1066 |
||
1067 |
^ WriteStream on:self |
|
2 | 1068 |
|
602 | 1069 |
" |
1070 |
|s| |
|
1071 |
||
1072 |
s := #() copy writeStream. |
|
1073 |
s nextPut:1. |
|
1074 |
s nextPut:2. |
|
1075 |
s nextPut:3. |
|
1076 |
s contents inspect |
|
1077 |
" |
|
88 | 1078 |
|
1079 |
" |
|
602 | 1080 |
|s| |
2 | 1081 |
|
602 | 1082 |
s := OrderedCollection new writeStream. |
1083 |
s nextPut:1. |
|
1084 |
s nextPut:2. |
|
1085 |
s nextPut:3. |
|
1086 |
s contents inspect |
|
1087 |
" |
|
1088 |
! ! |
|
1 | 1089 |
|
602 | 1090 |
!Collection methodsFor:'copying'! |
1091 |
||
3381
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1092 |
copy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1093 |
"return a copy of the receiver. |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1094 |
Redefined to pass the original as argument to tyhe postCopyFrom method." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1095 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1096 |
^ self shallowCopy postCopyFrom:self |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1097 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1098 |
"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
|
1099 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1100 |
|
602 | 1101 |
copyEmpty |
1102 |
"return a copy of the receiver with no elements. |
|
1103 |
This is used by copying and enumeration methods |
|
1104 |
to get a new instance which is similar to the receiver." |
|
1105 |
||
1106 |
^ self species new |
|
1 | 1107 |
! |
1108 |
||
602 | 1109 |
copyEmpty:size |
1110 |
"return a copy of the receiver with no elements, but space for |
|
1111 |
size elements. This is used by copying and enumeration methods |
|
1112 |
to get a new instance which is similar to the receiver. |
|
1113 |
This method should be redefined in subclasses with instance |
|
1114 |
variables, which should be put into the copy too. |
|
1115 |
For example, SortedCollection has to copy its sortBlock into the |
|
1116 |
new collection." |
|
1 | 1117 |
|
602 | 1118 |
^ self species new:size |
360 | 1119 |
! |
1120 |
||
602 | 1121 |
copyEmptyAndGrow:size |
1122 |
"return a copy of the receiver with size nil elements. |
|
1123 |
This is used by copying and enumeration methods |
|
1124 |
to get a new instance which is similar to the receiver." |
|
155 | 1125 |
|
602 | 1126 |
^ (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
|
1127 |
! |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1128 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1129 |
postCopyFrom:original |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1130 |
"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
|
1131 |
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
|
1132 |
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
|
1133 |
setHash: functionality)." |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1134 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1135 |
"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
|
1136 |
^ self postCopy |
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1137 |
|
e1c12fa92fbf
moved #postCopyFrom: from object; redefined copy to send this instead
Claus Gittinger <cg@exept.de>
parents:
3367
diff
changeset
|
1138 |
"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
|
1139 |
"Modified: / 19.4.1998 / 20:03:57 / cg" |
1 | 1140 |
! ! |
1141 |
||
1142 |
!Collection methodsFor:'enumerating'! |
|
1143 |
||
602 | 1144 |
addAllTo:aCollection |
1145 |
"add all elements of the receiver, to aCollection. |
|
1146 |
Return aCollection." |
|
77 | 1147 |
|
602 | 1148 |
self do:[:each | aCollection add:each]. |
1149 |
^ aCollection |
|
5254 | 1150 |
|
1151 |
" |
|
1152 |
#(1 2 3 4 5 1 2 3 4 5) addAllTo:Set new |
|
1153 |
" |
|
1154 |
||
1155 |
"Modified: / 11.2.2000 / 11:22:14 / cg" |
|
77 | 1156 |
! |
1157 |
||
5207
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1158 |
allSatisfy:aBlock |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1159 |
"evaluate aBlock for each of the receiver's elements. |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1160 |
Return true, if aBlock returns true for all elements, false otherwise |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1161 |
(i.e. false if any element fails to satisfy the block-condition). |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1162 |
This is an ANSI renomer of #donform:" |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1163 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1164 |
^ self conform:aBlock. |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1165 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1166 |
" |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1167 |
#(1 2 3 4 5) allSatisfy:[:el | el odd] |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1168 |
#(2 4 6 8 10) allSatisfy:[:el | el odd] |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1169 |
#(2 4 6 8 10) allSatisfy:[:el | el even] |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1170 |
" |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1171 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1172 |
! |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1173 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1174 |
anySatisfy:aBlock |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1175 |
"evaluate aBlock for each of the receiver's elements. |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1176 |
Return true, if aBlock ever returns true, false otherwise |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1177 |
(i.e. if any element satisfies the block-condition). |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1178 |
This is an ANSI renomer of #contains:" |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1179 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1180 |
^ self contains:aBlock. |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1181 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1182 |
" |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1183 |
#(1 2 3 4 5) anySatisfy:[:el | el odd] |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1184 |
#(2 4 6 8 10) anySatisfy:[:el | el odd] |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1185 |
" |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1186 |
|
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1187 |
! |
1b927d32eb51
added #anySatisfy:: and #allSatisy: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
5048
diff
changeset
|
1188 |
|
1 | 1189 |
collect:aBlock |
1190 |
"for each element in the receiver, evaluate the argument, aBlock |
|
1191 |
and return a new collection with the results" |
|
1192 |
||
1193 |
|newCollection| |
|
1194 |
||
201 | 1195 |
newCollection := self species new. |
1196 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 1197 |
^ newCollection |
68 | 1198 |
|
1199 |
" |
|
1200 |
#(1 2 3 4) collect:[:n | n * 2] |
|
1201 |
" |
|
1 | 1202 |
! |
1203 |
||
4890 | 1204 |
collect:collectBlock thenSelect:selectBlock |
1205 |
"combination of collect followed by select. |
|
1206 |
May be redefined by some subclasses for optimal performance |
|
5265 | 1207 |
(avoidong the creation of intermediate garbage)" |
4890 | 1208 |
|
4892 | 1209 |
^ (self collect:collectBlock) select:selectBlock |
1210 |
||
1211 |
" |
|
1212 |
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10] |
|
1213 |
" |
|
4890 | 1214 |
|
1215 |
! |
|
1216 |
||
602 | 1217 |
conform:aBlock |
1218 |
"return true, if every element conforms to some condition. |
|
1219 |
I.e. return false, if aBlock returns false for any element; |
|
1220 |
true otherwise." |
|
77 | 1221 |
|
602 | 1222 |
self do:[:element | (aBlock value:element) ifFalse:[^ false]]. |
1223 |
^ true |
|
1224 |
! |
|
77 | 1225 |
|
2665
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1226 |
contains:aBlock |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1227 |
"evaluate aBlock for each of the receiver's elements. |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1228 |
Return true, if aBlock ever returns true, false otherwise." |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1229 |
|
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1230 |
self detect:aBlock ifNone:[^ false]. |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1231 |
^ true |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1232 |
|
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1233 |
" |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1234 |
#(1 2 3 4 5) contains:[:el | el odd] |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1235 |
#(2 4 6 8 10) contains:[:el | el odd] |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1236 |
" |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1237 |
|
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1238 |
"Modified: 26.5.1997 / 17:38:57 / cg" |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1239 |
! |
13998f6189af
added #contains: for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
2639
diff
changeset
|
1240 |
|
602 | 1241 |
count:aBlock |
1242 |
"count elements, for which aBlock returns true. |
|
1243 |
Return the sum." |
|
1244 |
||
1245 |
^ self inject:0 into:[:sumSoFar :element | |
|
1246 |
(aBlock value:element) ifTrue:[sumSoFar + 1] |
|
1247 |
ifFalse:[sumSoFar] |
|
1248 |
] |
|
77 | 1249 |
|
1250 |
" |
|
602 | 1251 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
1252 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
|
77 | 1253 |
" |
1254 |
! |
|
1255 |
||
1 | 1256 |
detect:aBlock |
1257 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1258 |
the block returns true; in this case return the element which caused |
|
1259 |
the true evaluation. |
|
345 | 1260 |
If none of the evaluations returns true, report an error" |
1 | 1261 |
|
1262 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 1263 |
|
1264 |
" |
|
1265 |
#(1 2 3 4) detect:[:n | n odd] |
|
1266 |
#(2 4 6 8) detect:[:n | n odd] |
|
1267 |
" |
|
1 | 1268 |
! |
1269 |
||
1270 |
detect:aBlock ifNone:exceptionBlock |
|
1271 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1272 |
the block returns true; in this case return the element which caused |
|
1273 |
the true evaluation. |
|
1274 |
If none of the evaluations returns true, return the result of the |
|
1275 |
evaluation of the exceptionBlock" |
|
1276 |
||
1277 |
self do:[:each | |
|
155 | 1278 |
(aBlock value:each) ifTrue:[^ each]. |
1 | 1279 |
]. |
1280 |
^ exceptionBlock value |
|
68 | 1281 |
|
1282 |
" |
|
1283 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
1284 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
1285 |
" |
|
1 | 1286 |
! |
1287 |
||
421 | 1288 |
detectLast:aBlock |
1289 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1290 |
the block returns true; in this case return the element which caused |
|
1291 |
the true evaluation. The elements are processed in reverse order. |
|
1292 |
If none of the evaluations returns true, report an error" |
|
1293 |
||
1294 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
1295 |
||
1296 |
" |
|
1297 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
1298 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
1299 |
" |
|
1300 |
! |
|
1301 |
||
1302 |
detectLast:aBlock ifNone:exceptionBlock |
|
1303 |
"evaluate the argument, aBlock for each element in the receiver until |
|
1304 |
the block returns true; in this case return the element which caused |
|
1305 |
the true evaluation. The elements are processed in reverse order. |
|
1306 |
If none of the evaluations returns true, return the result of the |
|
1307 |
evaluation of the exceptionBlock" |
|
1308 |
||
1309 |
self reverseDo:[:each | |
|
1310 |
(aBlock value:each) ifTrue:[^ each]. |
|
1311 |
]. |
|
1312 |
^ exceptionBlock value |
|
1313 |
||
1314 |
" |
|
1315 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
1316 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
1317 |
" |
|
1318 |
! |
|
1319 |
||
602 | 1320 |
do:aBlock |
1321 |
"evaluate the argument, aBlock for each element" |
|
1322 |
||
1323 |
^ self subclassResponsibility |
|
1324 |
! |
|
1325 |
||
4394 | 1326 |
do:aBlock inBetweenDo:betweenBlock |
1327 |
"evaluate the argument, aBlock for each element. |
|
1328 |
Between elements (i.e. after each except for the last), |
|
1329 |
evaluate betweenBlock. |
|
5254 | 1330 |
This is a utility helper for collection printers |
1331 |
(for example, to print a space between elements)." |
|
4394 | 1332 |
|
1333 |
|first| |
|
1334 |
||
1335 |
first := true. |
|
1336 |
self do:[:element | |
|
1337 |
first ifTrue:[ |
|
1338 |
first := false |
|
1339 |
] ifFalse:[ |
|
1340 |
betweenBlock value. |
|
1341 |
]. |
|
1342 |
aBlock value:element |
|
1343 |
]. |
|
1344 |
||
1345 |
" |
|
1346 |
#(1 2 3 4) do:[:el | Transcript show:el] |
|
1347 |
inBetweenDo:[ Transcript show:'-'] |
|
1348 |
" |
|
5254 | 1349 |
|
1350 |
"Modified: / 11.2.2000 / 11:23:15 / cg" |
|
4394 | 1351 |
! |
1352 |
||
1212 | 1353 |
doWithExit:aBlock |
1354 |
"evaluate the argument, aBlock for each element. |
|
1355 |
Passes an additional exit object, which can be used to leave |
|
1356 |
the loop early, by sending it a #value: message. |
|
1357 |
Returns nil or the value passed to the exit>>value: message. |
|
1358 |
||
1359 |
Notice, that this is different to a return from the block, which |
|
1360 |
returns from the enclosed method, NOT only from the block." |
|
1361 |
||
1362 |
^ [:exit | |
|
1363 |
self do:[:el | |
|
1364 |
aBlock value:el value:exit |
|
1365 |
]. |
|
1366 |
nil |
|
1367 |
] valueWithExit. |
|
1368 |
||
1369 |
"Example: |
|
1370 |
search for the first element which is >= 99; return it. |
|
1371 |
remaining elements are not processed: |
|
1372 |
||
1373 |
#(1 2 3 4 999 5 6 7 8 9) |
|
1374 |
doWithExit:[:element :exit | |
|
1422 | 1375 |
Transcript showCR:element. |
1212 | 1376 |
element >= 99 ifTrue:[exit value:element]] |
1377 |
||
1378 |
#(1 2 3 4 5 6 7 8 9) |
|
1379 |
doWithExit:[:element :exit | |
|
1422 | 1380 |
Transcript showCR:element. |
1212 | 1381 |
element >= 99 ifTrue:[exit value:element]] |
1382 |
" |
|
1383 |
||
1384 |
"to demonstrate the difference to returning from the block: |
|
1385 |
this works: |
|
1386 |
||
1387 |
|el| |
|
1388 |
||
1389 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
1390 |
doWithExit:[:element :exit | |
|
1391 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 1392 |
Transcript showCR:el. |
1212 | 1393 |
|
1394 |
||
1422 | 1395 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 1396 |
|
1397 |
|el| |
|
1398 |
||
1399 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
1400 |
do:[:element | |
|
1401 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 1402 |
Transcript showCR:el. |
1212 | 1403 |
" |
1404 |
||
1405 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
1406 |
! |
|
1407 |
||
1 | 1408 |
inject:thisValue into:binaryBlock |
1262 | 1409 |
"starting with thisValue for value, pass this value and each element |
1410 |
to binaryBlock, replacing value with the result returned from the block |
|
1411 |
in the next iteration." |
|
1412 |
||
1 | 1413 |
|nextValue| |
1414 |
||
1415 |
nextValue := thisValue. |
|
1416 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
1417 |
^ nextValue |
|
68 | 1418 |
|
1262 | 1419 |
"sum up the elements of a collection: |
1420 |
||
68 | 1421 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
1422 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 1423 |
|
1424 |
find the minimum: |
|
1425 |
||
1426 |
|coll| |
|
1427 |
coll := #(1 99 -15 20 100). |
|
1428 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 1429 |
" |
1262 | 1430 |
|
1431 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 1432 |
! |
1433 |
||
602 | 1434 |
keysAndValuesDo:aTwoArgBlock |
1435 |
"evaluate the argument, aBlock for every element in the collection, |
|
1436 |
passing both index and element as arguments." |
|
1437 |
||
1438 |
^ self errorNotKeyed |
|
1439 |
! |
|
1440 |
||
1359 | 1441 |
keysAndValuesReverseDo:aTwoArgBlock |
1442 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
1443 |
passing both index and element as arguments." |
|
1444 |
||
1445 |
^ self errorNotKeyed |
|
1446 |
||
1447 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
1448 |
! |
|
1449 |
||
602 | 1450 |
pairsDo:aBlock |
1451 |
"evaluate the argument, aBlock for every element in the collection, |
|
1452 |
which is supposed to consist of 2-element collections. |
|
1453 |
The block is called with 2 arguments for each collection in the receiver." |
|
1454 |
||
1455 |
self do:[:aPair | |
|
1456 |
aBlock value:(aPair at:1) value:(aPair at:2). |
|
1457 |
] |
|
1458 |
" |
|
1459 |
#( (1 one) (2 two) (3 three) (4 four) (5 five) (6 six)) |
|
1422 | 1460 |
pairsDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
602 | 1461 |
|
1462 |
||
1463 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 1464 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 1465 |
" |
1466 |
! |
|
1467 |
||
1 | 1468 |
reject:aBlock |
1469 |
"return a new collection with all elements from the receiver, for which |
|
1470 |
the argument aBlock evaluates to false" |
|
1471 |
||
1472 |
^ self select:[:element | (aBlock value:element) == false] |
|
68 | 1473 |
|
1474 |
" |
|
1475 |
#(1 2 3 4) reject:[:e | e odd] |
|
1476 |
(1 to:10) reject:[:e | e even] |
|
1477 |
" |
|
1 | 1478 |
! |
1479 |
||
602 | 1480 |
reverseDo:aBlock |
1481 |
"evaluate the argument, aBlock for each element in reverse order." |
|
1482 |
||
1483 |
"it could be defined in terms of do: - but very inefficient. |
|
1484 |
Better force programmer to define a better version ..." |
|
1485 |
||
1486 |
^ self subclassResponsibility |
|
1487 |
! |
|
1488 |
||
1 | 1489 |
select:aBlock |
1490 |
"return a new collection with all elements from the receiver, for which |
|
5265 | 1491 |
the argument aBlock evaluates to true. |
1492 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
1 | 1493 |
|
1494 |
|newCollection| |
|
1495 |
||
201 | 1496 |
newCollection := self species new. |
1 | 1497 |
self do:[:each | |
5265 | 1498 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 1499 |
]. |
1500 |
^ newCollection |
|
68 | 1501 |
|
1502 |
" |
|
1503 |
#(1 2 3 4) select:[:e | e odd] |
|
1504 |
(1 to:10) select:[:e | e even] |
|
1505 |
" |
|
1506 |
! |
|
1507 |
||
1508 |
select:aBlock ifNone:exceptionBlock |
|
1509 |
"try a new collection with all elements from the receiver, for which |
|
1510 |
the argument aBlock evaluates to true. If none of the elements passes |
|
5265 | 1511 |
the check of aBlock, return the result of evaluating exceptionBlock. |
1512 |
See also: #removeAllFoundIn: and #removeAllSuchThat:" |
|
68 | 1513 |
|
1514 |
|newCollection| |
|
1515 |
||
1516 |
newCollection := self select:aBlock. |
|
1517 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
1518 |
^ newCollection |
|
1519 |
||
1520 |
" |
|
1521 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
1522 |
" |
|
12 | 1523 |
! |
1524 |
||
4892 | 1525 |
select:selectBlock thenCollect:collectBlock |
1526 |
"combination of select followed by collect. |
|
1527 |
May be redefined by some subclasses for optimal performance |
|
5265 | 1528 |
(avoidong the creation of intermediate garbage)" |
4892 | 1529 |
|
1530 |
^ (self select:selectBlock) collect:collectBlock |
|
1531 |
||
1532 |
" |
|
1533 |
#(1 2 3 4 5 6 7) select:[:i | i even] thenCollect:[:i | i * 2] |
|
1534 |
" |
|
1535 |
! |
|
1536 |
||
362 | 1537 |
triplesDo:aBlock |
1538 |
"evaluate the argument, aBlock for every element in the collection, |
|
1539 |
which is supposed to consist of 3-element collections. |
|
1540 |
The block is called with 3 arguments for each collection in the receiver." |
|
1541 |
||
1542 |
self do:[:aTriple | |
|
2639 | 1543 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
362 | 1544 |
] |
1545 |
" |
|
2639 | 1546 |
#( (1 one eins) (2 two zwei) (3 three drei) (4 four vier) (5 five #'fuenf') (6 six sechs)) |
1422 | 1547 |
triplesDo:[:num :sym1 :sym2 | Transcript show:num; space; show:sym1; space; showCR:sym2] |
362 | 1548 |
" |
2639 | 1549 |
|
1550 |
"Modified: 10.5.1997 / 14:15:43 / cg" |
|
1 | 1551 |
! |
1552 |
||
602 | 1553 |
with:aCollection collect:aTwoArgBlock |
1554 |
"evaluate the argument, aBlock for successive elements from |
|
1555 |
each the receiver and the argument, aSequenceableCollection; |
|
1556 |
The second argument, aBlock must be a two-argument block, which is |
|
1557 |
evaluated for each element-pair. |
|
1558 |
Collect the results and return a collection containing them. |
|
1559 |
This method fails if neither the receiver nor aCollection is |
|
1560 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 1561 |
|
602 | 1562 |
|index "{ Class: SmallInteger }" |
1563 |
newCollection| |
|
1 | 1564 |
|
602 | 1565 |
newCollection := self species new. |
1566 |
index := 1. |
|
1567 |
aCollection isSequenceable ifFalse:[ |
|
1568 |
self isSequenceable ifFalse:[ |
|
1569 |
^ self error:'neither collection is sequenceable'. |
|
1570 |
]. |
|
1571 |
aCollection do:[:element | |
|
1572 |
newCollection add:(aTwoArgBlock value:(self at:index) |
|
1573 |
value:element). |
|
1574 |
index := index + 1 |
|
1575 |
] |
|
1576 |
] ifTrue:[ |
|
1577 |
self do:[:element | |
|
1578 |
newCollection add:(aTwoArgBlock value:element |
|
1579 |
value:(aCollection at:index)). |
|
1580 |
index := index + 1 |
|
1581 |
] |
|
1582 |
]. |
|
1583 |
^ newCollection |
|
201 | 1584 |
|
1585 |
" |
|
602 | 1586 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
1587 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
|
201 | 1588 |
" |
2 | 1589 |
! |
1590 |
||
602 | 1591 |
with:aCollection do:aTwoArgBlock |
1592 |
"evaluate the argument, aBlock for successive elements from |
|
1593 |
each the receiver and the argument, aSequenceableCollection. |
|
1594 |
The second argument, aBlock must be a two-argument block. |
|
1595 |
This method fails if neither the receiver nor aCollection is |
|
1596 |
a sequenceable collection (i.e. implements numeric key access)." |
|
1597 |
||
1598 |
|index "{ Class: SmallInteger }"| |
|
2 | 1599 |
|
602 | 1600 |
index := 1. |
1601 |
aCollection isSequenceable ifFalse:[ |
|
1602 |
self isSequenceable ifFalse:[ |
|
1603 |
^ self error:'neither collection is sequenceable'. |
|
1604 |
]. |
|
1605 |
aCollection do:[:element | |
|
1606 |
aTwoArgBlock value:(self at:index) value:element. |
|
1607 |
index := index + 1 |
|
1608 |
] |
|
1609 |
] ifTrue:[ |
|
1610 |
self do:[:element | |
|
1611 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
1612 |
index := index + 1 |
|
1613 |
] |
|
1614 |
] |
|
201 | 1615 |
|
1616 |
" |
|
602 | 1617 |
(1 to:3) with:#(one two three) do:[:num :sym | |
1422 | 1618 |
Transcript showCR:(num->sym) |
602 | 1619 |
] |
1620 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
1422 | 1621 |
Transcript showCR:(num->sym) |
602 | 1622 |
]. |
1623 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
1422 | 1624 |
Transcript showCR:(num->sym) |
602 | 1625 |
] |
201 | 1626 |
" |
1 | 1627 |
! ! |
1628 |
||
602 | 1629 |
!Collection methodsFor:'error handling'! |
1630 |
||
1631 |
emptyCheck |
|
1632 |
"check if the receiver is empty; report an error if so" |
|
1633 |
||
1634 |
self size == 0 ifTrue:[ |
|
1635 |
^ self emptyCollectionError. |
|
1636 |
]. |
|
1637 |
! |
|
1638 |
||
1639 |
emptyCollectionError |
|
1640 |
"report an error that the operation is not allowed for |
|
1641 |
empty collections" |
|
1642 |
||
1643 |
^ EmptyCollectionSignal raise |
|
1644 |
! |
|
1645 |
||
1646 |
errorInvalidKey:aKey |
|
1647 |
"report an error that the given key was invalid" |
|
1648 |
||
1649 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
1650 |
! |
|
1651 |
||
1652 |
errorNotKeyed |
|
1653 |
"report an error that keyed access methods are not allowed" |
|
1654 |
||
1655 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
1656 |
! |
|
1657 |
||
1658 |
errorValueNotFound:anObject |
|
1659 |
"report an error that an object was not found in the collection" |
|
1660 |
||
3075 | 1661 |
^ ValueNotFoundSignal |
1662 |
raiseRequestWith:anObject |
|
1663 |
||
1664 |
"Modified: / 30.10.1997 / 15:52:18 / cg" |
|
602 | 1665 |
! |
1666 |
||
1667 |
notEnoughElementsError |
|
1668 |
"report an error that the operation is not allowed, |
|
1669 |
since not enough elements are in the collection" |
|
1670 |
||
1671 |
^ NotEnoughElementsSignal raise |
|
1672 |
! ! |
|
1673 |
||
1674 |
!Collection methodsFor:'growing'! |
|
1675 |
||
1676 |
grow |
|
1677 |
"make the receiver larger" |
|
1678 |
||
1679 |
self grow:(self size + self growSize) |
|
1680 |
! |
|
1681 |
||
1682 |
grow:howBig |
|
1683 |
"change the receivers size" |
|
1684 |
||
1685 |
^ self subclassResponsibility |
|
1686 |
! |
|
1687 |
||
1688 |
growSize |
|
1689 |
"return a suitable size increment for growing. |
|
1690 |
The default returned here may be (and is) redefined in subclasses." |
|
1691 |
||
1692 |
^ self size max:2 |
|
1693 |
! ! |
|
1694 |
||
1 | 1695 |
!Collection methodsFor:'printing & storing'! |
1696 |
||
602 | 1697 |
displayString |
1698 |
"return a printed representation of the receiver for display in inspectors etc." |
|
1699 |
||
1700 |
|thisString buffer count string noneYet total limit| |
|
1701 |
||
1702 |
thisContext isRecursive ifTrue:[ |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1703 |
'Collection [error]: displayString of self referencing collection.' errorPrintCR. |
1413 | 1704 |
^ '#("recursive")' |
602 | 1705 |
]. |
1706 |
||
3964
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1707 |
string := self displayStringName , '('. |
602 | 1708 |
noneYet := true. |
1709 |
buffer := ''. |
|
1710 |
count := 0. |
|
1711 |
total := 0. |
|
1712 |
limit := self maxPrint. |
|
1713 |
||
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1714 |
self printElementsDo: [:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1715 |
thisString := element displayString. |
1413 | 1716 |
noneYet ifTrue:[ |
1717 |
noneYet := false. |
|
1718 |
buffer := buffer , thisString |
|
1719 |
] ifFalse:[ |
|
1720 |
buffer := buffer , (' ' , thisString) |
|
1721 |
]. |
|
1722 |
count := count + 1. |
|
1723 |
(count == 20) ifTrue:[ |
|
1724 |
string := string , buffer. |
|
1725 |
buffer := ''. |
|
1726 |
count := 0 |
|
1727 |
]. |
|
1728 |
total := total + 1. |
|
1729 |
(total > limit) ifTrue:[ |
|
1730 |
string := string , buffer , '... )'. |
|
1731 |
^ string |
|
1732 |
] |
|
602 | 1733 |
]. |
1734 |
string := string , buffer , ')'. |
|
1735 |
^ string |
|
1413 | 1736 |
|
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1737 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1738 |
#(1 2 3 'hello' $a) asOrderedCollection displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1739 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1740 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1741 |
at:#foo put:'bar'; yourself) displayString |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1742 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1743 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1744 |
"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
|
1745 |
"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
|
1746 |
! |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1747 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1748 |
displayStringName |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1749 |
"redefinable helper for displayString" |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1750 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1751 |
^ (self class name) |
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1752 |
|
a3129832fe16
allow redefinition of the displayed class name (displayString)
Claus Gittinger <cg@exept.de>
parents:
3767
diff
changeset
|
1753 |
"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
|
1754 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1755 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1756 |
maxPrint |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1757 |
"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
|
1758 |
than the limit defined here." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1759 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1760 |
^ 5000 |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1761 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1762 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1763 |
printElementsDo:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1764 |
"perform aBlock (1 arg) for all elements. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1765 |
Used in #printOn:. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1766 |
Subclasses (e.g. Dictionary) may redefine this." |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1767 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1768 |
^ self do:aBlock |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1769 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1770 |
"Created: / 20.1.1998 / 14:11:02 / stefan" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1771 |
! |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1772 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1773 |
printOn:aStream |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1774 |
"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
|
1775 |
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
|
1776 |
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
|
1777 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1778 |
|
3402 | 1779 |
|limit firstOne s| |
3191
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1780 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1781 |
thisContext isRecursive ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1782 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1783 |
aStream nextPutAll:'#("recursive")'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1784 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1785 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1786 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1787 |
aStream nextPutAll:self class name. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1788 |
aStream nextPut:$(. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1789 |
firstOne := true. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1790 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1791 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1792 |
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
|
1793 |
(needed for limit calculation) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1794 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1795 |
aStream isPositionable ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1796 |
s := aStream. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1797 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1798 |
s := WriteStream on:(String new:50). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1799 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1800 |
limit := s position + self maxPrint. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1801 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1802 |
self printElementsDo:[:element | |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1803 |
firstOne ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1804 |
s space |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1805 |
] ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1806 |
firstOne := false |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1807 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1808 |
(s position >= limit) ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1809 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1810 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1811 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1812 |
aStream nextPutAll:'...etc...)'. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1813 |
^ self |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1814 |
] ifFalse:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1815 |
element printOn:s. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1816 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1817 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1818 |
s ~~ aStream ifTrue:[ |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1819 |
aStream nextPutAll:(s contents). |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1820 |
]. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1821 |
aStream nextPut:$) |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1822 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1823 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1824 |
#(1 2 3 'hello' $a) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1825 |
(Array new:100000) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1826 |
(Array new:100000) printOn:Stdout |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1827 |
(Array new:100000) printString size |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1828 |
(Dictionary new at:#hello put:'world'; |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1829 |
at:#foo put:'bar'; yourself) printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1830 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1831 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1832 |
|a| |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1833 |
a := Array new:3. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1834 |
a at:2 put:a. |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1835 |
a printOn:Transcript |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1836 |
" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1837 |
|
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1838 |
"Modified: / 28.1.1997 / 00:39:17 / cg" |
390764c0d7b3
Make #printOn: more general (and optimal).
Stefan Vogel <sv@exept.de>
parents:
3124
diff
changeset
|
1839 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
602 | 1840 |
! |
1841 |
||
1 | 1842 |
storeOn:aStream |
88 | 1843 |
"output a printed representation onto the argument, aStream. |
1844 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
1845 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
1846 |
(use storeBinaryOn: to handle those)." |
|
1 | 1847 |
|
5254 | 1848 |
|notEmpty| |
1 | 1849 |
|
44 | 1850 |
thisContext isRecursive ifTrue:[ |
5497 | 1851 |
RecursiveCollectionStoreStringSignal raiseRequestWith:self. |
2289 | 1852 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 1853 |
aStream nextPutAll:'#recursive'. |
1854 |
^ self |
|
44 | 1855 |
]. |
1856 |
||
1857 |
aStream nextPutAll:'('. |
|
1 | 1858 |
aStream nextPutAll:(self class name). |
44 | 1859 |
aStream nextPutAll:' new'. |
5254 | 1860 |
notEmpty := false. |
1 | 1861 |
self do:[:element | |
1413 | 1862 |
aStream nextPutAll:' add:'. |
1863 |
element storeOn:aStream. |
|
1864 |
aStream nextPutAll:';'. |
|
5254 | 1865 |
notEmpty := true |
1 | 1866 |
]. |
5254 | 1867 |
notEmpty ifTrue:[aStream nextPutAll:' yourself']. |
1 | 1868 |
aStream nextPut:$) |
44 | 1869 |
|
88 | 1870 |
" |
1871 |
OrderedCollection new storeOn:Transcript |
|
1872 |
(1 to:10) storeOn:Transcript |
|
1873 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
1874 |
" |
|
293 | 1875 |
" |
1876 |
|s| |
|
1877 |
||
1878 |
s := Set new. |
|
1879 |
s add:1; add:'hello'; add:s. |
|
1880 |
s storeOn:Transcript |
|
1881 |
" |
|
1413 | 1882 |
|
5254 | 1883 |
"Modified: / 11.2.2000 / 11:24:56 / cg" |
1 | 1884 |
! ! |
602 | 1885 |
|
1886 |
!Collection methodsFor:'queries'! |
|
1887 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1888 |
defaultElement |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1889 |
^ nil |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1890 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1891 |
|
602 | 1892 |
isCollection |
1893 |
"return true, if the receiver is some kind of collection; |
|
1894 |
true is returned here - the method is redefined from Object." |
|
1895 |
||
1896 |
^ true |
|
1897 |
! |
|
1898 |
||
1174 | 1899 |
isSorted |
1900 |
"return true, if the receiver is sorted. |
|
1901 |
Collections which hold their elements in sorted order |
|
1902 |
should return true. Some algorithms (quicksort) degenerate when |
|
1903 |
operating on sorted collections and can be avoided if this information |
|
1904 |
is given. The default returned here (false) should not hurt. |
|
1905 |
I.e. you should NEVER depend on that in your application." |
|
1906 |
||
1907 |
^ false |
|
1908 |
||
1909 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
1910 |
! |
|
1911 |
||
5359
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1912 |
isSortedBy:aBlock |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1913 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock). |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1914 |
Collections which hold their elements in sorted order |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1915 |
should return true. Some algorithms (quicksort) degenerate when |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1916 |
operating on sorted collections and can be avoided if this information |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1917 |
is given. The default returned here (false) should not hurt. |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1918 |
I.e. you should NEVER depend on that in your application." |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1919 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1920 |
^ false |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1921 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1922 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1923 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1924 |
isSortedCollection |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1925 |
"return true, if the receiver is a sortedCollection." |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1926 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1927 |
^ false |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1928 |
|
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1929 |
! |
396b77b6f1d2
added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents:
5268
diff
changeset
|
1930 |
|
602 | 1931 |
longestCommonPrefix |
1246 | 1932 |
"return the longest common prefix of my elements. |
1933 |
Typically used with string collections." |
|
1934 |
||
602 | 1935 |
|try allMatching matchLen| |
1936 |
||
1937 |
" |
|
1938 |
find the longest common prefix |
|
1939 |
" |
|
1940 |
matchLen := 0. |
|
1941 |
self do:[:aName | |
|
1246 | 1942 |
aName size > matchLen ifTrue:[ |
1943 |
matchLen := aName size. |
|
1944 |
try := aName |
|
1945 |
] |
|
602 | 1946 |
]. |
1947 |
||
1948 |
allMatching := true. |
|
1949 |
||
1950 |
[true] whileTrue:[ |
|
1246 | 1951 |
allMatching := true. |
1952 |
self do:[:aName | |
|
1953 |
(aName startsWith:try) ifFalse:[ |
|
1954 |
allMatching := false |
|
1955 |
] |
|
1956 |
]. |
|
1957 |
allMatching ifTrue:[ |
|
1958 |
^ try |
|
1959 |
]. |
|
1960 |
matchLen := matchLen - 1. |
|
2434
0a84bd4173c2
longestCommonPrefix - failed if there was no common prefix at all.
Claus Gittinger <cg@exept.de>
parents:
2383
diff
changeset
|
1961 |
matchLen == 0 ifTrue:[^ '']. |
1246 | 1962 |
try := try copyTo:matchLen. |
602 | 1963 |
]. |
1964 |
^ try |
|
1965 |
||
1966 |
" |
|
1967 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1968 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1969 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
1970 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
1971 |
" |
|
1246 | 1972 |
|
2434
0a84bd4173c2
longestCommonPrefix - failed if there was no common prefix at all.
Claus Gittinger <cg@exept.de>
parents:
2383
diff
changeset
|
1973 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
602 | 1974 |
! ! |
1975 |
||
1976 |
!Collection methodsFor:'testing'! |
|
1977 |
||
1978 |
capacity |
|
1979 |
"return the number of elements, that the receiver is |
|
1980 |
prepared to take. For most collections, this is the actual |
|
1981 |
size. However, some have more space preallocated to allow |
|
1982 |
for faster adding of elements. |
|
1983 |
Not used by the system; added for ST-80 compatibility." |
|
1984 |
||
1985 |
^ self size |
|
1986 |
! |
|
1987 |
||
1988 |
includes:anElement |
|
1989 |
"return true, if an object equal to the argument, anObject is in the list. |
|
1990 |
This compares using #= (i.e. it does not look for the object itself, |
|
1991 |
instead, one that compares equal). |
|
1992 |
See #includesIdentical: when identity is asked for." |
|
1993 |
||
1994 |
self do:[:element | |
|
1995 |
(anElement = element) ifTrue:[^ true]. |
|
1996 |
]. |
|
1997 |
^ false |
|
1998 |
! |
|
1999 |
||
2000 |
includesAll:aCollection |
|
2001 |
"return true, if the the receiver includes all elements of |
|
2002 |
the argument, aCollection; false if any is missing. |
|
2003 |
Notice: this method has O-square runtime behavior and may be |
|
2004 |
slow for big receivers/args. Think about using a Set, |
|
2005 |
or Dictionary." |
|
2006 |
||
2007 |
aCollection do:[:element | |
|
2008 |
(self includes:element) ifFalse:[^ false]. |
|
2009 |
]. |
|
2010 |
^ true |
|
2011 |
||
2012 |
" |
|
2013 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
2014 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
2015 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
2016 |
" |
|
2017 |
! |
|
2018 |
||
2019 |
includesAny:aCollection |
|
2020 |
"return true, if the the receiver includes any elements of |
|
2021 |
the argument, aCollection; false if it includes none. |
|
2022 |
Notice: this method has O-square runtime behavior and may be |
|
2023 |
slow for big receivers/args. Think about using a Set, |
|
2024 |
or Dictionary. Speedup is possible, by arrangy highly |
|
2025 |
probable elements towards the beginning of aCollection, |
|
2026 |
to avoid useless searches." |
|
2027 |
||
2028 |
aCollection do:[:element | |
|
2029 |
(self includes:element) ifTrue:[^ true]. |
|
2030 |
]. |
|
2031 |
^ false |
|
2032 |
||
2033 |
" |
|
2034 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
2035 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
2036 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
2037 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
2038 |
" |
|
2039 |
! |
|
2040 |
||
2041 |
includesIdentical:anElement |
|
2042 |
"return true, if the argument, anObject is in the list. |
|
2043 |
This compares using #== (i.e. object identity). |
|
2044 |
See #includes: when equality is asked for." |
|
2045 |
||
2046 |
self do:[:element | |
|
2047 |
(anElement == element) ifTrue:[^ true]. |
|
2048 |
]. |
|
2049 |
^ false |
|
2050 |
! |
|
2051 |
||
2052 |
isEmpty |
|
2053 |
"return true, if the receiver is empty" |
|
2054 |
||
2055 |
^ self size == 0 |
|
2056 |
! |
|
2057 |
||
2058 |
max |
|
2097 | 2059 |
"return the maximum value in the receiver collection, |
2060 |
using #> to compare elements." |
|
602 | 2061 |
|
2062 |
^ self inject:nil |
|
2097 | 2063 |
into:[:maxSoFar :this | |
2064 |
(maxSoFar isNil |
|
2065 |
or:[this > maxSoFar]) ifTrue:[this] |
|
2066 |
ifFalse:[maxSoFar]] |
|
602 | 2067 |
|
2068 |
" |
|
2069 |
#(15 1 -9 10 5) max |
|
2070 |
(1 to:15) max |
|
2071 |
" |
|
2097 | 2072 |
|
2073 |
"Modified: 8.1.1997 / 22:09:37 / cg" |
|
602 | 2074 |
! |
2075 |
||
2076 |
min |
|
2097 | 2077 |
"return the minimum value in the receiver collection, |
2078 |
using < to compare elements." |
|
602 | 2079 |
|
2080 |
^ self inject:nil |
|
2097 | 2081 |
into:[:minSoFar :this | |
2082 |
(minSoFar isNil |
|
2083 |
or:[this < minSoFar]) ifTrue:[this] |
|
2084 |
ifFalse:[minSoFar]] |
|
602 | 2085 |
|
2086 |
" |
|
2087 |
#(15 1 -9 10 5) min |
|
2088 |
(1 to:15) min |
|
2089 |
" |
|
2097 | 2090 |
|
2091 |
"Modified: 8.1.1997 / 22:09:49 / cg" |
|
602 | 2092 |
! |
2093 |
||
2094 |
notEmpty |
|
2095 |
"return true, if the receiver is not empty" |
|
2096 |
||
2097 |
^ self isEmpty not |
|
2098 |
! |
|
2099 |
||
2100 |
occurrencesOf:anElement |
|
2101 |
"return the number of occurrences of the argument, anElement in |
|
5048 | 2102 |
the receiver. Uses #= (i.e. equality) compare." |
602 | 2103 |
|
2104 |
|count "{ Class: SmallInteger }" | |
|
2105 |
||
2106 |
count := 0. |
|
2107 |
self do:[:element | |
|
5048 | 2108 |
(anElement = element) ifTrue:[ |
2109 |
count := count + 1 |
|
2110 |
]. |
|
602 | 2111 |
]. |
2112 |
^ count |
|
2113 |
! |
|
2114 |
||
2115 |
size |
|
2116 |
"return the number of elements in the receiver. |
|
2117 |
This is usually redefined in subclasses for more performance." |
|
2118 |
||
2119 |
|count "{ Class: SmallInteger }" | |
|
2120 |
||
2121 |
count := 0. |
|
2122 |
self do:[:element | |
|
2123 |
count := count + 1 |
|
2124 |
]. |
|
2125 |
^ count |
|
2126 |
! ! |
|
2127 |
||
4680 | 2128 |
!Collection methodsFor:'tracing'! |
2129 |
||
2130 |
traceInto:aRequestor level:level from:referrer |
|
2131 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
2132 |
||
2133 |
^ aRequestor traceCollection:self level:level from:referrer |
|
2134 |
||
2135 |
||
2136 |
! ! |
|
2137 |
||
2097 | 2138 |
!Collection class methodsFor:'documentation'! |
628 | 2139 |
|
2140 |
version |
|
5497 | 2141 |
^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.99 2000-08-04 16:44:03 tm Exp $' |
628 | 2142 |
! ! |
602 | 2143 |
Collection initialize! |