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 sample5.cpp 00025 Example demonstrates using enumerators - the fastest way to retrieve 00026 indexes of 1 bits from the bitvector. This approach works faster than 00027 get_first/get_next functions. 00028 00029 \sa bm::bvector<>::enumerator 00030 00031 For more information please visit: http://bmagic.sourceforge.net 00032 00033 */ 00034 00035 #include <iostream> 00036 #include <algorithm> 00037 #include "bm.h" 00038 00039 using namespace std; 00040 00041 void Print(unsigned n) 00042 { 00043 cout << n << endl;; 00044 } 00045 00046 int main(void) 00047 { 00048 bm::bvector<> bv; 00049 00050 bv[10] = true; 00051 bv[100] = true; 00052 bv[10000] = true; 00053 00054 bm::bvector<>::enumerator en = bv.first(); 00055 bm::bvector<>::enumerator en_end = bv.end(); 00056 00057 while (en < en_end) 00058 { 00059 cout << *en << endl; 00060 ++en; // Fastest way to increment enumerator 00061 } 00062 00063 en = bv.first(); 00064 00065 // This is not the fastest way to do the job, because for_each 00066 // often will try to calculate difference between iterators, 00067 // which is expensive for enumerators. 00068 // But it can be useful for some STL loyal applications. 00069 00070 std::for_each(en, en_end, Print); 00071 00072 return 0; 00073 }