BitMagic-C++
bvsample01_64.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2019 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 bvsample01_64.cpp
20  Example how to use bvector<> in 64-bit mode
21  */
22 
23 /*! \file bvsample01_64.cpp
24  \brief Example: how to use 64-bit mode
25 
26  By default BitMagic uses 32-bit address mode even when if it is
27  compiled for 64-bit version. This way it supports 2^32-1 address space.
28 
29 
30  There are two ways to run BitMagic in 64-bit address mode:
31 
32  1. define BM64ADDR in your project
33  Or
34  2. include "bm64.h"
35 
36  Limitations:
37  - you CANNOT use 32-bit and 64-bit in the same compilation unit.
38  - Current implementation internally is 48-bit (which is a lot),
39  so your range will be [0..2^48-1]
40  - 32-bit vectors can be serialized and read as 64-bit, but
41  not vice versa.
42 */
43 #include <iostream>
44 #include <assert.h>
45 
46 #include "bm64.h"
47 
48 using namespace std;
49 
50 int main(void)
51 {
52  try
53  {
54  bm::bvector<> bv { 1, 2, 3, bm::id_max-1 }; // Bitvector variable declaration with init list
55  bm::bvector<>::size_type count = bv.count();
56 
57  cout << "BitCount = " << count << endl;
58  cout << "Max possible ID = " << bm::id_max-1 << endl;
59 
60  bm::bvector<>::size_type first, last;
61  bool range_found = bv.find_range(first, last);
62  assert(range_found);
63  (void)range_found; // to silence the warning on unused var
64 
65  cout << "[" << first << ", " << last << "]" << endl;
66 
67  bm::bvector<> bv_full;
68  bv_full.set(); // set the whole vector to 1111111....11111
69 
70  auto full_count = bv_full.count();
71  cout << "Full vector bitcount = " << full_count << endl;
72 
73  bv_full.set(bm::id_max - 1, false);
74 
75  bv &= bv_full;
76  bm::bvector<>::enumerator en = bv.first();
77  for (; en.valid(); ++en)
78  {
79  bm::id64_t idx = *en;
80  cout << idx << ", ";
81  }
82  cout << endl;
83  }
84  catch(std::exception& ex)
85  {
86  std::cerr << ex.what() << std::endl;
87  return 1;
88  }
89 
90 
91  return 0;
92 }
93 
main
int main(void)
Definition: bvsample01_64.cpp:50
bm::id64_t
unsigned long long int id64_t
Definition: bmconst.h:34
bm::bvector::enumerator
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:599
bm::bvector<>
bm::bvector::set
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:3581
bm::id_max
const unsigned id_max
Definition: bmconst.h:108
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2194
bm::bvector::iterator_base::valid
bool valid() const BMNOEXCEPT
Checks if iterator is still valid. Analog of != 0 comparison for pointers.
Definition: bm.h:280
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117