author | Claus Gittinger <cg@exept.de> |
Thu, 24 Apr 1997 16:23:20 +0200 | |
changeset 2585 | 6c25622320aa |
parent 2434 | 0a84bd4173c2 |
child 2639 | 4789c2cfbed0 |
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:[ |
302 | 72 |
InvalidKeySignal := ErrorSignal newSignalMayProceed:true. |
155 | 73 |
InvalidKeySignal nameClass:self message:#invalidKeySignal. |
345 | 74 |
InvalidKeySignal notifierString:'invalid key:'. |
75 |
||
76 |
ValueNotFoundSignal := ErrorSignal newSignalMayProceed:true. |
|
77 |
ValueNotFoundSignal nameClass:self message:#valueNotFoundSignal. |
|
78 |
ValueNotFoundSignal notifierString:'value not found:'. |
|
155 | 79 |
|
348 | 80 |
NotEnoughElementsSignal := ErrorSignal newSignalMayProceed:true. |
81 |
NotEnoughElementsSignal nameClass:self message:#notEnoughElementsSignal. |
|
82 |
NotEnoughElementsSignal notifierString:'not enough elements in collection'. |
|
83 |
||
84 |
EmptyCollectionSignal := NotEnoughElementsSignal newSignalMayProceed:true. |
|
155 | 85 |
EmptyCollectionSignal nameClass:self message:#emptyCollectionSignal. |
86 |
EmptyCollectionSignal notifierString:'operation not allowed for empty collections'. |
|
54 | 87 |
] |
10 | 88 |
! ! |
89 |
||
2097 | 90 |
!Collection class methodsFor:'instance creation'! |
155 | 91 |
|
602 | 92 |
new:size withAll:element |
93 |
"return a new COllection of size, where all elements are |
|
94 |
initialized to element" |
|
348 | 95 |
|
602 | 96 |
|newCollection| |
92 | 97 |
|
602 | 98 |
newCollection := self new:size. |
99 |
size timesRepeat:[newCollection add:element] |
|
100 |
! |
|
1 | 101 |
|
102 |
with:anObject |
|
103 |
"return a new Collection with one element:anObject" |
|
104 |
||
105 |
|newCollection| |
|
106 |
||
107 |
newCollection := self new. |
|
108 |
newCollection add:anObject. |
|
109 |
^ newCollection |
|
110 |
! |
|
111 |
||
112 |
with:firstObject with:secondObject |
|
113 |
"return a new Collection with two elements:firstObject and secondObject" |
|
114 |
||
115 |
|newCollection| |
|
116 |
||
117 |
newCollection := self new. |
|
1112 | 118 |
newCollection add:firstObject; add:secondObject. |
1 | 119 |
^ newCollection |
120 |
! |
|
121 |
||
122 |
with:firstObject with:secondObject with:thirdObject |
|
123 |
"return a new Collection with three elements" |
|
124 |
||
125 |
|newCollection| |
|
126 |
||
127 |
newCollection := self new. |
|
1112 | 128 |
newCollection add:firstObject; add:secondObject; add:thirdObject. |
1 | 129 |
^ newCollection |
130 |
! |
|
131 |
||
132 |
with:firstObject with:secondObject with:thirdObject with:fourthObject |
|
133 |
"return a new Collection with four elements" |
|
134 |
||
135 |
|newCollection| |
|
136 |
||
137 |
newCollection := self new. |
|
1112 | 138 |
newCollection add:firstObject; add:secondObject; add:thirdObject; add:fourthObject. |
1 | 139 |
^ newCollection |
140 |
! |
|
141 |
||
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
142 |
with:a1 with:a2 with:a3 with:a4 with:a5 |
345 | 143 |
"return a new Collection with five elements" |
144 |
||
145 |
|newCollection| |
|
146 |
||
147 |
newCollection := self new. |
|
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
148 |
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
|
149 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
150 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
151 |
"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
|
152 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
153 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
154 |
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
|
155 |
"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
|
156 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
157 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
158 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
159 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
160 |
newCollection add:a1; add:a2; add:a3; add:a4; add:a5; add:a6. |
345 | 161 |
^ newCollection |
2225
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
162 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
163 |
"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
|
164 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
165 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
166 |
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
|
167 |
"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
|
168 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
169 |
|newCollection| |
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 := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
172 |
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
|
173 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
174 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
175 |
"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
|
176 |
! |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
177 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
178 |
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
|
179 |
"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
|
180 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
181 |
|newCollection| |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
182 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
183 |
newCollection := self new. |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
184 |
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
|
185 |
^ newCollection |
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
186 |
|
53313e47007a
added with:with: type of instance creation messages
Claus Gittinger <cg@exept.de>
parents:
2167
diff
changeset
|
187 |
"Created: 22.1.1997 / 19:34:34 / cg" |
345 | 188 |
! |
189 |
||
1 | 190 |
withAll:aCollection |
191 |
"return a new Collection with all elements taken from the argument, |
|
192 |
aCollection" |
|
193 |
||
194 |
|newCollection| |
|
195 |
||
196 |
newCollection := self new. |
|
197 |
newCollection addAll:aCollection. |
|
198 |
^newCollection |
|
199 |
! ! |
|
200 |
||
2097 | 201 |
!Collection class methodsFor:'Signal constants'! |
10 | 202 |
|
602 | 203 |
emptyCollectionSignal |
204 |
"return the signal used to report non-allowed operation on empty collections" |
|
345 | 205 |
|
602 | 206 |
^ EmptyCollectionSignal |
308 | 207 |
! |
208 |
||
602 | 209 |
invalidKeySignal |
210 |
"return the signal used to report bad key usage" |
|
308 | 211 |
|
602 | 212 |
^ InvalidKeySignal |
201 | 213 |
! |
214 |
||
602 | 215 |
notEnoughElementsSignal |
216 |
"return the signal used to report attempts for an operation, for which |
|
217 |
there are not enough elements in the collection" |
|
249 | 218 |
|
602 | 219 |
^ NotEnoughElementsSignal |
404 | 220 |
! |
221 |
||
602 | 222 |
valueNotFoundSignal |
223 |
"return the signal used to report a nonexisting element." |
|
404 | 224 |
|
602 | 225 |
^ ValueNotFoundSignal |
226 |
! ! |
|
404 | 227 |
|
2097 | 228 |
!Collection class methodsFor:'queries'! |
404 | 229 |
|
602 | 230 |
growIsCheap |
231 |
"return true, if this collection can easily grow |
|
232 |
(i.e. without a need for become:). |
|
233 |
Returns true here; this method is redefined in fix-size |
|
234 |
collections" |
|
404 | 235 |
|
602 | 236 |
^ true |
257 | 237 |
! ! |
238 |
||
1 | 239 |
!Collection methodsFor:'accessing'! |
240 |
||
602 | 241 |
anElement |
242 |
"return any element from the collection, or nil if there is none" |
|
243 |
||
244 |
self do: [:each | ^ each]. |
|
245 |
^ nil |
|
246 |
! |
|
247 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
248 |
atAll:indexCollection put:anObject |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
249 |
"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
|
250 |
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
|
251 |
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
|
252 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
253 |
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
|
254 |
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
|
255 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
256 |
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
|
257 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
258 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
259 |
(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
|
260 |
(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
|
261 |
(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
|
262 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
263 |
raises an error: |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
264 |
(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
|
265 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
266 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
267 |
"Modified: 11.5.1996 / 11:04:59 / cg" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
268 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1359
diff
changeset
|
269 |
|
68 | 270 |
first |
271 |
"return the first element of the collection. |
|
272 |
This should be redefined in subclasses." |
|
273 |
||
274 |
self do:[:e | ^ e]. |
|
275 |
||
276 |
"error if collection is empty" |
|
155 | 277 |
^ self emptyCollectionError |
68 | 278 |
! |
279 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
280 |
firstIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
281 |
"return the first element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
282 |
If its empty, return the excveptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
283 |
(i.e. dont trigger an error as done in #first)" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
284 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
285 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
286 |
^ self first |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
287 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
288 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
289 |
! |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
290 |
|
68 | 291 |
last |
292 |
"return the last element of the collection. |
|
293 |
This should be redefined in subclasses." |
|
294 |
||
295 |
|theLastOne any| |
|
296 |
||
297 |
any := false. |
|
298 |
self do:[:e | any := true. theLastOne := e]. |
|
299 |
any ifTrue:[ |
|
155 | 300 |
^ theLastOne |
68 | 301 |
]. |
302 |
||
303 |
"error if collection is empty" |
|
155 | 304 |
^ self emptyCollectionError |
68 | 305 |
! |
306 |
||
936
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
307 |
lastIfEmpty:exceptionValue |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
308 |
"return the last element of the collection. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
309 |
If its empty, return the exceptionValue. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
310 |
(i.e. dont trigger an error as done in #last)" |
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 |
self isEmpty ifTrue:[^ exceptionValue value]. |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
313 |
^ self last |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
314 |
|
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
315 |
"Modified: 6.2.1996 / 15:27:17 / cg" |
9054edb471b3
added #firstIfEmpty: / #lastIfEmpty:
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
316 |
"Created: 6.2.1996 / 15:27:49 / cg" |
1 | 317 |
! ! |
318 |
||
319 |
!Collection methodsFor:'adding & removing'! |
|
320 |
||
321 |
add:anObject |
|
88 | 322 |
"add the argument, anObject to the receiver. |
323 |
If the receiver is ordered, the position of the new element is undefined |
|
2351 | 324 |
(i.e. dont depend on where it will be put). |
325 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 326 |
|
327 |
^ self subclassResponsibility |
|
2351 | 328 |
|
329 |
"Modified: 1.2.1997 / 11:57:08 / cg" |
|
1 | 330 |
! |
331 |
||
1373
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
332 |
add:newObject withOccurrences:anInteger |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
333 |
"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
|
334 |
Returns the object." |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
335 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
336 |
anInteger timesRepeat:[self add:newObject]. |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
337 |
^ newObject |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
338 |
|
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
339 |
"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
|
340 |
! |
2c7cab1a4a2e
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1370
diff
changeset
|
341 |
|
1 | 342 |
addAll:aCollection |
1163 | 343 |
"add all elements of the argument, aCollection to the receiver. |
344 |
Returns the argument, aCollection." |
|
1 | 345 |
|
346 |
aCollection do:[:element | |
|
1163 | 347 |
self add:element |
1 | 348 |
]. |
349 |
^ aCollection |
|
88 | 350 |
|
351 |
" |
|
352 |
#(1 2 3 4) copy addAll:#(5 6 7 8) |
|
353 |
#(1 2 3 4) asOrderedCollection addAll:#(5 6 7 8) |
|
354 |
" |
|
1163 | 355 |
|
356 |
"Modified: 12.4.1996 / 13:29:20 / cg" |
|
1 | 357 |
! |
358 |
||
12 | 359 |
addAllFirst:aCollection |
360 |
"insert all elements of the argument, aCollection at the beginning |
|
1163 | 361 |
of the receiver. Returns the argument, aCollection." |
12 | 362 |
|
363 |
aCollection reverseDo:[:element | |
|
1163 | 364 |
self addFirst:element |
12 | 365 |
]. |
366 |
^ aCollection |
|
367 |
||
368 |
" |
|
369 |
|c| |
|
370 |
c := #(4 3 2 1) asOrderedCollection. |
|
1163 | 371 |
c addAllFirst:#(9 8 7 6 5). |
372 |
c |
|
12 | 373 |
" |
1163 | 374 |
|
375 |
"Modified: 12.4.1996 / 13:30:10 / cg" |
|
12 | 376 |
! |
377 |
||
602 | 378 |
addAllLast:aCollection |
1163 | 379 |
"add all elements of the argument, aCollection to the receiver. |
380 |
Returns the argument, aCollection." |
|
602 | 381 |
|
382 |
aCollection do:[:element | |
|
1163 | 383 |
self addLast:element |
602 | 384 |
]. |
385 |
^ aCollection |
|
386 |
||
387 |
" |
|
388 |
|c| |
|
389 |
c := #(4 3 2 1) asOrderedCollection. |
|
390 |
c addAllLast:#(9 8 7 6 5) |
|
391 |
" |
|
1163 | 392 |
|
393 |
"Modified: 12.4.1996 / 13:30:54 / cg" |
|
602 | 394 |
! |
395 |
||
396 |
addFirst:anObject |
|
397 |
"add the argument, anObject to the receiver. |
|
2351 | 398 |
If the receiver is ordered, the new element will be added at the beginning. |
399 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
1 | 400 |
|
401 |
^ self subclassResponsibility |
|
2351 | 402 |
|
403 |
"Modified: 1.2.1997 / 11:57:19 / cg" |
|
1 | 404 |
! |
405 |
||
602 | 406 |
addLast:anObject |
407 |
"add the argument, anObject to the receiver. |
|
1163 | 408 |
If the receiver is ordered, the new element will be added at the end. |
409 |
Return the argument, anObject." |
|
602 | 410 |
|
411 |
^ self add:anObject |
|
1163 | 412 |
|
413 |
"Modified: 12.4.1996 / 13:31:12 / cg" |
|
602 | 414 |
! |
415 |
||
1 | 416 |
remove:anObject |
2351 | 417 |
"search for the first element, which is equal to anObject; |
418 |
if found, remove and return it. |
|
419 |
If not found, report a 'value not found'-error. |
|
420 |
Uses equality compare (=) to search for the occurrence." |
|
1 | 421 |
|
1163 | 422 |
^ self remove:anObject ifAbsent:[self errorValueNotFound:anObject] |
423 |
||
2351 | 424 |
"Modified: 1.2.1997 / 11:58:48 / cg" |
1 | 425 |
! |
426 |
||
602 | 427 |
remove:anObject ifAbsent:exceptionBlock |
2351 | 428 |
"search for the first element, which is equal to anObject; |
429 |
if found, remove and return it. |
|
430 |
If not found, return the the value of the exceptionBlock. |
|
431 |
Uses equality compare (=) to search for the occurrence. |
|
432 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 433 |
|
434 |
^ self subclassResponsibility |
|
2351 | 435 |
|
436 |
"Modified: 1.2.1997 / 11:56:53 / cg" |
|
602 | 437 |
! |
438 |
||
134 | 439 |
removeAll |
1163 | 440 |
"remove all elements from the receiver. Returns the receiver. |
134 | 441 |
This should be reimplemented in subclasses for better |
442 |
performance." |
|
443 |
||
2383 | 444 |
[self isEmpty] whileFalse:[ |
1163 | 445 |
self removeFirst |
134 | 446 |
]. |
1163 | 447 |
|
2383 | 448 |
"Modified: 12.2.1997 / 12:40:29 / cg" |
134 | 449 |
! |
450 |
||
1 | 451 |
removeAll:aCollection |
360 | 452 |
"remove all elements of the argument, aCollection from the receiver. |
1163 | 453 |
Return the argument, aCollection. |
454 |
||
360 | 455 |
Notice: for some collections (those not tuned for |
1163 | 456 |
resizing themself) this may be very slow. |
457 |
If the number of removed elements is big compared to to |
|
458 |
the receivers size, it may be better to copy the |
|
459 |
ones which are not to be removed into a new collection." |
|
1 | 460 |
|
461 |
aCollection do:[:element | self remove:element]. |
|
462 |
^ aCollection |
|
1163 | 463 |
|
464 |
"Modified: 12.4.1996 / 13:35:48 / cg" |
|
348 | 465 |
! |
466 |
||
467 |
removeFirst |
|
468 |
"remove the first element from the receiver. |
|
469 |
Return the removed element." |
|
470 |
||
1561
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
471 |
self do:[:element | |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
472 |
self remove:element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
473 |
^ element. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
474 |
]. |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
475 |
^ self emptyCollectionError |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
476 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
477 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
478 |
(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
|
479 |
" |
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
480 |
|
1f64a9b522c2
at least a suboptimal #removeFirst: can be provided by Collection
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
481 |
"Modified: 28.6.1996 / 18:55:33 / cg" |
348 | 482 |
! |
483 |
||
484 |
removeFirst:n |
|
485 |
"remove the first n elements from the receiver. |
|
360 | 486 |
Return an array filled with the removed elements. |
487 |
Notice: for some collections (those not tuned for |
|
488 |
resizing themself) this may be very slow." |
|
348 | 489 |
|
490 |
|ret| |
|
491 |
||
492 |
self size < n ifTrue:[ |
|
493 |
^ self notEnoughElementsError |
|
494 |
]. |
|
495 |
ret := Array new:n. |
|
496 |
1 to:n do:[:i | |
|
497 |
ret at:i put:(self removeFirst). |
|
498 |
]. |
|
499 |
^ ret |
|
500 |
! |
|
501 |
||
2351 | 502 |
removeIdentical:anObject |
503 |
"search for the first element, which is identical to anObject; |
|
504 |
if found, remove and return it. |
|
505 |
If not found, report a 'value not found'-error. |
|
506 |
Uses identity compare (==) to search for the occurrence." |
|
507 |
||
508 |
^ self removeIdentical:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
509 |
||
510 |
"Modified: 12.4.1996 / 13:33:30 / cg" |
|
511 |
"Created: 1.2.1997 / 11:59:10 / cg" |
|
512 |
! |
|
513 |
||
514 |
removeIdentical:anObject ifAbsent:exceptionBlock |
|
515 |
"search for the first element, which is identical to anObject; |
|
516 |
if found, remove and return it. |
|
517 |
If not found, return the the value of the exceptionBlock. |
|
518 |
Uses identity compare (==) to search for the occurrence. |
|
519 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
520 |
||
521 |
^ self subclassResponsibility |
|
522 |
||
523 |
"Created: 1.2.1997 / 11:56:01 / cg" |
|
524 |
"Modified: 1.2.1997 / 11:56:59 / cg" |
|
525 |
! |
|
526 |
||
602 | 527 |
removeLast |
528 |
"remove the last element from the receiver. |
|
2351 | 529 |
Return the removed element. |
530 |
An error is raised here - it is to be implemented by a concrete subclass." |
|
602 | 531 |
|
532 |
^ self subclassResponsibility |
|
2351 | 533 |
|
534 |
"Modified: 1.2.1997 / 11:57:53 / cg" |
|
602 | 535 |
! |
536 |
||
348 | 537 |
removeLast:n |
538 |
"remove the last n elements from the receiver. |
|
360 | 539 |
Return an array filled with the removed elements. |
540 |
Notice: for some collections (those not tuned for |
|
541 |
resizing themself) this may be very slow." |
|
348 | 542 |
|
543 |
|ret| |
|
544 |
||
545 |
self size < n ifTrue:[ |
|
546 |
^ self notEnoughElementsError |
|
547 |
]. |
|
548 |
ret := Array new:n. |
|
549 |
n to:1 by:-1 do:[:i | |
|
550 |
ret at:i put:(self removeLast). |
|
551 |
]. |
|
552 |
^ ret |
|
1 | 553 |
! ! |
554 |
||
602 | 555 |
!Collection methodsFor:'converting'! |
556 |
||
557 |
asArray |
|
558 |
"return a new Array with the collections elements" |
|
559 |
||
560 |
|anArray |
|
561 |
index "{ Class: SmallInteger }" | |
|
1 | 562 |
|
602 | 563 |
anArray := Array new:(self size). |
564 |
index := 1. |
|
565 |
self do:[:each | |
|
566 |
anArray at:index put:each. |
|
567 |
index := index + 1 |
|
568 |
]. |
|
569 |
^ anArray |
|
1 | 570 |
! |
571 |
||
602 | 572 |
asBag |
573 |
"return a new Bag with the receiver collections elements" |
|
1 | 574 |
|
602 | 575 |
^ self addAllTo:(Bag new) |
1 | 576 |
! |
577 |
||
602 | 578 |
asByteArray |
579 |
"return a new ByteArray with the collections elements |
|
580 |
(which must convert to integers in the range 0..255)." |
|
1 | 581 |
|
602 | 582 |
|aByteArray |
583 |
index "{ Class: SmallInteger }" | |
|
1 | 584 |
|
602 | 585 |
aByteArray := ByteArray new:(self size). |
586 |
index := 1. |
|
587 |
self do:[:each | |
|
588 |
aByteArray at:index put:each asInteger. |
|
589 |
index := index + 1 |
|
590 |
]. |
|
591 |
^ aByteArray |
|
1 | 592 |
! |
593 |
||
602 | 594 |
asDoubleArray |
595 |
"return a new DoubleArray with the collections elements |
|
596 |
(which must convert to floats)." |
|
597 |
||
598 |
|aDoubleArray |
|
599 |
index "{ Class: SmallInteger }" | |
|
600 |
||
601 |
aDoubleArray := DoubleArray new:(self size). |
|
602 |
index := 1. |
|
603 |
self do:[:each | |
|
604 |
aDoubleArray at:index put:each asFloat. |
|
605 |
index := index + 1 |
|
606 |
]. |
|
607 |
^ aDoubleArray |
|
608 |
! |
|
68 | 609 |
|
602 | 610 |
asFloatArray |
611 |
"return a new FloatArray with the collections elements |
|
612 |
(which must convert to floats)." |
|
613 |
||
614 |
|aFloatArray |
|
615 |
index "{ Class: SmallInteger }" | |
|
616 |
||
617 |
aFloatArray := FloatArray new:(self size). |
|
618 |
index := 1. |
|
619 |
self do:[:each | |
|
620 |
aFloatArray at:index put:each asFloat. |
|
621 |
index := index + 1 |
|
622 |
]. |
|
623 |
^ aFloatArray |
|
624 |
! |
|
625 |
||
626 |
asIdentitySet |
|
627 |
"return a new IdentitySet with the receiver collections elements" |
|
628 |
||
629 |
^ self addAllTo:(IdentitySet new:self size) |
|
68 | 630 |
! |
631 |
||
2167 | 632 |
asList |
633 |
"return a new List with the receiver collections elements" |
|
634 |
||
635 |
^ self addAllTo:(List new:self size) |
|
636 |
||
637 |
"Created: 14.2.1997 / 16:25:23 / cg" |
|
638 |
! |
|
639 |
||
602 | 640 |
asOrderedCollection |
641 |
"return a new OrderedCollection with the receiver collections elements" |
|
642 |
||
643 |
^ self addAllTo:(OrderedCollection new:self size) |
|
644 |
! |
|
645 |
||
1394 | 646 |
asRunArray |
647 |
"return a new RunArray with the collections elements" |
|
648 |
||
649 |
|anArray| |
|
650 |
||
651 |
anArray := RunArray new. |
|
652 |
self do:[:each | |
|
653 |
anArray add:each. |
|
654 |
]. |
|
655 |
^ anArray |
|
656 |
||
657 |
" |
|
658 |
#(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray |
|
659 |
" |
|
660 |
||
661 |
"Modified: 14.5.1996 / 14:58:41 / cg" |
|
662 |
! |
|
663 |
||
602 | 664 |
asSet |
665 |
"return a new Set with the receiver collections elements" |
|
666 |
||
667 |
^ self addAllTo:(Set new:self size) |
|
668 |
! |
|
669 |
||
670 |
asSortedCollection |
|
671 |
"return a new SortedCollection with the receiver collections elements" |
|
1 | 672 |
|
602 | 673 |
|aSortedCollection| |
674 |
||
675 |
aSortedCollection := SortedCollection new:self size. |
|
676 |
aSortedCollection addAll:self. |
|
677 |
^ aSortedCollection |
|
678 |
! |
|
679 |
||
680 |
asSortedCollection:sortBlock |
|
681 |
"return a new SortedCollection with the receiver collections elements, |
|
682 |
using sortBlock for comparing" |
|
683 |
||
684 |
|aSortedCollection| |
|
685 |
||
686 |
aSortedCollection := SortedCollection sortBlock:sortBlock. |
|
687 |
aSortedCollection addAll:self. |
|
688 |
^ aSortedCollection |
|
1 | 689 |
! |
690 |
||
602 | 691 |
asString |
692 |
"return a String with the collections elements |
|
693 |
(which must convert to characters)" |
|
694 |
||
695 |
|aString |
|
696 |
index "{ Class: SmallInteger }" | |
|
360 | 697 |
|
602 | 698 |
aString := String new:(self size). |
699 |
index := 1. |
|
700 |
self do:[:each | |
|
701 |
aString at:index put:each asCharacter. |
|
702 |
index := index + 1 |
|
360 | 703 |
]. |
602 | 704 |
^ aString |
705 |
! |
|
706 |
||
707 |
asStringCollection |
|
1413 | 708 |
"return a new string collection containing the elements; |
709 |
these ought to be strings. (i.e. String or Text instances)" |
|
602 | 710 |
|
711 |
^ self addAllTo:(StringCollection new) |
|
1413 | 712 |
|
713 |
"Modified: 18.5.1996 / 13:54:34 / cg" |
|
360 | 714 |
! |
715 |
||
602 | 716 |
asText |
717 |
"return a Text-object (collection of lines) from myself. |
|
718 |
BIG warning: asText is totally misnamed here |
|
719 |
- ST/X's asText has nothing to do with PP's asText. |
|
720 |
Therefore it will be removed/renamed soon. |
|
721 |
Please use #asStringCollection." |
|
1 | 722 |
|
602 | 723 |
self obsoleteMethodWarning:'use #asStringCollection; #asText will change its meaning'. |
724 |
||
725 |
^ self asStringCollection |
|
726 |
! |
|
727 |
||
728 |
readStream |
|
729 |
"return a stream for reading from the receiver" |
|
730 |
||
731 |
^ ReadStream on:self |
|
88 | 732 |
|
733 |
" |
|
602 | 734 |
|s| |
735 |
||
736 |
s := 'hello world' readStream. |
|
737 |
s next:5. |
|
738 |
s next. |
|
739 |
(s next:5) inspect |
|
88 | 740 |
" |
1 | 741 |
! |
742 |
||
602 | 743 |
writeStream |
744 |
"return a stream for writing onto the receiver" |
|
745 |
||
746 |
^ WriteStream on:self |
|
2 | 747 |
|
602 | 748 |
" |
749 |
|s| |
|
750 |
||
751 |
s := #() copy writeStream. |
|
752 |
s nextPut:1. |
|
753 |
s nextPut:2. |
|
754 |
s nextPut:3. |
|
755 |
s contents inspect |
|
756 |
" |
|
88 | 757 |
|
758 |
" |
|
602 | 759 |
|s| |
2 | 760 |
|
602 | 761 |
s := OrderedCollection new writeStream. |
762 |
s nextPut:1. |
|
763 |
s nextPut:2. |
|
764 |
s nextPut:3. |
|
765 |
s contents inspect |
|
766 |
" |
|
767 |
! ! |
|
1 | 768 |
|
602 | 769 |
!Collection methodsFor:'copying'! |
770 |
||
771 |
copyEmpty |
|
772 |
"return a copy of the receiver with no elements. |
|
773 |
This is used by copying and enumeration methods |
|
774 |
to get a new instance which is similar to the receiver." |
|
775 |
||
776 |
^ self species new |
|
1 | 777 |
! |
778 |
||
602 | 779 |
copyEmpty:size |
780 |
"return a copy of the receiver with no elements, but space for |
|
781 |
size elements. This is used by copying and enumeration methods |
|
782 |
to get a new instance which is similar to the receiver. |
|
783 |
This method should be redefined in subclasses with instance |
|
784 |
variables, which should be put into the copy too. |
|
785 |
For example, SortedCollection has to copy its sortBlock into the |
|
786 |
new collection." |
|
1 | 787 |
|
602 | 788 |
^ self species new:size |
360 | 789 |
! |
790 |
||
602 | 791 |
copyEmptyAndGrow:size |
792 |
"return a copy of the receiver with size nil elements. |
|
793 |
This is used by copying and enumeration methods |
|
794 |
to get a new instance which is similar to the receiver." |
|
155 | 795 |
|
602 | 796 |
^ (self copyEmpty:size) grow:size |
1 | 797 |
! ! |
798 |
||
799 |
!Collection methodsFor:'enumerating'! |
|
800 |
||
602 | 801 |
addAllTo:aCollection |
802 |
"add all elements of the receiver, to aCollection. |
|
803 |
Return aCollection." |
|
77 | 804 |
|
602 | 805 |
self do:[:each | aCollection add:each]. |
806 |
^ aCollection |
|
77 | 807 |
! |
808 |
||
1 | 809 |
collect:aBlock |
810 |
"for each element in the receiver, evaluate the argument, aBlock |
|
811 |
and return a new collection with the results" |
|
812 |
||
813 |
|newCollection| |
|
814 |
||
201 | 815 |
newCollection := self species new. |
816 |
self do:[:element | newCollection add:(aBlock value:element)]. |
|
1 | 817 |
^ newCollection |
68 | 818 |
|
819 |
" |
|
820 |
#(1 2 3 4) collect:[:n | n * 2] |
|
821 |
" |
|
1 | 822 |
! |
823 |
||
602 | 824 |
conform:aBlock |
825 |
"return true, if every element conforms to some condition. |
|
826 |
I.e. return false, if aBlock returns false for any element; |
|
827 |
true otherwise." |
|
77 | 828 |
|
602 | 829 |
self do:[:element | (aBlock value:element) ifFalse:[^ false]]. |
830 |
^ true |
|
831 |
! |
|
77 | 832 |
|
602 | 833 |
count:aBlock |
834 |
"count elements, for which aBlock returns true. |
|
835 |
Return the sum." |
|
836 |
||
837 |
^ self inject:0 into:[:sumSoFar :element | |
|
838 |
(aBlock value:element) ifTrue:[sumSoFar + 1] |
|
839 |
ifFalse:[sumSoFar] |
|
840 |
] |
|
77 | 841 |
|
842 |
" |
|
602 | 843 |
#(1 2 3 4 6 8 10) count:[:a | a even] |
844 |
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil] |
|
77 | 845 |
" |
846 |
! |
|
847 |
||
1 | 848 |
detect:aBlock |
849 |
"evaluate the argument, aBlock for each element in the receiver until |
|
850 |
the block returns true; in this case return the element which caused |
|
851 |
the true evaluation. |
|
345 | 852 |
If none of the evaluations returns true, report an error" |
1 | 853 |
|
854 |
^ self detect:aBlock ifNone:[self errorNotFound] |
|
68 | 855 |
|
856 |
" |
|
857 |
#(1 2 3 4) detect:[:n | n odd] |
|
858 |
#(2 4 6 8) detect:[:n | n odd] |
|
859 |
" |
|
1 | 860 |
! |
861 |
||
862 |
detect:aBlock ifNone:exceptionBlock |
|
863 |
"evaluate the argument, aBlock for each element in the receiver until |
|
864 |
the block returns true; in this case return the element which caused |
|
865 |
the true evaluation. |
|
866 |
If none of the evaluations returns true, return the result of the |
|
867 |
evaluation of the exceptionBlock" |
|
868 |
||
869 |
self do:[:each | |
|
155 | 870 |
(aBlock value:each) ifTrue:[^ each]. |
1 | 871 |
]. |
872 |
^ exceptionBlock value |
|
68 | 873 |
|
874 |
" |
|
875 |
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry'] |
|
876 |
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry'] |
|
877 |
" |
|
1 | 878 |
! |
879 |
||
421 | 880 |
detectLast:aBlock |
881 |
"evaluate the argument, aBlock for each element in the receiver until |
|
882 |
the block returns true; in this case return the element which caused |
|
883 |
the true evaluation. The elements are processed in reverse order. |
|
884 |
If none of the evaluations returns true, report an error" |
|
885 |
||
886 |
^ self detectLast:aBlock ifNone:[self errorNotFound] |
|
887 |
||
888 |
" |
|
889 |
#(1 2 3 4) detectLast:[:n | n odd] |
|
890 |
#(2 4 6 8) detectLast:[:n | n odd] |
|
891 |
" |
|
892 |
! |
|
893 |
||
894 |
detectLast:aBlock ifNone:exceptionBlock |
|
895 |
"evaluate the argument, aBlock for each element in the receiver until |
|
896 |
the block returns true; in this case return the element which caused |
|
897 |
the true evaluation. The elements are processed in reverse order. |
|
898 |
If none of the evaluations returns true, return the result of the |
|
899 |
evaluation of the exceptionBlock" |
|
900 |
||
901 |
self reverseDo:[:each | |
|
902 |
(aBlock value:each) ifTrue:[^ each]. |
|
903 |
]. |
|
904 |
^ exceptionBlock value |
|
905 |
||
906 |
" |
|
907 |
#(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry'] |
|
908 |
#(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry'] |
|
909 |
" |
|
910 |
! |
|
911 |
||
602 | 912 |
do:aBlock |
913 |
"evaluate the argument, aBlock for each element" |
|
914 |
||
915 |
^ self subclassResponsibility |
|
916 |
! |
|
917 |
||
1212 | 918 |
doWithExit:aBlock |
919 |
"evaluate the argument, aBlock for each element. |
|
920 |
Passes an additional exit object, which can be used to leave |
|
921 |
the loop early, by sending it a #value: message. |
|
922 |
Returns nil or the value passed to the exit>>value: message. |
|
923 |
||
924 |
Notice, that this is different to a return from the block, which |
|
925 |
returns from the enclosed method, NOT only from the block." |
|
926 |
||
927 |
^ [:exit | |
|
928 |
self do:[:el | |
|
929 |
aBlock value:el value:exit |
|
930 |
]. |
|
931 |
nil |
|
932 |
] valueWithExit. |
|
933 |
||
934 |
"Example: |
|
935 |
search for the first element which is >= 99; return it. |
|
936 |
remaining elements are not processed: |
|
937 |
||
938 |
#(1 2 3 4 999 5 6 7 8 9) |
|
939 |
doWithExit:[:element :exit | |
|
1422 | 940 |
Transcript showCR:element. |
1212 | 941 |
element >= 99 ifTrue:[exit value:element]] |
942 |
||
943 |
#(1 2 3 4 5 6 7 8 9) |
|
944 |
doWithExit:[:element :exit | |
|
1422 | 945 |
Transcript showCR:element. |
1212 | 946 |
element >= 99 ifTrue:[exit value:element]] |
947 |
" |
|
948 |
||
949 |
"to demonstrate the difference to returning from the block: |
|
950 |
this works: |
|
951 |
||
952 |
|el| |
|
953 |
||
954 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
955 |
doWithExit:[:element :exit | |
|
956 |
element >= 99 ifTrue:[exit value:element]]. |
|
1422 | 957 |
Transcript showCR:el. |
1212 | 958 |
|
959 |
||
1422 | 960 |
this does NOT work as expected by a newComer ;-) (the showCR is not reached): |
1212 | 961 |
|
962 |
|el| |
|
963 |
||
964 |
el := #(1 2 3 4 999 5 6 7 8 9) |
|
965 |
do:[:element | |
|
966 |
element >= 99 ifTrue:[^ element]]. |
|
1422 | 967 |
Transcript showCR:el. |
1212 | 968 |
" |
969 |
||
970 |
"Modified: 18.4.1996 / 14:16:59 / cg" |
|
971 |
! |
|
972 |
||
1 | 973 |
inject:thisValue into:binaryBlock |
1262 | 974 |
"starting with thisValue for value, pass this value and each element |
975 |
to binaryBlock, replacing value with the result returned from the block |
|
976 |
in the next iteration." |
|
977 |
||
1 | 978 |
|nextValue| |
979 |
||
980 |
nextValue := thisValue. |
|
981 |
self do: [:each | nextValue := binaryBlock value:nextValue value:each]. |
|
982 |
^ nextValue |
|
68 | 983 |
|
1262 | 984 |
"sum up the elements of a collection: |
985 |
||
68 | 986 |
#(1 2 3 4) inject:0 into:[:accu :element | accu + element] |
987 |
(1 to:10) inject:0 into:[:accu :element | accu + element] |
|
1262 | 988 |
|
989 |
find the minimum: |
|
990 |
||
991 |
|coll| |
|
992 |
coll := #(1 99 -15 20 100). |
|
993 |
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element] |
|
68 | 994 |
" |
1262 | 995 |
|
996 |
"Modified: 23.4.1996 / 13:47:06 / cg" |
|
1 | 997 |
! |
998 |
||
602 | 999 |
keysAndValuesDo:aTwoArgBlock |
1000 |
"evaluate the argument, aBlock for every element in the collection, |
|
1001 |
passing both index and element as arguments." |
|
1002 |
||
1003 |
^ self errorNotKeyed |
|
1004 |
! |
|
1005 |
||
1359 | 1006 |
keysAndValuesReverseDo:aTwoArgBlock |
1007 |
"evaluate the argument, aBlock in reverse order for every element in the collection, |
|
1008 |
passing both index and element as arguments." |
|
1009 |
||
1010 |
^ self errorNotKeyed |
|
1011 |
||
1012 |
"Created: 9.5.1996 / 00:58:24 / cg" |
|
1013 |
! |
|
1014 |
||
602 | 1015 |
pairsDo:aBlock |
1016 |
"evaluate the argument, aBlock for every element in the collection, |
|
1017 |
which is supposed to consist of 2-element collections. |
|
1018 |
The block is called with 2 arguments for each collection in the receiver." |
|
1019 |
||
1020 |
self do:[:aPair | |
|
1021 |
aBlock value:(aPair at:1) value:(aPair at:2). |
|
1022 |
] |
|
1023 |
" |
|
1024 |
#( (1 one) (2 two) (3 three) (4 four) (5 five) (6 six)) |
|
1422 | 1025 |
pairsDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
602 | 1026 |
|
1027 |
||
1028 |
#( (1 1) (1 2) (1 3) (1 4) (1 5)) |
|
1422 | 1029 |
pairsDo:[:x :y | Transcript showCR:x@y] |
602 | 1030 |
" |
1031 |
! |
|
1032 |
||
1 | 1033 |
reject:aBlock |
1034 |
"return a new collection with all elements from the receiver, for which |
|
1035 |
the argument aBlock evaluates to false" |
|
1036 |
||
1037 |
^ self select:[:element | (aBlock value:element) == false] |
|
68 | 1038 |
|
1039 |
" |
|
1040 |
#(1 2 3 4) reject:[:e | e odd] |
|
1041 |
(1 to:10) reject:[:e | e even] |
|
1042 |
" |
|
1 | 1043 |
! |
1044 |
||
602 | 1045 |
reverseDo:aBlock |
1046 |
"evaluate the argument, aBlock for each element in reverse order." |
|
1047 |
||
1048 |
"it could be defined in terms of do: - but very inefficient. |
|
1049 |
Better force programmer to define a better version ..." |
|
1050 |
||
1051 |
^ self subclassResponsibility |
|
1052 |
! |
|
1053 |
||
1 | 1054 |
select:aBlock |
1055 |
"return a new collection with all elements from the receiver, for which |
|
1056 |
the argument aBlock evaluates to true" |
|
1057 |
||
1058 |
|newCollection| |
|
1059 |
||
201 | 1060 |
newCollection := self species new. |
1 | 1061 |
self do:[:each | |
155 | 1062 |
(aBlock value:each) ifTrue:[newCollection add:each]. |
1 | 1063 |
]. |
1064 |
^ newCollection |
|
68 | 1065 |
|
1066 |
" |
|
1067 |
#(1 2 3 4) select:[:e | e odd] |
|
1068 |
(1 to:10) select:[:e | e even] |
|
1069 |
" |
|
1070 |
! |
|
1071 |
||
1072 |
select:aBlock ifNone:exceptionBlock |
|
1073 |
"try a new collection with all elements from the receiver, for which |
|
1074 |
the argument aBlock evaluates to true. If none of the elements passes |
|
1075 |
the check of aBlock, return the result of evaluating exceptionBlock." |
|
1076 |
||
1077 |
|newCollection| |
|
1078 |
||
1079 |
newCollection := self select:aBlock. |
|
1080 |
newCollection isEmpty ifTrue:[^ exceptionBlock value]. |
|
1081 |
^ newCollection |
|
1082 |
||
1083 |
" |
|
1084 |
#(1 2 3 4) select:[:e | e > 10] ifNone:['sorry'] |
|
1085 |
" |
|
12 | 1086 |
! |
1087 |
||
362 | 1088 |
triplesDo:aBlock |
1089 |
"evaluate the argument, aBlock for every element in the collection, |
|
1090 |
which is supposed to consist of 3-element collections. |
|
1091 |
The block is called with 3 arguments for each collection in the receiver." |
|
1092 |
||
1093 |
self do:[:aTriple | |
|
1094 |
aBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3). |
|
1095 |
] |
|
1096 |
" |
|
1097 |
#( (1 one eins) (2 two zwei) (3 three drei) (4 four vier) (5 five #'fünf') (6 six sechs)) |
|
1422 | 1098 |
triplesDo:[:num :sym1 :sym2 | Transcript show:num; space; show:sym1; space; showCR:sym2] |
362 | 1099 |
" |
1 | 1100 |
! |
1101 |
||
602 | 1102 |
with:aCollection collect:aTwoArgBlock |
1103 |
"evaluate the argument, aBlock for successive elements from |
|
1104 |
each the receiver and the argument, aSequenceableCollection; |
|
1105 |
The second argument, aBlock must be a two-argument block, which is |
|
1106 |
evaluated for each element-pair. |
|
1107 |
Collect the results and return a collection containing them. |
|
1108 |
This method fails if neither the receiver nor aCollection is |
|
1109 |
a sequenceable collection (i.e. implements numeric key access)." |
|
260 | 1110 |
|
602 | 1111 |
|index "{ Class: SmallInteger }" |
1112 |
newCollection| |
|
1 | 1113 |
|
602 | 1114 |
newCollection := self species new. |
1115 |
index := 1. |
|
1116 |
aCollection isSequenceable ifFalse:[ |
|
1117 |
self isSequenceable ifFalse:[ |
|
1118 |
^ self error:'neither collection is sequenceable'. |
|
1119 |
]. |
|
1120 |
aCollection do:[:element | |
|
1121 |
newCollection add:(aTwoArgBlock value:(self at:index) |
|
1122 |
value:element). |
|
1123 |
index := index + 1 |
|
1124 |
] |
|
1125 |
] ifTrue:[ |
|
1126 |
self do:[:element | |
|
1127 |
newCollection add:(aTwoArgBlock value:element |
|
1128 |
value:(aCollection at:index)). |
|
1129 |
index := index + 1 |
|
1130 |
] |
|
1131 |
]. |
|
1132 |
^ newCollection |
|
201 | 1133 |
|
1134 |
" |
|
602 | 1135 |
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] |
1136 |
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)] |
|
201 | 1137 |
" |
2 | 1138 |
! |
1139 |
||
602 | 1140 |
with:aCollection do:aTwoArgBlock |
1141 |
"evaluate the argument, aBlock for successive elements from |
|
1142 |
each the receiver and the argument, aSequenceableCollection. |
|
1143 |
The second argument, aBlock must be a two-argument block. |
|
1144 |
This method fails if neither the receiver nor aCollection is |
|
1145 |
a sequenceable collection (i.e. implements numeric key access)." |
|
1146 |
||
1147 |
|index "{ Class: SmallInteger }"| |
|
2 | 1148 |
|
602 | 1149 |
index := 1. |
1150 |
aCollection isSequenceable ifFalse:[ |
|
1151 |
self isSequenceable ifFalse:[ |
|
1152 |
^ self error:'neither collection is sequenceable'. |
|
1153 |
]. |
|
1154 |
aCollection do:[:element | |
|
1155 |
aTwoArgBlock value:(self at:index) value:element. |
|
1156 |
index := index + 1 |
|
1157 |
] |
|
1158 |
] ifTrue:[ |
|
1159 |
self do:[:element | |
|
1160 |
aTwoArgBlock value:element value:(aCollection at:index). |
|
1161 |
index := index + 1 |
|
1162 |
] |
|
1163 |
] |
|
201 | 1164 |
|
1165 |
" |
|
602 | 1166 |
(1 to:3) with:#(one two three) do:[:num :sym | |
1422 | 1167 |
Transcript showCR:(num->sym) |
602 | 1168 |
] |
1169 |
(1 to:3) with:#(one two three) asSet do:[:num :sym | |
|
1422 | 1170 |
Transcript showCR:(num->sym) |
602 | 1171 |
]. |
1172 |
(1 to:3) asSet with:#(one two three) do:[:num :sym | |
|
1422 | 1173 |
Transcript showCR:(num->sym) |
602 | 1174 |
] |
201 | 1175 |
" |
1 | 1176 |
! ! |
1177 |
||
602 | 1178 |
!Collection methodsFor:'error handling'! |
1179 |
||
1180 |
emptyCheck |
|
1181 |
"check if the receiver is empty; report an error if so" |
|
1182 |
||
1183 |
self size == 0 ifTrue:[ |
|
1184 |
^ self emptyCollectionError. |
|
1185 |
]. |
|
1186 |
! |
|
1187 |
||
1188 |
emptyCollectionError |
|
1189 |
"report an error that the operation is not allowed for |
|
1190 |
empty collections" |
|
1191 |
||
1192 |
^ EmptyCollectionSignal raise |
|
1193 |
! |
|
1194 |
||
1195 |
errorInvalidKey:aKey |
|
1196 |
"report an error that the given key was invalid" |
|
1197 |
||
1198 |
^ InvalidKeySignal raiseRequestWith:aKey |
|
1199 |
! |
|
1200 |
||
1201 |
errorNotKeyed |
|
1202 |
"report an error that keyed access methods are not allowed" |
|
1203 |
||
1204 |
^ self error:(self class name, 's do not respond to keyed accessing messages') |
|
1205 |
! |
|
1206 |
||
1207 |
errorValueNotFound:anObject |
|
1208 |
"report an error that an object was not found in the collection" |
|
1209 |
||
1210 |
^ ValueNotFoundSignal raiseRequestWith:anObject |
|
1211 |
! |
|
1212 |
||
1213 |
notEnoughElementsError |
|
1214 |
"report an error that the operation is not allowed, |
|
1215 |
since not enough elements are in the collection" |
|
1216 |
||
1217 |
^ NotEnoughElementsSignal raise |
|
1218 |
! ! |
|
1219 |
||
1220 |
!Collection methodsFor:'growing'! |
|
1221 |
||
1222 |
grow |
|
1223 |
"make the receiver larger" |
|
1224 |
||
1225 |
self grow:(self size + self growSize) |
|
1226 |
! |
|
1227 |
||
1228 |
grow:howBig |
|
1229 |
"change the receivers size" |
|
1230 |
||
1231 |
^ self subclassResponsibility |
|
1232 |
! |
|
1233 |
||
1234 |
growSize |
|
1235 |
"return a suitable size increment for growing. |
|
1236 |
The default returned here may be (and is) redefined in subclasses." |
|
1237 |
||
1238 |
^ self size max:2 |
|
1239 |
! ! |
|
1240 |
||
1 | 1241 |
!Collection methodsFor:'printing & storing'! |
1242 |
||
602 | 1243 |
displayString |
1244 |
"return a printed representation of the receiver for display in inspectors etc." |
|
1245 |
||
1246 |
^ self printOrDisplayStringUsing:#displayString |
|
1247 |
! |
|
1248 |
||
1 | 1249 |
maxPrint |
169 | 1250 |
"the print-limit; printOn: will try to not produce more output |
88 | 1251 |
than the limit defined here." |
1252 |
||
1 | 1253 |
^ 5000 |
1254 |
! |
|
1255 |
||
1256 |
printOn:aStream |
|
88 | 1257 |
"append a user readable representation of the receiver to aStream. |
1258 |
The text appended is not meant to be read back for reconstruction of |
|
169 | 1259 |
the receiver. Also, this method limits the size of generated string. |
88 | 1260 |
" |
1261 |
||
169 | 1262 |
|limit firstOne string| |
1 | 1263 |
|
44 | 1264 |
thisContext isRecursive ifTrue:[ |
2289 | 1265 |
'Collection [error]: printOn: of self referencing collection.' errorPrintCR. |
1413 | 1266 |
aStream nextPutAll:'#("recursive")'. |
1267 |
^ self |
|
44 | 1268 |
]. |
1269 |
||
1270 |
limit := self maxPrint. |
|
1 | 1271 |
aStream nextPutAll:self class name. |
1272 |
aStream nextPut:$(. |
|
1273 |
firstOne := true. |
|
169 | 1274 |
|
1 | 1275 |
self do:[:element | |
1413 | 1276 |
firstOne ifFalse:[ |
1277 |
aStream space |
|
1278 |
] ifTrue:[ |
|
1279 |
firstOne := false |
|
1280 |
]. |
|
1281 |
(limit <= 0) ifTrue:[ |
|
1282 |
aStream nextPutAll:'...etc...)'. |
|
1283 |
^ self |
|
1284 |
] ifFalse:[ |
|
92 | 1285 |
"/ old code, worked only on positionable streams |
1286 |
"/ |
|
1287 |
"/ pos1 := aStream position. |
|
1288 |
"/ element printOn:aStream. |
|
1289 |
"/ limit := limit - (aStream position - pos1) |
|
169 | 1290 |
|
1413 | 1291 |
string := element printString. |
1292 |
aStream nextPutAll:string. |
|
1293 |
limit := limit - string size. |
|
1294 |
]. |
|
1 | 1295 |
]. |
1296 |
aStream nextPut:$) |
|
44 | 1297 |
|
88 | 1298 |
" |
1299 |
#(1 2 3 'hello' $a) printOn:Transcript |
|
169 | 1300 |
(Array new:100000) printOn:Transcript |
1301 |
(Array new:100000) printString size |
|
88 | 1302 |
(Dictionary new at:#hello put:'world'; yourself) printOn:Transcript |
169 | 1303 |
" |
1304 |
" |
|
88 | 1305 |
|a| |
1306 |
a := Array new:3. |
|
1307 |
a at:2 put:a. |
|
1308 |
a printOn:Transcript |
|
1309 |
" |
|
1413 | 1310 |
|
2289 | 1311 |
"Modified: 28.1.1997 / 00:39:17 / cg" |
1 | 1312 |
! |
1313 |
||
602 | 1314 |
printOrDisplayStringUsing:aSelector |
1315 |
"common code for printString and displayString; they only differ in |
|
1316 |
the print-message sent to the elements" |
|
1317 |
||
1318 |
|thisString buffer count string noneYet total limit| |
|
1319 |
||
1320 |
thisContext isRecursive ifTrue:[ |
|
2289 | 1321 |
'Collection [error]: print/storeString of self referencing collection.' errorPrintCR. |
1413 | 1322 |
^ '#("recursive")' |
602 | 1323 |
]. |
1324 |
||
1325 |
string := (self class name) , '('. |
|
1326 |
noneYet := true. |
|
1327 |
buffer := ''. |
|
1328 |
count := 0. |
|
1329 |
total := 0. |
|
1330 |
limit := self maxPrint. |
|
1331 |
||
1332 |
self do: [:element | |
|
1413 | 1333 |
thisString := element perform:aSelector. |
1334 |
noneYet ifTrue:[ |
|
1335 |
noneYet := false. |
|
1336 |
buffer := buffer , thisString |
|
1337 |
] ifFalse:[ |
|
1338 |
buffer := buffer , (' ' , thisString) |
|
1339 |
]. |
|
1340 |
count := count + 1. |
|
1341 |
(count == 20) ifTrue:[ |
|
1342 |
string := string , buffer. |
|
1343 |
buffer := ''. |
|
1344 |
count := 0 |
|
1345 |
]. |
|
1346 |
total := total + 1. |
|
1347 |
(total > limit) ifTrue:[ |
|
1348 |
string := string , buffer , '... )'. |
|
1349 |
^ string |
|
1350 |
] |
|
602 | 1351 |
]. |
1352 |
string := string , buffer , ')'. |
|
1353 |
^ string |
|
1413 | 1354 |
|
2289 | 1355 |
"Modified: 28.1.1997 / 00:39:07 / cg" |
602 | 1356 |
! |
1357 |
||
1 | 1358 |
storeOn:aStream |
88 | 1359 |
"output a printed representation onto the argument, aStream. |
1360 |
The text can be re-read to reconstruct (a copy of) the receiver. |
|
1361 |
Recursive (i.e. cyclic) collections cannot be stored correctly |
|
1362 |
(use storeBinaryOn: to handle those)." |
|
1 | 1363 |
|
1364 |
|isEmpty| |
|
1365 |
||
44 | 1366 |
thisContext isRecursive ifTrue:[ |
2289 | 1367 |
'Collection [error]: storeOn: of self referencing collection.' errorPrintCR. |
1413 | 1368 |
aStream nextPutAll:'#recursive'. |
1369 |
^ self |
|
44 | 1370 |
]. |
1371 |
||
1372 |
aStream nextPutAll:'('. |
|
1 | 1373 |
aStream nextPutAll:(self class name). |
44 | 1374 |
aStream nextPutAll:' new'. |
1 | 1375 |
isEmpty := true. |
1376 |
self do:[:element | |
|
1413 | 1377 |
aStream nextPutAll:' add:'. |
1378 |
element storeOn:aStream. |
|
1379 |
aStream nextPutAll:';'. |
|
1380 |
isEmpty := false |
|
1 | 1381 |
]. |
1382 |
isEmpty ifFalse:[aStream nextPutAll:' yourself']. |
|
1383 |
aStream nextPut:$) |
|
44 | 1384 |
|
88 | 1385 |
" |
1386 |
OrderedCollection new storeOn:Transcript |
|
1387 |
(1 to:10) storeOn:Transcript |
|
1388 |
(Set new add:1; add:'hello'; yourself) storeOn:Transcript |
|
1389 |
" |
|
293 | 1390 |
" |
1391 |
|s| |
|
1392 |
||
1393 |
s := Set new. |
|
1394 |
s add:1; add:'hello'; add:s. |
|
1395 |
s storeOn:Transcript |
|
1396 |
" |
|
1413 | 1397 |
|
2289 | 1398 |
"Modified: 28.1.1997 / 00:38:57 / cg" |
1 | 1399 |
! ! |
602 | 1400 |
|
1401 |
!Collection methodsFor:'queries'! |
|
1402 |
||
1403 |
isCollection |
|
1404 |
"return true, if the receiver is some kind of collection; |
|
1405 |
true is returned here - the method is redefined from Object." |
|
1406 |
||
1407 |
^ true |
|
1408 |
! |
|
1409 |
||
1174 | 1410 |
isSorted |
1411 |
"return true, if the receiver is sorted. |
|
1412 |
Collections which hold their elements in sorted order |
|
1413 |
should return true. Some algorithms (quicksort) degenerate when |
|
1414 |
operating on sorted collections and can be avoided if this information |
|
1415 |
is given. The default returned here (false) should not hurt. |
|
1416 |
I.e. you should NEVER depend on that in your application." |
|
1417 |
||
1418 |
^ false |
|
1419 |
||
1420 |
"Created: 13.4.1996 / 12:35:55 / cg" |
|
1421 |
! |
|
1422 |
||
602 | 1423 |
longestCommonPrefix |
1246 | 1424 |
"return the longest common prefix of my elements. |
1425 |
Typically used with string collections." |
|
1426 |
||
602 | 1427 |
|try allMatching matchLen| |
1428 |
||
1429 |
" |
|
1430 |
find the longest common prefix |
|
1431 |
" |
|
1432 |
matchLen := 0. |
|
1433 |
self do:[:aName | |
|
1246 | 1434 |
aName size > matchLen ifTrue:[ |
1435 |
matchLen := aName size. |
|
1436 |
try := aName |
|
1437 |
] |
|
602 | 1438 |
]. |
1439 |
||
1440 |
allMatching := true. |
|
1441 |
||
1442 |
[true] whileTrue:[ |
|
1246 | 1443 |
allMatching := true. |
1444 |
self do:[:aName | |
|
1445 |
(aName startsWith:try) ifFalse:[ |
|
1446 |
allMatching := false |
|
1447 |
] |
|
1448 |
]. |
|
1449 |
allMatching ifTrue:[ |
|
1450 |
^ try |
|
1451 |
]. |
|
1452 |
matchLen := matchLen - 1. |
|
2434
0a84bd4173c2
longestCommonPrefix - failed if there was no common prefix at all.
Claus Gittinger <cg@exept.de>
parents:
2383
diff
changeset
|
1453 |
matchLen == 0 ifTrue:[^ '']. |
1246 | 1454 |
try := try copyTo:matchLen. |
602 | 1455 |
]. |
1456 |
^ try |
|
1457 |
||
1458 |
" |
|
1459 |
#('Array' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1460 |
#('Arra' 'ArrayedCollection' 'ArrayOfFoo') longestCommonPrefix |
|
1461 |
#('Arra' 'b' 'c') longestCommonPrefix |
|
1462 |
#( (1 2 3 4) (1 2 3) (1 2 3 7) (1 2 3 9 10 11)) longestCommonPrefix |
|
1463 |
" |
|
1246 | 1464 |
|
2434
0a84bd4173c2
longestCommonPrefix - failed if there was no common prefix at all.
Claus Gittinger <cg@exept.de>
parents:
2383
diff
changeset
|
1465 |
"Modified: 2.3.1997 / 00:21:41 / cg" |
602 | 1466 |
! ! |
1467 |
||
1468 |
!Collection methodsFor:'testing'! |
|
1469 |
||
1470 |
capacity |
|
1471 |
"return the number of elements, that the receiver is |
|
1472 |
prepared to take. For most collections, this is the actual |
|
1473 |
size. However, some have more space preallocated to allow |
|
1474 |
for faster adding of elements. |
|
1475 |
Not used by the system; added for ST-80 compatibility." |
|
1476 |
||
1477 |
^ self size |
|
1478 |
! |
|
1479 |
||
1480 |
includes:anElement |
|
1481 |
"return true, if an object equal to the argument, anObject is in the list. |
|
1482 |
This compares using #= (i.e. it does not look for the object itself, |
|
1483 |
instead, one that compares equal). |
|
1484 |
See #includesIdentical: when identity is asked for." |
|
1485 |
||
1486 |
self do:[:element | |
|
1487 |
(anElement = element) ifTrue:[^ true]. |
|
1488 |
]. |
|
1489 |
^ false |
|
1490 |
! |
|
1491 |
||
1492 |
includesAll:aCollection |
|
1493 |
"return true, if the the receiver includes all elements of |
|
1494 |
the argument, aCollection; false if any is missing. |
|
1495 |
Notice: this method has O-square runtime behavior and may be |
|
1496 |
slow for big receivers/args. Think about using a Set, |
|
1497 |
or Dictionary." |
|
1498 |
||
1499 |
aCollection do:[:element | |
|
1500 |
(self includes:element) ifFalse:[^ false]. |
|
1501 |
]. |
|
1502 |
^ true |
|
1503 |
||
1504 |
" |
|
1505 |
#(1 2 3 4 5 6 7) includesAll:#(1 2 3) |
|
1506 |
#('hello' 'there' 'world') includesAll:#('hello' 'world') |
|
1507 |
#(1 2 3 4 5 6 7) includesAll:#(7 8 9) |
|
1508 |
" |
|
1509 |
! |
|
1510 |
||
1511 |
includesAny:aCollection |
|
1512 |
"return true, if the the receiver includes any elements of |
|
1513 |
the argument, aCollection; false if it includes none. |
|
1514 |
Notice: this method has O-square runtime behavior and may be |
|
1515 |
slow for big receivers/args. Think about using a Set, |
|
1516 |
or Dictionary. Speedup is possible, by arrangy highly |
|
1517 |
probable elements towards the beginning of aCollection, |
|
1518 |
to avoid useless searches." |
|
1519 |
||
1520 |
aCollection do:[:element | |
|
1521 |
(self includes:element) ifTrue:[^ true]. |
|
1522 |
]. |
|
1523 |
^ false |
|
1524 |
||
1525 |
" |
|
1526 |
#(1 2 3 4 5 6 7) includesAny:#(1 2 3) |
|
1527 |
#('hello' 'there' 'world') includesAny:#('hello' 'world') |
|
1528 |
#(1 2 3 4 5 6 7) includesAny:#(7 8 9) |
|
1529 |
#(1 2 3 4 5 6 7) includesAny:#(8 9 10) |
|
1530 |
" |
|
1531 |
! |
|
1532 |
||
1533 |
includesIdentical:anElement |
|
1534 |
"return true, if the argument, anObject is in the list. |
|
1535 |
This compares using #== (i.e. object identity). |
|
1536 |
See #includes: when equality is asked for." |
|
1537 |
||
1538 |
self do:[:element | |
|
1539 |
(anElement == element) ifTrue:[^ true]. |
|
1540 |
]. |
|
1541 |
^ false |
|
1542 |
! |
|
1543 |
||
1544 |
isEmpty |
|
1545 |
"return true, if the receiver is empty" |
|
1546 |
||
1547 |
^ self size == 0 |
|
1548 |
! |
|
1549 |
||
1550 |
max |
|
2097 | 1551 |
"return the maximum value in the receiver collection, |
1552 |
using #> to compare elements." |
|
602 | 1553 |
|
1554 |
^ self inject:nil |
|
2097 | 1555 |
into:[:maxSoFar :this | |
1556 |
(maxSoFar isNil |
|
1557 |
or:[this > maxSoFar]) ifTrue:[this] |
|
1558 |
ifFalse:[maxSoFar]] |
|
602 | 1559 |
|
1560 |
" |
|
1561 |
#(15 1 -9 10 5) max |
|
1562 |
(1 to:15) max |
|
1563 |
" |
|
2097 | 1564 |
|
1565 |
"Modified: 8.1.1997 / 22:09:37 / cg" |
|
602 | 1566 |
! |
1567 |
||
1568 |
min |
|
2097 | 1569 |
"return the minimum value in the receiver collection, |
1570 |
using < to compare elements." |
|
602 | 1571 |
|
1572 |
^ self inject:nil |
|
2097 | 1573 |
into:[:minSoFar :this | |
1574 |
(minSoFar isNil |
|
1575 |
or:[this < minSoFar]) ifTrue:[this] |
|
1576 |
ifFalse:[minSoFar]] |
|
602 | 1577 |
|
1578 |
" |
|
1579 |
#(15 1 -9 10 5) min |
|
1580 |
(1 to:15) min |
|
1581 |
" |
|
2097 | 1582 |
|
1583 |
"Modified: 8.1.1997 / 22:09:49 / cg" |
|
602 | 1584 |
! |
1585 |
||
1586 |
notEmpty |
|
1587 |
"return true, if the receiver is not empty" |
|
1588 |
||
1589 |
^ self isEmpty not |
|
1590 |
! |
|
1591 |
||
1592 |
occurrencesOf:anElement |
|
1593 |
"return the number of occurrences of the argument, anElement in |
|
1594 |
the receiver" |
|
1595 |
||
1596 |
|count "{ Class: SmallInteger }" | |
|
1597 |
||
1598 |
count := 0. |
|
1599 |
self do:[:element | |
|
1600 |
(anElement = element) ifTrue:[ |
|
1601 |
count := count + 1 |
|
1602 |
]. |
|
1603 |
]. |
|
1604 |
^ count |
|
1605 |
! |
|
1606 |
||
1607 |
size |
|
1608 |
"return the number of elements in the receiver. |
|
1609 |
This is usually redefined in subclasses for more performance." |
|
1610 |
||
1611 |
|count "{ Class: SmallInteger }" | |
|
1612 |
||
1613 |
count := 0. |
|
1614 |
self do:[:element | |
|
1615 |
count := count + 1 |
|
1616 |
]. |
|
1617 |
^ count |
|
1618 |
! ! |
|
1619 |
||
2097 | 1620 |
!Collection class methodsFor:'documentation'! |
628 | 1621 |
|
1622 |
version |
|
2434
0a84bd4173c2
longestCommonPrefix - failed if there was no common prefix at all.
Claus Gittinger <cg@exept.de>
parents:
2383
diff
changeset
|
1623 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.68 1997-03-01 23:23:00 cg Exp $' |
628 | 1624 |
! ! |
602 | 1625 |
Collection initialize! |