[squeak-dev] The Trunk: Collections-ul.403.mcz

commits at source.squeak.org commits at source.squeak.org
Sun Nov 7 00:59:00 UTC 2010


Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.403.mcz

==================== Summary ====================

Name: Collections-ul.403
Author: ul
Time: 7 November 2010, 1:55:55.898 am
UUID: 7f81b384-8004-cd44-81e0-9b294d999c8a
Ancestors: Collections-ul.402

- merged Collection's #fold: and #reduce: (it's a bit faster than the original versions :))
- reimplemented Bag >> #sum to fix http://bugs.squeak.org/view.php?id=6560

=============== Diff against Collections-ul.402 ===============

Item was changed:
  ----- 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 first |
+ 	first := true.
+ 	contents keysAndValuesDo: [ :value :count |
+ 		first 
+ 			ifTrue: [ sum := value * count. first := false ]
+ 			ifFalse: [ sum := sum + (value * count) ] ].
+ 	first ifTrue: [ self errorEmptyCollection ].
- 	| sum |
- 	sum := 0.
- 	contents keysAndValuesDo: [:value :count | sum := sum + (value * count)].
  	^sum!

Item was changed:
  ----- Method: Collection>>fold: (in category 'enumerating') -----
  fold: binaryBlock
  	"Evaluate the block with the first two elements of the receiver,
  	 then with the result of the first evaluation and the next element,
  	 and so on.  Answer the result of the final evaluation. If the receiver
  	 is empty, raise an error. If the receiver has a single element, answer
  	 that element."
  	"#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b]"
  
+ 	^self reduce: binaryBlock!
- 	| firstValue nextValue |
- 	firstValue := nextValue := Object new. "something that can't be in the receiver"
- 	self do:
- 		[:each |
- 		nextValue := firstValue == nextValue
- 						ifTrue: [each]
- 						ifFalse: [binaryBlock value: nextValue value: each]].
- 	^nextValue == firstValue
- 		ifTrue: [self errorEmptyCollection]
- 		ifFalse: [nextValue]!

Item was changed:
  ----- Method: Collection>>reduce: (in category 'enumerating') -----
  reduce: binaryBlock
  	"Apply the argument, binaryBlock cumulatively to the elements of the receiver.
  	For sequenceable collections the elements will be used in order, for unordered
  	collections the order is unspecified."
  
  	| first nextValue |
- 	self emptyCheck.
  	first := true.
+ 	self do: [ :each |
+ 		first
+ 			ifTrue: [ nextValue := each. first := false ]
+ 			ifFalse: [ nextValue := binaryBlock value: nextValue value: each ] ].
+ 	first ifTrue: [ self errorEmptyCollection ].
- 	self do:[:each|
- 		first ifTrue:[nextValue := each. first := false]
- 			ifFalse:[nextValue := binaryBlock value: nextValue value: each]].
  	^nextValue!




More information about the Squeak-dev mailing list