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
48using namespace std;
49
50int main(void)
51{
52 try
53 {
54 bm::bvector<> bv { 1, 2, 3, bm::id_max-1 }; // Bitvector variable declaration with init list
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
int main(void)
Constant iterator designed to enumerate "ON" bits.
Definition bm.h:600
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition bm.h:280
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
bm::id_t size_type
Definition bm.h:117
bool find_range(size_type &first, size_type &last) const BMNOEXCEPT
Finds dynamic range of bit-vector [first, last].
Definition bm.h:4081
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
const unsigned id_max
Definition bmconst.h:108
unsigned long long int id64_t
Definition bmconst.h:34