OrderedCollection.st
changeset 16304 2d8de035fd7a
parent 15757 f7e84eeeaaa9
child 16305 964f7ffb315d
equal deleted inserted replaced
16303:aad4b514271c 16304:2d8de035fd7a
   591 
   591 
   592 clearContents
   592 clearContents
   593     "remove all elements from the collection but keep the contentsArray.
   593     "remove all elements from the collection but keep the contentsArray.
   594      Useful for huge lists, if the contents will be rebuild soon (using #add:) 
   594      Useful for huge lists, if the contents will be rebuild soon (using #add:) 
   595      to a size which is similar to the lists current size.
   595      to a size which is similar to the lists current size.
       
   596      Destructive: modifies the receiver.
   596      Returns the receiver."
   597      Returns the receiver."
   597 
   598 
   598     "/ clear those references, to give the garbage collector
   599     "/ clear those references, to give the garbage collector
   599     "/ a chance ...
   600     "/ a chance ...
   600 
   601 
   612 
   613 
   613 remove:anObject ifAbsent:exceptionBlock
   614 remove:anObject ifAbsent:exceptionBlock
   614     "remove the first element which is equal to anObject;
   615     "remove the first element which is equal to anObject;
   615      if found, remove and return it; 
   616      if found, remove and return it; 
   616      if not, return the value from evaluating exceptionBlock.
   617      if not, return the value from evaluating exceptionBlock.
       
   618      Destructive: modifies the receiver.
   617      Uses equality compare (=) to search for the element."
   619      Uses equality compare (=) to search for the element."
   618 
   620 
   619     |index retVal|
   621     |index retVal|
   620 
   622 
   621     index := self indexOf:anObject.
   623     index := self indexOf:anObject.
   639     "Modified: 8.2.1997 / 19:17:19 / cg"
   641     "Modified: 8.2.1997 / 19:17:19 / cg"
   640 !
   642 !
   641 
   643 
   642 removeAll
   644 removeAll
   643     "remove all elements from the collection.
   645     "remove all elements from the collection.
   644      Returns the receiver."
   646      Returns the receiver.
       
   647      Destructive: modifies the receiver."
   645 
   648 
   646     contentsArray size <= 20 ifTrue:[
   649     contentsArray size <= 20 ifTrue:[
   647         "/ reuse the contents array
   650         "/ reuse the contents array
   648         contentsArray atAllPut:nil.
   651         contentsArray atAllPut:nil.
   649         firstIndex := 1.
   652         firstIndex := 1.
   657 
   660 
   658 removeAllSuchThat:aBlock
   661 removeAllSuchThat:aBlock
   659     "remove all elements that meet a test criteria as specified in aBlock.
   662     "remove all elements that meet a test criteria as specified in aBlock.
   660      The argument, aBlock is evaluated for successive elements and all those,
   663      The argument, aBlock is evaluated for successive elements and all those,
   661      for which it returns true, are removed.
   664      for which it returns true, are removed.
       
   665      Destructive: modifies the receiver.
   662      Return a collection containing the removed elements.
   666      Return a collection containing the removed elements.
   663 
   667 
   664      Performance hint:
   668      Performance: 
   665         if a large number of objects is to be removed this way,
   669         this is an O(N) algorithm.
       
   670 
       
   671         If a large number of objects is to be removed this way,
   666         it may be better to rebuild a new collection via #select:,
   672         it may be better to rebuild a new collection via #select:,
   667         since removing elements out of the middle is somewhat slow
   673         since removing elements out of the middle is somewhat slow
   668         due to the need to copy over remaining elements within the collection."
   674         due to the need to copy over remaining elements within the collection."
   669 
   675 
   670     "/ this is a q&d implementation (possibly slow).
   676     "/ this is a q&d implementation (possibly slow).
   671 
   677 
   672     |runIndex removed element sz|
   678     |removed element dstIndex|
   673 
   679 
   674     removed := OrderedCollection new.
   680     removed := OrderedCollection new.
   675 
   681 
   676     runIndex := 1.
   682     dstIndex := firstIndex.
   677     sz := self size.
   683     firstIndex to: lastIndex do:[:srcIndex |
   678     [runIndex <= sz] whileTrue:[
   684         element := contentsArray at:srcIndex.
   679         element := self at:runIndex.
       
   680         (aBlock value:element) ifTrue:[
   685         (aBlock value:element) ifTrue:[
   681             removed add:element.
   686             removed add:element
   682             self removeAtIndex:runIndex.
       
   683             sz := sz - 1.
       
   684         ] ifFalse:[
   687         ] ifFalse:[
   685             runIndex := runIndex + 1
   688             contentsArray at:dstIndex put:element.
   686         ]
   689             dstIndex := dstIndex + 1.
   687     ].
   690         ].
       
   691     ].
       
   692     contentsArray from:dstIndex to:lastIndex put:nil.
       
   693     lastIndex := dstIndex - 1.
   688     ^ removed
   694     ^ removed
   689 
   695 
   690     "
   696     "
   691      |coll|
   697      |coll|
   692 
   698 
   693      coll := OrderedCollection withAll:(1 to:10).
   699      coll := OrderedCollection withAll:(1 to:10).
   694      Transcript showCR:(coll removeAllSuchThat:[:el | el even]).
   700      Transcript showCR:(coll removeAllSuchThat:[:el | el even]).
   695      Transcript showCR:coll
   701      Transcript showCR:coll
   696     "
   702     "
       
   703     "
       
   704      |coll1 coll2|
       
   705 
       
   706      coll1 := OrderedCollection withAll:(1 to:1000).
       
   707      Transcript show:'(1000) - '; showCR:(
       
   708         Time millisecondsToRun:[
       
   709             100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el even] ]
       
   710         ]).
       
   711 
       
   712      coll2 := OrderedCollection withAll:(1 to:10000).
       
   713      Transcript show:'(10000) - '; showCR:(
       
   714         Time millisecondsToRun:[
       
   715             100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ]
       
   716         ]).
       
   717 
       
   718      coll2 := OrderedCollection withAll:(1 to:50000).
       
   719      Transcript show:'(50000) - '; showCR:(
       
   720         Time millisecondsToRun:[
       
   721             100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ]
       
   722         ]).
       
   723     "
   697 
   724 
   698     "Modified: 8.2.1997 / 19:19:00 / cg"
   725     "Modified: 8.2.1997 / 19:19:00 / cg"
   699 !
   726 !
   700 
   727 
   701 removeFirst
   728 removeFirst
   702     "remove the first element from the collection; return the element.
   729     "remove the first element from the collection; return the element.
   703      If there is no element in the receiver collection, raise an error."
   730      If there is no element in the receiver collection, raise an error.
       
   731      Destructive: modifies the receiver"
   704 
   732 
   705     |anObject 
   733     |anObject 
   706      fI "{ Class: SmallInteger }" |
   734      fI "{ Class: SmallInteger }" |
   707 
   735 
   708     fI := firstIndex.
   736     fI := firstIndex.
   736     "Modified: / 5.2.1999 / 23:22:58 / cg"
   764     "Modified: / 5.2.1999 / 23:22:58 / cg"
   737 !
   765 !
   738 
   766 
   739 removeFirst:n
   767 removeFirst:n
   740     "remove the first n elements from the collection; 
   768     "remove the first n elements from the collection; 
   741      Return a collection containing the removed elements."
   769      Return a collection containing the removed elements.
       
   770      Destructive: modifies the receiver"
   742 
   771 
   743     |mySize ret newFirstIndex|
   772     |mySize ret newFirstIndex|
   744 
   773 
   745     mySize := self size.
   774     mySize := self size.
   746     mySize < n ifTrue:[
   775     mySize < n ifTrue:[
   777 !
   806 !
   778 
   807 
   779 removeFirstIfAbsent:exceptionBlock
   808 removeFirstIfAbsent:exceptionBlock
   780     "remove the first element from the collection; return the element.
   809     "remove the first element from the collection; return the element.
   781      If there is no element in the receiver collection, return the value from
   810      If there is no element in the receiver collection, return the value from
   782      exceptionBlock."
   811      exceptionBlock.
       
   812      Destructive: modifies the receiver"
   783 
   813 
   784     |anObject fI "{ Class: SmallInteger }" |
   814     |anObject fI "{ Class: SmallInteger }" |
   785 
   815 
   786     fI := firstIndex.
   816     fI := firstIndex.
   787 
   817 
   825 !
   855 !
   826 
   856 
   827 removeFromIndex:startIndex toIndex:stopIndex
   857 removeFromIndex:startIndex toIndex:stopIndex
   828     "remove the elements stored under startIndex up to and including
   858     "remove the elements stored under startIndex up to and including
   829      the elements under stopIndex.
   859      the elements under stopIndex.
       
   860      Destructive: modifies the receiver
   830      Return the receiver.
   861      Return the receiver.
   831      Returning the receiver here is a historic leftover - it may change.
   862      Returning the receiver here is a historic leftover - it may change.
   832      Please use yourself in a cascade, if you need the receiver's value
   863      Please use yourself in a cascade, if you need the receiver's value
   833      when using this method."
   864      when using this method."
   834 
   865 
   918 
   949 
   919 removeIdentical:anObject ifAbsent:exceptionBlock
   950 removeIdentical:anObject ifAbsent:exceptionBlock
   920     "remove the first element which is identical to anObject;
   951     "remove the first element which is identical to anObject;
   921      if found, remove and return it; 
   952      if found, remove and return it; 
   922      if not, return the value from evaluating exceptionBlock.
   953      if not, return the value from evaluating exceptionBlock.
       
   954      Destructive: modifies the receiver.
   923      Uses identity compare (==) to search for the element."
   955      Uses identity compare (==) to search for the element."
   924 
   956 
   925     |index|
   957     |index|
   926 
   958 
   927     index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   959     index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
   964     "
   996     "
   965 
   997 
   966     "Modified: 8.2.1997 / 18:57:43 / cg"
   998     "Modified: 8.2.1997 / 18:57:43 / cg"
   967 !
   999 !
   968 
  1000 
       
  1001 removeIndices:aSortedCollectionOfIndices
       
  1002     "remove all elements stored in any of aSortedCollectionOfIndices, 
       
  1003      which must be sorted and sequenceable.
       
  1004      Destructive: modifies the receiver.
       
  1005      Returns a collection of removed elements.
       
  1006 
       
  1007      Performance: 
       
  1008         this is an O(N) algorithm."
       
  1009 
       
  1010     |removed element dstIndex indexIndex nextIndex numIndices|
       
  1011 
       
  1012     removed := OrderedCollection new.
       
  1013 
       
  1014     dstIndex := firstIndex.
       
  1015     numIndices := aSortedCollectionOfIndices size.
       
  1016     indexIndex := 1.
       
  1017     nextIndex := aSortedCollectionOfIndices at:1.
       
  1018     firstIndex to: lastIndex do:[:srcIndex |
       
  1019         element := contentsArray at:srcIndex.
       
  1020         srcIndex == nextIndex ifTrue:[
       
  1021             removed add:element.
       
  1022             indexIndex := indexIndex + 1.
       
  1023             indexIndex > numIndices ifTrue:[
       
  1024                 nextIndex := nil.
       
  1025             ] ifFalse:[
       
  1026                 nextIndex := aSortedCollectionOfIndices at:indexIndex.
       
  1027             ].
       
  1028         ] ifFalse:[
       
  1029             contentsArray at:dstIndex put:element.
       
  1030             dstIndex := dstIndex + 1.
       
  1031         ].
       
  1032     ].
       
  1033     contentsArray from:dstIndex to:lastIndex put:nil.
       
  1034     lastIndex := dstIndex - 1.
       
  1035     ^ removed
       
  1036 
       
  1037     "
       
  1038      |coll|
       
  1039 
       
  1040      coll := OrderedCollection withAll:(1 to:10).
       
  1041      Transcript showCR:(coll removeIndices:#(1 5 7)).
       
  1042      Transcript showCR:coll
       
  1043     "
       
  1044 !
       
  1045 
   969 removeLast
  1046 removeLast
   970     "remove the last element from the collection; return the element"
  1047     "remove the last element from the collection.
       
  1048      Return the removed element.
       
  1049      Destructive: modifies the receiver"
   971 
  1050 
   972     |anObject
  1051     |anObject
   973      idx "{ Class: SmallInteger }" |
  1052      idx "{ Class: SmallInteger }" |
   974 
  1053 
   975     idx := lastIndex.
  1054     idx := lastIndex.
   999     "Modified: / 12.11.1997 / 17:58:57 / cg"
  1078     "Modified: / 12.11.1997 / 17:58:57 / cg"
  1000 !
  1079 !
  1001 
  1080 
  1002 removeLast:n
  1081 removeLast:n
  1003     "remove the last n elements from the receiver collection. 
  1082     "remove the last n elements from the receiver collection. 
       
  1083      Destructive: modifies the receiver.
  1004      Return a collection of removed elements."
  1084      Return a collection of removed elements."
  1005 
  1085 
  1006     |mySize ret|
  1086     |mySize ret|
  1007 
  1087 
  1008     mySize := self size.
  1088     mySize := self size.
  2032 ! !
  2112 ! !
  2033 
  2113 
  2034 !OrderedCollection class methodsFor:'documentation'!
  2114 !OrderedCollection class methodsFor:'documentation'!
  2035 
  2115 
  2036 version
  2116 version
  2037     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.110 2013-09-14 01:25:37 cg Exp $'
  2117     ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.111 2014-04-08 12:33:23 cg Exp $'
  2038 ! !
  2118 ! !
  2039 
  2119