RunArray.st
changeset 904 f4791de2295b
parent 895 799219e60ad6
child 906 30c1ab2aee8d
equal deleted inserted replaced
903:2e0a56e8892b 904:f4791de2295b
   565     ^ n
   565     ^ n
   566 
   566 
   567     "Modified: 11.5.1996 / 13:34:27 / cg"
   567     "Modified: 11.5.1996 / 13:34:27 / cg"
   568 ! !
   568 ! !
   569 
   569 
   570 !RunArray methodsFor:'adding'!
   570 !RunArray methodsFor:'adding & removing'!
   571 
   571 
   572 add:newObject
   572 add:newObject
   573     "add newObject at the end; returns the object (sigh)"
   573     "add newObject at the end; returns the object (sigh)"
   574 
   574 
   575     ^ self add:newObject withOccurrences:1.
   575     ^ self add:newObject withOccurrences:1.
   617      c add:1 withOccurrences:1000; yourself.
   617      c add:1 withOccurrences:1000; yourself.
   618      c add:2 withOccurrences:1000; yourself.
   618      c add:2 withOccurrences:1000; yourself.
   619     "
   619     "
   620 
   620 
   621     "Modified: 11.5.1996 / 13:34:37 / cg"
   621     "Modified: 11.5.1996 / 13:34:37 / cg"
   622 !
       
   623 
       
   624 grow:howBig
       
   625     "grow or shrink the receiver to contain howBig elements.
       
   626      If growing, new slots will be filled (logically) with nil."
       
   627 
       
   628     |sz info runIndex runOffset runSz newRuns|
       
   629 
       
   630     sz := self size.
       
   631 
       
   632     howBig == sz ifTrue:[^ self].
       
   633 
       
   634     howBig == 0 ifTrue:[
       
   635         contentsArray := nil.
       
   636         ^ self.
       
   637     ].
       
   638 
       
   639     contentsArray isNil ifTrue:[
       
   640         contentsArray := Array with:howBig with:nil.
       
   641         ^ self
       
   642     ].
       
   643 
       
   644     runSz := contentsArray size.
       
   645 
       
   646     howBig > sz ifTrue:[
       
   647         newRuns := Array new:(runSz + 2).
       
   648         newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1.
       
   649         newRuns at:(runSz+1) put:(howBig - sz).
       
   650         contentsArray := newRuns.
       
   651         ^ self
       
   652     ].
       
   653 
       
   654     "/ shrinking; possibly cut of a run
       
   655 
       
   656     info := self runIndexAndStartIndexForIndex:howBig.
       
   657     runIndex := info at:1.
       
   658     runOffset := info at:2.
       
   659 
       
   660     howBig == (runOffset - 1) ifTrue:[
       
   661         "/ we are lucky - new size is up-to previous run
       
   662 
       
   663         contentsArray := contentsArray copyFrom:1 to:runIndex-1.
       
   664     ] ifFalse:[
       
   665         contentsArray := contentsArray copyFrom:1 to:(runIndex+1).
       
   666         contentsArray at:runIndex put:(howBig - runOffset + 1)
       
   667     ].
       
   668 
       
   669     "
       
   670      |c|
       
   671 
       
   672      c := RunArray new.
       
   673      c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself.
       
   674 
       
   675      c grow:50; yourself.
       
   676 
       
   677      c grow:7; yourself.
       
   678 
       
   679      c grow:6; yourself.
       
   680 
       
   681      c grow:0; yourself.
       
   682     "
       
   683 
       
   684     "Modified: 11.5.1996 / 13:34:53 / cg"
       
   685 ! !
   622 ! !
   686 
   623 
   687 !RunArray methodsFor:'comparing'!
   624 !RunArray methodsFor:'comparing'!
   688 
   625 
   689 = aCollection
   626 = aCollection
   972     "Modified: / 30.10.1997 / 15:49:54 / cg"
   909     "Modified: / 30.10.1997 / 15:49:54 / cg"
   973 !
   910 !
   974 
   911 
   975 getContentsArray
   912 getContentsArray
   976     ^ contentsArray
   913     ^ contentsArray
   977 !
       
   978 
       
   979 isEmpty
       
   980     "Am I empty or not. Returns a boolean"
       
   981 
       
   982     ^ contentsArray notNil
       
   983 
       
   984     "Modified: 11.5.1996 / 13:35:17 / cg"
       
   985 !
   914 !
   986 
   915 
   987 runIndexAndStartIndexForIndex:anIndex
   916 runIndexAndStartIndexForIndex:anIndex
   988     "given a logical index, find the index of that run and the logical index
   917     "given a logical index, find the index of that run and the logical index
   989      of the first item in that run."
   918      of the first item in that run."
  1090         contentsArray at:idx+1 put:value.
  1019         contentsArray at:idx+1 put:value.
  1091         idx := idx + 2.
  1020         idx := idx + 2.
  1092     ].
  1021     ].
  1093 ! !
  1022 ! !
  1094 
  1023 
       
  1024 !RunArray methodsFor:'private-growing'!
       
  1025 
       
  1026 grow:howBig
       
  1027     "grow or shrink the receiver to contain howBig elements.
       
  1028      If growing, new slots will be filled (logically) with nil."
       
  1029 
       
  1030     |sz info runIndex runOffset runSz newRuns|
       
  1031 
       
  1032     sz := self size.
       
  1033 
       
  1034     howBig == sz ifTrue:[^ self].
       
  1035 
       
  1036     howBig == 0 ifTrue:[
       
  1037         contentsArray := nil.
       
  1038         ^ self.
       
  1039     ].
       
  1040 
       
  1041     contentsArray isNil ifTrue:[
       
  1042         contentsArray := Array with:howBig with:nil.
       
  1043         ^ self
       
  1044     ].
       
  1045 
       
  1046     runSz := contentsArray size.
       
  1047 
       
  1048     howBig > sz ifTrue:[
       
  1049         newRuns := Array new:(runSz + 2).
       
  1050         newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1.
       
  1051         newRuns at:(runSz+1) put:(howBig - sz).
       
  1052         contentsArray := newRuns.
       
  1053         ^ self
       
  1054     ].
       
  1055 
       
  1056     "/ shrinking; possibly cut of a run
       
  1057 
       
  1058     info := self runIndexAndStartIndexForIndex:howBig.
       
  1059     runIndex := info at:1.
       
  1060     runOffset := info at:2.
       
  1061 
       
  1062     howBig == (runOffset - 1) ifTrue:[
       
  1063         "/ we are lucky - new size is up-to previous run
       
  1064 
       
  1065         contentsArray := contentsArray copyFrom:1 to:runIndex-1.
       
  1066     ] ifFalse:[
       
  1067         contentsArray := contentsArray copyFrom:1 to:(runIndex+1).
       
  1068         contentsArray at:runIndex put:(howBig - runOffset + 1)
       
  1069     ].
       
  1070 
       
  1071     "
       
  1072      |c|
       
  1073 
       
  1074      c := RunArray new.
       
  1075      c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself.
       
  1076 
       
  1077      c grow:50; yourself.
       
  1078 
       
  1079      c grow:7; yourself.
       
  1080 
       
  1081      c grow:6; yourself.
       
  1082 
       
  1083      c grow:0; yourself.
       
  1084     "
       
  1085 
       
  1086     "Modified: 11.5.1996 / 13:34:53 / cg"
       
  1087 ! !
       
  1088 
  1095 !RunArray methodsFor:'searching'!
  1089 !RunArray methodsFor:'searching'!
  1096 
  1090 
  1097 findFirst:aBlock
  1091 findFirst:aBlock
  1098     "find the first element, for which evaluation of the argument, aBlock
  1092     "find the first element, for which evaluation of the argument, aBlock
  1099      returns true; return its index or 0 if none detected.
  1093      returns true; return its index or 0 if none detected.
  1228         ].
  1222         ].
  1229     ].
  1223     ].
  1230     ^ false
  1224     ^ false
  1231 
  1225 
  1232     "Created: / 30.10.1997 / 15:38:30 / cg"
  1226     "Created: / 30.10.1997 / 15:38:30 / cg"
       
  1227 !
       
  1228 
       
  1229 isEmpty
       
  1230     "Am I empty or not. Returns a boolean"
       
  1231 
       
  1232     ^ contentsArray notNil
       
  1233 
       
  1234     "Modified: 11.5.1996 / 13:35:17 / cg"
  1233 ! !
  1235 ! !
  1234 
  1236 
  1235 !RunArray methodsFor:'user interface'!
  1237 !RunArray methodsFor:'user interface'!
  1236 
  1238 
  1237 inspectorClass
  1239 inspectorClass
  1244 ! !
  1246 ! !
  1245 
  1247 
  1246 !RunArray class methodsFor:'documentation'!
  1248 !RunArray class methodsFor:'documentation'!
  1247 
  1249 
  1248 version
  1250 version
  1249     ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.17 2000-07-18 15:01:09 cg Exp $'
  1251     ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.18 2000-08-22 13:49:22 cg Exp $'
  1250 ! !
  1252 ! !