Andreas Raab uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ar.332.mcz==================== Summary ====================
Name: Collections-ar.332
Author: ar
Time: 9 March 2010, 9:59:22.438 pm
UUID: 13cc4a5a-e0a0-8e47-a637-ef1c0b28e643
Ancestors: Collections-ul.331
- Comments for Collection>>min,max
- Optimized implementations for Bag>>min,max,sum
=============== Diff against Collections-ul.331 ===============
Item was added:
+ ----- Method: Bag>>max (in category 'math functions') -----
+ max
+ "Answer the maximum value in the collection. This optimized version only looks at each unique value once."
+ ^contents keys inject: contents keys anyOne into: [:max :each | max max: each]
+ !
Item was added:
+ ----- Method: Bag>>min (in category 'math functions') -----
+ min
+ "Answer the minimum value in the collection. This optimized version only looks at each unique value once."
+ ^contents keys inject: contents keys anyOne into: [:min :each | min min: each]
+ !
Item was changed:
----- Method: Collection>>max (in category 'math functions') -----
max
+ "Answer the maximum value in the collection. The collection must be non-empty and contain 'compatible' Magnitudes (eg: don't try this with a collection containing both Dates and Characters)."
^ self inject: self anyOne into: [:max :each | max max: each]!
Item was changed:
----- Method: Collection>>min (in category 'math functions') -----
min
+ "Answer the minimum value in the collection. The collection must be non-empty and contain 'compatible' Magnitudes (eg: don't try this with a collection containing both Dates and Characters)."
^ self inject: self anyOne into: [:min :each | min min: each]!
Item was added:
+ ----- Method: Bag>>sum (in category 'math functions') -----
+ sum
+ "Faster than the superclass implementation when you hold many instances of the same value (which you probably do, otherwise you wouldn't be using a Bag)."
+ | sum |
+ sum := 0.
+ contents keysAndValuesDo: [:value :count | sum := sum + (value * count)].
+ ^sum!