BitMagic-C++
svsample01.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 svsample01.cpp
20 Example of how to use bm::sparse_vector<> template class to set or
21 import values.
22
23 bm::sparse_vector<> uses bit-splicing method to keep bit-planes separate
24 to provide on-the fly succinct data vector.
25
26 \sa bm::sparse_vector<>::import
27 \sa bm::sparse_vector<>::at
28 \sa bm::sparse_vector<>::optimize
29 \sa bm::sparse_vector<>::size
30*/
31
32/*! \file svsample01.cpp
33 \brief Example: sparse_vector<> container set values
34*/
35
36
37#include <iostream>
38#include <algorithm>
39
40#include "bm.h"
41#include "bmsparsevec.h"
42#include "bmundef.h" /* clear the pre-proc defines from BM */
43
44using namespace std;
45
48
49/**
50 bm::sparse_vector for unsigned int data
51*/
52static
53void Demo1()
54{
55 using value_type = bm::sparse_vector<int, bm::bvector<> >::value_type;
57
58 unsigned arr[3] = {1,2,3};
59 sv1.import(arr, 3); // import from a C-style array (fastest way to populate)
60
61 // optimize memory allocation of sparse vector
62 {
64 sv1.optimize(tb);
65 }
66
67 cout << "sv1.size() = " << sv1.size() << endl;
68 cout << "sv[]:";
69
70 // print the vector elements using direct access operator
71 for (unsigned i = 0; i < sv1.size(); ++i)
72 {
73 cout << sv1.at(i) << ",";
74 }
75 cout << endl;
76
77 // add more at the end
78 unsigned arr2[5] = {10, 20, 30, 40, 50};
79 sv1.import(arr2, 5, sv1.size());
80
81 cout << "sv1.size() = " << sv1.size() << endl;
82 cout << "sv[]:";
83
84 // print using std::for_each plus lambda
85 std::for_each(sv1.begin(), sv1.end(),
86 [](value_type n) { cout << n << ", "; });
87 cout << endl;
88}
89
90/**
91 bm::sparse_vector can also support signed ints.
92 The container transforms it from complementary code to more
93 optimal representation so that
94*/
95static
96void Demo2()
97{
99
100 int arr[3] = {1,-2,3};
101 sv1.import(arr, 3); // import from a C-style array (fastest way to populate)
102
103 // add more at the end
104 int arr2[5] = {-10, 20, 30, 40, -50};
105 sv1.import(arr2, 5, sv1.size());
106 {
108 sv1.optimize(tb);
109 }
110
111 cout << "sv1.size() = " << sv1.size() << endl;
112 cout << "sv[]:";
113
114 // print using std::for_each plus lambda
115 std::for_each(sv1.begin(), sv1.end(), [] (auto n) { cout << n << ", "; }
116 );
117 cout << endl;
118}
119
120
121int main(void)
122{
123 try
124 {
125 // Demo1 for unsigned int
126 Demo1();
127
128 cout << endl << endl;
129
130 // Demo2 for signed int
131 Demo2();
132 }
133 catch(std::exception& ex)
134 {
135 std::cerr << ex.what() << std::endl;
136 return 1;
137 }
138
139
140 return 0;
141}
142
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
const_iterator end() const BMNOEXCEPT
Provide const iterator access to the end
Definition: bmsparsevec.h:559
const_iterator begin() const BMNOEXCEPT
Provide const iterator access to container content
Definition: bmsparsevec.h:2256
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
static void Demo1()
bm::sparse_vector for unsigned int data
Definition: svsample01.cpp:53
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: svsample01.cpp:46
int main(void)
Definition: svsample01.cpp:121
bm::sparse_vector< int, bm::bvector<> > sparse_vector_i32
Definition: svsample01.cpp:47
static void Demo2()
bm::sparse_vector can also support signed ints.
Definition: svsample01.cpp:96