OrdColl.st
changeset 348 5ac1b6b43600
parent 345 cf2301210c47
child 349 33d5e92c4ce7
equal deleted inserted replaced
347:0d4c08ca9da3 348:5ac1b6b43600
    19 
    19 
    20 OrderedCollection comment:'
    20 OrderedCollection comment:'
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    22 	      All Rights Reserved
    22 	      All Rights Reserved
    23 
    23 
    24 $Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.24 1995-05-16 17:08:13 claus Exp $
    24 $Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.25 1995-05-18 15:09:50 claus Exp $
    25 '!
    25 '!
    26 
    26 
    27 !OrderedCollection class methodsFor:'documentation'!
    27 !OrderedCollection class methodsFor:'documentation'!
    28 
    28 
    29 copyright
    29 copyright
    40 "
    40 "
    41 !
    41 !
    42 
    42 
    43 version
    43 version
    44 "
    44 "
    45 $Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.24 1995-05-16 17:08:13 claus Exp $
    45 $Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.25 1995-05-18 15:09:50 claus Exp $
    46 "
    46 "
    47 !
    47 !
    48 
    48 
    49 documentation
    49 documentation
    50 "
    50 "
   198 	firstIndex := 1.
   198 	firstIndex := 1.
   199 	lastIndex := 0 
   199 	lastIndex := 0 
   200     ].
   200     ].
   201     ^ anObject
   201     ^ anObject
   202 
   202 
   203     "(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself"
   203     "
   204     "(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself"
   204      (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
   205     "(SortedCollection new) removeFirst"
   205      OrderedCollection new removeFirst
       
   206      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
       
   207     "
   206 !
   208 !
   207 
   209 
   208 removeLast
   210 removeLast
   209     "remove the last element from the collection; return the element"
   211     "remove the last element from the collection; return the element"
   210 
   212 
   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 
   255 	lastIndex := 0 
   338 	lastIndex := 0 
   256     ]
   339     ]
   257 
   340 
   258     "
   341     "
   259      #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
   342      #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
       
   343      #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
   260     "
   344     "
   261 !
   345 !
   262 
   346 
   263 remove:anObject ifAbsent:exceptionBlock
   347 remove:anObject ifAbsent:exceptionBlock
   264     "remove the first occurrence of anObject from the collection;
   348     "remove the first occurrence of anObject from the collection;
   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."
   391 
   513 
   392     idx := self indexOf:oldObject.
   514     idx := self indexOf:oldObject.
   393     idx ~~ 0 ifTrue:[
   515     idx ~~ 0 ifTrue:[
   394 	^ self add:newObject beforeIndex:(idx + 1).
   516 	^ self add:newObject beforeIndex:(idx + 1).
   395     ].
   517     ].
   396     self errorNotFound:oldObject
   518     self errorValueNotFound:oldObject
   397 
   519 
   398     "
   520     "
   399      |c|
   521      |c|
   400      c := #(4 3 2 1) asOrderedCollection.
   522      c := #(4 3 2 1) asOrderedCollection.
   401      c add:'here' after:3. 
   523      c add:'here' after:3. 
   422 
   544 
   423     idx := self indexOf:oldObject.
   545     idx := self indexOf:oldObject.
   424     idx ~~ 0 ifTrue:[
   546     idx ~~ 0 ifTrue:[
   425 	^ self add:newObject beforeIndex:idx.
   547 	^ self add:newObject beforeIndex:idx.
   426     ].
   548     ].
   427     self errorNotFound:oldObject
   549     self errorValueNotFound:oldObject
   428 
   550 
   429     "
   551     "
   430      |c|
   552      |c|
   431      c := #(4 3 2 1) asOrderedCollection.
   553      c := #(4 3 2 1) asOrderedCollection.
   432      c add:'here' before:3.
   554      c add:'here' before:3.
   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