BitMagic-C++
svsample01.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 For 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 values
21 
22  \sa bm::sparse_vector<>::import
23  \sa bm::sparse_vector<>::at
24  \sa bm::sparse_vector<>::optimize
25  \sa bm::sparse_vector<>::size
26 */
27 
28 /*! \file svsample01.cpp
29  \brief Example: sparse_vector<> container set values
30 */
31 
32 
33 #include <iostream>
34 
35 #include "bm.h"
36 #include "bmsparsevec.h"
37 
38 using namespace std;
39 
40 int main(void)
41 {
42  try
43  {
45 
46  unsigned arr[3] = {1,2,3};
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 
55  cout << "sv1.size() = " << sv1.size() << endl;
56  cout << "sv[]:";
57 
58  // print the vector elements using direct access operator
59  for (unsigned i = 0; i < sv1.size(); ++i)
60  {
61  cout << sv1.at(i) << ",";
62  }
63  cout << endl;
64 
65  // add more at the end
66  unsigned arr2[5] = {10, 20, 30, 40, 50};
67  sv1.import(arr2, 5, sv1.size());
68 
69  cout << "sv1.size() = " << sv1.size() << endl;
70  cout << "sv[]:";
71  for (unsigned i = 0; i < sv1.size(); ++i)
72  {
73  cout << sv1.at(i) << ",";
74  }
75  cout << endl;
76 
77 
78  }
79  catch(std::exception& ex)
80  {
81  std::cerr << ex.what() << std::endl;
82  return 1;
83  }
84 
85 
86  return 0;
87 }
88 
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
value_type at(size_type idx) const
access specified element with bounds checking
Definition: bmsparsevec.h:1426
int main(void)
Definition: svsample01.cpp:40
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
Definition: bmsparsevec.h:1718
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
void import(const value_type *arr, size_type arr_size, size_type offset=0)
Import list of elements from a C-style array.
Definition: bmsparsevec.h:953
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
size_type size() const
return size of the vector
Definition: bmsparsevec.h:613