OrderedCollection.st
changeset 2361 dd95b53674e9
parent 2355 9476ba4d810d
child 2362 23574f29c8af
equal deleted inserted replaced
2360:853d805159cf 2361:dd95b53674e9
   513 
   513 
   514     |index retVal|
   514     |index retVal|
   515 
   515 
   516     index := contentsArray indexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   516     index := contentsArray indexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   517     index ~~ 0 ifTrue:[
   517     index ~~ 0 ifTrue:[
   518         "/ mhmh - should we use removeAtIndex: here ?
       
   519 
       
   520         retVal := contentsArray at:index.
   518         retVal := contentsArray at:index.
   521         
   519         index := index - firstIndex + 1.
   522         index == firstIndex ifTrue:[
   520         self removeFromIndex:index toIndex:index.
   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             ].
       
   536 
       
   537             "/ nil it (helps GC)
       
   538             contentsArray at:lastIndex put:nil.
       
   539             lastIndex := lastIndex - 1.
       
   540         ].
       
   541 
       
   542         firstIndex > lastIndex ifTrue:[
       
   543             "reset to avoid ever growing"
       
   544             firstIndex := 1.
       
   545             lastIndex := 0 
       
   546         ].
       
   547         ^ retVal
   521         ^ retVal
   548     ].
   522     ].
   549     ^ exceptionBlock value
   523     ^ exceptionBlock value
   550 
   524 
   551     "
   525     "
   556      #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' 
   530      #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' 
   557      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
   531      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
   558      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   532      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   559     "
   533     "
   560 
   534 
   561     "Modified: 1.2.1997 / 12:29:55 / cg"
   535     "Modified: 8.2.1997 / 18:59:22 / cg"
   562 !
   536 !
   563 
   537 
   564 removeAll
   538 removeAll
   565     "remove all elements from the collection.
   539     "remove all elements from the collection.
   566      Returns the receiver."
   540      Returns the receiver."
   763 
   737 
   764 removeIdentical:anObject ifAbsent:exceptionBlock
   738 removeIdentical:anObject ifAbsent:exceptionBlock
   765     "remove the first element which is identical to anObject;
   739     "remove the first element which is identical to anObject;
   766      if found, remove and return it; 
   740      if found, remove and return it; 
   767      if not, return the value from evaluating exceptionBlock.
   741      if not, return the value from evaluating exceptionBlock.
   768      Used identity compare (==) to search for the element."
   742      Uses identity compare (==) to search for the element."
   769 
   743 
   770     |index|
   744     |index|
   771 
   745 
   772     index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   746     index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   773     index ~~ 0 ifTrue:[
   747     index ~~ 0 ifTrue:[
   774         "/ mhmh - should we use removeAtIndex: here ?
   748         index := index - firstIndex + 1.
   775 
   749         self removeFromIndex:index toIndex:index.
   776         index == firstIndex ifTrue:[
       
   777             "/ nil it (helps GC)
       
   778             contentsArray at:firstIndex put:nil.
       
   779             firstIndex := firstIndex + 1.
       
   780         ] ifFalse:[
       
   781             index ~~ lastIndex ifTrue:[
       
   782                 "/ shuffle
       
   783 
       
   784                 contentsArray 
       
   785                     replaceFrom:index
       
   786                     to:(contentsArray size - 1)
       
   787                     with:contentsArray 
       
   788                     startingAt:(index + 1).
       
   789             ].
       
   790 
       
   791             "/ nil it (helps GC)
       
   792             contentsArray at:lastIndex put:nil.
       
   793             lastIndex := lastIndex - 1.
       
   794         ].
       
   795 
       
   796         firstIndex > lastIndex ifTrue:[
       
   797             "reset to avoid ever growing"
       
   798             firstIndex := 1.
       
   799             lastIndex := 0 
       
   800         ].
       
   801         ^ anObject
   750         ^ anObject
   802     ].
   751     ].
   803     ^ exceptionBlock value
   752     ^ exceptionBlock value
   804 
   753 
   805     "
   754     "
   806      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
   755      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
       
   756      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'; yourself 
   807      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   757      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
   808     "
   758      #(fee foo bar baz) asOrderedCollection removeIdentical:#fee; yourself
   809 
   759      #(fee foo bar baz) asOrderedCollection removeIdentical:#foo; yourself
   810     "Modified: 1.2.1997 / 12:29:47 / cg"
   760      #(fee foo bar baz) asOrderedCollection removeIdentical:#baz; yourself
       
   761      #(fee) asOrderedCollection removeIdentical:#fee; yourself
       
   762     "
       
   763 
       
   764     "Modified: 8.2.1997 / 18:57:43 / cg"
   811 !
   765 !
   812 
   766 
   813 removeLast
   767 removeLast
   814     "remove the last element from the collection; return the element"
   768     "remove the last element from the collection; return the element"
   815 
   769 
  1661 ! !
  1615 ! !
  1662 
  1616 
  1663 !OrderedCollection class methodsFor:'documentation'!
  1617 !OrderedCollection class methodsFor:'documentation'!
  1664 
  1618 
  1665 version
  1619 version
  1666     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.58 1997-02-01 11:54:38 cg Exp $'
  1620     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.59 1997-02-08 18:00:05 cg Exp $'
  1667 ! !
  1621 ! !