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