[squeak-dev] The Trunk: Collections-nice.130.mcz

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


Nicolas Cellier uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-nice.130.mcz

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

Name: Collections-nice.130
Author: nice
Time: 14 September 2009, 8:48:49 am
UUID: bd79631f-87b9-4d13-aeca-b819eb6390ca
Ancestors: Collections-ar.129

add #removeAll suport to collections
This is to solve both
http://bugs.squeak.org/view.php?id=7177
http://bugs.squeak.org/view.php?id=6937

Comments about the implementation:

The super implementation might be enhanced with a shallowCopy in order to provide a working implementation in more cases:
    self shallowCopy do: [:each | self remove: each].
I believe much subclasses will override, so I did not even bother.

Subclass implementation tries to preserve original capacity.
This choice is arbitrary and really depends on application side...

CharacterSetComplement use a #become:. This is not efficient, but neither would be the addition of all existing characters to the complement (this set is defined by its complement as the name tells).

Collection>>#removeAll: also has been modified to handle case of (self removeAll: self). This extends and supersedes the changes from kwl at http://bugs.squeak.org/view.php?id=6937

The big question is what should (self removeAll: self) return?
My answer is simple: it returns self (an empty collection).
Beware, this will break some chaining:
(b removeAll: (a removeAll: a)) ~= (a removeAll: (b removeAll: a)).
Anyway, that is not worse than current implementation which will not lead to the most predictible results.

=============== Diff against Collections-ar.129 ===============

Item was added:
+ ----- Method: Set>>removeAll (in category 'removing') -----
+ removeAll
+ 	"remove all elements from this collection.
+ 	Preserve the capacity"
+ 	
+ 	self initialize: self capacity!

Item was added:
+ ----- Method: WideCharacterSet>>removeAll (in category 'collection ops') -----
+ removeAll
+ 	map removeAll!

Item was changed:
  ----- Method: Collection>>removeAll: (in category 'removing') -----
  removeAll: aCollection 
  	"Remove each element of aCollection from the receiver. If successful for 
  	each, answer aCollection. Otherwise create an error notification.
  	ArrayedCollections cannot respond to this message."
  
+ 	aCollection == self ifTrue: [^self removeAll].
  	aCollection do: [:each | self remove: each].
  	^ aCollection!

Item was added:
+ ----- Method: OrderedCollection>>removeAll (in category 'removing') -----
+ removeAll
+ 	"remove all the elements from this collection.
+ 	Keep same amount of storage"
+ 	
+ 	self setCollection: (Array new: array size)!

Item was added:
+ ----- Method: CharacterSetComplement>>removeAll (in category 'collection ops') -----
+ removeAll
+ 	| newSet |
+ 	newSet := CharacterSet new.
+ 	self become: newSet!

Item was added:
+ ----- Method: Bag>>removeAll (in category 'removing') -----
+ removeAll
+ 	"Implementation Note: as contents will be overwritten, a shallowCopy of self would be modified.
+ 	An alternative implementation preserving capacity would be to create a new contents:
+ 	self setContents: (self class contentsClass new: contents size)."
+ 	
+ 	contents removeAll!

Item was added:
+ ----- Method: KeyedSet>>removeAll (in category 'removing') -----
+ removeAll
+ 	"See super."
+ 	
+ 	| tmp |
+ 	tmp := keyBlock.
+ 	super removeAll.
+ 	keyBlock := tmp!

Item was added:
+ ----- Method: WeakRegistry>>removeAll (in category 'removing') -----
+ removeAll
+ 	"See super"
+ 	
+ 	self protected:[
+ 		valueDictionary removeAll.
+ 	].!

Item was added:
+ ----- Method: Collection>>removeAll (in category 'removing') -----
+ removeAll
+ 	"Remove each element from the receiver and leave it empty.
+ 	ArrayedCollections cannot respond to this message.
+ 	There are two good reasons why a subclass should override this message:
+ 	1) the subclass does not support being modified while being iterated
+ 	2) the subclass provides a much faster way than iterating through each element"
+ 
+ 	self do: [:each | self remove: each].!

Item was added:
+ ----- Method: LinkedList>>removeAll (in category 'removing') -----
+ removeAll
+ 	"Implementation note: this has to be fast"
+ 
+ 	firstLink := lastLink := nil!




More information about the Squeak-dev mailing list