Collection.st
changeset 11489 6009fd847761
parent 11475 f7e1a355df17
child 11498 df4f9e10b141
equal deleted inserted replaced
11488:8d948de20326 11489:6009fd847761
   225     "return a new collection which really provides space for n elements.
   225     "return a new collection which really provides space for n elements.
   226      Kludges around the stupid definition of OrderedCollection>>new:"
   226      Kludges around the stupid definition of OrderedCollection>>new:"
   227 
   227 
   228     ^ self withSize:n
   228     ^ self withSize:n
   229 ! !
   229 ! !
       
   230 
   230 
   231 
   231 !Collection class methodsFor:'Signal constants'!
   232 !Collection class methodsFor:'Signal constants'!
   232 
   233 
   233 emptyCollectionSignal
   234 emptyCollectionSignal
   234     "return the signal used to report non-allowed operation on empty collections"
   235     "return the signal used to report non-allowed operation on empty collections"
   381     ^ self keysAndValuesDo:[:key :index |
   382     ^ self keysAndValuesDo:[:key :index |
   382         aTwoArgBlock value:index value:key
   383         aTwoArgBlock value:index value:key
   383     ].
   384     ].
   384 ! !
   385 ! !
   385 
   386 
       
   387 
   386 !Collection methodsFor:'accessing'!
   388 !Collection methodsFor:'accessing'!
   387 
   389 
   388 anElement
   390 anElement
   389     "return any element from the collection, 
   391     "return any element from the collection, 
   390      report an error if there is none"
   392      report an error if there is none"
  1038      The reason for this is that it is not known whether we are dealing with number
  1040      The reason for this is that it is not known whether we are dealing with number
  1039      (i.e. if 0 is a good initial value for the sum). 
  1041      (i.e. if 0 is a good initial value for the sum). 
  1040      Consider a collection of measurement or physical objects, 0 would be the unitless 
  1042      Consider a collection of measurement or physical objects, 0 would be the unitless 
  1041      value and would not be appropriate to add with the unit-ed objects."
  1043      value and would not be appropriate to add with the unit-ed objects."
  1042 
  1044 
  1043     self emptyCheck.
  1045     self isEmpty ifTrue:[^ 0].
       
  1046 
  1044     ^ self 
  1047     ^ self 
  1045         inject:(self first class zero) 
  1048         inject:(self first class zero) 
  1046         into:[:accum :each | accum + each].
  1049         into:[:accum :each | accum + each].
  1047     
  1050     
  1048 
  1051 
  1049     "
  1052     "
  1050      TestCase should: [ { } sum ] raise:Error.
  1053      TestCase assert: ( {  } sum = 0 ).
  1051      TestCase should: [ ''  sum ] raise:Error.
       
  1052 
       
  1053      TestCase assert: ( { 1 } sum = 1 ).
  1054      TestCase assert: ( { 1 } sum = 1 ).
  1054      TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ).
  1055      TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ).
  1055      TestCase assert: ( (1 to:10) sum = 55 ).
  1056      TestCase assert: ( (1 to:10) sum = 55 ).
  1056      TestCase assert: ( 'abc' asByteArray sum = 294 ).
  1057      TestCase assert: ( 'abc' asByteArray sum = 294 ).
  1057      TestCase assert: ( { 10 +/- 2.
  1058      TestCase assert: ( { 10 +/- 2.
  2377     "return a suitable size increment for growing.
  2378     "return a suitable size increment for growing.
  2378      The default returned here may be (and is) redefined in subclasses."
  2379      The default returned here may be (and is) redefined in subclasses."
  2379 
  2380 
  2380     ^ self size max:2
  2381     ^ self size max:2
  2381 ! !
  2382 ! !
  2382 
       
  2383 
  2383 
  2384 
  2384 
  2385 !Collection methodsFor:'operations'!
  2385 !Collection methodsFor:'operations'!
  2386 
  2386 
  2387 decrementAt:aKey
  2387 decrementAt:aKey
  3338 ! !
  3338 ! !
  3339 
  3339 
  3340 !Collection class methodsFor:'documentation'!
  3340 !Collection class methodsFor:'documentation'!
  3341 
  3341 
  3342 version
  3342 version
  3343     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.216 2009-01-16 13:55:49 cg Exp $'
  3343     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.217 2009-01-22 20:24:47 cg Exp $'
  3344 ! !
  3344 ! !
  3345 
  3345 
  3346 Collection initialize!
  3346 Collection initialize!