Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals | Examples

sample1.cpp

Go to the documentation of this file.
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 
00039 #include <iostream>
00040 #include "bm.h"
00041 
00042 using namespace std;
00043 
00044 int main(void)
00045 {
00046     bm::bvector<>   bv;    // Bitvector variable declaration.
00047 
00048     cout << bv.count() << endl;
00049 
00050     // Set some bits.
00051 
00052     bv.set(10);
00053     bv.set(100);
00054     bv.set(1000000);
00055 
00056     // New bitvector's count.
00057 
00058     cout << bv.count() << endl;
00059 
00060 
00061     // Print the bitvector.
00062 
00063     unsigned value = bv.get_first();
00064     do
00065     {
00066         cout << value;
00067         value = bv.get_next(value);
00068         if (value)
00069         {
00070             cout << ",";
00071         }
00072         else
00073         {
00074             break;
00075         }
00076     } while(1);
00077 
00078     cout << endl;
00079 
00080     bv.clear();   // Clean up.
00081 
00082     cout << bv.count() << endl;
00083 
00084     // We also can use operators to set-clear bits;
00085 
00086     bv[10] = true;
00087     bv[100] = true;
00088     bv[10000] = true;
00089 
00090     cout << bv.count() << endl;
00091 
00092     if (bv[10])
00093     {
00094         bv[10] = false;
00095     }
00096 
00097     cout << bv.count() << endl;
00098 
00099     return 0;
00100 }
00101 

Generated on Sun Aug 5 14:12:26 2007 for BitMagic by  doxygen 1.4.1