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