author | Claus Gittinger <cg@exept.de> |
Fri, 24 Jan 1997 19:16:48 +0100 | |
changeset 2256 | ffd565c6e084 |
parent 2165 | 9ef030343739 |
child 2275 | 882e11d07295 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 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 |
Collection subclass:#SequenceableCollection |
|
911
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
14 |
instanceVariableNames:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
15 |
classVariableNames:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
16 |
poolDictionaries:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
17 |
category:'Collections-Abstract' |
1 | 18 |
! |
19 |
||
2069 | 20 |
!SequenceableCollection class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
SequenceableCollections have ordered elements which can be accessed via |
|
39 |
a numeric index. |
|
40 |
SequenceableCollection is an abstract class - there are no instances of |
|
1385 | 41 |
it in the system. |
42 |
||
43 |
The methods found here assume that implementations of (at least) |
|
44 |
#at:/#at:put: are implemented (which are always found in Object for |
|
45 |
indexable objects). |
|
46 |
||
47 |
For performance, some subclasses redefine more methods, knowing the details |
|
48 |
of how elements are stored. |
|
49 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
|
50 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this |
|
51 |
kind of tuneup. |
|
52 |
||
53 |
See concrete subclasses (such as OrderedCollection, Array or String). |
|
1289 | 54 |
|
55 |
[author:] |
|
56 |
Claus Gittinger |
|
1385 | 57 |
|
58 |
[see also:] |
|
59 |
OrderedCollection Array |
|
60 |
CharacterArray String |
|
88 | 61 |
" |
62 |
! ! |
|
1 | 63 |
|
2069 | 64 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 65 |
|
66 |
new:size withAll:element |
|
360 | 67 |
"return a new collection of size, where all elements are |
68 |
initialized to element." |
|
1 | 69 |
|
70 |
|newCollection| |
|
71 |
||
72 |
newCollection := self new:size. |
|
73 |
newCollection atAllPut:element. |
|
74 |
^ newCollection |
|
360 | 75 |
! |
76 |
||
77 |
withSize:size |
|
78 |
"return a new collection of size. |
|
79 |
For variable size collections, this is different from #new:, |
|
80 |
in that #new: creates an empty collection with preallocated size, |
|
81 |
while #withSize: creates a non empty one." |
|
82 |
||
83 |
|newCollection| |
|
84 |
||
85 |
newCollection := self new:size. |
|
86 |
newCollection grow:size. |
|
87 |
^ newCollection |
|
88 |
||
89 |
" |
|
90 |
(OrderedCollection new:10) inspect. |
|
91 |
(OrderedCollection withSize:10) inspect. |
|
92 |
(Array new:10) inspect. |
|
93 |
(Array withSize:10) inspect. |
|
94 |
" |
|
1 | 95 |
! ! |
96 |
||
97 |
!SequenceableCollection methodsFor:'accessing'! |
|
98 |
||
1366 | 99 |
after:anObject |
100 |
"return the element, after anObject. |
|
101 |
If anObject is not in the receiver, report an error. |
|
102 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
|
103 |
||
104 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
105 |
||
106 |
" |
|
107 |
#(4 3 2 1) asOrderedCollection after:3. |
|
108 |
#(4 3 2 1) asOrderedCollection after:5 |
|
109 |
#(4 3 2 1) asOrderedCollection after:1 |
|
110 |
" |
|
111 |
||
112 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
113 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
114 |
! |
|
115 |
||
116 |
after:anObject ifAbsent:exceptionBlock |
|
117 |
"return the element, after anObject. If there is no such element, |
|
118 |
return the value from exceptionBlock." |
|
119 |
||
120 |
^ self subclassResponsibility |
|
121 |
||
122 |
"Created: 10.5.1996 / 14:00:10 / cg" |
|
123 |
! |
|
124 |
||
32 | 125 |
at:index ifAbsent:exceptionBlock |
61 | 126 |
"return the element at index if valid. |
127 |
If the index is invalid, return the result of evaluating |
|
302 | 128 |
the exceptionblock. |
129 |
NOTICE: |
|
130 |
in ST-80, this message is only defined for Dictionaries, |
|
131 |
however, having a common protocol with indexed collections |
|
132 |
often simplifies things." |
|
32 | 133 |
|
134 |
((index < 1) or:[index > self size]) ifTrue:[ |
|
159 | 135 |
^ exceptionBlock value |
32 | 136 |
]. |
137 |
^ self at:index |
|
138 |
||
95 | 139 |
" |
140 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 141 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
142 |
||
95 | 143 |
(Dictionary with:(#foo -> #bar) |
159 | 144 |
with:(#frob -> #baz)) |
145 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 146 |
" |
362 | 147 |
! |
148 |
||
1366 | 149 |
before:anObject |
150 |
"return the element before the argument, anObject. |
|
151 |
If anObject is not in the receiver, report an error. |
|
152 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
|
153 |
||
154 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
155 |
||
156 |
" |
|
157 |
#(4 3 2 1) asOrderedCollection before:3. |
|
158 |
#(4 3 2 1) asOrderedCollection before:4 |
|
159 |
#(4 3 2 1) asOrderedCollection before:0 |
|
160 |
" |
|
161 |
||
162 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
163 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
164 |
! |
|
165 |
||
166 |
before:anObject ifAbsent:exceptionBlock |
|
167 |
"return the element, before anObject. If there is no such element, |
|
168 |
return the value from exceptionBlock." |
|
169 |
||
170 |
^ self subclassResponsibility |
|
171 |
||
172 |
"Created: 10.5.1996 / 14:03:53 / cg" |
|
173 |
! |
|
174 |
||
602 | 175 |
first |
176 |
"return the first element" |
|
177 |
||
178 |
^ self at:1 |
|
362 | 179 |
! |
180 |
||
181 |
keyAtEqualValue:value |
|
182 |
"return the index of a value. |
|
183 |
This is normally not used (use indexOf:), but makes the |
|
184 |
protocol more compatible with dictionaries." |
|
185 |
||
186 |
^ self indexOf:value |
|
187 |
! |
|
188 |
||
189 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
190 |
"return the index of a value. |
|
191 |
This is normally not used (use indexOf:), but makes the |
|
192 |
protocol more compatible with dictionaries." |
|
193 |
||
194 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 195 |
! |
196 |
||
197 |
keyAtValue:value |
|
198 |
"return the index of a value. |
|
199 |
This is normally not used (use indexOf:), but makes the |
|
200 |
protocol more compatible with dictionaries." |
|
201 |
||
202 |
^ self identityIndexOf:value |
|
203 |
! |
|
204 |
||
205 |
keyAtValue:value ifAbsent:exceptionBlock |
|
206 |
"return the index of a value. |
|
207 |
This is normally not used (use indexOf:), but makes the |
|
208 |
protocol more compatible with dictionaries." |
|
209 |
||
210 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
211 |
! |
|
212 |
||
213 |
last |
|
214 |
"return the last element" |
|
215 |
||
216 |
^ self at:(self size) |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
217 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
218 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
219 |
upTo:anElement |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
220 |
"return a new collection consisting of the receivers elements upTo |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
221 |
(but excluding) anElement. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
222 |
If anElement is not in the receiver, the returned collection |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
223 |
will consist of all elements of the receiver" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
224 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
225 |
|pos| |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
226 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
227 |
pos := self indexOf:anElement. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
228 |
pos == 0 ifTrue:[^ self copy]. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
229 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
230 |
^ self copyFrom:1 to:(pos - 1) |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
231 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
232 |
" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
233 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
234 |
'hello world' upTo:Character space |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
235 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
236 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
237 |
raises an error: |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
238 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
239 |
(Dictionary new |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
240 |
at:#foo put:'foo'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
241 |
at:#bar put:'bar'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
242 |
at:#baz put:'baz'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
243 |
yourself) upTo:#bar |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
244 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
245 |
" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
246 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
247 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
1 | 248 |
! ! |
249 |
||
602 | 250 |
!SequenceableCollection methodsFor:'adding & removing'! |
251 |
||
252 |
add:anObject |
|
253 |
"append the argument, anObject to the collection. |
|
254 |
Return the argument, anObject. |
|
255 |
||
256 |
Notice, that this is modifies the receiver NOT a copy. |
|
257 |
Also note, that it may be a slow operation for some collections, |
|
258 |
due to the grow:-message, which is inefficient for fixed size |
|
259 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
260 |
||
261 |
|newSize| |
|
262 |
||
263 |
newSize := self size + 1. |
|
264 |
self grow:newSize. |
|
265 |
self at:newSize put:anObject. |
|
266 |
^ anObject |
|
267 |
||
268 |
" |
|
269 |
|a| |
|
270 |
a := #(1 2 3 4 5 6 7 8). |
|
271 |
a add:'hello'. |
|
272 |
a |
|
273 |
" |
|
274 |
" |
|
275 |
|c| |
|
276 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
277 |
c add:'hello'. |
|
278 |
c |
|
279 |
" |
|
280 |
! |
|
281 |
||
282 |
add:anElement beforeIndex:index |
|
283 |
"insert the first argument, anObject into the collection before slot index. |
|
284 |
Return the receiver (sigh - ST-80 compatibility). |
|
285 |
||
286 |
Notice, that this is modifies the receiver NOT a copy. |
|
287 |
Also note, that it may be a slow operation for some collections, |
|
288 |
due to the grow:-message, which is inefficient for fixed size |
|
289 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
290 |
||
291 |
|newSize| |
|
292 |
||
293 |
newSize := self size + 1. |
|
294 |
self grow:newSize. |
|
295 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
296 |
self at:index put:anElement |
|
297 |
||
298 |
" |
|
299 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
300 |
" |
|
202 | 301 |
! |
302 |
||
602 | 303 |
addFirst:anObject |
304 |
"prepend the argument, anObject to the collection. |
|
305 |
Return the argument, anObject. |
|
306 |
||
307 |
Notice, that this is modifies the receiver NOT a copy. |
|
308 |
Also note, that it may be a slow operation for some collections, |
|
309 |
due to the grow:-message, which is inefficient for fixed size |
|
310 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
311 |
||
312 |
|newSize| |
|
313 |
||
314 |
newSize := self size + 1. |
|
315 |
self grow:newSize. |
|
316 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
317 |
self at:1 put:anObject. |
|
318 |
^ anObject |
|
319 |
||
320 |
" |
|
321 |
|a| |
|
322 |
a:= #(1 2 3 4 5 6 7 8). |
|
323 |
a addFirst:'hello'. |
|
324 |
a |
|
325 |
" |
|
326 |
" |
|
327 |
|c| |
|
328 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
329 |
c addFirst:'hello'. |
|
330 |
c |
|
331 |
" |
|
69 | 332 |
! |
333 |
||
602 | 334 |
remove:anElement ifAbsent:aBlock |
335 |
"search for anElement and, if present remove and return it. |
|
336 |
If not present, return the value of evaluating aBlock. |
|
337 |
||
338 |
Notice, that this is modifies the receiver NOT a copy. |
|
339 |
Also note, that it may be a slow operation for some collections, |
|
340 |
due to the grow:-message, which is inefficient for fixed size |
|
341 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
342 |
||
343 |
|any |
|
344 |
dstIndex "{ Class: SmallInteger }" |
|
345 |
sz "{ Class: SmallInteger }"| |
|
346 |
||
347 |
dstIndex := 1. |
|
348 |
any := false. |
|
349 |
sz := self size. |
|
350 |
1 to:sz do:[:srcIndex | |
|
351 |
(anElement = (self at:srcIndex)) ifTrue:[ |
|
352 |
any := true |
|
353 |
] ifFalse:[ |
|
354 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
355 |
self at:dstIndex put:(self at:srcIndex) |
|
356 |
]. |
|
357 |
dstIndex := dstIndex + 1 |
|
358 |
] |
|
359 |
]. |
|
360 |
any ifTrue:[ |
|
361 |
self grow:dstIndex - 1. |
|
362 |
^ anElement |
|
363 |
]. |
|
364 |
^ aBlock value |
|
365 |
||
366 |
" |
|
1422 | 367 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 368 |
#(1 2 3 4 5 6 7 8 9 0) remove:99 ifAbsent:[#oops] |
369 |
" |
|
370 |
! |
|
371 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
372 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
373 |
"remove all elements that meet a test criteria as specified in aBlock. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
374 |
The argument, aBlock is evaluated for successive elements and all those, |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
375 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
376 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
377 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
378 |
"/ this is a q&d implementation (possibly slow). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
379 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
380 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
381 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
382 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
383 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
384 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
385 |
[runIndex <= self size] whileTrue:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
386 |
element := self at:runIndex. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
387 |
(aBlock value:element) ifTrue:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
388 |
removed add:element. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
389 |
self removeAtIndex:runIndex |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
390 |
] ifFalse:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
391 |
runIndex := runIndex + 1 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
392 |
] |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
393 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
394 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
395 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
396 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
397 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
398 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
399 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 400 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
401 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
402 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
403 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
404 |
"Created: 11.5.1996 / 09:49:30 / cg" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
405 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
406 |
|
602 | 407 |
removeAtIndex:anIndex |
408 |
"remove the element stored at anIndex. Return the removed object. |
|
409 |
||
410 |
Notice, that this is modifies the receiver NOT a copy. |
|
411 |
Also note, that it may be a slow operation for some collections, |
|
412 |
due to the grow:-message, which is inefficient for fixed size |
|
413 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
414 |
||
415 |
|element| |
|
416 |
||
417 |
element := self at:anIndex. |
|
418 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
419 |
^ element |
|
420 |
||
421 |
" |
|
422 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
423 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
424 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
425 |
" |
|
13 | 426 |
! |
427 |
||
602 | 428 |
removeFirst |
429 |
"remove the first element of the receiver and return it. |
|
430 |
||
431 |
Notice, that this is modifies the receiver NOT a copy. |
|
432 |
Also note, that it may be a slow operation for some collections, |
|
433 |
due to the grow:-message, which is inefficient for fixed size |
|
434 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
435 |
||
436 |
^ self removeAtIndex:1 |
|
437 |
||
438 |
" |
|
439 |
|a| |
|
440 |
a := #(1 2 3 4 5 6). |
|
441 |
a removeFirst. |
|
442 |
a |
|
443 |
" |
|
13 | 444 |
! |
445 |
||
602 | 446 |
removeFromIndex:startIndex toIndex:endIndex |
447 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
448 |
Return the receiver. |
|
449 |
Returning the receiver is a historic leftover - it may at one |
|
450 |
time return a collection of the removed elements. |
|
451 |
||
452 |
Notice, that this is modifies the receiver - NOT a copy; |
|
453 |
therefore any other users of the receiver will also see this change. |
|
454 |
Also note, that it may be a slow operation for some collections, |
|
455 |
due to the grow:-message, which is inefficient for fixed size |
|
456 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
457 |
||
458 |
|size newSize| |
|
459 |
||
460 |
(endIndex >= (size := self size)) ifTrue:[ |
|
461 |
self grow:(startIndex - 1) |
|
462 |
] ifFalse:[ |
|
463 |
newSize := size - endIndex + startIndex - 1. |
|
464 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
465 |
self grow:newSize |
|
466 |
] |
|
467 |
||
468 |
"/ |newSize| |
|
469 |
"/ |
|
470 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
471 |
"/ newSize <= 0 ifTrue:[ |
|
472 |
"/ self grow:0 |
|
473 |
"/ ] ifFalse:[ |
|
474 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
475 |
"/ self grow:newSize |
|
476 |
"/ ] |
|
477 |
||
478 |
" |
|
479 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
|
480 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
481 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
482 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
483 |
" |
|
484 |
! |
|
485 |
||
486 |
removeIndex:index |
|
487 |
"remove the argument stored at index. Return the receiver. |
|
488 |
||
489 |
Notice, that this is modifies the receiver NOT a copy. |
|
490 |
Also note, that it may be a slow operation for some collections, |
|
491 |
due to the grow:-message, which is inefficient for fixed size |
|
492 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
493 |
||
494 |
self removeFromIndex:index toIndex:index |
|
495 |
||
496 |
" |
|
497 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
498 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
499 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
500 |
" |
|
77 | 501 |
! |
502 |
||
602 | 503 |
removeLast |
504 |
"remove the last element of the receiver and return it. |
|
505 |
||
506 |
Notice, that this is modifies the receiver NOT a copy. |
|
507 |
Also note, that it may be a slow operation for some collections, |
|
508 |
due to the grow:-message, which is inefficient for fixed size |
|
509 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
510 |
||
511 |
^ self removeAtIndex:(self size) |
|
512 |
||
513 |
" |
|
514 |
|a| |
|
515 |
a := #(1 2 3 4 5 6). |
|
516 |
a removeLast. |
|
517 |
a |
|
518 |
" |
|
519 |
! ! |
|
520 |
||
521 |
!SequenceableCollection methodsFor:'comparing'! |
|
522 |
||
523 |
= aCollection |
|
524 |
"return true if the receiver and aCollection represent collections |
|
525 |
with equal contents." |
|
526 |
||
527 |
|index "{ Class: SmallInteger }" |
|
528 |
stop "{ Class: SmallInteger }" | |
|
529 |
||
530 |
(aCollection == self) ifTrue:[^true]. |
|
531 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
532 |
||
533 |
stop := self size. |
|
534 |
stop == (aCollection size) ifFalse:[^false]. |
|
535 |
||
536 |
index := 1. |
|
537 |
[index <= stop] whileTrue:[ |
|
538 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
539 |
index := index + 1 |
|
540 |
]. |
|
541 |
^ true |
|
542 |
||
543 |
" |
|
544 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
|
545 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
546 |
#($1 $2 $3 $4 $5) = '12345' |
|
547 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
548 |
" |
|
549 |
! |
|
550 |
||
551 |
endsWith:aCollection |
|
552 |
"return true, if the receivers last elements match those |
|
553 |
of aCollection" |
|
554 |
||
555 |
|index1 "{ Class: SmallInteger }" |
|
556 |
index2 "{ Class: SmallInteger }" |
|
557 |
stop "{ Class: SmallInteger }" |
|
558 |
sz "{ Class: SmallInteger }"| |
|
559 |
||
560 |
(aCollection == self) ifTrue:[^ true]. |
|
561 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
562 |
||
563 |
stop := aCollection size. |
|
77 | 564 |
sz := self size. |
602 | 565 |
stop > sz ifTrue:[^false]. |
566 |
||
567 |
index1 := sz. |
|
568 |
index2 := stop. |
|
569 |
[index2 > 0] whileTrue:[ |
|
570 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
|
571 |
index1 := index1 - 1. |
|
572 |
index2 := index2 - 1 |
|
77 | 573 |
]. |
602 | 574 |
^ true |
575 |
||
576 |
" |
|
577 |
'abcde' endsWith:#($d $e) |
|
578 |
#[1 2 3 4] endsWith:#(3 4) |
|
579 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
|
580 |
" |
|
581 |
! |
|
582 |
||
583 |
hash |
|
584 |
"return a hash key for the receiver" |
|
585 |
||
586 |
"this hash is stupid - but for larger collections, the hashing |
|
587 |
time can become much bigger than the time lost in added probing. |
|
588 |
Time will show ..." |
|
589 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
590 |
^ ((self at:1 ifAbsent:0) hash * self size) bitAnd:16r3FFFFFFF |
602 | 591 |
|
592 |
" |
|
593 |
#(1 2 3 4 5) hash |
|
594 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
595 |
" |
|
596 |
! |
|
597 |
||
598 |
startsWith:aCollection |
|
599 |
"return true, if the receivers first elements match those |
|
600 |
of aCollection" |
|
601 |
||
602 |
|index "{ Class: SmallInteger }" |
|
603 |
stop "{ Class: SmallInteger }" | |
|
604 |
||
605 |
(aCollection == self) ifTrue:[^true]. |
|
606 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
607 |
||
608 |
stop := aCollection size. |
|
609 |
stop > self size ifTrue:[^false]. |
|
610 |
||
611 |
index := 1. |
|
612 |
[index <= stop] whileTrue:[ |
|
613 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
614 |
index := index + 1 |
|
615 |
]. |
|
616 |
^ true |
|
617 |
||
618 |
" |
|
619 |
'abcde' startsWith:#($a $b $c) |
|
620 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
621 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
622 |
" |
|
13 | 623 |
! ! |
624 |
||
260 | 625 |
!SequenceableCollection methodsFor:'converting'! |
626 |
||
1413 | 627 |
asStringCollection |
628 |
"return a new string collection containing the elements; |
|
629 |
these ought to be strings. (i.e. String or Text instances)" |
|
630 |
||
631 |
|newColl sz| |
|
632 |
||
633 |
sz := self size. |
|
634 |
newColl := (StringCollection new:sz) grow:sz. |
|
635 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
|
636 |
^ newColl |
|
637 |
||
638 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
639 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
640 |
! |
|
641 |
||
1188 | 642 |
asStringWith:sepChar |
643 |
"return a string generated by concatenating my elements |
|
644 |
(which must be strings or nil) and embedding sepChar characters in between. |
|
645 |
Nil entries and empty strings are counted as empty lines." |
|
646 |
||
647 |
^ self asStringWith:sepChar from:1 to:(self size) |
|
1413 | 648 |
|
649 |
" |
|
650 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
651 |
" |
|
652 |
||
653 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 654 |
! |
655 |
||
656 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
657 |
"return part of myself as a string with embedded sepCharacters. |
|
658 |
My elements must be strings or nil; nil entries and empty strings are |
|
659 |
taken as empty lines." |
|
660 |
||
661 |
^ self |
|
662 |
asStringWith:sepCharacter |
|
663 |
from:firstLine |
|
664 |
to:lastLine |
|
665 |
compressTabs:false |
|
666 |
final:nil |
|
667 |
" |
|
668 |
creating entries for searchpath: |
|
669 |
||
670 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
|
671 |
||
672 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
673 |
" |
|
674 |
||
675 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
676 |
! |
|
677 |
||
260 | 678 |
asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
679 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 680 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 681 |
taken as empty lines. |
682 |
If the argument compressTabs is true, leading spaces are converted |
|
683 |
to tab-characters (8col tabs). The last line is followed by a final |
|
684 |
character (if non-nil). |
|
685 |
||
686 |
This method is tuned for big collections, in not creating many |
|
263 | 687 |
intermediate strings (has linear runtime). For very small collections |
688 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 689 |
|
293 | 690 |
|idx1 "{ Class:SmallInteger }" |
691 |
idx2 "{ Class:SmallInteger }" |
|
692 |
totalLength "{ Class:SmallInteger }" |
|
260 | 693 |
pos "{ Class:SmallInteger }" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
694 |
newString lineString spaces idx nTabs sepCnt |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
695 |
any16Bit stringClass needEmphasis newRuns c |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
696 |
thisLen anyTab| |
260 | 697 |
|
698 |
" |
|
699 |
first accumulate the size of the string, to avoid |
|
700 |
countless reallocations. If tabs are compressed, |
|
701 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 702 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 703 |
" |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
704 |
any16Bit := needEmphasis := false. |
1015 | 705 |
stringClass := String. |
706 |
||
260 | 707 |
totalLength := 0. |
708 |
sepCnt := sepCharacter notNil ifTrue:[1] ifFalse:[0]. |
|
709 |
||
293 | 710 |
idx1 := firstLine. |
711 |
idx2 := lastLine. |
|
712 |
idx1 to:idx2 do:[:lineIndex | |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
713 |
lineString := self at:lineIndex. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
714 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
715 |
lineString isNil ifTrue:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
716 |
totalLength := totalLength + sepCnt |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
717 |
] ifFalse: [ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
718 |
lineString hasChangeOfEmphasis ifTrue:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
719 |
needEmphasis := true |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
720 |
]. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
721 |
(lineString bitsPerCharacter == 16) ifTrue:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
722 |
any16Bit := true. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
723 |
stringClass := lineString class |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
724 |
]. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
725 |
totalLength := totalLength + lineString size + sepCnt |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
726 |
]. |
260 | 727 |
]. |
728 |
endCharacter isNil ifTrue:[ |
|
1673
eea933dbf42d
care for empty collection in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
1537
diff
changeset
|
729 |
totalLength := totalLength - 1. |
eea933dbf42d
care for empty collection in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
1537
diff
changeset
|
730 |
totalLength < 0 ifTrue:[^ '']. |
260 | 731 |
]. |
732 |
spaces := ' '. |
|
1016 | 733 |
newString := stringClass new:totalLength. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
734 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
735 |
needEmphasis ifTrue:[ |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
736 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
737 |
]. |
260 | 738 |
|
739 |
" |
|
740 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
741 |
Be careful with runArrays: |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
742 |
replacing individual elements is VERY expensive. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
743 |
Therefore, create a new runArray from scratch. |
260 | 744 |
" |
745 |
pos := 1. |
|
293 | 746 |
idx1 to:idx2 do:[:lineIndex | |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
747 |
lineString := self at:lineIndex. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
748 |
thisLen := lineString size. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
749 |
thisLen ~~ 0 ifTrue:[ |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
750 |
(anyTab := compressTabs) ifTrue:[ |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
751 |
" |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
752 |
mhmh: could use withTabs from String-class here, |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
753 |
but we should avoid creating too many temporary strings |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
754 |
(especially, since this method is typically used when converting |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
755 |
big texts such as when saving in the filebrowser ...). |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
756 |
Therefore, we convert tabs inline here doing a direct replace |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
757 |
in newString." |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
758 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
759 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
760 |
nTabs := (idx-1) // 8. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
761 |
nTabs ~~ 0 ifTrue:[ |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
762 |
anyTab := true |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
763 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
764 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
765 |
anyTab ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
766 |
"any tabs" |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
767 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
768 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
769 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
770 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
771 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
772 |
newRuns add:nil withOccurrences:nTabs |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
773 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
774 |
pos := pos + nTabs. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
775 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
776 |
newString replaceFrom:pos with:lineString startingAt:idx. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
777 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
778 |
lineString hasChangeOfEmphasis ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
779 |
idx to:lineString size do:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
780 |
newRuns add:(lineString emphasisAt:pos) |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
781 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
782 |
] ifFalse:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
783 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
784 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
785 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
786 |
pos := pos + thisLen - (nTabs * 8). |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
787 |
] ifFalse:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
788 |
newString replaceFrom:pos with:lineString. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
789 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
790 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
791 |
lineString hasChangeOfEmphasis ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
792 |
newRuns addAll:(lineString emphasis) |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
793 |
] ifFalse:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
794 |
newRuns add:nil withOccurrences:lineString size |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
795 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
796 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
797 |
pos := pos + thisLen. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
798 |
]. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
799 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
800 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
801 |
lineIndex ~~ lastLine ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
802 |
c := sepCharacter |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
803 |
] ifFalse:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
804 |
c := endCharacter |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
805 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
806 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
807 |
c notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
808 |
newString at:pos put:c. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
809 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
810 |
newRuns add:nil. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
811 |
]. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
812 |
pos := pos + 1 |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
813 |
]. |
260 | 814 |
]. |
815 |
||
816 |
" |
|
817 |
in case of tab compression, the result has to be |
|
818 |
cut to size ... sorry |
|
819 |
" |
|
820 |
pos ~~ totalLength ifTrue:[ |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
821 |
newString := newString copyTo:(pos - 1) |
260 | 822 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
823 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
824 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
825 |
newString := Text string:newString runs:newRuns. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
826 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
827 |
|
260 | 828 |
^ newString |
829 |
||
830 |
" |
|
831 |
creating entries for searchpath: |
|
832 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
833 |
#('foo' 'bar' 'baz' '/foo/bar') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
834 |
asStringWith:$: |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
835 |
from:1 to:4 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
836 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
837 |
final:nil |
260 | 838 |
|
839 |
with trailing colon: |
|
840 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
841 |
#('foo' 'bar' 'baz' '/foo/bar') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
842 |
asStringWith:$: |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
843 |
from:1 to:4 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
844 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
845 |
final:$: |
260 | 846 |
|
847 |
concatenating all elements: |
|
848 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
849 |
#('foo' 'bar' 'baz') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
850 |
asStringWith:nil |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
851 |
from:1 to:3 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
852 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
853 |
final:nil |
260 | 854 |
|
855 |
creating a string from a collection of lines: |
|
856 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
857 |
#('foo' 'bar' 'baz') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
858 |
asStringWith:(Character cr) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
859 |
from:1 to:3 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
860 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
861 |
final:(Character cr) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
862 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
863 |
creating a text from a collection of mixed texts and strings: |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
864 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
865 |
(Array |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
866 |
with:'foo' |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
867 |
with:('bar' asText allBold) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
868 |
with:'baz' |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
869 |
with:('baz2' asText emphasizeAllWith:#italic) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
870 |
) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
871 |
asStringWith:(Character cr) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
872 |
from:1 to:4 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
873 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
874 |
final:(Character cr) |
260 | 875 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
876 |
|
1673
eea933dbf42d
care for empty collection in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
1537
diff
changeset
|
877 |
"Modified: 22.9.1996 / 17:35:23 / cg" |
379 | 878 |
! |
879 |
||
602 | 880 |
asStringWithCRs |
881 |
"return a string generated by concatenating my elements |
|
882 |
(which must be strings or nil) and embedding cr characters in between. |
|
883 |
Nil entries and empty strings are counted as empty lines." |
|
884 |
||
885 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
1413 | 886 |
|
887 |
" |
|
888 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
|
889 |
||
890 |
(OrderedCollection new |
|
891 |
add:'hello'; |
|
892 |
add:'world'; |
|
893 |
add:'foo'; |
|
894 |
add:('bar' asText allBold); |
|
895 |
yourself) asStringWithCRs |
|
1421 | 896 |
|
1422 | 897 |
Transcript showCR: |
1421 | 898 |
(OrderedCollection new |
899 |
add:'hello'; |
|
900 |
add:'world'; |
|
901 |
add:'foo'; |
|
902 |
add:('bar' asText allBold); |
|
903 |
yourself) asStringWithCRs |
|
1413 | 904 |
" |
905 |
||
1421 | 906 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
602 | 907 |
! |
908 |
||
909 |
asStringWithCRsFrom:firstLine to:lastLine |
|
910 |
"return a string generated by concatenating some of my elements |
|
911 |
(which must be strings or nil) and embedding cr characters in between. |
|
912 |
Nil entries and empty strings are counted as empty lines." |
|
913 |
||
914 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
1421 | 915 |
|
916 |
" |
|
917 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
|
918 |
||
919 |
" |
|
920 |
||
921 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
602 | 922 |
! |
923 |
||
924 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
925 |
"return part of myself as a string with embedded cr's. |
|
926 |
My elements must be strings or nil. |
|
927 |
If the argument compressTabs is true, leading spaces are converted |
|
928 |
to tab-characters (8col tabs). |
|
929 |
Nil entries and empty strings are taken as empty lines." |
|
930 |
||
931 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
932 |
! |
|
933 |
||
934 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
935 |
"return part of myself as a string with embedded cr's. |
|
936 |
My elements must be strings or nil; nil entries and empty strings are |
|
937 |
taken as empty lines. |
|
938 |
If the argument compressTabs is true, leading spaces are converted |
|
939 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
940 |
should be followed by a cr or not." |
|
941 |
||
942 |
^ self asStringWith:(Character cr) |
|
943 |
from:firstLine |
|
944 |
to:lastLine |
|
945 |
compressTabs:compressTabs |
|
946 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
947 |
! |
|
948 |
||
379 | 949 |
decodeAsLiteralArray |
1245 | 950 |
"given a literalEncoding in the receiver, |
951 |
create & return the corresponding object. |
|
952 |
The inverse operation to #literalArrayEncoding." |
|
953 |
||
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
954 |
|clsName cls| |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
955 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
956 |
clsName := self first. |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
957 |
cls := Smalltalk at:clsName ifAbsent:nil. |
379 | 958 |
cls isNil ifTrue:[ |
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
959 |
^ self error:'unknown class in spec: ' , clsName. |
379 | 960 |
]. |
961 |
^ cls new fromLiteralArrayEncoding:self. |
|
962 |
||
963 |
" |
|
964 |
#(Point 10 20) decodeAsLiteralArray |
|
965 |
#(Rectangle 100 200 400 500) decodeAsLiteralArray |
|
966 |
" |
|
1245 | 967 |
|
968 |
"Modified: 22.4.1996 / 12:59:03 / cg" |
|
422 | 969 |
! |
970 |
||
971 |
literalArrayEncoding |
|
1245 | 972 |
"encode myself as an array literal, from which a copy of the receiver |
973 |
can be reconstructed with #decodeAsLiteralArray." |
|
974 |
||
422 | 975 |
^ (self collect:[:el | el literalArrayEncoding]) asArray |
976 |
||
977 |
" |
|
978 |
(Array with:(Color red:50 green:50 blue:50) |
|
1245 | 979 |
with:(1 @ 2) |
422 | 980 |
) literalArrayEncoding |
981 |
" |
|
1245 | 982 |
|
983 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
260 | 984 |
! ! |
985 |
||
1 | 986 |
!SequenceableCollection methodsFor:'copying'! |
987 |
||
988 |
, aCollection |
|
989 |
"return a new collection formed from concatenating the receiver with |
|
44 | 990 |
the argument. The class of the new collection is determined by the |
991 |
receivers class, so mixing classes is possible, if the second collections |
|
992 |
elements can be stored into instances of the receivers class." |
|
1 | 993 |
|
994 |
|newCollection |
|
995 |
mySize "{ Class: SmallInteger }" |
|
996 |
newSize "{ Class: SmallInteger }" |
|
997 |
otherSize "{ Class: SmallInteger }" |
|
998 |
dstIndex "{ Class: SmallInteger }"| |
|
999 |
||
1000 |
mySize := self size. |
|
1001 |
otherSize := aCollection size. |
|
1002 |
newSize := mySize + otherSize. |
|
202 | 1003 |
|
252 | 1004 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 1005 |
|
1006 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
1007 |
dstIndex := mySize + 1. |
|
202 | 1008 |
aCollection isSequenceable ifTrue:[ |
1009 |
" |
|
1010 |
yes, aCollection has indexed elements |
|
1011 |
" |
|
159 | 1012 |
newCollection replaceFrom:dstIndex to:newSize |
1013 |
with:aCollection startingAt:1. |
|
1014 |
^ newCollection |
|
1 | 1015 |
] ifFalse:[ |
202 | 1016 |
" |
1017 |
no, enumerate aCollection |
|
1018 |
" |
|
159 | 1019 |
aCollection do:[:element | |
1020 |
newCollection at:dstIndex put:element. |
|
1021 |
dstIndex := dstIndex + 1 |
|
1022 |
] |
|
1 | 1023 |
]. |
1024 |
^ newCollection |
|
44 | 1025 |
|
95 | 1026 |
" |
1027 |
#($a $b $c) , #(1 2 3) |
|
1028 |
#($a $b $c) , '123' |
|
1029 |
'abc' , '123' |
|
1030 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
1031 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
1032 |
'abc' asArray , #(1 2 3 4 5) |
|
1033 |
" |
|
1 | 1034 |
! |
1035 |
||
602 | 1036 |
copyFirst:count |
1037 |
"return a new collection consisting of the receivers first count |
|
1038 |
elements - this is just a rename of copyTo: - for compatibility." |
|
1039 |
||
1040 |
^ self copyFrom:1 to:count |
|
1041 |
||
1042 |
" |
|
1043 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
1044 |
'1234567890' copyFirst:4 |
|
1045 |
" |
|
1046 |
! |
|
1047 |
||
1048 |
copyFrom:start |
|
1049 |
"return a new collection consisting of receivers elements |
|
1050 |
from start to the end of the collection" |
|
1051 |
||
1052 |
^ self copyFrom:start to:(self size) |
|
1053 |
||
1054 |
" |
|
1055 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
1056 |
'1234567890' copyFrom:2 |
|
1057 |
" |
|
1058 |
! |
|
1059 |
||
1060 |
copyFrom:start to:stop |
|
1061 |
"return a new collection consisting of receivers elements |
|
1062 |
between start and stop" |
|
1063 |
||
1064 |
|newCollection newSize| |
|
1065 |
||
1066 |
newSize := stop - start + 1. |
|
1067 |
newCollection := self copyEmptyAndGrow:newSize. |
|
1068 |
newCollection replaceFrom:1 to:newSize with:self startingAt:start. |
|
1069 |
^ newCollection |
|
1070 |
||
1071 |
" |
|
1072 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
1073 |
'1234567890' copyFrom:2 to:5 |
|
1074 |
" |
|
1075 |
! |
|
1076 |
||
1077 |
copyLast:count |
|
1078 |
"return a new collection consisting of the receivers last count |
|
1079 |
elements." |
|
1080 |
||
1081 |
|sz| |
|
1082 |
||
1083 |
sz := self size. |
|
1084 |
^ self copyFrom:(sz - count + 1) to:sz |
|
1085 |
||
1086 |
" |
|
1087 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
1088 |
'1234567890' copyLast:4 |
|
1089 |
" |
|
1090 |
! |
|
1091 |
||
1092 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
|
1093 |
"return a copy of the receiver, where the elements from startIndex to |
|
1094 |
endIndex have been replaced by the elements of aCollection" |
|
1095 |
||
1096 |
|newColl sz replSize| |
|
1097 |
||
1098 |
replSize := aCollection size. |
|
1099 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
1100 |
newColl := self copyEmptyAndGrow:sz. |
|
1101 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
1102 |
newColl replaceFrom:startIndex with:aCollection. |
|
1103 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
1104 |
^ newColl |
|
1105 |
||
1106 |
" |
|
1107 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 to:6 with:#(a b c d e f g h i j k) |
|
1108 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
|
1109 |
" |
|
1110 |
! |
|
1111 |
||
1112 |
copyReplaceFrom:startIndex with:aCollection |
|
1113 |
"return a copy of the receiver, where the elements from startIndex to |
|
1114 |
the end have been replaced by the elements of aCollection" |
|
1115 |
||
1116 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
1117 |
||
1118 |
" |
|
1119 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
|
1120 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
1121 |
" |
|
1122 |
! |
|
1123 |
||
1124 |
copyThrough:element |
|
1125 |
"return a new collection consisting of the receiver elements |
|
1126 |
up-to (AND including) the first occurence of element." |
|
1127 |
||
1128 |
|idx| |
|
1129 |
||
1130 |
idx := self indexOf:element. |
|
1131 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
1132 |
^ self copyFrom:1 to:idx |
|
1133 |
||
1134 |
" |
|
1135 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
1136 |
'1234567890' copyThrough:$5 |
|
1137 |
'1234567890' copyThrough:$a |
|
1138 |
'1234567890' copyThrough:$1 |
|
1139 |
" |
|
1140 |
! |
|
1141 |
||
1142 |
copyTo:stop |
|
1143 |
"return a new collection consisting of receivers elements |
|
1144 |
from 1 up to (including) index stop" |
|
1145 |
||
1146 |
^ self copyFrom:1 to:stop |
|
1147 |
||
1148 |
" |
|
1149 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
1150 |
'1234567890' copyTo:4 |
|
1151 |
" |
|
1152 |
! |
|
1153 |
||
1154 |
copyUpTo:element |
|
1155 |
"return a new collection consisting of the receiver elements |
|
1156 |
up-to (but excluding) the first occurence of element; |
|
1157 |
or to the end, if element is not included" |
|
1158 |
||
1159 |
|idx| |
|
1160 |
||
1161 |
idx := self indexOf:element. |
|
1162 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
1163 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
1164 |
^ self copyFrom:1 to:(idx-1) |
|
1165 |
||
1166 |
" |
|
1167 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
1168 |
'1234567890' copyUpTo:$5 |
|
1169 |
'1234567890' copyUpTo:$a |
|
1170 |
'1234567890' copyUpTo:$1 |
|
1171 |
'123456789' copyUpTo:$0 |
|
1172 |
" |
|
1173 |
! |
|
1174 |
||
1 | 1175 |
copyWith:newElement |
233 | 1176 |
"return a new collection containing the receivers elements |
1177 |
and the single new element, newElement. |
|
1178 |
This is different from concatentation, which expects another collection |
|
1179 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 1180 |
|
1181 |
|newCollection mySize newSize| |
|
1182 |
||
1183 |
mySize := self size. |
|
1184 |
newSize := mySize + 1. |
|
252 | 1185 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 1186 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
1187 |
newCollection at:newSize put:newElement. |
|
1188 |
^newCollection |
|
44 | 1189 |
|
95 | 1190 |
" |
1191 |
#(1 2 3 4 5) copyWith:$a |
|
1192 |
'abcdefg' copyWith:$h |
|
1193 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
1194 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
1195 |
" |
|
1 | 1196 |
! |
1197 |
||
61 | 1198 |
copyWithout:elementToSkip |
1199 |
"return a new collection consisting of a copy of the receiver, with |
|
1200 |
ALL elements equal to elementToSkip are left out. |
|
1201 |
No error is reported, if elementToSkip is not in the collection." |
|
1202 |
||
293 | 1203 |
|n "{ Class: SmallInteger }" |
1204 |
sz "{ Class: SmallInteger }" |
|
1205 |
srcIndex "{ Class: SmallInteger }" |
|
1206 |
dstIndex "{ Class: SmallInteger }" |
|
1207 |
skipIndex "{ Class: SmallInteger }" |
|
1208 |
copy l| |
|
61 | 1209 |
|
1210 |
"the code below may look like overkill, |
|
1211 |
however, for big collections its better to move data |
|
1212 |
around in big chunks" |
|
1213 |
||
1214 |
n := self occurrencesOf:elementToSkip. |
|
1215 |
n == 0 ifTrue:[^ self copy]. |
|
1216 |
||
1217 |
sz := self size. |
|
252 | 1218 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 1219 |
|
1220 |
srcIndex := 1. |
|
1221 |
dstIndex := 1. |
|
1222 |
||
1223 |
n timesRepeat:[ |
|
159 | 1224 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
1225 |
l := skipIndex - srcIndex. |
|
1226 |
l ~~ 0 ifTrue:[ |
|
1227 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
1228 |
with:self startingAt:srcIndex. |
|
1229 |
dstIndex := dstIndex + l |
|
1230 |
]. |
|
1231 |
srcIndex := skipIndex + 1 |
|
61 | 1232 |
]. |
1233 |
l := sz - srcIndex. |
|
1234 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
159 | 1235 |
with:self startingAt:srcIndex. |
61 | 1236 |
^ copy |
1237 |
||
95 | 1238 |
" |
1239 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
1240 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
1241 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
202 | 1242 |
'abcdefghi' copyWithout:$h |
1243 |
'abcdefg' copyWithout:$h |
|
95 | 1244 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
1245 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
1246 |
#(90 80 70 60 50) copyWithout:70 |
|
1247 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
1248 |
" |
|
61 | 1249 |
! |
1250 |
||
602 | 1251 |
copyWithoutFirst:elementToSkip |
1252 |
"return a new collection consisting of a copy of the receivers elements |
|
1253 |
without the first elementToSkip, if it was present. |
|
1254 |
No error is reported, if elementToSkip is not in the collection." |
|
1255 |
||
1256 |
|copy skipIndex sz| |
|
1257 |
||
1258 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
1259 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
1260 |
||
1261 |
sz := self size - 1. |
|
1262 |
copy := self copyEmptyAndGrow:sz. |
|
1263 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
1264 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
1265 |
^ copy |
|
1266 |
||
1267 |
" |
|
1268 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
1269 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
1270 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
1271 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
1272 |
" |
|
1273 |
! |
|
1274 |
||
61 | 1275 |
copyWithoutIndex:omitIndex |
1276 |
"return a new collection consisting of receivers elements |
|
1277 |
without the argument stored at omitIndex" |
|
1278 |
||
1279 |
|copy sz| |
|
1280 |
||
202 | 1281 |
sz := self size - 1. |
252 | 1282 |
copy := self copyEmptyAndGrow:sz. |
293 | 1283 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
1284 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 1285 |
^ copy |
1286 |
||
95 | 1287 |
" |
1288 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
1289 |
'abcdefghijkl' copyWithoutIndex:5 |
|
1290 |
" |
|
1 | 1291 |
! |
1292 |
||
202 | 1293 |
copyWithoutLast:count |
1294 |
"return a new collection consisting of the receivers elements |
|
1295 |
except the last count elements." |
|
1296 |
||
1297 |
|sz| |
|
1298 |
||
1299 |
sz := self size. |
|
1300 |
^ self copyTo:(sz - count) |
|
1301 |
||
1302 |
" |
|
1303 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
|
1304 |
'1234567890' copyWithoutLast:4 |
|
1305 |
" |
|
602 | 1306 |
! ! |
1307 |
||
1308 |
!SequenceableCollection methodsFor:'enumerating'! |
|
1309 |
||
1310 |
collect:aBlock |
|
1311 |
"evaluate the argument, aBlock for every element in the collection |
|
1312 |
and return a collection of the results" |
|
1313 |
||
1314 |
|newCollection |
|
1315 |
sz "{ Class:SmallInteger }"| |
|
1316 |
||
1317 |
sz := self size. |
|
1318 |
newCollection := self copyEmptyAndGrow:sz. |
|
1319 |
1 to:sz do:[:index | |
|
1320 |
newCollection at:index put:(aBlock value:(self at:index)). |
|
1321 |
]. |
|
1322 |
^ newCollection |
|
1323 |
||
1324 |
" |
|
1325 |
#(one two three four five six) collect:[:element | element asUppercase] |
|
1326 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
1327 |
(1 to:9) collect:[:element | element * element] |
|
1328 |
" |
|
202 | 1329 |
! |
1330 |
||
602 | 1331 |
do:aBlock |
1332 |
"evaluate the argument, aBlock for every element in the collection." |
|
1333 |
||
1334 |
|stop "{ Class:SmallInteger }"| |
|
1335 |
||
1336 |
stop := self size. |
|
1337 |
1 to:stop do:[:index | |
|
1338 |
aBlock value:(self at:index). |
|
1339 |
] |
|
1340 |
" |
|
1422 | 1341 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 1342 |
" |
1343 |
! |
|
1344 |
||
1345 |
from:startIndex do:aBlock |
|
1346 |
"evaluate the argument, aBlock for the elements starting with the |
|
1347 |
element at startIndex to the end." |
|
1348 |
||
1349 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 1350 |
|
95 | 1351 |
" |
1422 | 1352 |
#(one two three four five six) from:3 do:[:element | Transcript showCR:element] |
602 | 1353 |
" |
1354 |
! |
|
1355 |
||
1356 |
from:start to:stop collect:aBlock |
|
1357 |
"evaluate the argument, aBlock for the elements indexed by start |
|
1358 |
to stop in the collection and return a collection of the results" |
|
1359 |
||
1360 |
|newCollection sz |
|
1361 |
idx "{ Class:SmallInteger }"| |
|
1362 |
||
1363 |
sz := stop - start + 1. |
|
1364 |
newCollection := self copyEmptyAndGrow:sz. |
|
1365 |
idx := 1. |
|
1366 |
start to:stop do:[:index | |
|
1367 |
newCollection at:idx put:(aBlock value:(self at:index)). |
|
1368 |
idx := idx + 1 |
|
1369 |
]. |
|
1370 |
^ newCollection |
|
1371 |
||
1372 |
" |
|
1373 |
#(one two three four five six) from:2 to:4 collect:[:element | element asUppercase] |
|
1374 |
" |
|
1375 |
! |
|
1376 |
||
1377 |
from:index1 to:index2 do:aBlock |
|
1378 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1379 |
index2 in the collection" |
|
1380 |
||
1381 |
|start "{ Class:SmallInteger }" |
|
1382 |
stop "{ Class:SmallInteger }" | |
|
1383 |
||
1384 |
start := index1. |
|
1385 |
stop := index2. |
|
1386 |
start to:stop do:[:index | |
|
1387 |
aBlock value:(self at:index). |
|
1388 |
] |
|
1389 |
||
1390 |
" |
|
1422 | 1391 |
#(one two three four five six) from:3 to:5 do:[:element | Transcript showCR:element] |
602 | 1392 |
" |
1393 |
! |
|
1394 |
||
1395 |
from:index1 to:index2 reverseDo:aBlock |
|
1396 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1397 |
index2 in the collection. Step in reverse order" |
|
1398 |
||
1399 |
|start "{ Class:SmallInteger }" |
|
1400 |
stop "{ Class:SmallInteger }" | |
|
1401 |
||
1402 |
start := index1. |
|
1403 |
stop := index2. |
|
1404 |
stop to:start by:-1 do:[:index | |
|
1405 |
aBlock value:(self at:index). |
|
1406 |
] |
|
1407 |
||
1408 |
" |
|
1422 | 1409 |
#(one two three four five six) from:3 to:5 reverseDo:[:element | Transcript showCR:element] |
95 | 1410 |
" |
61 | 1411 |
! |
1412 |
||
602 | 1413 |
keysAndValuesDo:aTwoArgBlock |
1414 |
"evaluate the argument, aBlock for every element in the collection, |
|
1415 |
passing both index and element as arguments." |
|
1416 |
||
1417 |
|stop "{ Class:SmallInteger }"| |
|
1418 |
||
1419 |
stop := self size. |
|
1420 |
1 to:stop do:[:index | |
|
1421 |
aTwoArgBlock value:index value:(self at:index). |
|
1422 |
] |
|
1423 |
" |
|
1422 | 1424 |
#(one two three four five six) keysAndValuesDo:[:key :element | Transcript show:key; space; showCR:element] |
602 | 1425 |
" |
1426 |
! |
|
1427 |
||
1359 | 1428 |
keysAndValuesReverseDo:aTwoArgBlock |
1429 |
"evaluate the argument, aBlock in reverse order, for every element |
|
1430 |
in the collection, passing both index and element as arguments." |
|
1431 |
||
1432 |
|stop "{ Class:SmallInteger }"| |
|
1433 |
||
1434 |
stop := self size. |
|
1435 |
stop to:1 by:-1 do:[:index | |
|
1436 |
aTwoArgBlock value:index value:(self at:index). |
|
1437 |
] |
|
1438 |
||
1439 |
" |
|
1440 |
#(one two three four five six) |
|
1422 | 1441 |
keysAndValuesReverseDo:[:key :element | Transcript show:key; space; showCR:element] |
1359 | 1442 |
" |
1443 |
||
1444 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
1445 |
! |
|
1446 |
||
602 | 1447 |
nonNilElementsDo:aBlock |
1448 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
1449 |
||
1450 |
|stop "{ Class:SmallInteger }" |
|
1451 |
element| |
|
1452 |
||
1453 |
stop := self size. |
|
1454 |
1 to:stop do:[:index | |
|
1455 |
(element := self at:index) notNil ifTrue:[ |
|
1456 |
aBlock value:element. |
|
1457 |
] |
|
1458 |
] |
|
1459 |
" |
|
1422 | 1460 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 1461 |
" |
1462 |
! |
|
1463 |
||
1464 |
pairWiseCollect:aBlock |
|
1465 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1466 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1467 |
An error will be reported, if the number of elements in the receiver |
|
1468 |
is not a multiple of 2. |
|
1469 |
Collect the results and return a new collection containing those." |
|
1470 |
||
1471 |
|stop newCollection dstIdx "{ Class:SmallInteger }"| |
|
1472 |
||
1473 |
stop := self size. |
|
1474 |
newCollection := self copyEmptyAndGrow:stop // 2. |
|
1475 |
dstIdx := 1. |
|
1476 |
1 to:stop by:2 do:[:index | |
|
1477 |
newCollection at:dstIdx put:(aBlock value:(self at:index) value:(self at:index+1)). |
|
1478 |
dstIdx := dstIdx + 1 |
|
1479 |
]. |
|
1480 |
^ newCollection |
|
44 | 1481 |
|
95 | 1482 |
" |
602 | 1483 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
1484 |
pairWiseCollect:[:num :sym | sym->num] |
|
1485 |
||
1486 |
||
1487 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1488 |
pairWiseCollect:[:x :y | x@y] |
|
1489 |
" |
|
1490 |
! |
|
1491 |
||
1492 |
pairWiseDo:aBlock |
|
1493 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1494 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1495 |
An error will be reported, if the number of elements in the receiver |
|
1496 |
is not a multiple of 2." |
|
1497 |
||
1498 |
|stop "{ Class:SmallInteger }"| |
|
1499 |
||
1500 |
stop := self size. |
|
1501 |
1 to:stop by:2 do:[:index | |
|
1502 |
aBlock value:(self at:index) value:(self at:index+1). |
|
1503 |
] |
|
1504 |
" |
|
1505 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
1422 | 1506 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
602 | 1507 |
|
1508 |
||
1509 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1422 | 1510 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
95 | 1511 |
" |
61 | 1512 |
! |
1513 |
||
602 | 1514 |
reverseDo:aBlock |
1515 |
"evaluate the argument, aBlock for every element in the collection |
|
1516 |
in reverse order" |
|
1517 |
||
1518 |
|sz "{ Class:SmallInteger }"| |
|
1519 |
||
1520 |
sz := self size. |
|
1521 |
sz to:1 by:-1 do:[:index | |
|
1522 |
aBlock value:(self at:index). |
|
1523 |
] |
|
1524 |
||
1525 |
" |
|
1422 | 1526 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 1527 |
" |
1528 |
! |
|
1529 |
||
1530 |
select:aBlock |
|
1531 |
"evaluate the argument, aBlock for every element in the collection |
|
1532 |
and return a collection of all elements for which the block return |
|
1533 |
true" |
|
1534 |
||
1535 |
|element newColl species needCopy |
|
1536 |
sz "{ Class:SmallInteger }"| |
|
1537 |
||
1538 |
sz := self size. |
|
1539 |
species := self species. |
|
1540 |
species growIsCheap ifFalse:[ |
|
1541 |
newColl := OrderedCollection new:sz. |
|
1542 |
needCopy := true |
|
1543 |
] ifTrue:[ |
|
1544 |
newColl := self copyEmpty:sz. |
|
1545 |
needCopy := false |
|
1546 |
]. |
|
1547 |
1 to:sz do:[:index | |
|
1548 |
element := self at:index. |
|
1549 |
(aBlock value:element) ifTrue:[ |
|
1550 |
newColl add:element |
|
1551 |
]. |
|
1552 |
]. |
|
1553 |
needCopy ifTrue:[ |
|
1554 |
newColl := (species withAll:newColl) postCopyFrom:self |
|
1555 |
]. |
|
61 | 1556 |
^ newColl |
1557 |
||
95 | 1558 |
" |
602 | 1559 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
1560 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
1561 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
1562 |
(1 to:9) select:[:element | element odd] |
|
1563 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 1564 |
" |
186 | 1565 |
! |
1566 |
||
602 | 1567 |
with:aSequenceableCollection do:aTwoArgBlock |
1568 |
"evaluate the argument, aBlock for successive elements from |
|
1569 |
each the receiver and the argument, aSequenceableCollection. |
|
1570 |
The second argument, aBlock must be a two-argument block. |
|
1571 |
The collection argument must implement access via a numeric key." |
|
1572 |
||
1573 |
|stop "{ Class: SmallInteger }" | |
|
1574 |
||
1575 |
stop := self size. |
|
1576 |
1 to:stop do:[:index | |
|
1577 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
|
1578 |
] |
|
186 | 1579 |
" |
1422 | 1580 |
#(one two three four five six) with:(1 to:10) do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
186 | 1581 |
" |
1 | 1582 |
! ! |
1583 |
||
1584 |
!SequenceableCollection methodsFor:'filling and replacing'! |
|
1585 |
||
602 | 1586 |
atAllPut:anObject |
1587 |
"replace all elements of the collection by the argument, anObject. |
|
1588 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1589 |
therefore the change may affect all others referencing the receiver." |
|
1590 |
||
1591 |
self from:1 to:(self size) put:anObject |
|
1592 |
||
1593 |
" |
|
1594 |
(Array new:10) atAllPut:1 |
|
1595 |
(String new:10) atAllPut:$a |
|
1596 |
" |
|
1597 |
! |
|
1598 |
||
1 | 1599 |
from:index1 to:index2 put:anObject |
1600 |
"replace the elements from index1 to index2 of the collection |
|
44 | 1601 |
by the argument, anObject. |
1602 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1603 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1604 |
|
1605 |
|index "{ Class: SmallInteger }" |
|
1606 |
end "{ Class: SmallInteger }"| |
|
1607 |
||
1608 |
index := index1. |
|
1609 |
end := index2. |
|
1610 |
[index <= end] whileTrue:[ |
|
159 | 1611 |
self at:index put:anObject. |
1612 |
index := index + 1 |
|
1 | 1613 |
] |
44 | 1614 |
|
95 | 1615 |
" |
1616 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
|
1617 |
'abcdefghijkl' from:3 to:6 put:$X |
|
1618 |
" |
|
1 | 1619 |
! |
1620 |
||
1621 |
replaceAll:oldObject by:newObject |
|
44 | 1622 |
"replace all oldObjects by newObject in the receiver. |
1623 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1624 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1625 |
|
362 | 1626 |
^ self replaceAll:oldObject by:newObject from:1 to:self size |
1627 |
||
1628 |
" |
|
1629 |
'123123abc123' replaceAll:$1 by:$* |
|
1630 |
" |
|
1631 |
! |
|
293 | 1632 |
|
362 | 1633 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
1634 |
"replace all oldObjects found between startIndex and endIndex, |
|
1635 |
by newObject in the receiver. |
|
1636 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1637 |
therefore the change may affect all others referencing the receiver." |
|
1638 |
||
1639 |
|start "{ Class: SmallInteger }" |
|
1640 |
stop "{ Class: SmallInteger }"| |
|
1641 |
||
1642 |
start := startIndex. |
|
1643 |
stop := stopIndex. |
|
1644 |
startIndex to:stopIndex do:[:index | |
|
159 | 1645 |
(self at:index) = oldObject ifTrue:[ |
1646 |
self at:index put:newObject |
|
1647 |
] |
|
1 | 1648 |
] |
44 | 1649 |
|
95 | 1650 |
" |
362 | 1651 |
'123123abc123' replaceAll:$1 by:$* |
1652 |
'123123abc123' replaceAll:$1 by:$* from:1 to:6 |
|
95 | 1653 |
" |
1654 |
! |
|
1655 |
||
602 | 1656 |
replaceAll:oldObject with:newObject |
1657 |
"ST-80 compatibility: an alias for #replaceAll:by:" |
|
1658 |
||
1659 |
^ self replaceAll:oldObject by:newObject |
|
1660 |
! |
|
1661 |
||
1662 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
|
1663 |
"ST-80 compatibility: an alias for #replaceAll:by:from:to:" |
|
1664 |
||
1665 |
^ self replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
1666 |
! |
|
1667 |
||
95 | 1668 |
replaceAny:aCollection by:newObject |
1669 |
"replace all elements, which are in aCollection by newObject in the receiver. |
|
1670 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1671 |
therefore the change may affect all others referencing the receiver." |
|
1672 |
||
362 | 1673 |
^ self replaceAny:aCollection by:newObject from:1 to:self size |
1674 |
||
1675 |
" |
|
1676 |
'123123abc123' replaceAny:#($1 $2) by:$* |
|
1677 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) by:'*' |
|
1678 |
" |
|
1679 |
! |
|
293 | 1680 |
|
362 | 1681 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
1682 |
"replace all elements within a range, |
|
1683 |
which are in contained in aCollection by newObject. |
|
1684 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1685 |
therefore the change may affect all others referencing the receiver." |
|
1686 |
||
1687 |
|start "{ Class: SmallInteger }" |
|
1688 |
stop "{ Class: SmallInteger }"| |
|
1689 |
||
1690 |
start := startIndex. |
|
1691 |
stop := stopIndex. |
|
1692 |
startIndex to:stopIndex do:[:index | |
|
159 | 1693 |
(aCollection includes:(self at:index)) ifTrue:[ |
1694 |
self at:index put:newObject |
|
1695 |
] |
|
95 | 1696 |
] |
1697 |
||
1698 |
" |
|
362 | 1699 |
'123123abc123' replaceAny:#($1 $2) by:$* from:1 to:6 |
95 | 1700 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) by:'*' |
1701 |
" |
|
1 | 1702 |
! |
1703 |
||
1704 |
replaceFrom:start to:stop with:replacementCollection |
|
44 | 1705 |
"replace elements in the receiver between index start and stop, |
1706 |
with elements taken from replacementCollection starting at 1. |
|
1707 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1708 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1709 |
|
1710 |
^ self replaceFrom:start |
|
159 | 1711 |
to:stop |
1712 |
with:replacementCollection |
|
1713 |
startingAt:1 |
|
61 | 1714 |
|
95 | 1715 |
" |
1716 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
1717 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
1718 |
" |
|
1 | 1719 |
! |
1720 |
||
1721 |
replaceFrom:start to:stop with:replacementCollection startingAt:repStart |
|
44 | 1722 |
"replace elements in the receiver between index start and stop, |
1723 |
with elements taken from replacementCollection starting at repStart. |
|
1724 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1725 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1726 |
|
1727 |
|srcIndex "{ Class: SmallInteger }" |
|
1728 |
dstIndex "{ Class: SmallInteger }" |
|
1729 |
end "{ Class: SmallInteger }" | |
|
1730 |
||
293 | 1731 |
(repStart < start) ifTrue:[ |
1732 |
" must do reverse copy " |
|
1733 |
srcIndex := repStart + (stop - start). |
|
1734 |
dstIndex := stop. |
|
1735 |
end := start. |
|
1736 |
[dstIndex >= end] whileTrue:[ |
|
1737 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
1738 |
srcIndex := srcIndex - 1. |
|
1739 |
dstIndex := dstIndex - 1 |
|
1740 |
]. |
|
1741 |
^ self |
|
1 | 1742 |
]. |
1743 |
||
1744 |
srcIndex := repStart. |
|
1745 |
dstIndex := start. |
|
1746 |
end := stop. |
|
1747 |
[dstIndex <= end] whileTrue:[ |
|
159 | 1748 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
1749 |
srcIndex := srcIndex + 1. |
|
1750 |
dstIndex := dstIndex + 1 |
|
1 | 1751 |
] |
61 | 1752 |
|
95 | 1753 |
" |
282 | 1754 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
1755 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1756 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1757 |
||
1758 |
|c| |
|
1759 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1760 |
c replaceFrom:2 to:3 with:c startingAt:4 |
|
1761 |
||
1762 |
|c| |
|
1763 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1764 |
c replaceFrom:4 to:5 with:c startingAt:2 |
|
95 | 1765 |
" |
61 | 1766 |
! |
1767 |
||
602 | 1768 |
replaceFrom:start with:replacementCollection |
1769 |
"replace elements in the receiver starting at start, |
|
1770 |
with elements taken from replacementCollection starting at 1 |
|
1771 |
to the end of replacementCollection. |
|
1772 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1773 |
therefore the change may affect all others referencing the receiver." |
|
1774 |
||
1775 |
^ self replaceFrom:start |
|
1776 |
to:(start + replacementCollection size - 1) |
|
1777 |
with:replacementCollection |
|
1778 |
startingAt:1 |
|
1779 |
||
1780 |
" |
|
1781 |
'1234567890' replaceFrom:5 with:'abc' |
|
1782 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
1783 |
" |
|
1784 |
! |
|
1785 |
||
1786 |
replaceFrom:start with:replacementCollection startingAt:offset |
|
1787 |
"replace elements in the receiver starting at start, |
|
1788 |
with elements taken from replacementCollection starting at offset |
|
1789 |
to the end of replacementCollection. |
|
1790 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1791 |
therefore the change may affect all others referencing the receiver." |
|
1792 |
||
1793 |
^ self replaceFrom:start |
|
1794 |
to:(start + replacementCollection size - offset) |
|
1795 |
with:replacementCollection |
|
1796 |
startingAt:offset |
|
1797 |
||
1798 |
" |
|
1799 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
|
1800 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
|
1801 |
" |
|
1802 |
! |
|
1803 |
||
61 | 1804 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
1805 |
"replace elements in destColl with elements from the receiver. |
|
1806 |
Notice: This operation modifies the destination collection, NOT a copy; |
|
1807 |
therefore the change may affect all others referencing this object." |
|
1808 |
||
1809 |
destColl replaceFrom:destStartIndex to:destEndIndex with:self startingAt:sourceStart |
|
1810 |
||
1811 |
" |
|
1812 |
|s| |
|
1813 |
s := 'abcdefghijklmnop'. |
|
1814 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
1815 |
s'123defghijklmnop' |
|
1816 |
" |
|
1 | 1817 |
! ! |
1818 |
||
2069 | 1819 |
!SequenceableCollection methodsFor:'private sorting helpers'! |
1820 |
||
1821 |
quickSortFrom:inBegin to:inEnd |
|
1822 |
"actual quicksort worker for sort-message" |
|
1823 |
||
1824 |
|begin "{ Class: SmallInteger }" |
|
1825 |
end "{ Class: SmallInteger }" |
|
1826 |
b "{ Class: SmallInteger }" |
|
1827 |
e "{ Class: SmallInteger }" |
|
1828 |
middleElement temp1 temp2 | |
|
1829 |
||
1830 |
begin := inBegin. "/ this also does a type-check |
|
1831 |
end := inEnd. |
|
1832 |
||
1833 |
b := begin. |
|
1834 |
e := end. |
|
1835 |
middleElement := self at:((b + e) // 2). |
|
1836 |
||
1837 |
[b < e] whileTrue:[ |
|
1838 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
1839 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
1840 |
||
1841 |
(b <= e) ifTrue:[ |
|
1842 |
(b == e) ifFalse:[ |
|
1843 |
temp1 := self at:b. temp2 := self at:e. |
|
1844 |
self at:b put:temp2. self at:e put:temp1 |
|
1845 |
]. |
|
1846 |
b := b + 1. |
|
1847 |
e := e - 1 |
|
1848 |
] |
|
1849 |
]. |
|
1850 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e]. |
|
1851 |
(b < end) ifTrue:[self quickSortFrom:b to:end] |
|
1852 |
! |
|
1853 |
||
1854 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
|
1855 |
"actual quicksort worker for sort:-message" |
|
1856 |
||
1857 |
|begin "{ Class: SmallInteger }" |
|
1858 |
end "{ Class: SmallInteger }" |
|
1859 |
b "{ Class: SmallInteger }" |
|
1860 |
e "{ Class: SmallInteger }" |
|
1861 |
middleElement temp1 temp2 | |
|
1862 |
||
1863 |
begin := inBegin. "/ this also does a type-check |
|
1864 |
end := inEnd. |
|
1865 |
||
1866 |
b := begin. |
|
1867 |
e := end. |
|
1868 |
middleElement := self at:((b + e) // 2). |
|
1869 |
||
1870 |
[b < e] whileTrue:[ |
|
1871 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
|
1872 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
1873 |
||
1874 |
(b <= e) ifTrue:[ |
|
1875 |
(b == e) ifFalse:[ |
|
1876 |
temp1 := self at:b. temp2 := self at:e. |
|
1877 |
self at:b put:temp2. self at:e put:temp1 |
|
1878 |
]. |
|
1879 |
b := b + 1. |
|
1880 |
e := e - 1 |
|
1881 |
] |
|
1882 |
]. |
|
1883 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock]. |
|
1884 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock] |
|
1885 |
! |
|
1886 |
||
1887 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
1888 |
"actual quicksort worker for sort:with:-message" |
|
1889 |
||
1890 |
|begin "{ Class: SmallInteger }" |
|
1891 |
end "{ Class: SmallInteger }" |
|
1892 |
b "{ Class: SmallInteger }" |
|
1893 |
e "{ Class: SmallInteger }" |
|
1894 |
middleElement temp1 temp2 | |
|
1895 |
||
1896 |
begin := inBegin. "/ this also does a type-check |
|
1897 |
end := inEnd. |
|
1898 |
||
1899 |
b := begin. |
|
1900 |
e := end. |
|
1901 |
middleElement := self at:((b + e) // 2). |
|
1902 |
||
1903 |
[b < e] whileTrue:[ |
|
1904 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
|
1905 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
1906 |
||
1907 |
(b <= e) ifTrue:[ |
|
1908 |
(b == e) ifFalse:[ |
|
1909 |
temp1 := self at:b. temp2 := self at:e. |
|
1910 |
self at:b put:temp2. self at:e put:temp1. |
|
1911 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
1912 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
1913 |
]. |
|
1914 |
b := b + 1. |
|
1915 |
e := e - 1 |
|
1916 |
] |
|
1917 |
]. |
|
1918 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
|
1919 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
|
1920 |
||
1921 |
"Modified: 5.1.1997 / 18:12:36 / cg" |
|
1922 |
! |
|
1923 |
||
1924 |
quickSortFrom:inBegin to:inEnd with:aCollection |
|
1925 |
"actual quicksort worker for sortWith-message" |
|
1926 |
||
1927 |
|begin "{ Class: SmallInteger }" |
|
1928 |
end "{ Class: SmallInteger }" |
|
1929 |
b "{ Class: SmallInteger }" |
|
1930 |
e "{ Class: SmallInteger }" |
|
1931 |
middleElement temp1 temp2 | |
|
1932 |
||
1933 |
begin := inBegin. "/ this also does a type-check |
|
1934 |
end := inEnd. |
|
1935 |
||
1936 |
b := begin. |
|
1937 |
e := end. |
|
1938 |
middleElement := self at:((b + e) // 2). |
|
1939 |
||
1940 |
[b < e] whileTrue:[ |
|
1941 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
1942 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
1943 |
||
1944 |
(b <= e) ifTrue:[ |
|
1945 |
(b == e) ifFalse:[ |
|
1946 |
temp1 := self at:b. temp2 := self at:e. |
|
1947 |
self at:b put:temp2. self at:e put:temp1. |
|
1948 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
1949 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
1950 |
]. |
|
1951 |
b := b + 1. |
|
1952 |
e := e - 1 |
|
1953 |
] |
|
1954 |
]. |
|
1955 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e with:aCollection]. |
|
1956 |
(b < end) ifTrue:[self quickSortFrom:b to:end with:aCollection] |
|
1957 |
! ! |
|
1958 |
||
602 | 1959 |
!SequenceableCollection methodsFor:'queries'! |
1960 |
||
1961 |
firstIndex |
|
1962 |
"return the first elements index" |
|
1963 |
||
1964 |
^ 1 |
|
1965 |
! |
|
1966 |
||
1967 |
isSequenceable |
|
1968 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
1969 |
||
1970 |
^ true |
|
1 | 1971 |
! |
1972 |
||
602 | 1973 |
isSequenceableCollection |
1974 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
1975 |
This method is a historic leftover and will be removed." |
|
1976 |
||
1977 |
^ true |
|
1 | 1978 |
! |
1979 |
||
602 | 1980 |
keys |
1981 |
"return a collection with all keys in the Smalltalk dictionary" |
|
1982 |
||
1983 |
|sz| |
|
1984 |
||
1 | 1985 |
sz := self size. |
602 | 1986 |
sz == 0 ifTrue:[ |
1987 |
^ #() |
|
61 | 1988 |
]. |
602 | 1989 |
^ 1 to:sz |
1 | 1990 |
! |
1991 |
||
602 | 1992 |
lastIndex |
1993 |
"return the last elements index" |
|
1994 |
||
1995 |
^ self size |
|
61 | 1996 |
! |
1997 |
||
602 | 1998 |
size |
1999 |
"return the number of elements in the collection. |
|
2000 |
concrete implementations must define this" |
|
2001 |
||
2002 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2003 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2004 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2005 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2006 |
"return the index value which is returned when nothing |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2007 |
is found in an indexOf* kind of search. |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2008 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2009 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2010 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2011 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2012 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 2013 |
! ! |
2014 |
||
2015 |
!SequenceableCollection methodsFor:'searching'! |
|
2016 |
||
2017 |
detect:aBlock ifNone:exceptionBlock |
|
2018 |
"find the first element, for which evaluation of the argument, aBlock |
|
2019 |
return true; if none does so, return the evaluation of exceptionBlock |
|
2020 |
||
2021 |
reimplemented here for speed" |
|
2022 |
||
32 | 2023 |
|stop "{ Class: SmallInteger }" |
1 | 2024 |
element| |
2025 |
||
32 | 2026 |
stop := self size. |
2027 |
1 to:stop do:[:index | |
|
159 | 2028 |
element := self at:index. |
2029 |
(aBlock value:element) ifTrue:[ |
|
2030 |
^ element |
|
2031 |
]. |
|
1 | 2032 |
]. |
2033 |
^ exceptionBlock value |
|
159 | 2034 |
|
2035 |
" |
|
2036 |
#(11 12 13 14) detect:[:n | n odd] ifNone:['sorry'] |
|
2037 |
#(12 14 16 18) detect:[:n | n odd] ifNone:['sorry'] |
|
2038 |
" |
|
1 | 2039 |
! |
2040 |
||
602 | 2041 |
findFirst:aBlock |
2042 |
"find the first element, for which evaluation of the argument, aBlock |
|
2043 |
returns true; return its index or 0 if none detected." |
|
2044 |
||
2045 |
|stop "{ Class: SmallInteger }" | |
|
2046 |
||
32 | 2047 |
stop := self size. |
602 | 2048 |
1 to:stop do:[:index | |
2049 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
1 | 2050 |
]. |
2051 |
^ 0 |
|
159 | 2052 |
|
95 | 2053 |
" |
602 | 2054 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
2055 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
2056 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
2057 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
95 | 2058 |
" |
1 | 2059 |
! |
2060 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2061 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2062 |
"find the first element, for which evaluation of the argument, aBlock |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2063 |
returns true; return its index or 0 if none detected." |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2064 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2065 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2066 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2067 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2068 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2069 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2070 |
start to:stop do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2071 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2072 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2073 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2074 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2075 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2076 |
#(1 4 3 4 3 6) findFirst:[:x | (x > 3)] startingAt:4 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2077 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2078 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2079 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2080 |
|
602 | 2081 |
findLast:aBlock |
2082 |
"find the last element, for which evaluation of the argument, aBlock |
|
2083 |
returns true; return its index or 0 if none detected." |
|
2084 |
||
2085 |
|start "{ Class: SmallInteger }"| |
|
2086 |
||
2087 |
start := self size. |
|
2088 |
start to:1 by:-1 do:[:index | |
|
2089 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
252 | 2090 |
]. |
2091 |
^ 0 |
|
2092 |
||
2093 |
" |
|
602 | 2094 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
2095 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 2096 |
" |
2097 |
! |
|
2098 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2099 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2100 |
"find the last element, for which evaluation of the argument, aBlock |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2101 |
returns true; return its index or 0 if none detected." |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2102 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2103 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2104 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2105 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2106 |
start to:1 by:-1 do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2107 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2108 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2109 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2110 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2111 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2112 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2113 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2114 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2115 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2116 |
|
1 | 2117 |
identityIndexOf:anElement |
2118 |
"search the collection for anElement using identity compare (i.e. ==); |
|
2119 |
if found, return the index otherwise return 0." |
|
2120 |
||
32 | 2121 |
^ self identityIndexOf:anElement startingAt:1 |
44 | 2122 |
|
95 | 2123 |
" |
159 | 2124 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
2125 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 |
|
2126 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
360 | 2127 |
|
2128 |
be careful: |
|
2129 |
||
2130 |
#(10 20 30 40.0 50 60 70) indexOf:40.0 |
|
2131 |
#(10 20 30 40.0 50 60 70) identityIndexOf:40.0 |
|
2132 |
||
95 | 2133 |
" |
1 | 2134 |
! |
2135 |
||
2136 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
|
2137 |
"search the collection for anElement using identity compare (i.e. ==); |
|
2138 |
if found, return the index otherwise return the value of the |
|
2139 |
exceptionBlock." |
|
2140 |
||
2141 |
|index| |
|
2142 |
||
32 | 2143 |
index := self identityIndexOf:anElement startingAt:1. |
1 | 2144 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
2145 |
^ index |
|
159 | 2146 |
|
2147 |
" |
|
2148 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
2149 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
2150 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
2151 |
#(10 20 30 40 50 60 70) indexOf:40.0 ifAbsent:['none'] |
|
2152 |
" |
|
1 | 2153 |
! |
2154 |
||
2155 |
identityIndexOf:anElement startingAt:start |
|
159 | 2156 |
"search the collection for anElement, starting search at index start |
1 | 2157 |
using identity compare (i.e. ==); |
2158 |
if found, return the index otherwise return 0." |
|
2159 |
||
32 | 2160 |
|startIndex "{ Class: SmallInteger }" |
2161 |
stop "{ Class: SmallInteger }" | |
|
1 | 2162 |
|
32 | 2163 |
startIndex := start. |
2164 |
stop := self size. |
|
2165 |
startIndex to:stop do:[:index | |
|
159 | 2166 |
anElement == (self at:index) ifTrue:[^ index]. |
1 | 2167 |
]. |
2168 |
^ 0 |
|
44 | 2169 |
|
95 | 2170 |
" |
159 | 2171 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40 startingAt:5 |
2172 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
|
2173 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
95 | 2174 |
" |
1 | 2175 |
! |
2176 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2177 |
identityIndexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2178 |
"search the collection for anElement, starting the search at index start; |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2179 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2180 |
If found (within the range), return the index, otherwise return 0. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2181 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2182 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2183 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2184 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2185 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2186 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2187 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2188 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2189 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2190 |
anElement == (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2191 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2192 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2193 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2194 |
"Created: 12.4.1996 / 18:23:07 / cg" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2195 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2196 |
|
1 | 2197 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
159 | 2198 |
"search the collection for anElement, starting search at index start; |
1 | 2199 |
if found, return the index otherwise return the value of the |
2200 |
exceptionBlock. |
|
2201 |
This one searches for identical objects (i.e. ==)." |
|
2202 |
||
2203 |
|index| |
|
2204 |
||
2205 |
index := self identityIndexOf:anElement startingAt:start. |
|
2206 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2207 |
^ index |
|
159 | 2208 |
|
2209 |
" |
|
2210 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
2211 |
#(10 20 30 40 10) identityIndexOf:35 startingAt:3 ifAbsent:['none'] |
|
2212 |
" |
|
2213 |
! |
|
2214 |
||
602 | 2215 |
includes:anElement |
2216 |
"return true if the collection contains anElement; false otherwise. |
|
2217 |
Comparison is done using equality compare (i.e. =). |
|
2218 |
See #includesIdentical: if identity is asked for." |
|
2219 |
||
2220 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
2221 |
^ true |
|
2222 |
||
2223 |
" |
|
2224 |
#(10 20 30 40 50 60 70) includes:99 |
|
2225 |
#(10 20 30 40 50 60 70) includes:40 |
|
2226 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
2227 |
" |
|
2228 |
! |
|
2229 |
||
2230 |
includesIdentical:anElement |
|
2231 |
"return true if the collection contains anElement; false otherwise. |
|
2232 |
Comparison is done using identity compare (i.e. ==). |
|
2233 |
See #includes: if equality is asked for." |
|
2234 |
||
2235 |
((self identityIndexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
2236 |
^ true |
|
2237 |
||
2238 |
" |
|
2239 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
2240 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
2241 |
#(10 20 30 40 50 60 70) includes:40 |
|
2242 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
2243 |
||
2244 |
be careful: |
|
2245 |
||
2246 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
|
2247 |
#(10 20 30 40.0 50 60 70) includesIdentical:40.0 |
|
2248 |
" |
|
2249 |
! |
|
2250 |
||
2251 |
indexOf:anElement |
|
2252 |
"search the collection for anElement; |
|
2253 |
if found, return the index otherwise return 0. |
|
2254 |
The comparison is done using = |
|
2255 |
(i.e. equality test - not identity test)." |
|
2256 |
||
2257 |
^ self indexOf:anElement startingAt:1 |
|
2258 |
||
2259 |
" |
|
2260 |
#(10 20 30 40 50 60 70) indexOf:40 |
|
2261 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
2262 |
" |
|
2263 |
! |
|
2264 |
||
2265 |
indexOf:anElement ifAbsent:exceptionBlock |
|
2266 |
"search the collection for anElement; |
|
2267 |
if found, return the index otherwise return the value of the |
|
2268 |
exceptionBlock. |
|
2269 |
The comparison is done using = |
|
2270 |
(i.e. equality test - not identity test)." |
|
2271 |
||
2272 |
|index| |
|
2273 |
||
2274 |
index := self indexOf:anElement startingAt:1. |
|
2275 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2276 |
^ index |
|
2277 |
||
2278 |
" |
|
2279 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
2280 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
2281 |
#(10 20 30 40 10 20 30 40) indexOf:35 ifAbsent:['none'] |
|
2282 |
" |
|
2283 |
! |
|
2284 |
||
2285 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
|
2286 |
"search for the first occurence of elementToFind starting at start, |
|
2287 |
stopping the search at stop. If found, replace the element by replacement |
|
2288 |
and return the index. |
|
2289 |
If not found, return 0." |
|
2290 |
||
2291 |
|idx| |
|
2292 |
||
2293 |
idx := self indexOf:elementToFind startingAt:start. |
|
2294 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
|
2295 |
self at:idx put:replacement. |
|
2296 |
^ idx |
|
2297 |
]. |
|
2298 |
^ 0 |
|
2299 |
||
2300 |
" |
|
2301 |
|a| |
|
2302 |
||
2303 |
a := #(10 20 30 40 50 60 70). |
|
2304 |
(a indexOf:30 replaceWith:nil startingAt:1 stoppingAt:7) printNL. |
|
2305 |
a printNL. |
|
2306 |
" |
|
2307 |
! |
|
2308 |
||
2309 |
indexOf:anElement startingAt:start |
|
2310 |
"search the collection for anElement, starting the search at index start; |
|
2311 |
if found, return the index otherwise return 0. |
|
2312 |
The comparison is done using = |
|
2313 |
(i.e. equality test - not identity test)." |
|
2314 |
||
2315 |
|startIndex "{ Class: SmallInteger }" |
|
2316 |
stop "{ Class: SmallInteger }" | |
|
2317 |
||
2318 |
startIndex := start. |
|
2319 |
stop := self size. |
|
2320 |
startIndex to:stop do:[:index | |
|
2321 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2322 |
]. |
|
2323 |
^ 0 |
|
2324 |
||
2325 |
" |
|
2326 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
2327 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
2328 |
" |
|
2329 |
! |
|
2330 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2331 |
indexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2332 |
"search the collection for anElement, starting the search at index start; |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2333 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2334 |
If found (within the range), return the index, otherwise return 0. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2335 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2336 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2337 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2338 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2339 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2340 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2341 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2342 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2343 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2344 |
anElement = (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2345 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2346 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2347 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2348 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2349 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2350 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2351 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2352 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2353 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2354 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2355 |
"Created: 12.4.1996 / 17:37:13 / cg" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2356 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2357 |
|
602 | 2358 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
2359 |
"search the collection for anElement starting the search at index start; |
|
2360 |
if found, return the index otherwise return the value of the |
|
2361 |
exceptionBlock. |
|
2362 |
The comparison is done using = |
|
2363 |
(i.e. equality test - not identity test)." |
|
2364 |
||
2365 |
|index| |
|
2366 |
||
2367 |
index := self indexOf:anElement startingAt:start. |
|
2368 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2369 |
^ index |
|
2370 |
||
2371 |
" |
|
2372 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
2373 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
2374 |
#(10 20 30 40 10 20 30 40) indexOf:35 startingAt:5 ifAbsent:['none'] |
|
2375 |
" |
|
2376 |
! |
|
2377 |
||
159 | 2378 |
indexOfAny:aCollection |
2379 |
"search the collection for an element in aCollection. |
|
2380 |
if found, return the index otherwise return 0. |
|
2381 |
The comparison is done using = |
|
2382 |
(i.e. equality test - not identity test). |
|
2383 |
||
2384 |
Notice, that for big collections, the runtime of this search |
|
2385 |
grows proportional to size(receiver) * size(aCollection). |
|
2386 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
2387 |
||
2388 |
^ self indexOfAny:aCollection startingAt:1 |
|
2389 |
||
2390 |
" |
|
2391 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
2392 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
2393 |
" |
|
2394 |
! |
|
2395 |
||
2396 |
indexOfAny:aCollection startingAt:start |
|
2397 |
"search the collection for an element in aCollection, |
|
2398 |
starting the search at index start; |
|
2399 |
if found, return the index otherwise return 0. |
|
2400 |
The comparison is done using = |
|
2401 |
(i.e. equality test - not identity test). |
|
2402 |
||
2403 |
Notice, that for big collections, the runtime of this search |
|
2404 |
grows proportional to size(receiver) * size(aCollection). |
|
2405 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
2406 |
||
2407 |
||
2408 |
|startIndex "{ Class: SmallInteger }" |
|
2409 |
stop "{ Class: SmallInteger }" | |
|
2410 |
||
2411 |
startIndex := start. |
|
2412 |
stop := self size. |
|
2413 |
startIndex to:stop do:[:index | |
|
2414 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
|
2415 |
]. |
|
2416 |
^ 0 |
|
2417 |
" |
|
2418 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
|
2419 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
2420 |
" |
|
1 | 2421 |
! |
2422 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2423 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2424 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2425 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2426 |
if found, return the index. If not, return the value returned by exceptionBlock. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2427 |
The comparison is done using = |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2428 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2429 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2430 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2431 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2432 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2433 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2434 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2435 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2436 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2437 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2438 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2439 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2440 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2441 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2442 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2443 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2444 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(99 88 77) startingAt:5 ifAbsent:['oops'] |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2445 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2446 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2447 |
|
368 | 2448 |
indexOfSubCollection:aCollection |
2449 |
"find a subcollection. |
|
2450 |
If found, return the index; if not found, return 0." |
|
2451 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2452 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 2453 |
|
2454 |
" |
|
2455 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
|
2456 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
2457 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
2458 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
2459 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
2460 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
2461 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
2462 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
2463 |
" |
|
2464 |
! |
|
2465 |
||
602 | 2466 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
2467 |
"find a subcollection. If found, return the index; |
|
2468 |
if not found, return the result of evaluating exceptionBlock." |
|
2469 |
||
2470 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
2471 |
! |
|
2472 |
||
368 | 2473 |
indexOfSubCollection:aCollection startingAt:startIndex |
2474 |
"find a subcollection starting at index. |
|
2475 |
If found, return the index; if not found, return 0." |
|
2476 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2477 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 2478 |
! |
2479 |
||
2480 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
2481 |
"find a subcollection, starting at index. If found, return the index; |
|
2482 |
if not found, return the result of evaluating exceptionBlock. |
|
2483 |
This is a q&d hack - not very efficient" |
|
2484 |
||
2485 |
|same first |
|
2486 |
mySize "{Class: SmallInteger }" |
|
2487 |
checkIndex "{Class: SmallInteger }" |
|
2488 |
cmpIndex "{Class: SmallInteger }" |
|
2489 |
sz "{Class: SmallInteger }"| |
|
2490 |
||
2491 |
mySize := self size. |
|
2492 |
sz := aCollection size. |
|
2493 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
2494 |
first := aCollection at:1. |
|
2495 |
||
2496 |
checkIndex := startIndex - 1. |
|
2497 |
[true] whileTrue:[ |
|
2498 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
|
2499 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
2500 |
||
2501 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
2502 |
^ 0 |
|
2503 |
]. |
|
2504 |
same := true. |
|
2505 |
cmpIndex := 1. |
|
2506 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
2507 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
2508 |
same := false |
|
2509 |
] ifTrue:[ |
|
2510 |
cmpIndex := cmpIndex + 1 |
|
2511 |
] |
|
2512 |
]. |
|
2513 |
same ifTrue:[^ checkIndex]. |
|
2514 |
] |
|
2515 |
||
2516 |
" |
|
2517 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
2518 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
2519 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
2520 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
2521 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
2522 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
2523 |
" |
|
602 | 2524 |
! |
2525 |
||
2526 |
lastIndexOf:anElement |
|
2527 |
"search the collection backwards for anElement; |
|
2528 |
if found, return the index otherwise return 0. |
|
2529 |
The comparison is done using = |
|
2530 |
(i.e. equality test - not identity test)." |
|
2531 |
||
2532 |
^ self lastIndexOf:anElement startingAt:self size |
|
2533 |
||
2534 |
" |
|
2535 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
2536 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
2537 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
2538 |
#(10 20 30 40 50 60 70) lastIndexOf:10 |
|
2539 |
" |
|
2540 |
! |
|
2541 |
||
2542 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
|
2543 |
"search the collection backwards for anElement; |
|
2544 |
if found, return the index otherwise return the value of the |
|
2545 |
exceptionBlock. |
|
2546 |
The comparison is done using = |
|
2547 |
(i.e. equality test - not identity test)." |
|
2548 |
||
2549 |
|index| |
|
2550 |
||
2551 |
index := self lastIndexOf:anElement startingAt:self size. |
|
2552 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2553 |
^ index |
|
2554 |
||
2555 |
" |
|
2556 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
|
2557 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
2558 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
2559 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 ifAbsent:['none'] |
|
2560 |
" |
|
2561 |
! |
|
2562 |
||
2563 |
lastIndexOf:anElement startingAt:start |
|
2564 |
"search the collection backwards for anElement, starting the search at index start; |
|
2565 |
if found, return the index otherwise return 0. |
|
2566 |
The comparison is done using = |
|
2567 |
(i.e. equality test - not identity test)." |
|
2568 |
||
2569 |
|startIndex "{ Class: SmallInteger }"| |
|
2570 |
||
2571 |
startIndex := start. |
|
2572 |
startIndex to:1 by:-1 do:[:index | |
|
2573 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2574 |
]. |
|
2575 |
^ 0 |
|
2576 |
||
2577 |
" |
|
2578 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
|
2579 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
2580 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
2581 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
2582 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:4 |
|
2583 |
" |
|
2584 |
! |
|
2585 |
||
2586 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
|
2587 |
"search the collection backwards for anElement starting the search at |
|
2588 |
index start; if found, return the index |
|
2589 |
otherwise return the value of the exceptionBlock. |
|
2590 |
The comparison is done using = |
|
2591 |
(i.e. equality test - not identity test)." |
|
2592 |
||
2593 |
|index| |
|
2594 |
||
2595 |
index := self lastIndexOf:anElement startingAt:start. |
|
2596 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2597 |
^ index |
|
2598 |
||
2599 |
" |
|
2600 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
2601 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
2602 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
2603 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 ifAbsent:['none'] |
|
2604 |
" |
|
2605 |
! |
|
2606 |
||
2607 |
nextIndexOf:anElement from:start to:stop |
|
2608 |
"search the collection for anElement, starting the search at index start, |
|
2609 |
stopping at:stop; |
|
2610 |
if found, return the index otherwise return 0. |
|
2611 |
The comparison is done using = |
|
2612 |
(i.e. equality test - not identity test)." |
|
2613 |
||
2614 |
|startIndex "{ Class: SmallInteger }" |
|
2615 |
stopIndex "{ Class: SmallInteger }" | |
|
2616 |
||
2617 |
startIndex := start. |
|
2618 |
stopIndex := stop. |
|
2619 |
startIndex to:stop do:[:index | |
|
2620 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2621 |
]. |
|
2622 |
^ 0 |
|
2623 |
||
2624 |
" |
|
2625 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
2626 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
2627 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
2628 |
" |
|
2629 |
! |
|
2630 |
||
2631 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
2632 |
"search the collection for anElement, starting the search at index start |
|
2633 |
and stopping at stop; |
|
2634 |
if found, return the index otherwise return the value of the |
|
2635 |
exceptionBlock. |
|
2636 |
The comparison is done using = |
|
2637 |
(i.e. equality test - not identity test)." |
|
2638 |
||
2639 |
|index| |
|
2640 |
||
2641 |
index := self nextIndexOf:anElement from:start to:stop. |
|
2642 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2643 |
^ index |
|
2644 |
||
2645 |
" |
|
2646 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
2647 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
2648 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
2649 |
" |
|
1 | 2650 |
! ! |
2651 |
||
2652 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
2653 |
||
602 | 2654 |
reverse |
2655 |
"reverse the order of the elements inplace" |
|
2656 |
||
2657 |
|lowIndex "{ Class: SmallInteger }" |
|
2658 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2659 |
t1 t2| |
602 | 2660 |
|
2661 |
hiIndex := self size. |
|
2662 |
lowIndex := 1. |
|
2663 |
[lowIndex < hiIndex] whileTrue:[ |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2664 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2665 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2666 |
|
602 | 2667 |
lowIndex := lowIndex + 1. |
2668 |
hiIndex := hiIndex - 1 |
|
2669 |
] |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2670 |
|
69 | 2671 |
" |
602 | 2672 |
#(4 5 6 7 7) reverse |
2673 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 2674 |
" |
1 | 2675 |
! |
2676 |
||
2677 |
sort |
|
2678 |
"sort the collection inplace. The elements are compared using |
|
2069 | 2679 |
'<' i.e. they should offer a magnitude-like protocol. |
159 | 2680 |
The implementation uses the quicksort algorithm, which may not be |
2681 |
the best possible for all situations." |
|
13 | 2682 |
|
32 | 2683 |
|stop| |
1 | 2684 |
|
32 | 2685 |
stop := self size. |
2686 |
(stop > 1) ifTrue:[ |
|
2069 | 2687 |
self quickSortFrom:1 to:stop |
1 | 2688 |
] |
2689 |
||
95 | 2690 |
" |
2691 |
#(1 16 7 98 3 19 4 0) sort |
|
2692 |
||
2693 |
|data| |
|
2694 |
data := Random new next:100000. |
|
2695 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2696 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2697 |
data reverse. |
|
2698 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2699 |
" |
|
2069 | 2700 |
|
2701 |
"Modified: 5.1.1997 / 17:51:03 / cg" |
|
1 | 2702 |
! |
2703 |
||
2704 |
sort:sortBlock |
|
2705 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2706 |
for comparison. This allows any sort criteria to be implemented." |
|
2707 |
||
32 | 2708 |
|stop| |
1 | 2709 |
|
32 | 2710 |
stop := self size. |
2711 |
(stop > 1) ifTrue:[ |
|
2069 | 2712 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
1 | 2713 |
] |
2714 |
||
95 | 2715 |
" |
2069 | 2716 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
2717 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
2718 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
2719 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 2720 |
" |
2069 | 2721 |
|
2722 |
"Modified: 5.1.1997 / 17:52:27 / cg" |
|
1 | 2723 |
! |
2724 |
||
2725 |
sort:sortBlock with:aCollection |
|
2726 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 2727 |
for comparison. Also reorder the elements in aCollection. |
2069 | 2728 |
Use this, when you have a key collection to sort some other collection with." |
1 | 2729 |
|
32 | 2730 |
|stop| |
1 | 2731 |
|
32 | 2732 |
stop := self size. |
2733 |
(stop > 1) ifTrue:[ |
|
2069 | 2734 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
1 | 2735 |
] |
2736 |
||
69 | 2737 |
" |
2738 |
|c1 c2| |
|
1 | 2739 |
c1 := #(1 16 7 9). |
2740 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
2741 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 2742 |
c1 printNL. |
2743 |
c2 printNL |
|
69 | 2744 |
" |
2069 | 2745 |
|
2746 |
"Modified: 5.1.1997 / 17:52:45 / cg" |
|
1 | 2747 |
! |
2748 |
||
602 | 2749 |
sortWith:aCollection |
2069 | 2750 |
"sort the receiver collection inplace, using '<' to compare elements. |
2751 |
Also, the elements of aCollection are reordered with it. |
|
2752 |
Use this, when you have a key collection to sort another collection with." |
|
602 | 2753 |
|
2754 |
|stop| |
|
356 | 2755 |
|
2756 |
stop := self size. |
|
602 | 2757 |
(stop > 1) ifTrue:[ |
2069 | 2758 |
self quickSortFrom:1 to:stop with:aCollection |
1 | 2759 |
] |
77 | 2760 |
|
2761 |
" |
|
602 | 2762 |
|c1 c2| |
2763 |
c1 := #(1 16 7 9). |
|
2764 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
2765 |
c1 sortWith:c2. |
|
2766 |
c1 printNL. |
|
2767 |
c2 printNL |
|
424 | 2768 |
" |
2069 | 2769 |
|
2770 |
"Modified: 5.1.1997 / 17:53:26 / cg" |
|
424 | 2771 |
! |
2772 |
||
602 | 2773 |
topologicalSort:sortBlock |
2774 |
"sort the collection inplace using a sloooow sort algorithm. |
|
2775 |
This algorithm has O-square runtime behavior and should be used only |
|
2776 |
in special situations. |
|
2777 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
2778 |
a>c (for example, to sort classes by inheritance) |
|
2779 |
||
2780 |
In other situations, use #sort; which implements the quicksort algorithm. |
|
77 | 2781 |
" |
602 | 2782 |
|
2783 |
|smallestIndex "{ Class: SmallInteger }" |
|
2784 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2785 |
index2 "{ Class: SmallInteger }" |
602 | 2786 |
smallest thisOne| |
2787 |
||
2788 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
2789 |
||
2790 |
end := self size. |
|
862 | 2791 |
1 to:end-1 do:[:index | |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2792 |
smallest := self at:index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2793 |
smallestIndex := index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2794 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2795 |
index2 := index + 1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2796 |
[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2797 |
thisOne := self at:index2. |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2798 |
(index2 ~~ smallestIndex |
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2799 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2800 |
smallestIndex := index2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2801 |
smallest := thisOne. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2802 |
index2 := index + 1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2803 |
] ifFalse:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2804 |
index2 := index2 + 1.. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2805 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2806 |
] doWhile:[index2 <= end]. |
862 | 2807 |
|
2808 |
"/ (index + 1) to:end do:[:index2 | |
|
2809 |
"/ thisOne := self at:index2. |
|
2810 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
2811 |
"/ smallestIndex := index2. |
|
2812 |
"/ smallest := thisOne |
|
2813 |
"/ ]. |
|
2814 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
2815 |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2816 |
(smallestIndex ~~ index) ifTrue:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2817 |
thisOne := self at:index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2818 |
self at:index put:smallest. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2819 |
self at:smallestIndex put:thisOne |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2820 |
]. |
1 | 2821 |
] |
95 | 2822 |
|
77 | 2823 |
" |
602 | 2824 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
2825 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
2826 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
2827 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 2828 |
" |
1 | 2829 |
! ! |
602 | 2830 |
|
2069 | 2831 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 2832 |
|
2833 |
version |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2834 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.71 1997-01-14 15:19:42 cg Exp $' |
628 | 2835 |
! ! |