The idea is to define a set (of integers, to simplify), as a function; you pass the element and it returns whether the element is in the set or not. So, we can define a Set as a function that takes a boolean and returns an int. I also define a predicate, which is identical to a set. BTW, I'm using the new std::function type.
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
typedef std::function<bool(int)> Set; | |
typedef std::function<bool(int)> Predicate; |
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
Set singletonSet(int val) { | |
return [=](int x){return x==val;} ; | |
} |
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
Set set_union(Set s1, Set s2) | |
{ | |
return [=](int x){return contains(s1,x) || contains(s2,x);}; | |
} | |
Set set_intersection(Set s1, Set s2) | |
{ | |
return [=](int x){return contains(s1,x) && contains(s2,x);}; | |
} |
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
void demo_union(void) | |
{ | |
Set s1=singletonSet(1); | |
Set s2=singletonSet(2); | |
Set s3=singletonSet(3); | |
Set un=set_union(s1,s2); | |
Set all=set_union(s1,set_union(s2,s3)); | |
cout << contains(s1,2) << endl; // this should print 0 (meaning false :) | |
cout << contains(s2,2) << endl; // this should print 1 (meaning true :) | |
cout << contains(all,3) << endl; // this should print 1 (meaning true :) | |
} |
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
void demo_intersection(void) | |
{ | |
Set s1=singletonSet(1); | |
Set s2=singletonSet(2); | |
Set s3=singletonSet(3); | |
Set all=set_union(s1,set_union(s2,s3)); | |
Set s1b=set_intersection(all,s1); // back to s1, since it is the intersection of all=(s1 U s2 U s3) | |
Set empty=set_intersection(s1,s2); // empty set ! why ? | |
cout << contains(s1b,1) << endl; // what would this print and why? | |
} |
Why do you need a contains function ? Would it not be enough to implement union as [=](int x) { return s1(x) || s2(x); } ?
ReplyDeleteAlso, you could do with fewer ... dysfunctional ^-^ ... comments if you did `std::cout << std::boolalpha;` too :)
ReplyDeleteI used contains since the assignment had it :) I like it because it reminds me I'm viewing it as a set, rather than a function.
ReplyDeleteAnd, thanks for reminding about boolalpha :)