OrderedCollection.st
changeset 93 e31220cb391f
parent 88 81dacba7a63a
child 114 0353ec6749c8
equal deleted inserted replaced
92:0c73b48551ac 93:e31220cb391f
    18 !
    18 !
    19 
    19 
    20 OrderedCollection comment:'
    20 OrderedCollection comment:'
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    21 COPYRIGHT (c) 1989 by Claus Gittinger
    22               All Rights Reserved
    22               All Rights Reserved
       
    23 
       
    24 $Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.14 1994-08-05 00:59:17 claus Exp $
    23 '!
    25 '!
    24 
    26 
    25 !OrderedCollection class methodsFor:'documentation'!
    27 !OrderedCollection class methodsFor:'documentation'!
    26 
    28 
    27 copyright
    29 copyright
    38 "
    40 "
    39 !
    41 !
    40 
    42 
    41 version
    43 version
    42 "
    44 "
    43 $Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.13 1994-06-02 16:21:11 claus Exp $
    45 $Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.14 1994-08-05 00:59:17 claus Exp $
    44 "
    46 "
    45 !
    47 !
    46 
    48 
    47 documentation
    49 documentation
    48 "
    50 "
    93      the argument"
    95      the argument"
    94 
    96 
    95     |newCollection|
    97     |newCollection|
    96 
    98 
    97     newCollection := self species new:(self size + aCollection size).
    99     newCollection := self species new:(self size + aCollection size).
       
   100     newCollection finalizeCopyFrom:self.
    98     self do:[:element |
   101     self do:[:element |
    99         newCollection add:element
   102         newCollection add:element
   100     ].
   103     ].
   101     aCollection do:[:element |
   104     aCollection do:[:element |
   102         newCollection add:element
   105         newCollection add:element
   103     ].
   106     ].
   104     ^ newCollection
   107     ^ newCollection
   105 
   108 
   106     "#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection"
   109     "
       
   110      #(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection
       
   111      #(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection
       
   112     "
   107 !
   113 !
   108 
   114 
   109 copyWith:newElement
   115 copyWith:newElement
   110     "return a new collection consisting of a copy of the receivers elements
   116     "return a new collection consisting of a copy of the receivers elements
   111      plus the argument."
   117      plus the argument."
   112 
   118 
   113     |newCollection mySize newSize|
   119     |newCollection mySize newSize|
   114 
   120 
   115     mySize := self size.
   121     mySize := self size.
   116     newSize := mySize + 1.
   122     newSize := mySize + 1.
   117     newCollection := self species new:newSize.
   123 "/    newCollection := self species new:newSize.
       
   124     newCollection := (self species new:newSize) finalizeCopyFrom:self.
   118     newCollection grow:newSize.
   125     newCollection grow:newSize.
   119     newCollection replaceFrom:1 to:mySize with:self startingAt:1.
   126     newCollection replaceFrom:1 to:mySize with:self startingAt:1.
   120     newCollection at:newSize put:newElement.
   127     newCollection at:newSize put:newElement.
   121     ^newCollection
   128     ^newCollection
   122 
   129 
   123     "#(1 2 3 4 5) copyWith:$a"
   130     "
   124     "'abcdefg' copyWith:$h"
   131      #(1 2 3 4 5) copyWith:$a
   125     "'abcdefg' copyWith:'123'"  "-- will fail: string cannot be stored into string"
   132      'abcdefg' copyWith:$h
   126     "'abcdefg' copyWith:1"      "-- will fail: integer cannot be stored into string"
   133      'abcdefg' copyWith:'123'    -- will fail: string cannot be stored into string
       
   134      'abcdefg' copyWith:1        -- will fail: integer cannot be stored into string
       
   135     "
   127 !
   136 !
   128 
   137 
   129 copy
   138 copy
   130     "return a new OrderedCollection containing the elements of the receiver."
   139     "return a new OrderedCollection containing the elements of the receiver."
   131 
   140 
   141      from start to stop."
   150      from start to stop."
   142 
   151 
   143     |newCollection sz|
   152     |newCollection sz|
   144 
   153 
   145     sz := stop - start + 1.
   154     sz := stop - start + 1.
   146     newCollection := self species new:sz.
   155 "/    newCollection := self species new:sz.
       
   156     newCollection := (self species new:sz) finalizeCopyFrom:self.
   147     newCollection grow:sz.
   157     newCollection grow:sz.
   148     newCollection replaceFrom:1 to:sz with:self startingAt:start.
   158     newCollection replaceFrom:1 to:sz with:self startingAt:start.
   149     ^ newCollection
   159     ^ newCollection
   150 ! !
   160 ! !
   151 
   161 
   283     contentsArray at:firstIndex put:anObject.
   293     contentsArray at:firstIndex put:anObject.
   284     ^ anObject
   294     ^ anObject
   285 
   295 
   286     "
   296     "
   287      |c|
   297      |c|
   288      c := #(4 3 2 1) asOrderedCollection.
   298      c := #(1 2 3 4) asOrderedCollection.
   289      c addFirst:'here'
   299      c addFirst:'here'.
       
   300      c
       
   301     "
       
   302 
       
   303     "
       
   304      |c|
       
   305      c := #() asOrderedCollection.
       
   306      c addFirst:'here'.
       
   307      c
   290     "
   308     "
   291 !
   309 !
   292 
   310 
   293 add:anObject beforeIndex:index
   311 add:anObject beforeIndex:index
   294     "insert the argument, anObject to become located at index.
   312     "insert the argument, anObject to become located at index.
   558      newSize    "{ Class:SmallInteger }" 
   576      newSize    "{ Class:SmallInteger }" 
   559      startIndex "{ Class:SmallInteger }"
   577      startIndex "{ Class:SmallInteger }"
   560      sz         "{ Class:SmallInteger }"|
   578      sz         "{ Class:SmallInteger }"|
   561 
   579 
   562     oldSize := contentsArray size.
   580     oldSize := contentsArray size.
       
   581     oldSize == 0 ifTrue:[ 
       
   582         contentsArray := Array new:3.
       
   583         firstIndex := 2. lastIndex := 1.
       
   584         ^ self
       
   585     ].
       
   586 
   563     sz := self size.
   587     sz := self size.
   564 
   588 
   565     "if there is lots of room at the end (> 50%), shift instead of growing"
   589     "if there is lots of room at the end (> 50%), shift instead of growing"
   566     oldSize > (sz * 2) ifTrue:[
   590     oldSize > (sz * 2) ifTrue:[
   567         startIndex := oldSize // 4.
   591         startIndex := oldSize // 4.
   756 
   780 
   757     |newCollection
   781     |newCollection
   758      start  "{ Class:SmallInteger }"
   782      start  "{ Class:SmallInteger }"
   759      stop   "{ Class:SmallInteger }" |
   783      stop   "{ Class:SmallInteger }" |
   760 
   784 
   761     newCollection := (self species new).
   785 "/    newCollection := (self species new).
       
   786     newCollection := (self species new) finalizeCopyFrom:self.
   762     stop := lastIndex.
   787     stop := lastIndex.
   763     start := firstIndex.
   788     start := firstIndex.
   764     start to:stop do:[:index |
   789     start to:stop do:[:index |
   765         newCollection add:(aBlock value:(contentsArray at:index)).
   790         newCollection add:(aBlock value:(contentsArray at:index)).
   766     ].
   791     ].