diff -r 4a1b75bf25a8 -r d919bc2f0078 VariableArray.st --- a/VariableArray.st Thu Nov 23 02:18:58 1995 +0100 +++ b/VariableArray.st Thu Nov 23 02:23:34 1995 +0100 @@ -11,10 +11,10 @@ " ArrayedCollection subclass:#VariableArray - instanceVariableNames:'tally contentsArray' - classVariableNames:'' - poolDictionaries:'' - category:'Collections-Sequenceable' + instanceVariableNames:'tally contentsArray' + classVariableNames:'' + poolDictionaries:'' + category:'Collections-Sequenceable' ! !VariableArray class methodsFor:'documentation'! @@ -33,10 +33,6 @@ " ! -version - ^ '$Header: /cvs/stx/stx/libbasic2/VariableArray.st,v 1.12 1995-11-11 15:22:00 cg Exp $' -! - documentation " VariableArrays can grow and shrink - in contrast to Arrays which are @@ -47,6 +43,10 @@ Use OrderedCollection, which offers more functionality, and is even a bit faster in some operations. " +! + +version + ^ '$Header: /cvs/stx/stx/libbasic2/VariableArray.st,v 1.13 1995-11-23 01:20:36 cg Exp $' ! ! !VariableArray class methodsFor:'instance creation'! @@ -63,54 +63,44 @@ ^ (self basicNew) setContents:(Array new:size) ! ! -!VariableArray methodsFor:'kludges'! +!VariableArray methodsFor:'accessing'! -shallowCopy:anArray - "return a shallow copy of the receiver - have to kludge the kludge ... - shallow copy the contents array" - - |newText| +at:index + "return the element at index" - newText := self class new. - newText setContents:(contentsArray shallowCopy). - ^ newText -! ! - -!VariableArray methodsFor:'private'! - -getContents - "return the contents array" - - ^ contentsArray + (index between:1 and:tally) ifFalse:[ + ^ self subscriptBoundsError:index + ]. + ^ contentsArray at:index ! -setInitialContents:anArray - "set the contents array but make size zero" +at:index put:anObject + "set the element at index" - tally := 0. - contentsArray := anArray -! - -setContents:anArray - "set the contents array" - - tally := anArray size. - contentsArray := anArray + (index between:1 and:tally) ifFalse:[ + ^ self subscriptBoundsError:index + ]. + ^ contentsArray at:index put:anObject ! ! -!VariableArray methodsFor:'inquiries'! +!VariableArray methodsFor:'enumerating'! -size - "return the number of array elements" +do:aBlock + "evaluate the argument, aBlock for each element + in the collection" - ^ tally + contentsArray from:1 to:tally do:aBlock ! -isFixedSize - "return true if the receiver cannot grow - this will vanish once - Arrays and Strings learn how to grow ..." +from:start to:stop do:aBlock + "evaluate the argument, aBlock for some elements + in the collection" - ^ false + (stop <= tally) ifTrue:[ + contentsArray from:start to:stop do:aBlock + ] ifFalse:[ + super from:start to:stop do:aBlock + ] ! ! !VariableArray methodsFor:'filling & replacing'! @@ -136,6 +126,91 @@ ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart ! ! +!VariableArray methodsFor:'grow & shrink'! + +add:anElement + "add anElement to the end of the array" + + |newSize "{ Class: SmallInteger }" | + + newSize := tally + 1. + (newSize <= contentsArray size) ifTrue:[ + tally := newSize + ] ifFalse:[ + self grow:newSize + ]. + contentsArray at:tally put:anElement +! + +grow:newSize + "grow to newSize" + + |newArray| + + (newSize == tally) ifTrue:[^ self]. + + (newSize > tally) ifTrue:[ + (newSize > contentsArray size) ifTrue:[ + newArray := Array new:(newSize * 2). + newArray replaceFrom:1 to:tally with:contentsArray startingAt:1. + contentsArray := newArray + ] + ] ifFalse:[ + contentsArray from:(newSize + 1) to:tally put:nil + ]. + tally := newSize +! ! + +!VariableArray methodsFor:'inquiries'! + +isFixedSize + "return true if the receiver cannot grow - this will vanish once + Arrays and Strings learn how to grow ..." + + ^ false +! + +size + "return the number of array elements" + + ^ tally +! ! + +!VariableArray methodsFor:'kludges'! + +shallowCopy:anArray + "return a shallow copy of the receiver + have to kludge the kludge ... - shallow copy the contents array" + + |newText| + + newText := self class new. + newText setContents:(contentsArray shallowCopy). + ^ newText +! ! + +!VariableArray methodsFor:'private'! + +getContents + "return the contents array" + + ^ contentsArray +! + +setContents:anArray + "set the contents array" + + tally := anArray size. + contentsArray := anArray +! + +setInitialContents:anArray + "set the contents array but make size zero" + + tally := 0. + contentsArray := anArray +! ! + !VariableArray methodsFor:'removing'! removeFromIndex:startIndex toIndex:endIndex @@ -154,10 +229,22 @@ !VariableArray methodsFor:'testing'! -occurrencesOf:anObject - "return the number of occurrences of anObject in the receiver" +identityIndexOf:anElement startingAt:start + "search the collection for anElement starting search at index start + using == for compares. + if found, return the index otherwise return 0" + + |index| - ^ contentsArray occurrencesOf:anObject + (start > tally) ifFalse:[ + index := contentsArray identityIndexOf:anElement startingAt:start. + index == 0 ifFalse:[ + (index between:1 and:tally) ifTrue:[ + ^ index + ] + ] + ]. + ^ 0 ! includes:anObject @@ -184,95 +271,9 @@ ^ 0 ! -identityIndexOf:anElement startingAt:start - "search the collection for anElement starting search at index start - using == for compares. - if found, return the index otherwise return 0" - - |index| +occurrencesOf:anObject + "return the number of occurrences of anObject in the receiver" - (start > tally) ifFalse:[ - index := contentsArray identityIndexOf:anElement startingAt:start. - index == 0 ifFalse:[ - (index between:1 and:tally) ifTrue:[ - ^ index - ] - ] - ]. - ^ 0 -! ! - -!VariableArray methodsFor:'accessing'! - -at:index - "return the element at index" - - (index between:1 and:tally) ifFalse:[ - ^ self subscriptBoundsError:index - ]. - ^ contentsArray at:index -! - -at:index put:anObject - "set the element at index" - - (index between:1 and:tally) ifFalse:[ - ^ self subscriptBoundsError:index - ]. - ^ contentsArray at:index put:anObject + ^ contentsArray occurrencesOf:anObject ! ! -!VariableArray methodsFor:'grow & shrink'! - -grow:newSize - "grow to newSize" - - |newArray| - - (newSize == tally) ifTrue:[^ self]. - - (newSize > tally) ifTrue:[ - (newSize > contentsArray size) ifTrue:[ - newArray := Array new:(newSize * 2). - newArray replaceFrom:1 to:tally with:contentsArray startingAt:1. - contentsArray := newArray - ] - ] ifFalse:[ - contentsArray from:(newSize + 1) to:tally put:nil - ]. - tally := newSize -! - -add:anElement - "add anElement to the end of the array" - - |newSize "{ Class: SmallInteger }" | - - newSize := tally + 1. - (newSize <= contentsArray size) ifTrue:[ - tally := newSize - ] ifFalse:[ - self grow:newSize - ]. - contentsArray at:tally put:anElement -! ! - -!VariableArray methodsFor:'enumerating'! - -do:aBlock - "evaluate the argument, aBlock for each element - in the collection" - - contentsArray from:1 to:tally do:aBlock -! - -from:start to:stop do:aBlock - "evaluate the argument, aBlock for some elements - in the collection" - - (stop <= tally) ifTrue:[ - contentsArray from:start to:stop do:aBlock - ] ifFalse:[ - super from:start to:stop do:aBlock - ] -! !