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