BitMagic-C++
sample20.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 sample20.cpp
20
21 Example for shifting - insertion of bits.
22
23 \sa bm::bvector::insert
24 \sa bm::bvector::shift_right
25*/
26
27/*! \file sample20.cpp
28 \brief Example: bvector<> - bit-shifts
29*/
30
31#include <iostream>
32
33#include "bm.h"
34#include "bmundef.h" /* clear the pre-proc defines from BM */
35
36using namespace std;
37
38// Utility template function used to print container
39template<class T> void PrintContainer(T first, T last)
40{
41 if (first == last)
42 cout << "<EMPTY SET>";
43 else
44 for(;first != last; ++first)
45 cout << *first << ", ";
46 cout << endl;
47}
48
49int main(void)
50{
51 try
52 {
53 bm::bvector<> bv { 1, 10, 100 };
54
55 cout << "Source set:";
56 PrintContainer(bv.first(), bv.end());
57
58 // shift the vector right by 1
59 //
60 bv.shift_right();
61 PrintContainer(bv.first(), bv.end()); // 2, 11, 101
62
63 // insert 0 bit into position 0 (equivalent of shift_right)
64 //
65 bv.insert(0, false);
66 PrintContainer(bv.first(), bv.end()); // 3, 12, 102
67
68 // insert at a random position
69 bv.insert(4, true);
70 PrintContainer(bv.first(), bv.end()); // 3, 4, 13, 103
71
72 }
73 catch(std::exception& ex)
74 {
75 std::cerr << ex.what() << std::endl;
76 return 1;
77 }
78
79
80 return 0;
81}
82
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
void PrintContainer(T first, T last)
Definition: sample20.cpp:39
int main(void)
Definition: sample20.cpp:49