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