BitMagic-C++
sample15.cpp

Example for finding first and last bits in bit-vector (dynamic range).Ranges of bit-vectors can be used to find probability of intersection. For instance, in some corner cases AND product can be predicted empty if vectors belong to different ranges.

@sa bm::bvector<>::find()
@sa bm::bvector<>::find_reverse()
@sa bm::bvector<>::find_range()
/*
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 sample15.cpp
Example for finding first and last bits in bit-vector (dynamic range).
Ranges of bit-vectors can be used to find probability of intersection.
For instance, in some corner cases AND product can be predicted empty if vectors
belong to different ranges.
@sa bm::bvector<>::find()
@sa bm::bvector<>::find_reverse()
@sa bm::bvector<>::find_range()
*/
/*! \file sample15.cpp
\brief Example: bvector<> methods to find last bit and bit-vectors effective range
*/
#include <stdlib.h>
#include <iostream>
#include <vector>
#include "bm.h"
using namespace std;
const unsigned MAX_VALUE = 10000000;
static
{
unsigned start = MAX_VALUE / (rand()%10);
for (unsigned i = start; i < MAX_VALUE; ++i)
{
if ((rand() % 10))
{
bv->set(i);
}
}
}
int main(void)
{
try
{
fill_bvector(&bv1);
fill_bvector(&bv2);
cout << "bv1 count = " << bv1.count() << endl;
cout << "bv2 count = " << bv2.count() << endl;
bool found;
found = bv1.find(first);
if (found)
cout << "bv1 first = " << first << endl;
found = bv1.find_reverse(last);
if (found)
cout << "bv1 last = " << last << endl;
found = bv2.find(first);
if (found)
cout << "bv2 first = " << first << endl;
found = bv2.find_reverse(last);
if (found)
cout << "bv2 last = " << last << endl;
found = bv1.find_range(first, last);
if (found)
cout << "bv1 range = [" << first << ", " << last << "]" << endl;
found = bv2.find_range(first, last);
if (found)
cout << "bv2 range = [" << first << ", " << last << "]" << endl;
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
fill_bvector
static void fill_bvector(bm::bvector<> *bv)
Definition: sample15.cpp:47
main
int main(void)
Definition: sample15.cpp:60
bm::bvector<>
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:3595
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2208
MAX_VALUE
const unsigned MAX_VALUE
Definition: sample15.cpp:44
bm::bvector::find
bool find(size_type &pos) const BMNOEXCEPT
Finds index of first 1 bit.
Definition: bm.h:4049
bm::bvector::find_range
bool find_range(size_type &first, size_type &last) const BMNOEXCEPT
Finds dynamic range of bit-vector [first, last].
Definition: bm.h:4095
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::bvector::find_reverse
bool find_reverse(size_type &pos) const BMNOEXCEPT
Finds last index of 1 bit.
Definition: bm.h:3992