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
38using namespace std;
39
40int main(void)
41{
44 unsigned i;
45 unsigned arr[3] = {1,2,3};
46
47 sv1.import(arr, 3); // import from a C-style array (fastest way to populate)
48
49 // optimize memory allocation of sparse vector
50 {
52 sv1.optimize(tb);
53 }
54 cout << "sv1.size() = " << sv1.size() << ": ";
55 for (i = 0; i < sv1.size(); ++i)
56 {
57 cout << sv1.at(i) << ",";
58 }
59 cout << endl;
60
61 // populate second sparse vector using dunamic resize assignment
62 //
63 for (i = 65536; i < 65536+10; ++i)
64 {
65 sv2.set(i, 256+i);
66 }
67 cout << "sv2.size() = " << sv2.size() << endl;
68
69 cout << "Perform sparse_vector<>::join()" << endl;
70 // join sv2 into sv1
71 // operation implemented using bitset OR on all bit-planes.
72 sv1.join(sv2);
73 sv1.optimize(); // please note, we use default optimize here (it is a bit slower)
74
75 cout << "Now sv1.size() = " << sv1.size() << endl;
76
77
78 std::vector<unsigned> v1(16);
79 sv1.decode(&v1[0], 65530, 16); // extract 16 elements starting from 65530
80 for (i = 0; i < 16; ++i)
81 {
82 cout << v1[i] << ",";
83 }
84 cout << endl;
85
86
87
88 return 0;
89}
90
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.
sparse vector with runtime compression using bit transposition method
Definition bmsparsevec.h:82
value_type at(size_type idx) const
access specified element with bounds checking
size_type size() const BMNOEXCEPT
return size of the vector
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
sparse_vector< Val, BV > & join(const sparse_vector< Val, BV > &sv)
join all with another sparse vector using OR operation
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.
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.
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 plains
int main(void)