BitMagic-C++: sample20.cpp Source File

BitMagic-C++

sample20.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 For 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
35 using namespace std;
36
37 // Utility template function used to print container
38 template<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
48 int main(void)
49 {
50 try
51 {
52 bm::bvector<> bv { 1, 10, 100 };
53
54 cout << "Source set:";
55 PrintContainer(bv.first(), bv.end());
56
57 // shift the vector right by 1
58 //
59 bv.shift_right();
60 PrintContainer(bv.first(), bv.end()); // 2, 11, 101
61
62 // insert 0 bit into position 0 (equivalent of shift_right)
63 //
64 bv.insert(0, false);
65 PrintContainer(bv.first(), bv.end()); // 3, 12, 102
66
67 // insert at a random position
68 bv.insert(4, true);
69 PrintContainer(bv.first(), bv.end()); // 3, 4, 13, 103
70
71 }
72 catch(std::exception& ex)
73 {
74 std::cerr << ex.what() << std::endl;
75 return 1;
76 }
77
78
79 return 0;
80 }
81
main
int main(void)
Definition: sample20.cpp:48
bm::bvector<>
PrintContainer
void PrintContainer(T first, T last)
Definition: sample20.cpp:38
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.