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