The Trunk: Collections-eem.757.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Collections-eem.757.mcz

commits-2
Eliot Miranda uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-eem.757.mcz

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

Name: Collections-eem.757
Author: eem
Time: 23 June 2017, 11:32:54.271499 am
UUID: bdb8992f-4170-46e3-a573-f2bb4cd7a2af
Ancestors: Collections-eem.756

Add Set>>ifAbsentAdd: as a better named equivalent of addNewElement:.
Reimplement withoutDuplicates using ifAbsentAdd:.
Add the obvious Set-specific implementation of addIfNotPresent:.
Fix a bug with RunArray>>=; the old code would error if compared against other than a collection.

=============== Diff against Collections-eem.756 ===============

Item was changed:
+ ----- Method: RunArray>>= (in category 'comparing') -----
+ = anObject
+ "Test if all my elements are equal to those of anObject"
- ----- Method: RunArray>>= (in category 'accessing') -----
- = otherArray
- "Test if all my elements are equal to those of otherArray"
 
+ ^anObject class == self class
+ ifTrue: "Faster test between two RunArrays"
+ [(runs hasEqualElements: anObject runs)
+ and: [values hasEqualElements: anObject values]]
+ ifFalse:
+ [anObject isCollection and: [self hasEqualElements: anObject]]!
- (otherArray isMemberOf: RunArray) ifFalse: [^ self hasEqualElements: otherArray].
-
- "Faster test between two RunArrays"
-   ^ (runs hasEqualElements: otherArray runs)
- and: [values hasEqualElements: otherArray values]!

Item was changed:
  ----- Method: SequenceableCollection>>withoutDuplicates (in category 'copying') -----
  withoutDuplicates
  "Answer a copy of the receiver that preserves order but eliminates any duplicates."
  | seen |
  seen := Set new: self size.
+ ^self select: [:each| seen ifAbsentAdd: each]!
- ^self select: [:each|
-  (seen includes: each)
- ifTrue: [false]
- ifFalse: [seen add: each. true]]!

Item was added:
+ ----- Method: Set>>addIfNotPresent: (in category 'adding') -----
+ addIfNotPresent: anObject
+ "Include anObject as one of the receiver's elements, but only if there
+ is no such element already. Anwser anObject."
+
+ ^self add: anObject!

Item was added:
+ ----- Method: Set>>ifAbsentAdd: (in category 'adding') -----
+ ifAbsentAdd: anObject
+ "Ensure anObject is part of the receiver.  Answer whether its membership was newly acquired."
+ | index |
+ index := self scanFor: anObject.
+ (array at: index) ifNil:
+ [self
+ atNewIndex: index
+ put: anObject asSetElement.
+ ^true].
+ ^false!


Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Collections-eem.757.mcz

Tobias Pape
Nice :)

One suggestion:
> On 23.06.2017, at 20:33, [hidden email] wrote:
>
> + (array at: index) ifNil:
> + [self
> + atNewIndex: index
> + put: anObject asSetElement.
> + ^true].
> + ^false!

What about (safe for formatting) the current implementation of addNewElement: tho?

        | index |
        index := self scanFor: anObject.
        ^ (array at: index)
                ifNil:
                        [ self
                                atNewIndex: index
                                put: anObject asSetElement.
                        true ]
                ifNotNil: [ false ]

Shouldn't #addNewElement: be renamed #ifAbsentAdd: and #addNewElement: a deprecated alias?

Best regards
        -Tobias