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