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 sample1.cpp 00025 Example of how to use bvector template class to set 00026 bits and then retrieve indexes of ON bits. 00027 00028 For more information please visit: http://bmagic.sourceforge.net 00029 00030 \sa bm::bvector<>::get_next() 00031 \sa bm::bvector<>::get_first() 00032 \sa bm::bvector<>::set() 00033 \sa bm::bvector<>::count() 00034 \sa bm::bvector<>::clear() 00035 00036 */ 00037 00038 #include <iostream> 00039 #include "bm.h" 00040 00041 using namespace std; 00042 00043 int main(void) 00044 { 00045 bm::bvector<> bv; // Bitvector variable declaration. 00046 00047 cout << bv.count() << endl; 00048 00049 // Set some bits. 00050 00051 bv.set(10); 00052 bv.set(100); 00053 bv.set(1000000); 00054 00055 // New bitvector's count. 00056 00057 cout << bv.count() << endl; 00058 00059 00060 // Print the bitvector. 00061 00062 unsigned value = bv.get_first(); 00063 do 00064 { 00065 cout << value; 00066 value = bv.get_next(value); 00067 if (value) 00068 { 00069 cout << ","; 00070 } 00071 else 00072 { 00073 break; 00074 } 00075 } while(1); 00076 00077 cout << endl; 00078 00079 bv.clear(); // Clean up. 00080 00081 cout << bv.count() << endl; 00082 00083 // We also can use operators to set-clear bits; 00084 00085 bv[10] = true; 00086 bv[100] = true; 00087 bv[10000] = true; 00088 00089 cout << bv.count() << endl; 00090 00091 if (bv[10]) 00092 { 00093 bv[10] = false; 00094 } 00095 00096 cout << bv.count() << endl; 00097 00098 return 0; 00099 } 00100