BitMagic-C++
sample19.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 sample19.cpp
20 Example of bit-vector merge
21 \sa bm::bvector::merge
22
23*/
24
25/*! \file sample19.cpp
26 \brief Example: bit-vector merge
27
28 Merge operation is an equivalent of logical OR, except
29 it destroys the argument. This is done to more efficiently
30 transfer the information to the target vector by re-assigning
31 memory blocks, instead of doing allocation, copy and logical OR.
32
33 After merge the argument vector will have undefined content, so
34 it is better to clear or destroy it.
35
36 Typical use case is to merge vectors after some multi-threaded
37 partitioned processing.
38*/
39
40#include <stdlib.h>
41#include <iostream>
42
43#include "bm.h"
44#include "bmundef.h" /* clear the pre-proc defines from BM */
45
46using namespace std;
47
48int main(void)
49{
50 try
51 {
52 bm::bvector<> bv1 { 1, 2, 10};
53 bm::bvector<> bv2 { 10, 100000000 };
54
55 std::cout << "Before merge bv2 count = " << bv2.count() << std::endl; // 2
56
57 std::cout << "Merge." << std::endl;
58
59 bv1.merge(bv2); // merge may change bv2 content (undefined content)
60
61 std::cout << "bv1 count = " << bv1.count() << std::endl; // 4
62 std::cout << "bv2 count = " << bv2.count() << std::endl; // undefined result
63 }
64 catch(std::exception& ex)
65 {
66 std::cerr << ex.what() << std::endl;
67 return 1;
68 }
69
70
71 return 0;
72}
73
74
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
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
int main(void)
Definition: sample19.cpp:48