#include #include #include #include "BitSet.h" using namespace std; // Checks to see if the bi = b1 intersection b2 is actually correct // by going bit by bit through the result. Note that this tests the // correctness of all possible elements in and not in the intersection. // // Also, is_member isn't required by the homework but you'll need it to // test the results bool checkIntersection(BitSet& b1, BitSet& b2, BitSet& bi) { for (int i = 0; i < 255; i++) { if (bi.is_member(i)) { if (!b1.is_member(i) || !b2.is_member(i)) { return false; } } else { if (b1.is_member(i) && b2.is_member(i)) { return false; } } } return true; } // Checks to see if the bi = b1 union b2 is actually correct // by going bit by bit through the result. Note that this tests the // correctness of all possible elements in and not in the intersection. bool checkUnion(BitSet& b1, BitSet& b2, BitSet& bu) { for (int i = 0; i < 255; i++) { if (bu.is_member(i)) { if (!b1.is_member(i) && !b2.is_member(i)) { return false; } } else { if (b1.is_member(i) || b2.is_member(i)) { return false; } } } return true; } // This program takes a number of elements to add to each of two sets // then generates the union and intersection of those two sets, and tests // the correctness of the results /* Example bmd@BMD-LT-3A[/cygdrive/c/bmd/211-Winter-2004/lab3/solutions]$ ./bitset 10 2 b1: BitSet(10)[5 17 24 71 91 143 199 207 211 252 ] b2: BitSet(10)[23 39 43 45 50 52 63 97 180 249 ] Intersection: BitSet(0)[] Check: 1 Union: BitSet(20)[5 17 23 24 39 43 45 50 52 63 71 91 97 143 180 199 207 211 249 252 ] Check: 1 b1: BitSet(10)[18 23 68 88 97 149 164 175 240 250 ] b2: BitSet(10)[18 30 38 58 74 86 100 115 158 178 ] Intersection: BitSet(1)[18 ] Check: 1 Union: BitSet(19)[18 23 30 38 58 68 74 86 88 97 100 115 149 158 164 175 178 240 250 ] Check: 1 bmd@BMD-LT-3A[/cygdrive/c/bmd/211-Winter-2004/lab3/solutions]$ N.B. my BitSet printer also prints out the size of the bitset in parens */ int main(int argc, char* argv[]) { int cnt; BitSet bit_set; srand(time(0)); if (argc < 2) { cerr << "Usage: " << argv[0] << " num_to_add num_trials "; exit(1); } int num_to_add = atoi(argv[1]); int num_trials = atoi(argv[2]); for (int j = 0; j < num_trials; j++) { int i, d; BitSet b1; BitSet b2; // Note the actual number in the set may be less // than num_to_add due to insertion of duplicates for(i = 0; i < num_to_add; i++) { d = rand() % 255; b1.insertElement(d); } for (i = 0; i < num_to_add; i++) { d = rand() % 255; b2.insertElement(d); } cout << "b1: " << b1 << endl; cout << "b2: " << b2 << endl; BitSet b3 = b1.intersectionOfSets(b2); cout << "Intersection: " << b3 << endl; cout << "Check: " << checkIntersection(b1, b2, b3) << endl; BitSet b4 = b1.unionOfSets(b2); cout << "Union: " << b4 << endl; cout << "Check: " << checkUnion(b1, b2, b4) << endl; } return 0; }