-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from ba-st/comparison
Improve hash combination capabilities
- Loading branch information
Showing
12 changed files
with
224 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
source/Buoy-Comparison-Tests/BitwiseExclusiveDisjunctionHashCombinatorTest.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
" | ||
A BitwiseExclusiveDisjunctionHashCombinatorTest is a test class for testing the behavior of BitwiseExclusiveDisjunctionHashCombinator | ||
" | ||
Class { | ||
#name : #BitwiseExclusiveDisjunctionHashCombinatorTest, | ||
#superclass : #TestCase, | ||
#instVars : [ | ||
'hashCombinator' | ||
], | ||
#category : #'Buoy-Comparison-Tests' | ||
} | ||
|
||
{ #category : #running } | ||
BitwiseExclusiveDisjunctionHashCombinatorTest >> setUp [ | ||
|
||
super setUp. | ||
hashCombinator := BitwiseExclusiveDisjunctionHashCombinator new | ||
] | ||
|
||
{ #category : #tests } | ||
BitwiseExclusiveDisjunctionHashCombinatorTest >> testCannotCombineAnEmptyCollection [ | ||
|
||
self | ||
should: [ hashCombinator combineAll: #() ] | ||
raise: AssertionFailed | ||
withMessageText: 'Cannot combine an empty collection of hashes' | ||
] | ||
|
||
{ #category : #tests } | ||
BitwiseExclusiveDisjunctionHashCombinatorTest >> testCombineAll [ | ||
|
||
self | ||
assert: ( hashCombinator combineAll: #(2 5 6) ) equals: ( ( 2 bitXor: 5 ) bitXor: 6 ); | ||
assert: ( hashCombinator combineAll: #(20 1) ) equals: ( 1 bitXor: 20 ); | ||
assert: ( hashCombinator combineAll: #(100 0 4 50) ) | ||
equals: ( ( ( 100 bitXor: 50 ) bitXor: 4 ) bitXor: 0 ); | ||
assert: ( hashCombinator combineAll: #(2) ) equals: 2; | ||
assert: ( hashCombinator combineAll: #(20) ) equals: 20; | ||
assert: ( hashCombinator combineAll: #(100) ) equals: 100 | ||
] | ||
|
||
{ #category : #tests } | ||
BitwiseExclusiveDisjunctionHashCombinatorTest >> testCombineHashOfWith [ | ||
|
||
self | ||
assert: ( hashCombinator combineHashOf: 'alpha' with: 'beta' ) | ||
equals: ( 'alpha' hash bitXor: 'beta' hash ) | ||
] | ||
|
||
{ #category : #tests } | ||
BitwiseExclusiveDisjunctionHashCombinatorTest >> testCombineHashesOfAll [ | ||
|
||
self | ||
assert: ( hashCombinator combineHashesOfAll: #('alpha' 'beta' 'gamma') ) | ||
equals: ( ( 'alpha' hash bitXor: 'beta' hash ) bitXor: 'gamma' hash ) | ||
] |
11 changes: 11 additions & 0 deletions
11
source/Buoy-Comparison-Tests/BuoyComparisonObjectExtensionsTest.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Class { | ||
#name : #BuoyComparisonObjectExtensionsTest, | ||
#superclass : #TestCase, | ||
#category : #'Buoy-Comparison-Tests' | ||
} | ||
|
||
{ #category : #tests } | ||
BuoyComparisonObjectExtensionsTest >> testHash [ | ||
|
||
self assert: ( ObjectUsingEqualityHashCombinator with: 1 and: 2 ) hash equals: ( 1 bitXor: 2 ) | ||
] |
41 changes: 0 additions & 41 deletions
41
source/Buoy-Comparison-Tests/ExclusiveLogicalOrTest.class.st
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
source/Buoy-Comparison-Tests/ObjectUsingEqualityHashCombinator.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Class { | ||
#name : #ObjectUsingEqualityHashCombinator, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'first', | ||
'second' | ||
], | ||
#category : #'Buoy-Comparison-Tests' | ||
} | ||
|
||
{ #category : #'instance creation' } | ||
ObjectUsingEqualityHashCombinator class >> with: anInteger and: anInteger2 [ | ||
|
||
^ self new initializeWith: anInteger and: anInteger2 | ||
] | ||
|
||
{ #category : #comparing } | ||
ObjectUsingEqualityHashCombinator >> hash [ | ||
|
||
^ self equalityHashCombinator combineHashOf: first with: second | ||
] | ||
|
||
{ #category : #initialization } | ||
ObjectUsingEqualityHashCombinator >> initializeWith: anInteger and: anInteger2 [ | ||
|
||
first := anInteger. | ||
second := anInteger2 | ||
] |
14 changes: 14 additions & 0 deletions
14
source/Buoy-Comparison/BitwiseExclusiveDisjunctionHashCombinator.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
" | ||
I'm a HashCombinator that uses bitXor: as the operation combinator. | ||
" | ||
Class { | ||
#name : #BitwiseExclusiveDisjunctionHashCombinator, | ||
#superclass : #HashCombinator, | ||
#category : #'Buoy-Comparison' | ||
} | ||
|
||
{ #category : #combining } | ||
BitwiseExclusiveDisjunctionHashCombinator >> combine: acumulatedHashValue with: hashValue [ | ||
|
||
^ acumulatedHashValue bitXor: hashValue | ||
] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
" | ||
I'm an abstract class defining the interface for hash combination. My intent is to provide a way of generate a hash by combining the hash of other objects. | ||
" | ||
Class { | ||
#name : #HashCombinator, | ||
#superclass : #Object, | ||
#category : #'Buoy-Comparison' | ||
} | ||
|
||
{ #category : #combining } | ||
HashCombinator >> combine: acumulatedHashValue with: hashValue [ | ||
|
||
self subclassResponsibility | ||
] | ||
|
||
{ #category : #combining } | ||
HashCombinator >> combineAll: hashes [ | ||
|
||
^ self combineAll: hashes doing: [ :hash | hash ] | ||
] | ||
|
||
{ #category : #combining } | ||
HashCombinator >> combineAll: aCollection doing: aMonadycBlock [ | ||
|
||
AssertionChecker | ||
enforce: [ aCollection notEmpty ] | ||
because: 'Cannot combine an empty collection of hashes'. | ||
|
||
^ aCollection withoutFirst | ||
inject: ( aMonadycBlock value: aCollection first ) | ||
into: [ :combined :each | self combine: combined with: ( aMonadycBlock value: each ) ] | ||
] | ||
|
||
{ #category : #combining } | ||
HashCombinator >> combineHashOf: anObject with: anotherObject [ | ||
|
||
^ self combine: anObject hash with: anotherObject hash | ||
] | ||
|
||
{ #category : #combining } | ||
HashCombinator >> combineHashesOfAll: aCollection [ | ||
|
||
^ self combineAll: aCollection doing: [ :object | object hash ] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Extension { #name : #Object } | ||
|
||
{ #category : #'*Buoy-Comparison' } | ||
Object >> equalityHashCombinator [ | ||
|
||
^ BitwiseExclusiveDisjunctionHashCombinator new | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Class { | ||
#name : #ExclusiveLogicalOr, | ||
#superclass : #Object, | ||
#category : #'Buoy-Deprecated' | ||
} | ||
|
||
{ #category : #Computing } | ||
ExclusiveLogicalOr class >> collecting: aBlock ofAll: anObjectCollection [ | ||
|
||
self | ||
deprecated: 'Use self equalityHashCombinator' | ||
transformWith: | ||
' ExclusiveLogicalOr collecting: `@aBlock ofAll: `@anObjectCollection' | ||
-> 'self equalityHashCombinator combineAll: `@collection doing: `@aBlock'. | ||
^ self equalityHashCombinator combineAll: anObjectCollection doing: aBlock | ||
] | ||
|
||
{ #category : #testing } | ||
ExclusiveLogicalOr class >> isDeprecated [ | ||
|
||
^ true | ||
] | ||
|
||
{ #category : #Computing } | ||
ExclusiveLogicalOr class >> ofAll: anObjectCollection [ | ||
|
||
self | ||
deprecated: 'Use self equalityHashCombinator' | ||
transformWith: | ||
' ExclusiveLogicalOr ofAll: `@collection' | ||
-> 'self equalityHashCombinator combineAll: `@collection'. | ||
^ self equalityHashCombinator combineAll: anObjectCollection | ||
] | ||
|
||
{ #category : #Computing } | ||
ExclusiveLogicalOr class >> ofHashesOfAll: anObjectCollection [ | ||
|
||
self | ||
deprecated: 'Use self equalityHashCombinator' | ||
transformWith: | ||
' ExclusiveLogicalOr ofHashesOfAll: `@collection' | ||
-> 'self equalityHashCombinator combineHashesOfAll: `@collection'. | ||
^ self equalityHashCombinator combineHashesOfAll: anObjectCollection | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package { #name : #'Buoy-Deprecated' } |