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