BitMagic-C++
svsample03.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 svsample03.cpp
20 Example of how to merge sparse vectors and extract values fast
21
22 \sa bm::sparse_vector<>::set
23 \sa bm::sparse_vector<>::import
24 \sa bm::sparse_vector<>::decode
25 \sa bm::sparse_vector<>::join
26*/
27
28/*! \file svsample03.cpp
29 \brief Example: sparse_vector<> merge and fast extraction of content
30*/
31
32
33#include <iostream>
34#include <vector>
35#include "bm.h"
36#include "bmsparsevec.h"
37#include "bmundef.h" /* clear the pre-proc defines from BM */
38
39using namespace std;
40
41int main(void)
42{
45 unsigned i;
46 unsigned arr[3] = {1,2,3};
47
48 sv1.import(arr, 3); // import from a C-style array (fastest way to populate)
49
50 // optimize memory allocation of sparse vector
51 {
53 sv1.optimize(tb);
54 }
55 cout << "sv1.size() = " << sv1.size() << ": ";
56 for (i = 0; i < sv1.size(); ++i)
57 {
58 cout << sv1.at(i) << ",";
59 }
60 cout << endl;
61
62 // populate second sparse vector using dunamic resize assignment
63 //
64 for (i = 65536; i < 65536+10; ++i)
65 {
66 sv2.set(i, 256+i);
67 }
68 cout << "sv2.size() = " << sv2.size() << endl;
69
70 cout << "Perform sparse_vector<>::join()" << endl;
71 // join sv2 into sv1
72 // operation implemented using bitset OR on all bit-planes.
73 sv1.join(sv2);
74 sv1.optimize(); // please note, we use default optimize here (it is a bit slower)
75
76 cout << "Now sv1.size() = " << sv1.size() << endl;
77
78
79 std::vector<unsigned> v1(16);
80 sv1.decode(&v1[0], 65530, 16); // extract 16 elements starting from 65530
81 for (i = 0; i < 16; ++i)
82 {
83 cout << v1[i] << ",";
84 }
85 cout << endl;
86
87
88
89 return 0;
90}
91
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
pre-processor un-defines to avoid global space pollution (internal)
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
value_type at(size_type idx) const
access specified element with bounds checking
Definition: bmsparsevec.h:1730
size_type size() const BMNOEXCEPT
return size of the vector
Definition: bmsparsevec.h:711
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1804
sparse_vector< Val, BV > & join(const sparse_vector< Val, BV > &sv)
join all with another sparse vector using OR operation
Definition: bmsparsevec.h:2127
size_type decode(value_type *arr, size_type idx_from, size_type dec_size, bool zero_mem=true) const
Bulk export list of elements to a C-style array.
Definition: bmsparsevec.h:1338
void import(const value_type *arr, size_type arr_size, size_type offset=0, bool set_not_null=true)
Import list of elements from a C-style array.
Definition: bmsparsevec.h:1154
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename sparse_vector< Val, BV >::statistics *stat=0)
run memory optimization for all vector planes
Definition: bmsparsevec.h:2094
int main(void)
Definition: svsample03.cpp:41