223 firstIndex := 1. |
225 firstIndex := 1. |
224 lastIndex := 0 |
226 lastIndex := 0 |
225 ]. |
227 ]. |
226 ^ anObject |
228 ^ anObject |
227 |
229 |
228 "(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself" |
230 " |
229 "(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself" |
231 (OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself |
|
232 OrderedCollection new removeLast |
|
233 (SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself |
|
234 " |
|
235 ! |
|
236 |
|
237 removeFirst:n |
|
238 "remove the first n elements from the collection; |
|
239 return the elements in an Array" |
|
240 |
|
241 |mySize ret| |
|
242 |
|
243 mySize := self size. |
|
244 mySize < n ifTrue:[ |
|
245 "error if collection has not enough elements" |
|
246 ^ self notEnoughElementsError. |
|
247 ]. |
|
248 |
|
249 ret := Array new:n. |
|
250 ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex. |
|
251 "/ |
|
252 "/ nil-out contents array, to not keep elements from being GC'd |
|
253 "/ |
|
254 contentsArray from:firstIndex to:firstIndex + n - 1 put:nil. |
|
255 firstIndex := firstIndex + n. |
|
256 |
|
257 firstIndex > lastIndex ifTrue:[ |
|
258 "reset to avoid ever growing" |
|
259 firstIndex := 1. |
|
260 lastIndex := 0 |
|
261 ]. |
|
262 ^ ret |
|
263 |
|
264 " |
|
265 (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself |
|
266 (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself |
|
267 OrderedCollection new removeFirst:2 |
|
268 (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 |
|
269 (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself |
|
270 " |
|
271 ! |
|
272 |
|
273 removeLast:n |
|
274 "remove the last n elements from the collection; |
|
275 return the elements in an Array" |
|
276 |
|
277 |mySize ret| |
|
278 |
|
279 mySize := self size. |
|
280 mySize < n ifTrue:[ |
|
281 "error if collection has not enough elements" |
|
282 ^ self notEnoughElementsError. |
|
283 ]. |
|
284 |
|
285 ret := Array new:n. |
|
286 ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1. |
|
287 "/ |
|
288 "/ nil-out contents array, to not keep elements from being GC'd |
|
289 "/ |
|
290 contentsArray from:lastIndex - n + 1 to:lastIndex put:nil. |
|
291 lastIndex := lastIndex - n. |
|
292 |
|
293 firstIndex > lastIndex ifTrue:[ |
|
294 "reset to avoid ever growing" |
|
295 firstIndex := 1. |
|
296 lastIndex := 0 |
|
297 ]. |
|
298 ^ ret |
|
299 |
|
300 " |
|
301 (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself |
|
302 (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself |
|
303 (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself |
|
304 (SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself |
|
305 " |
230 ! |
306 ! |
231 |
307 |
232 removeFromIndex:startIndex toIndex:stopIndex |
308 removeFromIndex:startIndex toIndex:stopIndex |
233 "remove the elements stored under startIndex up to and including |
309 "remove the elements stored under startIndex up to and including |
234 the elements under stopIndex. |
310 the elements under stopIndex. |
235 return the receiver." |
311 Return the receiver. |
|
312 Returning the receiver here is a historic leftover - it may change. |
|
313 Please use yourself in a cascade, if you need the receivers value |
|
314 when using this method." |
236 |
315 |
237 |nDeleted| |
316 |nDeleted| |
238 |
317 |
|
318 (startIndex < firstIndex |
|
319 or:[stopIndex > lastIndex]) ifTrue:[ |
|
320 ^ self notEnoughElementsError |
|
321 ]. |
239 nDeleted := stopIndex - startIndex + 1. |
322 nDeleted := stopIndex - startIndex + 1. |
240 contentsArray |
323 contentsArray |
241 replaceFrom:(firstIndex + startIndex - 1) |
324 replaceFrom:(firstIndex + startIndex - 1) |
242 to:(lastIndex - nDeleted) |
325 to:(lastIndex - nDeleted) |
243 with:contentsArray |
326 with:contentsArray |
285 ^ anObject |
369 ^ anObject |
286 ]. |
370 ]. |
287 index := index + 1 |
371 index := index + 1 |
288 ]. |
372 ]. |
289 ^ exceptionBlock value |
373 ^ exceptionBlock value |
|
374 |
|
375 " |
|
376 #(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops' |
|
377 #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' |
|
378 " |
290 ! |
379 ! |
291 |
380 |
292 removeAll |
381 removeAll |
293 "remove all elements from the collection." |
382 "remove all elements from the collection." |
294 |
383 |
295 self initContents:10 |
384 self initContents:10 |
|
385 ! |
|
386 |
|
387 removeAllSuchThat:aBlock |
|
388 "remove all elements that meet a test criteria as specified in aBlock. |
|
389 The argument, aBlock is evaluated for successive elements and all those, |
|
390 for which it returns true, are removed." |
|
391 |
|
392 "/ this is a q&d implementation (slow). |
|
393 "/ it should be rewritten to |
|
394 |
|
395 |runIndex removed element| |
|
396 |
|
397 removed := self species new. |
|
398 |
|
399 runIndex := 1. |
|
400 [runIndex <= self size] whileTrue:[ |
|
401 element := self at:runIndex. |
|
402 (aBlock value:element) ifTrue:[ |
|
403 removed add:element. |
|
404 self removeAtIndex:runIndex |
|
405 ] ifFalse:[ |
|
406 runIndex := runIndex + 1 |
|
407 ] |
|
408 ]. |
|
409 ^ removed |
|
410 |
|
411 " |
|
412 |coll| |
|
413 |
|
414 coll := OrderedCollection withAll:(1 to:10). |
|
415 Transcript showCr:(coll removeAllSuchThat:[:el | el even]). |
|
416 Transcript showCr:coll |
|
417 " |
296 ! |
418 ! |
297 |
419 |
298 add:anObject |
420 add:anObject |
299 "add the argument, anObject to the end of the collection |
421 "add the argument, anObject to the end of the collection |
300 Return the argument, anObject." |
422 Return the argument, anObject." |
570 |
692 |
571 after:anObject |
693 after:anObject |
572 "return the element, after anObject. |
694 "return the element, after anObject. |
573 If anObject is not in the receiver, report an error." |
695 If anObject is not in the receiver, report an error." |
574 |
696 |
575 ^ self after:anObject ifAbsent:[self errorNotFound:anObject] |
697 ^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
576 |
698 |
577 " |
699 " |
578 #(4 3 2 1) asOrderedCollection after:3. |
700 #(4 3 2 1) asOrderedCollection after:3. |
579 #(4 3 2 1) asOrderedCollection after:5 |
701 #(4 3 2 1) asOrderedCollection after:5 |
580 #(4 3 2 1) asOrderedCollection after:1 |
702 #(4 3 2 1) asOrderedCollection after:1 |
603 |
725 |
604 before:anObject |
726 before:anObject |
605 "return the element before the argument, anObject. |
727 "return the element before the argument, anObject. |
606 If anObject is not in the receiver, report an error." |
728 If anObject is not in the receiver, report an error." |
607 |
729 |
608 ^ self before:anObject ifAbsent:[self errorNotFound:anObject] |
730 ^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
609 |
731 |
610 " |
732 " |
611 #(4 3 2 1) asOrderedCollection before:3. |
733 #(4 3 2 1) asOrderedCollection before:3. |
612 #(4 3 2 1) asOrderedCollection before:4 |
734 #(4 3 2 1) asOrderedCollection before:4 |
613 #(4 3 2 1) asOrderedCollection before:0 |
735 #(4 3 2 1) asOrderedCollection before:0 |