BitMagic-C++
sample5.cpp

Example demonstrates using enumerators - the fastest way to retrieve indexes of 1 bits from the bitvector.

Example demonstrates using enumerators - the fastest way to retrieve indexes of 1 bits from the bitvector. This approach works faster than get_first()/get_next() functions.

See also
bm::bvector<>::enumerator
bm::bvector<>::first()
bm::bvector<>::end()
bm::bvector<>::get_enumerator()
/*
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For more information please visit: http://bitmagic.io
*/
/** \example sample5.cpp
Example demonstrates using enumerators - the fastest way to retrieve
indexes of 1 bits from the bitvector. This approach works faster than
get_first()/get_next() functions.
\sa bm::bvector<>::enumerator
\sa bm::bvector<>::first()
\sa bm::bvector<>::end()
\sa bm::bvector<>::get_enumerator()
*/
/*! \file sample5.cpp
\brief Example: bvector<>::enumerator use
*/
#include <iostream>
#include <algorithm>
#include "bm.h"
using namespace std;
inline
{
cout << n << endl;;
}
int main(void)
{
try
{
bv[10] = true;
bv[100] = true;
bv[10000] = true;
bv[65536] = true;
bv[65537] = true;
bv[65538] = true;
bv[65540] = true;
while (en < en_end)
{
cout << *en << ", ";
++en; // Fastest way to increment enumerator
}
cout << endl;
en = bv.first();
// This is not the fastest way to do the job, because for_each
// often will try to calculate difference between iterators,
// which is expensive for enumerators.
// But it can be useful for some STL loyal applications.
std::for_each(en, en_end, Print);
cout << endl;
// example to illustrate random positioning of enumerator
// go to a random bit number, enumerator automatically finds the available bit
//
en.go_to(65537);
for (; en.valid(); ++en)
{
cout << *en << ", ";
}
cout << endl;
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Constant iterator designed to enumerate "ON" bits.
Definition bm.h:600
bool go_to(size_type pos) BMNOEXCEPT
go to a specific position in the bit-vector (or next)
Definition bm.h:6870
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition bm.h:280
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
bm::id_t size_type
Definition bm.h:117
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition bm.h:1770
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition bm.h:1776
int main(void)
Definition sample1.cpp:38
void Print(bm::bvector<>::size_type n)
Definition sample5.cpp:41