VarArray.st
changeset 124 d919bc2f0078
parent 112 3e18f2cfe430
child 131 19e548711b65
equal deleted inserted replaced
123:4a1b75bf25a8 124:d919bc2f0078
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 ArrayedCollection subclass:#VariableArray
    13 ArrayedCollection subclass:#VariableArray
    14        instanceVariableNames:'tally contentsArray'
    14 	 instanceVariableNames:'tally contentsArray'
    15        classVariableNames:''
    15 	 classVariableNames:''
    16        poolDictionaries:''
    16 	 poolDictionaries:''
    17        category:'Collections-Sequenceable'
    17 	 category:'Collections-Sequenceable'
    18 !
    18 !
    19 
    19 
    20 !VariableArray class methodsFor:'documentation'!
    20 !VariableArray class methodsFor:'documentation'!
    21 
    21 
    22 copyright
    22 copyright
    31  other person.  No title to or ownership of the software is
    31  other person.  No title to or ownership of the software is
    32  hereby transferred.
    32  hereby transferred.
    33 "
    33 "
    34 !
    34 !
    35 
    35 
    36 version
       
    37     ^ '$Header: /cvs/stx/stx/libbasic2/Attic/VarArray.st,v 1.12 1995-11-11 15:22:00 cg Exp $'
       
    38 !
       
    39 
       
    40 documentation
    36 documentation
    41 "
    37 "
    42     VariableArrays can grow and shrink - in contrast to Arrays which are
    38     VariableArrays can grow and shrink - in contrast to Arrays which are
    43     fixed in size. 
    39     fixed in size. 
    44     WARNING: Do not use this class for new applications - its a historic 
    40     WARNING: Do not use this class for new applications - its a historic 
    45 	     leftover from times when no OrderedCollection existed.
    41 	     leftover from times when no OrderedCollection existed.
    46 
    42 
    47     Use OrderedCollection, which offers more functionality, and is even
    43     Use OrderedCollection, which offers more functionality, and is even
    48     a bit faster in some operations.
    44     a bit faster in some operations.
    49 "
    45 "
       
    46 !
       
    47 
       
    48 version
       
    49     ^ '$Header: /cvs/stx/stx/libbasic2/Attic/VarArray.st,v 1.13 1995-11-23 01:20:36 cg Exp $'
    50 ! !
    50 ! !
    51 
    51 
    52 !VariableArray class methodsFor:'instance creation'!
    52 !VariableArray class methodsFor:'instance creation'!
    53 
    53 
    54 new
    54 new
    61     "return a new VariableArray"
    61     "return a new VariableArray"
    62 
    62 
    63     ^ (self basicNew) setContents:(Array new:size)
    63     ^ (self basicNew) setContents:(Array new:size)
    64 ! !
    64 ! !
    65 
    65 
    66 !VariableArray methodsFor:'kludges'!
    66 !VariableArray methodsFor:'accessing'!
    67 
    67 
    68 shallowCopy:anArray
    68 at:index
    69     "return a shallow copy of the receiver
    69     "return the element at index"
    70      have to kludge the kludge ... - shallow copy the contents array"
    70 
    71 
    71     (index between:1 and:tally) ifFalse:[
    72     |newText|
    72 	^ self subscriptBoundsError:index
    73 
    73     ].
    74     newText := self class new.
    74     ^ contentsArray at:index
    75     newText setContents:(contentsArray shallowCopy).
    75 !
    76     ^ newText
    76 
    77 ! !
    77 at:index put:anObject
    78 
    78     "set the element at index"
    79 !VariableArray methodsFor:'private'!
    79 
    80 
    80     (index between:1 and:tally) ifFalse:[
    81 getContents
    81 	^ self subscriptBoundsError:index
    82     "return the contents array"
    82     ].
    83 
    83     ^ contentsArray at:index put:anObject
    84     ^ contentsArray
    84 ! !
    85 !
    85 
    86 
    86 !VariableArray methodsFor:'enumerating'!
    87 setInitialContents:anArray
    87 
    88     "set the contents array but make size zero"
    88 do:aBlock
    89 
    89     "evaluate the argument, aBlock for each element
    90     tally := 0.
    90      in the collection"
    91     contentsArray := anArray
    91 
    92 !
    92     contentsArray from:1 to:tally do:aBlock
    93 
    93 !
    94 setContents:anArray
    94 
    95     "set the contents array"
    95 from:start to:stop do:aBlock
    96 
    96     "evaluate the argument, aBlock for some elements
    97     tally := anArray size.
    97      in the collection"
    98     contentsArray := anArray
    98 
    99 ! !
    99     (stop <= tally) ifTrue:[
   100 
   100 	contentsArray from:start to:stop do:aBlock
   101 !VariableArray methodsFor:'inquiries'!
   101     ] ifFalse:[
   102 
   102 	super from:start to:stop do:aBlock
   103 size
   103     ]
   104     "return the number of array elements"
       
   105 
       
   106     ^ tally
       
   107 !
       
   108 
       
   109 isFixedSize
       
   110     "return true if the receiver cannot grow - this will vanish once
       
   111      Arrays and Strings learn how to grow ..."
       
   112 
       
   113     ^ false
       
   114 ! !
   104 ! !
   115 
   105 
   116 !VariableArray methodsFor:'filling & replacing'!
   106 !VariableArray methodsFor:'filling & replacing'!
   117 
   107 
   118 replaceFrom:start to:stop with:aCollection startingAt:repStart
   108 replaceFrom:start to:stop with:aCollection startingAt:repStart
   134 	]
   124 	]
   135     ].
   125     ].
   136     ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
   126     ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
   137 ! !
   127 ! !
   138 
   128 
       
   129 !VariableArray methodsFor:'grow & shrink'!
       
   130 
       
   131 add:anElement
       
   132     "add anElement to the end of the array"
       
   133 
       
   134     |newSize "{ Class: SmallInteger }" |
       
   135 
       
   136     newSize := tally + 1.
       
   137     (newSize <= contentsArray size) ifTrue:[
       
   138 	tally := newSize
       
   139     ] ifFalse:[
       
   140 	self grow:newSize
       
   141     ].
       
   142     contentsArray at:tally put:anElement
       
   143 !
       
   144 
       
   145 grow:newSize
       
   146     "grow to newSize"
       
   147 
       
   148     |newArray|
       
   149 
       
   150     (newSize == tally) ifTrue:[^ self].
       
   151 
       
   152     (newSize > tally) ifTrue:[
       
   153 	(newSize > contentsArray size) ifTrue:[
       
   154 	    newArray := Array new:(newSize * 2).
       
   155 	    newArray replaceFrom:1 to:tally with:contentsArray startingAt:1.
       
   156 	    contentsArray := newArray
       
   157 	]
       
   158     ] ifFalse:[
       
   159 	contentsArray from:(newSize + 1) to:tally put:nil
       
   160     ].
       
   161     tally := newSize
       
   162 ! !
       
   163 
       
   164 !VariableArray methodsFor:'inquiries'!
       
   165 
       
   166 isFixedSize
       
   167     "return true if the receiver cannot grow - this will vanish once
       
   168      Arrays and Strings learn how to grow ..."
       
   169 
       
   170     ^ false
       
   171 !
       
   172 
       
   173 size
       
   174     "return the number of array elements"
       
   175 
       
   176     ^ tally
       
   177 ! !
       
   178 
       
   179 !VariableArray methodsFor:'kludges'!
       
   180 
       
   181 shallowCopy:anArray
       
   182     "return a shallow copy of the receiver
       
   183      have to kludge the kludge ... - shallow copy the contents array"
       
   184 
       
   185     |newText|
       
   186 
       
   187     newText := self class new.
       
   188     newText setContents:(contentsArray shallowCopy).
       
   189     ^ newText
       
   190 ! !
       
   191 
       
   192 !VariableArray methodsFor:'private'!
       
   193 
       
   194 getContents
       
   195     "return the contents array"
       
   196 
       
   197     ^ contentsArray
       
   198 !
       
   199 
       
   200 setContents:anArray
       
   201     "set the contents array"
       
   202 
       
   203     tally := anArray size.
       
   204     contentsArray := anArray
       
   205 !
       
   206 
       
   207 setInitialContents:anArray
       
   208     "set the contents array but make size zero"
       
   209 
       
   210     tally := 0.
       
   211     contentsArray := anArray
       
   212 ! !
       
   213 
   139 !VariableArray methodsFor:'removing'!
   214 !VariableArray methodsFor:'removing'!
   140 
   215 
   141 removeFromIndex:startIndex toIndex:endIndex
   216 removeFromIndex:startIndex toIndex:endIndex
   142     "remove the elements stored at indexes between startIndex and endIndex"
   217     "remove the elements stored at indexes between startIndex and endIndex"
   143 
   218 
   152     ]
   227     ]
   153 ! !
   228 ! !
   154 
   229 
   155 !VariableArray methodsFor:'testing'!
   230 !VariableArray methodsFor:'testing'!
   156 
   231 
   157 occurrencesOf:anObject
   232 identityIndexOf:anElement startingAt:start
   158     "return the number of occurrences of anObject in the receiver"
   233     "search the collection for anElement starting search at index start
   159 
   234      using == for compares.
   160     ^ contentsArray occurrencesOf:anObject
   235      if found, return the index otherwise return 0"
       
   236 
       
   237     |index|
       
   238 
       
   239     (start > tally) ifFalse:[
       
   240 	index := contentsArray identityIndexOf:anElement startingAt:start.
       
   241 	index == 0 ifFalse:[
       
   242 	    (index between:1 and:tally) ifTrue:[
       
   243 		^ index
       
   244 	    ]
       
   245 	]
       
   246     ].
       
   247     ^ 0
   161 !
   248 !
   162 
   249 
   163 includes:anObject
   250 includes:anObject
   164     "return true, if the receiver contains the argument, anObject"
   251     "return true, if the receiver contains the argument, anObject"
   165 
   252 
   182 	]
   269 	]
   183     ].
   270     ].
   184     ^ 0
   271     ^ 0
   185 !
   272 !
   186 
   273 
   187 identityIndexOf:anElement startingAt:start
   274 occurrencesOf:anObject
   188     "search the collection for anElement starting search at index start
   275     "return the number of occurrences of anObject in the receiver"
   189      using == for compares.
   276 
   190      if found, return the index otherwise return 0"
   277     ^ contentsArray occurrencesOf:anObject
   191 
   278 ! !
   192     |index|
   279 
   193 
       
   194     (start > tally) ifFalse:[
       
   195 	index := contentsArray identityIndexOf:anElement startingAt:start.
       
   196 	index == 0 ifFalse:[
       
   197 	    (index between:1 and:tally) ifTrue:[
       
   198 		^ index
       
   199 	    ]
       
   200 	]
       
   201     ].
       
   202     ^ 0
       
   203 ! !
       
   204     
       
   205 !VariableArray methodsFor:'accessing'!
       
   206 
       
   207 at:index
       
   208     "return the element at index"
       
   209 
       
   210     (index between:1 and:tally) ifFalse:[
       
   211 	^ self subscriptBoundsError:index
       
   212     ].
       
   213     ^ contentsArray at:index
       
   214 !
       
   215 
       
   216 at:index put:anObject
       
   217     "set the element at index"
       
   218 
       
   219     (index between:1 and:tally) ifFalse:[
       
   220 	^ self subscriptBoundsError:index
       
   221     ].
       
   222     ^ contentsArray at:index put:anObject
       
   223 ! !
       
   224 
       
   225 !VariableArray methodsFor:'grow & shrink'!
       
   226 
       
   227 grow:newSize
       
   228     "grow to newSize"
       
   229 
       
   230     |newArray|
       
   231 
       
   232     (newSize == tally) ifTrue:[^ self].
       
   233 
       
   234     (newSize > tally) ifTrue:[
       
   235 	(newSize > contentsArray size) ifTrue:[
       
   236 	    newArray := Array new:(newSize * 2).
       
   237 	    newArray replaceFrom:1 to:tally with:contentsArray startingAt:1.
       
   238 	    contentsArray := newArray
       
   239 	]
       
   240     ] ifFalse:[
       
   241 	contentsArray from:(newSize + 1) to:tally put:nil
       
   242     ].
       
   243     tally := newSize
       
   244 !
       
   245 
       
   246 add:anElement
       
   247     "add anElement to the end of the array"
       
   248 
       
   249     |newSize "{ Class: SmallInteger }" |
       
   250 
       
   251     newSize := tally + 1.
       
   252     (newSize <= contentsArray size) ifTrue:[
       
   253 	tally := newSize
       
   254     ] ifFalse:[
       
   255 	self grow:newSize
       
   256     ].
       
   257     contentsArray at:tally put:anElement
       
   258 ! !
       
   259 
       
   260 !VariableArray methodsFor:'enumerating'!
       
   261 
       
   262 do:aBlock
       
   263     "evaluate the argument, aBlock for each element
       
   264      in the collection"
       
   265 
       
   266     contentsArray from:1 to:tally do:aBlock
       
   267 !
       
   268 
       
   269 from:start to:stop do:aBlock
       
   270     "evaluate the argument, aBlock for some elements
       
   271      in the collection"
       
   272 
       
   273     (stop <= tally) ifTrue:[
       
   274 	contentsArray from:start to:stop do:aBlock
       
   275     ] ifFalse:[
       
   276 	super from:start to:stop do:aBlock
       
   277     ]
       
   278 ! !