sample1.cpp
Go to the documentation of this file.
/*
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;
}
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bmundef.h
pre-processor un-defines to avoid global space pollution (internal)
bm::bvector
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:115
bm::bvector::test
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
bm::bvector::count
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition bm.h:2401
bm::bvector::set
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
bm::bvector::get_next
size_type get_next(size_type prev) const BMNOEXCEPT
Finds the number of the next bit ON.
Definition bm.h:1609
bm::bvector::swap
void swap(bvector< Alloc > &bvect) BMNOEXCEPT
Exchanges content of bv and this bvector.
Definition bm.h:3966
bm::bvector::get_first
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
bm::bvector::clear
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
main
int main(void)
Definition sample1.cpp:43