VariableArray.st
changeset 0 1cf8d1747859
child 1 85d662a8509f
equal deleted inserted replaced
-1:000000000000 0:1cf8d1747859
       
     1 "
       
     2  COPYRIGHT (c) 1989-92 by Claus Gittinger
       
     3               All Rights Reserved
       
     4 
       
     5  This software is furnished under a license and may be used
       
     6  only in accordance with the terms of that license and with the
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 
       
    13 ArrayedCollection subclass:#VariableArray
       
    14        instanceVariableNames:'tally contentsArray'
       
    15        classVariableNames:''
       
    16        poolDictionaries:''
       
    17        category:'Collections-Indexed'
       
    18 !
       
    19 
       
    20 VariableArray comment:'
       
    21 
       
    22 COPYRIGHT (c) 1989-92 by Claus Gittinger
       
    23               All Rights Reserved
       
    24 
       
    25 VariableArrays can grow and shrink - in contrast to Arrays which are
       
    26 fixed in size - this may change in the future.
       
    27 (make Array be FixedArray, and this one named Array
       
    28 
       
    29 %W% %E%
       
    30 '!
       
    31 
       
    32 !VariableArray class methodsFor:'instance creation'!
       
    33 
       
    34 new
       
    35     "return a new VariableArray - with size 0"
       
    36 
       
    37     ^ (self basicNew) setInitialContents:(Array new:10)
       
    38 !
       
    39 
       
    40 new:size
       
    41     "return a new VariableArray"
       
    42 
       
    43     ^ (self basicNew) setContents:(Array new:size)
       
    44 ! !
       
    45 
       
    46 !VariableArray methodsFor:'kludges'!
       
    47 
       
    48 shallowCopy:anArray
       
    49     "return a shallow copy of the receiver
       
    50      have to kludge the kludge ... - shallow copy the contents array"
       
    51 
       
    52     |newText|
       
    53 
       
    54     newText := self class new.
       
    55     newText setContents:(contentsArray shallowCopy).
       
    56     ^ newText
       
    57 ! !
       
    58 
       
    59 !VariableArray methodsFor:'private'!
       
    60 
       
    61 getContents
       
    62     "return the contents array"
       
    63 
       
    64     ^ contentsArray
       
    65 !
       
    66 
       
    67 setInitialContents:anArray
       
    68     "set the contents array but make size zero"
       
    69 
       
    70     tally := 0.
       
    71     contentsArray := anArray
       
    72 !
       
    73 
       
    74 setContents:anArray
       
    75     "set the contents array"
       
    76 
       
    77     tally := anArray size.
       
    78     contentsArray := anArray
       
    79 ! !
       
    80 
       
    81 !VariableArray methodsFor:'inquiries'!
       
    82 
       
    83 size
       
    84     "return the number of array elements"
       
    85 
       
    86     ^ tally
       
    87 !
       
    88 
       
    89 isFixedSize
       
    90     "return true if the receiver cannot grow - this will vanish once
       
    91      Arrays and Strings learn how to grow ..."
       
    92 
       
    93     ^ false
       
    94 ! !
       
    95 
       
    96 !VariableArray methodsFor:'filling & replacing'!
       
    97 
       
    98 replaceFrom:start to:stop with:aCollection startingAt:repStart
       
    99     "reimplemented for speed
       
   100      - can use Arrays fast replace if aCollection is Array or VariableArray"
       
   101 
       
   102     |col|
       
   103 
       
   104     col := aCollection.
       
   105     (aCollection isKindOf:VariableArray) ifTrue:[
       
   106         ((stop - start + repStart) <= aCollection size) ifTrue:[
       
   107             col := aCollection getContents
       
   108         ]
       
   109     ].
       
   110     (col isMemberOf:Array) ifTrue:[
       
   111         (stop <= tally) ifTrue:[
       
   112             ^ contentsArray replaceFrom:start to:stop with:col startingAt:repStart
       
   113         ]
       
   114     ].
       
   115     ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
       
   116 ! !
       
   117 
       
   118 !VariableArray methodsFor:'removing'!
       
   119 
       
   120 removeFromIndex:startIndex toIndex:endIndex
       
   121     "remove the elements stored at indexes between startIndex and endIndex"
       
   122 
       
   123     |newSize|
       
   124 
       
   125     (endIndex >= tally) ifTrue:[
       
   126         self grow:(startIndex - 1)
       
   127     ] ifFalse:[
       
   128         newSize := tally - endIndex + startIndex - 1.
       
   129         contentsArray replaceFrom:startIndex to:newSize with:contentsArray startingAt:(endIndex + 1).
       
   130         self grow:newSize
       
   131     ]
       
   132 ! !
       
   133 
       
   134 !VariableArray methodsFor:'testing'!
       
   135 
       
   136 occurrencesOf:anObject
       
   137     "return the number of occurrences of anObject in the receiver"
       
   138 
       
   139     ^ contentsArray occurrencesOf:anObject
       
   140 !
       
   141 
       
   142 includes:anObject
       
   143     "return true, if the receiver contains the argument, anObject"
       
   144 
       
   145     ^ contentsArray includes:anObject
       
   146 !
       
   147 
       
   148 indexOf:anElement startingAt:start
       
   149     "search the collection for anElement starting search at index start
       
   150      using = for compares.
       
   151      if found, return the index otherwise return 0"
       
   152 
       
   153     |index|
       
   154 
       
   155     (start > tally) ifFalse:[
       
   156         index := contentsArray indexOf:anElement startingAt:start.
       
   157         index == 0 ifFalse:[
       
   158             (index between:1 and:tally) ifTrue:[
       
   159                 ^ index
       
   160             ]
       
   161         ]
       
   162     ].
       
   163     ^ 0
       
   164 !
       
   165 
       
   166 identityIndexOf:anElement startingAt:start
       
   167     "search the collection for anElement starting search at index start
       
   168      using == for compares.
       
   169      if found, return the index otherwise return 0"
       
   170 
       
   171     |index|
       
   172 
       
   173     (start > tally) ifFalse:[
       
   174         index := contentsArray identityIndexOf:anElement startingAt:start.
       
   175         index == 0 ifFalse:[
       
   176             (index between:1 and:tally) ifTrue:[
       
   177                 ^ index
       
   178             ]
       
   179         ]
       
   180     ].
       
   181     ^ 0
       
   182 ! !
       
   183     
       
   184 !VariableArray methodsFor:'accessing'!
       
   185 
       
   186 at:index
       
   187     "return the element at index"
       
   188 
       
   189     (index between:1 and:tally) ifFalse:[
       
   190         ^ self subscriptBoundsError:index
       
   191     ].
       
   192     ^ contentsArray at:index
       
   193 !
       
   194 
       
   195 at:index put:anObject
       
   196     "set the element at index"
       
   197 
       
   198     (index between:1 and:tally) ifFalse:[
       
   199         ^ self subscriptBoundsError:index
       
   200     ].
       
   201     ^ contentsArray at:index put:anObject
       
   202 ! !
       
   203 
       
   204 !VariableArray methodsFor:'grow & shrink'!
       
   205 
       
   206 grow:newSize
       
   207     "grow to newSize"
       
   208 
       
   209     |newArray|
       
   210 
       
   211     (newSize > tally) ifTrue:[
       
   212         (newSize > contentsArray size) ifTrue:[
       
   213             newArray := Array new:(newSize * 2).
       
   214             newArray replaceFrom:1 to:tally with:contentsArray startingAt:1.
       
   215             contentsArray := newArray
       
   216         ]
       
   217     ] ifFalse:[
       
   218         contentsArray from:(newSize + 1) to:tally put:nil
       
   219     ].
       
   220     tally := newSize
       
   221 !
       
   222 
       
   223 add:anElement
       
   224     "add anElement to the end of the array"
       
   225 
       
   226     |newSize "{ Class: SmallInteger }" |
       
   227 
       
   228     newSize := tally + 1.
       
   229     (newSize <= contentsArray size) ifTrue:[
       
   230         tally := newSize
       
   231     ] ifFalse:[
       
   232         self grow:newSize
       
   233     ].
       
   234     contentsArray at:tally put:anElement
       
   235 ! !
       
   236 
       
   237 !VariableArray methodsFor:'enumerating'!
       
   238 
       
   239 do:aBlock
       
   240     "evaluate the argument, aBlock for each element
       
   241      in the collection"
       
   242 
       
   243     contentsArray from:1 to:tally do:aBlock
       
   244 !
       
   245 
       
   246 from:start to:stop do:aBlock
       
   247     "evaluate the argument, aBlock for some elements
       
   248      in the collection"
       
   249 
       
   250     (stop <= tally) ifTrue:[
       
   251         contentsArray from:start to:stop do:aBlock
       
   252     ] ifFalse:[
       
   253         super from:start to:stop do:aBlock
       
   254     ]
       
   255 ! !