BitMagic-C++
bmbvimport.h
Go to the documentation of this file.
1#ifndef BMBVIMPORT__H__INCLUDED__
2#define BMBVIMPORT__H__INCLUDED__
3/*
4Copyright(c) 2002-2021 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18For more information please visit: http://bitmagic.io
19*/
20/*! \file bmbvimport.h
21 \brief Import of bvector<> from native type bit-arrays
22*/
23
24#ifndef BM__H__INCLUDED__
25// BitMagic utility headers do not include main "bm.h" declaration
26// #include "bm.h" or "bm64.h" explicitly
27# error missing include (bm.h or bm64.h)
28#endif
29
30/** \defgroup bvimport Import bvector<> from native bit-arrays
31 \ingroup bvector
32 */
33namespace bm
34{
35
36/**
37 Import native stream of bits (represented as 32-bit unsigned ints)
38 @param bv [out] - target bvector
39 @param bit_arr [in] - source array
40 @param bit_arr_size [in] - source array size in words (NOT in bits or bytes)
41 @param optimize [in] - flag to optimize/compress target bvector on the fly
42
43 @ingroup bvimport
44 */
45template<class BV>
46void bit_import_u32(BV& bv,
47 const unsigned int* BMRESTRICT bit_arr,
48 typename BV::size_type bit_arr_size,
49 bool optimize)
50{
51 BM_ASSERT(bit_arr);
52
53 bv.clear(true);
54 if (!bit_arr_size)
55 return;
56
57 using block_idx_type = typename BV::block_idx_type;
58 using bv_size_type = typename BV::size_type;
59
60 block_idx_type total_blocks = bit_arr_size / bm::set_block_size;
61 unsigned top_blocks = unsigned(total_blocks / bm::set_sub_array_size) + 1;
62 BM_ASSERT(top_blocks);
63
64 typename BV::blocks_manager_type& bman = bv.get_blocks_manager();
65 bman.reserve_top_blocks(top_blocks);
66
67 for (unsigned i = 0; i < top_blocks; ++i)
68 bman.check_alloc_top_subblock(i);
69
70 typename BV::block_idx_type nb = 0;
71 for (; nb < total_blocks; ++nb)
72 {
73 bm::word_t* block = bman.borrow_tempblock();
74 bman.set_block(nb, block);
75 const unsigned int* BMRESTRICT bit_arr_block_ptr =
76 &bit_arr[nb*bm::set_block_size];
77 bm::bit_block_copy_unalign(block, bit_arr_block_ptr);
78 if (optimize)
79 {
80 unsigned i0, j0;
81 bm::get_block_coord(nb, i0, j0);
82 bman.optimize_bit_block(i0, j0, BV::opt_compress); // returns tem_block if needed
83 }
84 } // for nb
85
86 // tail processing
87 //
88 typename BV::size_type tail_idx = nb * bm::set_block_size;
89 if (tail_idx < bit_arr_size)
90 {
91 BM_ASSERT(bit_arr_size - tail_idx < bm::set_block_size);
92 bm::word_t* block = bman.borrow_tempblock();
93 bman.set_block(nb, block);
94 bv_size_type i = tail_idx;
95 unsigned k = 0;
96 while (i < bit_arr_size) // copy the array's tail
97 block[k++] = bit_arr[i++];
98 while (k < bm::set_block_size) // zero the block's tail
99 block[k++] = 0;
100 if (optimize)
101 {
102 unsigned i0, j0;
103 bm::get_block_coord(nb, i0, j0);
104 bman.optimize_bit_block(i0, j0, BV::opt_compress); // returns tem_block if needed
105 }
106 }
107 if (optimize)
108 bman.free_temp_block();
109}
110
111} // namespace bm
112
113
114#endif
#define BMRESTRICT
Definition: bmdef.h:203
#define BM_ASSERT
Definition: bmdef.h:139
void bit_block_copy_unalign(bm::word_t *BMRESTRICT dst, const bm::word_t *BMRESTRICT src) BMNOEXCEPT
Bitblock copy operation (unaligned src)
Definition: bmfunc.h:6761
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
Definition: bm.h:78
unsigned int word_t
Definition: bmconst.h:39
const unsigned set_sub_array_size
Definition: bmconst.h:95
BMFORCEINLINE void get_block_coord(BI_TYPE nb, unsigned &i, unsigned &j) BMNOEXCEPT
Recalc linear bvector block index into 2D matrix coordinates.
Definition: bmfunc.h:172
const unsigned set_block_size
Definition: bmconst.h:55
bm::bvector ::size_type bv_size_type
Definition: sample24.cpp:54