[squeak-dev] The Trunk: Collections-ar.129.mcz

commits at source.squeak.org commits at source.squeak.org
Mon Sep 14 00:18:14 UTC 2009


Andreas Raab uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ar.129.mcz

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

Name: Collections-ar.129
Author: ar
Time: 13 September 2009, 5:17:44 am
UUID: 64ca8d2a-4608-ab4c-b4f4-bdbe61efcdab
Ancestors: Collections-cbc.128, Collections-ul.128

Merging Collections-ul.128:

- introduced #new:streamContents: in SequenceableCollection class. It's like #streamContents: but if you know the size of the new collection this method doesn't copy the result.
- updated SequenceableCollection class >> #streamContents: to use #new:streamContents:, kept the original default size 100.
- also updated Dictionary >> #association, Bag >> #sortedCounts and Dictionary >> #keysSortedSafely to use #new:streamContents:.

- introduced #sort: in OrderedCollection. It uses Array's merge sort to sort the internal array. This is much faster than sorting with SortedCollection (though it uses an extra Array object). Also added #sort which uses the same default block as Array >> #sort.
- updated Bag >> #sortedCounts, Bag >> #sortedElements, Dictionary >> #keysSortedSafely, SequenceableCollection >> #sortBy: to use OrderedCollection's or Array's #sort method which can be significantly faster in these cases.

- modified OrderedCollection class >> #newFrom: to avoid reallocating the internal array of the new OrderedCollection when the sender of #newFrom: doesn't extend the collection.

- changed the variable name from anOrderedCollection to aCollection in OrderedCollection >> #addAllLast: since it is being used with other Collections. Also changed the comment which seems to be superfluous.

All related tests are green.

=============== Diff against Collections-cbc.128 ===============

Item was changed:
  ----- Method: Bag>>sortedCounts (in category 'accessing') -----
  sortedCounts
  	"Answer with a collection of counts with elements, sorted by decreasing
  	count."
  
+ 	^(Array new: contents size streamContents: [ :stream |
+ 		contents associationsDo: [ :each |
+ 			stream nextPut: each value -> each key ] ])
+ 		sort: [:x :y | x >= y ];
+ 		yourself!
- 	| counts |
- 	counts := SortedCollection sortBlock: [:x :y | x >= y].
- 	contents associationsDo:
- 		[:assn |
- 		counts add: (Association key: assn value value: assn key)].
- 	^ counts!

Item was changed:
  ----- Method: Dictionary>>associations (in category 'accessing') -----
  associations
  	"Answer a Collection containing the receiver's associations."
+ 	
+ 	^Array new: self size streamContents: [ :stream |
+ 		self associationsDo: [ :each | stream nextPut: each ] ]!
- 	| out |
- 	out := WriteStream on: (Array new: self size).
- 	self associationsDo: [:value | out nextPut: value].
- 	^ out contents!

Item was changed:
  ----- Method: OrderedCollection>>addAllLast: (in category 'adding') -----
+ addAllLast: aCollection 
+ 	"Add each element of aCollection at the end of the receiver. 
+ 	Answer aCollection."
- addAllLast: anOrderedCollection 
- 	"Add each element of anOrderedCollection at the end of the receiver. 
- 	Answer anOrderedCollection."
  
+ 	aCollection do: [ :each | self addLast: each].
+ 	^aCollection!
- 	anOrderedCollection do: [:each | self addLast: each].
- 	^anOrderedCollection!

Item was changed:
  ----- Method: SequenceableCollection>>sortBy: (in category 'copying') -----
  sortBy: aBlock
  	"Create a copy that is sorted.  Sort criteria is the block that accepts two arguments.
  	When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending
  	order)."
  
+ 	^ self asOrderedCollection
+ 		sort: aBlock;
+ 		yourself!
- 	^ (self asSortedCollection: aBlock) asOrderedCollection!

