00001 /* 00002 Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com) 00003 00004 Permission is hereby granted, free of charge, to any person 00005 obtaining a copy of this software and associated documentation 00006 files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, 00008 publish, distribute, sublicense, and/or sell copies of the Software, 00009 and to permit persons to whom the Software is furnished to do so, 00010 subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included 00013 in all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00017 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00018 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00019 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00020 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00021 OTHER DEALINGS IN THE SOFTWARE. 00022 */ 00023 00024 /** \example sample2.cpp 00025 Example demonstrates using set operations AND, OR, XOR, etc. 00026 For more information please visit: http://bmagic.sourceforge.net 00027 00028 */ 00029 00030 00031 #include <iostream> 00032 #include "bm.h" 00033 00034 using namespace std; 00035 00036 void print_bvector(const bm::bvector<>& bv) 00037 { 00038 unsigned value = bv.get_first(); 00039 do 00040 { 00041 cout << value; 00042 value = bv.get_next(value); 00043 if (value) 00044 { 00045 cout << ","; 00046 } 00047 else 00048 { 00049 break; 00050 } 00051 } while(1); 00052 cout << endl; 00053 } 00054 00055 int main(void) 00056 { 00057 bm::bvector<> bv1; 00058 bm::bvector<> bv2; 00059 bm::bvector<> bv3; 00060 00061 bv1.set(10); 00062 bv1.set(100); 00063 bv1.set(1000000); 00064 00065 00066 bv2.set(10); 00067 bv2.set(100); 00068 00069 // Logical AND operation on bv2 (bv1 is the argument) 00070 // bv2 = bv2 AND bv1 00071 00072 bv3 = bv1 & bv2; 00073 print_bvector(bv3); 00074 00075 bv2 &= bv1; // You also can use: bv2.bit_and(bv1); 00076 print_bvector(bv2); 00077 00078 // bv2 = bv2 OR bv1 00079 00080 bv3 = bv1 | bv2; 00081 print_bvector(bv3); 00082 00083 bv2 |= bv1; // You can also use: bv2.bit_or(bv1); 00084 print_bvector(bv2); 00085 00086 00087 bv1.set(1000000, false); 00088 00089 // bv2 = bv2 SUB bv1 00090 00091 bv3 = bv2 - bv1; 00092 print_bvector(bv3); 00093 00094 bv2 -= bv1; // You can also use: bv2.bit_sub(bv1); 00095 print_bvector(bv2); 00096 00097 // bv2 XOR bv1 00098 00099 bv3 = bv2 ^ bv1; 00100 print_bvector(bv3); 00101 00102 bv2 ^= bv1; // You can also use: bv2.bit_xor(bv1); 00103 print_bvector(bv2); 00104 00105 // For lexicographical comparison there is set of overloaded 00106 // operators and function compare. 00107 00108 if (bv2 == bv3) 00109 { 00110 cerr << "Equivalent. Comparison result = " 00111 << bv2.compare(bv3) << endl; 00112 } 00113 else 00114 { 00115 cout << "Error." << endl; 00116 return 1; 00117 } 00118 00119 00120 return 0; 00121 }