BitMagic-C++
sample18a.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 sample18a.cpp
20 Example of bit-vector import from an external bit-stream
21 \sa bm::bvector::bulk_insert_iterator
22*/
23
24/*! \file sample18a.cpp
25 \brief Example: import from an external bit-stream
26*/
27
28#include <stdlib.h>
29#include <assert.h>
30#include <iostream>
31
32#include "bm.h"
33#include "bmbvimport.h"
34#include "bmundef.h" /* clear the pre-proc defines from BM */
35
36using namespace std;
37
38int main(void)
39{
40 try
41 {
43
44 // specify bits in an array of unsigned ints (32-bit)
45 // we expect that each set bit can be efficiently imported into a
46 // bvector<>. Import function breaks the bit stream into blocks,
47 // each block can be optimized/compressed on the fly, while data is
48 // still in the CPU cache
49
50 unsigned int arr[2058] = {0, };
51 arr[0] = 1 << 16;
52 arr[2047] = 1u << 31;
53 arr[2048] = 1u << 7;
54
55 bm::bit_import_u32(bv, arr, sizeof(arr)/sizeof(arr[0]), true/* optimize on the fly*/);
56 auto cnt = bv.count();
57 cout << "Imported " << cnt << " bits." << endl;
58
59 assert(cnt == 3);
60 assert(bv.test(16));
61 assert(bv.test(65535));
62 assert(bv.test(65536 + 7));
63
64 }
65 catch(std::exception& ex)
66 {
67 std::cerr << ex.what() << std::endl;
68 return 1;
69 }
70
71 return 0;
72}
73
74
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Import of bvector<> from native type bit-arrays.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bool test(size_type n) const BMNOEXCEPT
returns true if bit n is set and false is bit n is 0.
Definition: bm.h:1480
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
void bit_import_u32(BV &bv, const unsigned int *BMRESTRICT bit_arr, typename BV::size_type bit_arr_size, bool optimize)
Import native stream of bits (represented as 32-bit unsigned ints)
Definition: bmbvimport.h:46
int main(void)
Definition: sample18a.cpp:38