OrderedCollection.st
changeset 68 59faa75185ba
parent 56 be0ed17e6f85
child 77 6c38ca59927f
equal deleted inserted replaced
67:e52341804063 68:59faa75185ba
    20 OrderedCollection comment:'
    20 OrderedCollection comment:'
    21 
    21 
    22 COPYRIGHT (c) 1989 by Claus Gittinger
    22 COPYRIGHT (c) 1989 by Claus Gittinger
    23               All Rights Reserved
    23               All Rights Reserved
    24 
    24 
    25 $Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.10 1994-02-25 13:00:53 claus Exp $
    25 $Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.11 1994-03-30 09:36:27 claus Exp $
    26 written spring 89 by claus
    26 written spring 89 by claus
    27 '!
    27 '!
    28 
    28 
    29 !OrderedCollection class methodsFor:'documentation'!
    29 !OrderedCollection class methodsFor:'documentation'!
    30 
    30 
    31 documentation
    31 documentation
    32 "
    32 "
    33 OrderedCollection have ordered elements. Insertion and removal at both ends
    33     OrderedCollection have ordered elements. Insertion and removal at both ends
    34 is possible - therefore they can be used for queues and stacks.
    34     is possible - therefore they can be used for queues and stacks.
    35 
    35 
    36 Instance variables:
    36     Instance variables:
    37 
    37 
    38 contentsArray   <Array>         the actual contents
    38     contentsArray   <Array>         the actual contents
    39 firstIndex      <SmallInteger>  index of first valid element
    39     firstIndex      <SmallInteger>  index of first valid element
    40 lastIndex       <SmallInteger>  index of last valid element
    40     lastIndex       <SmallInteger>  index of last valid element
    41 "
    41 "
    42 ! !
    42 ! !
    43 
    43 
    44 !OrderedCollection class methodsFor:'instance creation'!
    44 !OrderedCollection class methodsFor:'instance creation'!
    45 
    45 
    86         newCollection add:element
    86         newCollection add:element
    87     ].
    87     ].
    88     ^ newCollection
    88     ^ newCollection
    89 
    89 
    90     "#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection"
    90     "#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection"
       
    91 !
       
    92 
       
    93 copyWith:newElement
       
    94     "return a new collection consisting of a copy of the receivers elements
       
    95      plus the argument."
       
    96 
       
    97     |newCollection mySize newSize|
       
    98 
       
    99     mySize := self size.
       
   100     newSize := mySize + 1.
       
   101     newCollection := self species new:newSize.
       
   102     newCollection grow:newSize.
       
   103     newCollection replaceFrom:1 to:mySize with:self startingAt:1.
       
   104     newCollection at:newSize put:newElement.
       
   105     ^newCollection
       
   106 
       
   107     "#(1 2 3 4 5) copyWith:$a"
       
   108     "'abcdefg' copyWith:$h"
       
   109     "'abcdefg' copyWith:'123'"  "-- will fail: string cannot be stored into string"
       
   110     "'abcdefg' copyWith:1"      "-- will fail: integer cannot be stored into string"
    91 !
   111 !
    92 
   112 
    93 copy
   113 copy
    94     "return a new OrderedCollection containing the elements of the receiver."
   114     "return a new OrderedCollection containing the elements of the receiver."
    95 
   115 
   479     |newContents
   499     |newContents
   480      newSize "{ Class:SmallInteger }" 
   500      newSize "{ Class:SmallInteger }" 
   481      oldSize "{ Class:SmallInteger }" |
   501      oldSize "{ Class:SmallInteger }" |
   482 
   502 
   483     oldSize := contentsArray size.
   503     oldSize := contentsArray size.
   484     firstIndex > (oldSize // 4) ifTrue:[
   504     ((firstIndex > 1) and:[firstIndex > (oldSize // 4)]) ifTrue:[
   485         "there is room at the beginning"
   505         "there is room at the beginning"
   486 
   506 
   487         index == 1 ifFalse:[
   507         index == 1 ifFalse:[
   488             contentsArray replaceFrom:(firstIndex - 1) to:(index - 1)
   508             contentsArray replaceFrom:(firstIndex - 1) to:(index - 1)
   489                                  with:contentsArray startingAt:firstIndex.
   509                                  with:contentsArray startingAt:firstIndex.