BitMagic-C++
sample19.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 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 
45 using namespace std;
46 
47 int main(void)
48 {
49  try
50  {
51  bm::bvector<> bv1 { 1, 2, 10};
52  bm::bvector<> bv2 { 10, 100000000 };
53 
54  std::cout << "Before merge bv2 count = " << bv2.count() << std::endl; // 2
55 
56  std::cout << "Merge." << std::endl;
57 
58  bv1.merge(bv2); // merge may change bv2 content (undefined content)
59 
60  std::cout << "bv1 count = " << bv1.count() << std::endl; // 4
61  std::cout << "bv2 count = " << bv2.count() << std::endl; // undefined result
62  }
63  catch(std::exception& ex)
64  {
65  std::cerr << ex.what() << std::endl;
66  return 1;
67  }
68 
69 
70  return 0;
71 }
72 
73 
bm::bvector<>
main
int main(void)
Definition: sample19.cpp:47
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2220
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.