Item was added:
+ ----- Method: OrderedCollection>>sort: (in category 'sorting') -----
+ sort: aSortBlock 
+ 	"Sort this array using aSortBlock. The block should take two arguments
+ 	and return true if the first element should preceed the second one."
+ 
+ 	self ifNotEmpty: [
+ 		array
+ 			mergeSortFrom: firstIndex
+ 			to: lastIndex
+ 			by: aSortBlock ]!

Item was changed:
  ----- Method: Dictionary>>keysSortedSafely (in category 'accessing') -----
  keysSortedSafely
+ 	"Answer an Array containing the receiver's keys."
+ 	
- 	"Answer a SortedCollection containing the receiver's keys."
  	| sortedKeys |
+ 	sortedKeys := Array new: self size streamContents: [ :stream |
+ 		self keysDo: [ :each | stream nextPut: each ] ].
+ 	sortedKeys sort: [ :x :y |
+ 		"Should really be use <obj, string, num> compareSafely..."
+ 		((x isString and: [ y isString ])
+ 			or: [ x isNumber and: [ y isNumber ] ])
+ 			ifTrue: [ x < y ]
+ 			ifFalse: [ x class == y class
+ 				ifTrue: [ x printString < y printString ]
+ 				ifFalse: [ x class name < y class name ] ] ].
+ 	^sortedKeys!
- 	sortedKeys := SortedCollection new: self size.
- 	sortedKeys sortBlock:
- 		[:x :y |  "Should really be use <obj, string, num> compareSafely..."
- 		((x isString and: [y isString])
- 			or: [x isNumber and: [y isNumber]])
- 			ifTrue: [x < y]
- 			ifFalse: [x class == y class
- 				ifTrue: [x printString < y printString]
- 				ifFalse: [x class name < y class name]]].
- 	self keysDo: [:each | sortedKeys addLast: each].
- 	^ sortedKeys reSort!

Item was added:
+ ----- Method: SequenceableCollection class>>new:streamContents: (in category 'stream creation') -----
+ new: newSize streamContents: blockWithArg
+ 
+ 	| stream |
+ 	stream := WriteStream on: (self new: newSize).
+ 	blockWithArg value: stream.
+ 	stream position = newSize
+ 		ifTrue: [ ^stream originalContents ]
+ 		ifFalse: [ ^stream contents ]!

Item was changed:
  ----- Method: OrderedCollection class>>newFrom: (in category 'instance creation') -----
  newFrom: aCollection 
  	"Answer an instance of me containing the same elements as aCollection."
  
+ 	^(self new: aCollection size)
+ 		resetTo: 1;
+ 		addAll: aCollection;
+ 		yourself
- 	| newCollection |
- 	newCollection := self new: aCollection size.
- 	newCollection addAll: aCollection.
- 	^newCollection
  
  "	OrderedCollection newFrom: {1. 2. 3}
  	{1. 2. 3} as: OrderedCollection
  	{4. 2. 7} as: SortedCollection
  "!

Item was added:
+ ----- Method: OrderedCollection>>sort (in category 'sorting') -----
+ sort
+ 	"Sort this array into ascending order using the '<=' operator."
+ 
+ 	self sort: [:a :b | a <= b]!

Item was changed:
  ----- Method: SequenceableCollection class>>streamContents: (in category 'stream creation') -----
  streamContents: blockWithArg
+ 	
+ 	^self new: 100 streamContents: blockWithArg!
- 	| stream |
- 	stream := WriteStream on: (self new: 100).
- 	blockWithArg value: stream.
- 	^stream contents!

Item was changed:
  ----- Method: Bag>>sortedElements (in category 'accessing') -----
  sortedElements
  	"Answer with a collection of elements with counts, sorted by element."
  
+ 	^contents associations
+ 		sort;
+ 		yourself!
- 	| elements |
- 	elements := SortedCollection new.
- 	contents associationsDo: [:assn | elements add: assn].
- 	^elements!




More information about the Squeak-dev mailing list