BitMagic-C++
sample1.cpp

Example how to use bvector<> to set bits and then retrieve indexes of ON bits.

Example how to use bvector<> to set bits and then retrieve indexes of ON bits

See also
bm::bvector<>::get_next()
bm::bvector<>::get_first()
bm::bvector<>::set()
bm::bvector<>::count()
bm::bvector<>::clear()
bm::bvector<>::swap()
/*
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 sample1.cpp
Example how to use bvector<> to set bits and then retrieve indexes of ON bits
\sa bm::bvector<>::get_next()
\sa bm::bvector<>::get_first()
\sa bm::bvector<>::set()
\sa bm::bvector<>::count()
\sa bm::bvector<>::clear()
\sa bm::bvector<>::swap()
*/
/*! \file sample1.cpp
\brief Example: bvector<> set bits and then retrieve indexes of ON bits
*/
#include <cassert>
#include <iostream>
#include "bm.h"
#include "bmundef.h" /* clear the pre-proc defines from BM */
using namespace std;
int main(void)
{
try
{
bm::bvector<> bv { 1, 2, 3 }; // Bitvector variable declaration with init list
cout << "1. bitcount: " << bv.count() << endl;
// Set some bits.
bv.set(10);
bv.set(100);
bv.set(1000000);
// New bitvector's count.
cout << "2. bitcount: " << bv.count() << endl;
// Print the bitvector.
auto value = bv.get_first();
do
{
cout << value;
value = bv.get_next(value);
if (value)
{
cout << ",";
}
else
{
break;
}
} while(1);
cout << endl;
bv.clear(); // Clean up.
cout << "3. bitcount: " << bv.count() << endl;
// We also can use operators to set-clear bits;
bv[10] = true;
bv[100] = true;
bv[10000] = true;
cout << "4. bitcount: " << bv.count() << endl;
if (bv[10])
{
bv[10] = false;
}
cout << "5. bitcount: " << bv.count() << endl; // 2
// swap two values
bv.swap(10, 100);
cout << "6. bitcount: " << bv.count() << endl; // 2 again
assert(bv.test(10)); // make sure values swapped ok
assert(!bv.test(100));
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:115
bool test(size_type n) const BMNOEXCEPT
returns true if bit n is set and false is bit n is 0.
Definition bm.h:1502
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition bm.h:2401
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition bm.h:4188
size_type get_next(size_type prev) const BMNOEXCEPT
Finds the number of the next bit ON.
Definition bm.h:1609
void swap(bvector< Alloc > &bvect) BMNOEXCEPT
Exchanges content of bv and this bvector.
Definition bm.h:3966
size_type get_first() const BMNOEXCEPT
find first 1 bit in vector. Function may return 0 and this requires an extra check if bit 0 is actual...
Definition bm.h:1600
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition bm.h:4149
int main(void)
Definition sample1.cpp:43