BitMagic-C++
sample21.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For more information please visit: http://bitmagic.io
17*/
18
19/** \example sample21.cpp
20
21 Example for shifting - erase of bits.
22
23 \sa bm::bvector::erase
24 \sa bm::bvector::shift_left
25*/
26
27/*! \file sample21.cpp
28 \brief Example: bvector<> - bit-shifts
29*/
30
31#include <iostream>
32
33#include "bm.h"
34
35using namespace std;
36
37// Utility template function used to print container
38template<class T> void PrintContainer(T first, T last)
39{
40 if (first == last)
41 cout << "<EMPTY SET>";
42 else
43 for(;first != last; ++first)
44 cout << *first << ", ";
45 cout << endl;
46}
47
48int main(void)
49{
50 try
51 {
52 bm::bvector<> bv { 1, 10, 100 };
53 bv.optimize(); // run memory compression
54
55 cout << "Source set:";
56 PrintContainer(bv.first(), bv.end());
57
58 // shift the vector right by 1
59 //
60 bool carry_over = bv.shift_left();
61 cout << "CO=" << carry_over << endl;
62 PrintContainer(bv.first(), bv.end()); // 0, 9, 99
63
64 // erase 0 bit (equivalent of shift_left)
65 //
66 bv.erase(0);
67 PrintContainer(bv.first(), bv.end()); // 8, 98
68
69 // erase/insert manipulations with bit-vector may de-optimize
70 // the bit-vector so it needs re-optimization at some point
71 //
72 bv.optimize();
73
74 }
75 catch(std::exception& ex)
76 {
77 std::cerr << ex.what() << std::endl;
78 return 1;
79 }
80
81
82 return 0;
83}
84
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
Definition bm.h:3071
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition bm.h:1770
bool shift_left()
Shift left by 1 bit, fill with zero return carry out.
Definition bm.h:4368
void erase(size_type n)
Erase bit in the specified position All the vector content after erase position is shifted left.
Definition bm.h:4555
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition bm.h:1776
void PrintContainer(T first, T last)
Definition sample21.cpp:38
int main(void)
Definition sample21.cpp:48