-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for a join Semilattice #14
Open
SimonVervisch
wants to merge
4
commits into
warlock-labs:master
Choose a base branch
from
SimonVervisch:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use noether::{AssociativeJoin, CommutativeJoin, IdempotentJoin, Join, JoinSemiLattice}; | ||
use std::collections::HashSet; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct SetJoinSemilattice<T: Eq + std::hash::Hash + Clone> { | ||
set: HashSet<T>, | ||
} | ||
impl<T: Eq + std::hash::Hash + Clone> SetJoinSemilattice<T> { | ||
pub const fn new(set: HashSet<T>) -> Self { | ||
Self { set } | ||
} | ||
} | ||
impl<T: Eq + std::hash::Hash + Clone> CommutativeJoin for SetJoinSemilattice<T> {} | ||
impl<T: Eq + std::hash::Hash + Clone> AssociativeJoin for SetJoinSemilattice<T> {} | ||
|
||
impl<T: Eq + std::hash::Hash + Clone> IdempotentJoin for SetJoinSemilattice<T> {} | ||
|
||
impl<T: Eq + std::hash::Hash + Clone> Join for SetJoinSemilattice<T> { | ||
fn join(self, other: &Self) -> Self { | ||
let union: HashSet<T> = self.set.union(&other.set).cloned().collect(); | ||
Self { set: union } | ||
} | ||
|
||
/// The identity element: an empty set. | ||
/// Satisfies: a ⋁ {} == a | ||
fn identity(&self) -> Self { | ||
Self { | ||
set: HashSet::new(), | ||
} | ||
} | ||
} | ||
|
||
// // impl Join for {}; | ||
|
||
impl<T: Eq + std::hash::Hash + Clone> JoinSemiLattice for SetJoinSemilattice<T> {} | ||
|
||
fn main() { | ||
// Test 1: check join of different sets | ||
let set_a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | ||
let set_b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | ||
|
||
let semilattice_a = SetJoinSemilattice { set: set_a }; | ||
let semilattice_b = SetJoinSemilattice { set: set_b }; | ||
|
||
// Perform the join operation (union) | ||
let result: SetJoinSemilattice<i32> = semilattice_a.join(&semilattice_b); | ||
println!("Resulting Set: {:?}", result.set); | ||
|
||
// Test 2: check join of same element | ||
|
||
let set_c: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | ||
let set_d: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | ||
|
||
let semilattice_c = SetJoinSemilattice { set: set_c }; | ||
let semilattice_d = SetJoinSemilattice { set: set_d }; | ||
|
||
let result_same_set: SetJoinSemilattice<i32> = semilattice_c.join(&semilattice_d); | ||
println!("Resulting Same Set: {:?}", result_same_set.set); | ||
|
||
// Test 3: check join with identity element should return the same set | ||
|
||
let set_e: HashSet<_> = vec![1, 2, 4].into_iter().collect(); | ||
|
||
let semilattice_e = SetJoinSemilattice { set: set_e }; | ||
let identity = semilattice_e.identity(); | ||
|
||
let result_identity: SetJoinSemilattice<i32> = semilattice_e.join(&identity); | ||
println!( | ||
"Resulting of join with identity should return same set {:?}", | ||
result_identity.set | ||
); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might look at a blanket implementation here when the traits are satisfied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impl<T: Join> JoinSemiLattice for T {}
Like this? (For all types T)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah