BitMagic-C++
rscsample01.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 rscsample01.cpp
20  Example of how to use bm::rsc_sparse_vector<> template class
21 
22  \sa bm::sparse_vector
23  \sa bm::rsc_sparse_vector
24  \sa bm::sparse_vector::push_back
25  \sa bm::sparse_vector_serialize
26  \sa bm::sparse_vector_deserialize
27 
28 */
29 
30 /*! \file rscsample01.cpp
31  \brief Example: rsc_sparse_vector<> usage
32 
33  rsc_sparse_vector<> is a sparse vector which uses bit-transposition and
34  rank-select succinct method of compression of NULL (unassigned) values.
35  Unassigned values are dropped (as transposed columns) from the bit-matrix.
36 
37  rsc_sparse_vector<> is basically a read-only structure, which can be used
38  for compact data storage and search. Random access to elements is possible
39  with a penalty of bit-vector Rank or Select operations.
40 */
41 
42 #include <iostream>
43 #include <vector>
44 
45 #include "bm.h"
46 #include "bmsparsevec.h"
47 #include "bmsparsevec_compr.h"
48 #include "bmsparsevec_serial.h"
49 
50 using namespace std;
51 
54 
55 template<class SV>
56 void print_svector(const SV& sv, bool show_nulls = false)
57 {
58  std::cout << sv.size() << ": ";
59  for (unsigned i = 0; i < sv.size(); ++i)
60  {
61  if (show_nulls)
62  {
63  if (sv.is_null(i))
64  std::cout << "NULL, ";
65  else
66  {
67  typename SV::value_type v = sv.get(i);
68  std::cout << v << ", ";
69  }
70  }
71  else
72  {
73  typename SV::value_type v = sv.get(i);
74  std::cout << v << ", ";
75  }
76  }
77  std::cout << std::endl;
78 }
79 
80 int main(void)
81 {
82  // temp buffer to avoid unnecessary re-allocations
84 
85  try
86  {
87  sparse_vector_u32 sv1(bm::use_null); // use null is needed to build rsc vector
89 
90  // fill in sparse vector leaving some unassigned gaps
91  for (unsigned i = 0; i < 15; i+=3)
92  {
93  sv1[i] = i;
94  }
95 
96  print_svector(sv1); // print sparse vector disregard NULL values (printed as 0)
97  print_svector(sv1, true); // print sparse vector show NULLs (unassigned)
98 
99  csv2.load_from(sv1); // load rank-select-compacted (rsc) sparse vector
100 
101  // print results - it should look the same
102  print_svector(csv2);
103  print_svector(csv2, true);
104 
105  // serialize rsc vector
106 
107  // optimize memory allocation of sparse vector
108  csv2.optimize(tb);
109 
111  bm::sparse_vector_serialize(csv2, sv_lay, tb);
112 
113  // memory copy just simulates network or DB transaction
114  const unsigned char* buf = sv_lay.buf();
115  size_t buf_size = sv_lay.size();
116 
117  vector<unsigned char> tmp_buf(buf_size);
118  ::memcpy(&tmp_buf[0], buf, buf_size);
119 
120 
121  rsc_sparse_vector_u32 csv3; // target vector
122 
123  int res = bm::sparse_vector_deserialize(csv3, &tmp_buf[0], tb);
124  if (res != 0)
125  {
126  std::cerr << "De-Serialization error!" << std::endl;
127  return 1;
128  }
129  if (!csv3.equal(csv2) )
130  {
131  cerr << "Error! Please report a bug to BitMagic project support." << endl;
132  return 1;
133  }
134 
135  // unload rsc to sv, this makes a round-trip to an editable form
137  csv3.load_to(sv3);
138 
139  if (!sv3.equal(sv1) )
140  {
141  std::cerr << "Error! Please report a bug to BitMagic project support." << std::endl;
142  return 1;
143  }
144 
145  print_svector(sv1, true); // print sparse vector again
146 
147  }
148  catch(std::exception& ex)
149  {
150  std::cerr << ex.what() << std::endl;
151  return 1;
152  }
153 
154 
155 
156  return 0;
157 }
158 
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
size_t size() const
return current serialized size
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, statistics *stat=0)
run memory optimization for all vector plains
Rank-Select compressed sparse vector.
void load_to(sparse_vector_type &sv) const
Exort compressed vector to a sparse vector (with NULLs)
void sparse_vector_serialize(const SV &sv, sparse_vector_serial_layout< SV > &sv_layout, bm::word_t *temp_block=0)
Serialize sparse vector into a memory buffer(s) structure.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
void load_from(const sparse_vector_type &sv_src)
Load compressed vector from a sparse vector (with NULLs)
support "non-assigned" or "NULL" logic
Definition: bmconst.h:216
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample01.cpp:53
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: rscsample01.cpp:52
Serialization for sparse_vector<>
bool equal(const rsc_sparse_vector< Val, SV > &csv) const
check if another vector has the same content
int main(void)
Definition: rscsample01.cpp:80
void print_svector(const SV &sv, bool show_nulls=false)
Definition: rscsample01.cpp:56
bool equal(const sparse_vector< Val, BV > &sv, bm::null_support null_able=bm::use_null) const
check if another sparse vector has the same content and size
Definition: bmsparsevec.h:1900
layout class for serialization buffer structure
const unsigned char * buf() const
Return serialization buffer pointer.
int sparse_vector_deserialize(SV &sv, const unsigned char *buf, bm::word_t *temp_block=0)
Deserialize sparse vector.
Compressed sparse container rsc_sparse_vector<> for integer types.