OrderedCollection.st
changeset 2354 652d17daeeb5
parent 2348 523e9d6e0049
child 2355 9476ba4d810d
equal deleted inserted replaced
2353:ce92d04c41cc 2354:652d17daeeb5
   509     "remove the first element which is equal to anObject;
   509     "remove the first element which is equal to anObject;
   510      if found, remove and return it; 
   510      if found, remove and return it; 
   511      if not, return the value from evaluating exceptionBlock.
   511      if not, return the value from evaluating exceptionBlock.
   512      Used equality compare (=) to search for the element."
   512      Used equality compare (=) to search for the element."
   513 
   513 
   514     |index "{ Class:SmallInteger }"|
   514     |index retVal|
   515 
   515 
   516     index := firstIndex.
   516     index := contentsArray indexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   517     [index <= lastIndex] whileTrue:[
   517     index ~~ 0 ifTrue:[
   518         anObject = (contentsArray at:index) ifTrue:[
   518         "/ mhmh - should we use removeAtIndex: here ?
   519             contentsArray 
   519 
   520                 replaceFrom:index
   520         retVal := contentsArray at:index.
   521                 to:(contentsArray size - 1)
   521         
   522                 with:contentsArray 
   522         index == firstIndex ifTrue:[
   523                 startingAt:(index + 1).
   523             "/ nil it (helps GC)
       
   524             contentsArray at:firstIndex put:nil.
       
   525             firstIndex := firstIndex + 1.
       
   526         ] ifFalse:[
       
   527             index ~~ lastIndex ifTrue:[
       
   528                 "/ shuffle
       
   529 
       
   530                 contentsArray 
       
   531                     replaceFrom:index
       
   532                     to:(contentsArray size - 1)
       
   533                     with:contentsArray 
       
   534                     startingAt:(index + 1).
       
   535             ].
   524 
   536 
   525             "/ nil it (helps GC)
   537             "/ nil it (helps GC)
   526             contentsArray at:lastIndex put:nil.
   538             contentsArray at:lastIndex put:nil.
   527 
       
   528             lastIndex := lastIndex - 1.
   539             lastIndex := lastIndex - 1.
   529             firstIndex > lastIndex ifTrue:[
       
   530                 "reset to avoid ever growing"
       
   531                 firstIndex := 1.
       
   532                 lastIndex := 0 
       
   533             ].
       
   534             ^ anObject
       
   535         ].
   540         ].
   536         index := index + 1
   541 
       
   542         firstIndex > lastIndex ifTrue:[
       
   543             "reset to avoid ever growing"
       
   544             firstIndex := 1.
       
   545             lastIndex := 0 
       
   546         ].
       
   547         ^ retVal
   537     ].
   548     ].
   538     ^ exceptionBlock value
   549     ^ exceptionBlock value
   539 
   550 
   540     "
   551     "
   541      #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt]
   552      #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt]
   545      #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' 
   556      #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' 
   546      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
   557      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
   547      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   558      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   548     "
   559     "
   549 
   560 
   550     "Modified: 1.2.1997 / 11:53:12 / cg"
   561     "Modified: 1.2.1997 / 12:29:55 / cg"
   551 !
   562 !
   552 
   563 
   553 removeAll
   564 removeAll
   554     "remove all elements from the collection.
   565     "remove all elements from the collection.
   555      Returns the receiver."
   566      Returns the receiver."
   604     "remove the first element from the collection; return the element."
   615     "remove the first element from the collection; return the element."
   605 
   616 
   606     |anObject |
   617     |anObject |
   607 
   618 
   608     firstIndex > lastIndex ifTrue:[
   619     firstIndex > lastIndex ifTrue:[
   609 	"error if collection is empty"
   620         "error if collection is empty"
   610 	^ self emptyCollectionError.
   621         ^ self emptyCollectionError.
   611     ].
   622     ].
       
   623 
   612     anObject := contentsArray at:firstIndex.
   624     anObject := contentsArray at:firstIndex.
   613 
   625 
   614     "/ nil it out, to allow GC to reclaim it.
   626     "/ nil it out, to allow GC to reclaim it.
   615 
       
   616     contentsArray at:firstIndex put:nil.
   627     contentsArray at:firstIndex put:nil.
       
   628 
   617     firstIndex := firstIndex + 1.
   629     firstIndex := firstIndex + 1.
   618 
   630 
   619     firstIndex > lastIndex ifTrue:[
   631     firstIndex > lastIndex ifTrue:[
   620 	"reset to avoid ever growing"
   632         "reset to avoid ever growing"
   621 	firstIndex := 1.
   633         firstIndex := 1.
   622 	lastIndex := 0 
   634         lastIndex := 0 
   623     ].
   635     ].
   624     ^ anObject
   636     ^ anObject
   625 
   637 
   626     "
   638     "
   627      (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
   639      (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
   628      OrderedCollection new removeFirst
   640      OrderedCollection new removeFirst
   629      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
   641      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
   630     "
   642     "
       
   643 
       
   644     "Modified: 1.2.1997 / 12:30:50 / cg"
   631 !
   645 !
   632 
   646 
   633 removeFirst:n
   647 removeFirst:n
   634     "remove the first n elements from the collection; 
   648     "remove the first n elements from the collection; 
   635      Return a collection containing the removed elements."
   649      Return a collection containing the removed elements."
   711 
   725 
   712 removeIdentical:anObject ifAbsent:exceptionBlock
   726 removeIdentical:anObject ifAbsent:exceptionBlock
   713     "remove the first element which is identical to anObject;
   727     "remove the first element which is identical to anObject;
   714      if found, remove and return it; 
   728      if found, remove and return it; 
   715      if not, return the value from evaluating exceptionBlock.
   729      if not, return the value from evaluating exceptionBlock.
   716      Used equality compare (==) to search for the element."
   730      Used identity compare (==) to search for the element."
   717 
   731 
   718     |index "{ Class:SmallInteger }"|
   732     |index|
   719 
   733 
   720     index := firstIndex.
   734     index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   721     [index <= lastIndex] whileTrue:[
   735     index ~~ 0 ifTrue:[
   722         anObject == (contentsArray at:index) ifTrue:[
   736         "/ mhmh - should we use removeAtIndex: here ?
   723             contentsArray 
   737 
   724                 replaceFrom:index
   738         index == firstIndex ifTrue:[
   725                 to:(contentsArray size - 1)
   739             "/ nil it (helps GC)
   726                 with:contentsArray 
   740             contentsArray at:firstIndex put:nil.
   727                 startingAt:(index + 1).
   741             firstIndex := firstIndex + 1.
       
   742         ] ifFalse:[
       
   743             index ~~ lastIndex ifTrue:[
       
   744                 "/ shuffle
       
   745 
       
   746                 contentsArray 
       
   747                     replaceFrom:index
       
   748                     to:(contentsArray size - 1)
       
   749                     with:contentsArray 
       
   750                     startingAt:(index + 1).
       
   751             ].
   728 
   752 
   729             "/ nil it (helps GC)
   753             "/ nil it (helps GC)
   730             contentsArray at:lastIndex put:nil.
   754             contentsArray at:lastIndex put:nil.
   731 
       
   732             lastIndex := lastIndex - 1.
   755             lastIndex := lastIndex - 1.
   733             firstIndex > lastIndex ifTrue:[
       
   734                 "reset to avoid ever growing"
       
   735                 firstIndex := 1.
       
   736                 lastIndex := 0 
       
   737             ].
       
   738             ^ anObject
       
   739         ].
   756         ].
   740         index := index + 1
   757 
       
   758         firstIndex > lastIndex ifTrue:[
       
   759             "reset to avoid ever growing"
       
   760             firstIndex := 1.
       
   761             lastIndex := 0 
       
   762         ].
       
   763         ^ anObject
   741     ].
   764     ].
   742     ^ exceptionBlock value
   765     ^ exceptionBlock value
   743 
   766 
   744     "
   767     "
   745      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'  
   768      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
   746      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   769      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   747     "
   770     "
   748 
   771 
   749     "Modified: 1.2.1997 / 11:53:47 / cg"
   772     "Modified: 1.2.1997 / 12:29:47 / cg"
   750 !
   773 !
   751 
   774 
   752 removeLast
   775 removeLast
   753     "remove the last element from the collection; return the element"
   776     "remove the last element from the collection; return the element"
   754 
   777 
  1600 ! !
  1623 ! !
  1601 
  1624 
  1602 !OrderedCollection class methodsFor:'documentation'!
  1625 !OrderedCollection class methodsFor:'documentation'!
  1603 
  1626 
  1604 version
  1627 version
  1605     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.56 1997-02-01 10:54:01 cg Exp $'
  1628     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.57 1997-02-01 11:37:19 cg Exp $'
  1606 ! !
  1629 ! !