BitMagic-C++
bvsample01_64.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2019 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 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#include "bmundef.h" /* clear the pre-proc defines from BM */
48
49using namespace std;
50
51int main(void)
52{
53 try
54 {
55 bm::bvector<> bv { 1, 2, 3, bm::id_max-1 }; // Bitvector variable declaration with init list
56 bm::bvector<>::size_type count = bv.count();
57
58 cout << "BitCount = " << count << endl;
59 cout << "Max possible ID = " << bm::id_max-1 << endl;
60
61 bm::bvector<>::size_type first, last;
62 bool range_found = bv.find_range(first, last);
63 assert(range_found);
64 (void)range_found; // to silence the warning on unused var
65
66 cout << "[" << first << ", " << last << "]" << endl;
67
68 bm::bvector<> bv_full;
69 bv_full.set(); // set the whole vector to 1111111....11111
70
71 auto full_count = bv_full.count();
72 cout << "Full vector bitcount = " << full_count << endl;
73
74 bv_full.set(bm::id_max - 1, false);
75
76 bv &= bv_full;
77 bm::bvector<>::enumerator en = bv.first();
78 for (; en.valid(); ++en)
79 {
80 bm::id64_t idx = *en;
81 cout << idx << ", ";
82 }
83 cout << endl;
84 }
85 catch(std::exception& ex)
86 {
87 std::cerr << ex.what() << std::endl;
88 return 1;
89 }
90
91
92 return 0;
93}
94
pre-processor un-defines to avoid global space pollution (internal)
int main(void)
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition: bm.h:283
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
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:4153
bvector_size_type size_type
Definition: bm.h:121
const unsigned id_max
Definition: bmconst.h:109
unsigned long long int id64_t
Definition: bmconst.h:35