BitMagic-C++
rscsample01.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 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#include "bmundef.h" /* clear the pre-proc defines from BM */
50
51using namespace std;
52
55
56/// Prints the vector using is_null() and get()
57/// Please note, that random access is not the fastest, const_iterator
58/// is preferred in many cases (better performance)
59///
60template<class SV>
61void print_svector(const SV& sv, bool show_nulls = false)
62{
63 std::cout << sv.size() << ": ";
64 for (unsigned i = 0; i < sv.size(); ++i)
65 {
66 if (show_nulls)
67 {
68 if (sv.is_null(i))
69 std::cout << "NULL, ";
70 else
71 {
72 typename SV::value_type v = sv.get(i);
73 std::cout << v << ", ";
74 }
75 }
76 else
77 {
78 typename SV::value_type v = sv.get(i);
79 std::cout << v << ", ";
80 }
81 }
82 std::cout << std::endl;
83}
84
85/// Prints succinct vector using try_get() random access method
86///
87///
88template<class SV>
89void print_svector2(const SV& sv, bool show_nulls = false)
90{
91 using value_type = typename SV::value_type;
92 value_type v;
93 std::cout << sv.size() << ": ";
94 for (unsigned i = 0; i < sv.size(); ++i)
95 {
96 if (show_nulls)
97 {
98 if (!sv.try_get(i, v))
99 std::cout << "NULL, ";
100 else
101 std::cout << v << ", ";
102 }
103 else
104 {
105 v = sv.get(i);
106 std::cout << v << ", ";
107 }
108 }
109 std::cout << std::endl;
110}
111
112int main(void)
113{
114 // temp buffer to avoid unnecessary re-allocations
116
117 try
118 {
119 sparse_vector_u32 sv1(bm::use_null); // use null is needed to build rsc vector
121
122 // fill in sparse vector leaving some unassigned gaps
123 for (unsigned i = 0; i < 15; i+=3)
124 {
125 sv1[i] = i;
126 }
127
128 print_svector(sv1); // print sparse vector disregard NULL values (printed as 0)
129 print_svector2(sv1, true); // print sparse vector show NULLs (unassigned)
130
131 csv2.load_from(sv1); // load rank-select-compacted (rsc) sparse vector
132
133 // print results - it should look the same
134 print_svector(csv2);
135 print_svector2(csv2, true);
136
137 // serialize rsc vector
138
139 // optimize memory allocation of sparse vector
140 csv2.optimize(tb);
141
143 bm::sparse_vector_serialize(csv2, sv_lay, tb);
144
145 // memory copy just simulates network or DB transaction
146 const unsigned char* buf = sv_lay.buf();
147 size_t buf_size = sv_lay.size();
148
149 vector<unsigned char> tmp_buf(buf_size);
150 ::memcpy(&tmp_buf[0], buf, buf_size);
151
152
153 rsc_sparse_vector_u32 csv3; // target vector
154
155 int res = bm::sparse_vector_deserialize(csv3, &tmp_buf[0], tb);
156 if (res != 0)
157 {
158 std::cerr << "De-Serialization error!" << std::endl;
159 return 1;
160 }
161 if (!csv3.equal(csv2) )
162 {
163 cerr << "Error! Please report a bug to BitMagic project support." << endl;
164 return 1;
165 }
166
167 // unload rsc to sv, this makes a round-trip to an editable form
169 csv3.load_to(sv3);
170
171 if (!sv3.equal(sv1) )
172 {
173 std::cerr << "Error! Please report a bug to BitMagic project support." << std::endl;
174 return 1;
175 }
176
177 print_svector(sv1, true); // print sparse vector again
178
179 }
180 catch(std::exception& ex)
181 {
182 std::cerr << ex.what() << std::endl;
183 return 1;
184 }
185
186
187
188 return 0;
189}
190
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.
Compressed sparse container rsc_sparse_vector<> for integer types.
Serialization for sparse_vector<>
pre-processor un-defines to avoid global space pollution (internal)
void load_from(const sparse_vector_type &sv_src)
Load compressed vector from a sparse vector (with NULLs)
void load_to(sparse_vector_type &sv) const
Exort compressed vector to a sparse vector (with NULLs)
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 slices
bool equal(const rsc_sparse_vector< Val, SV > &csv) const BMNOEXCEPT
check if another vector has the same content
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
bool equal(const sparse_vector< Val, BV > &sv, bm::null_support null_able=bm::use_null) const BMNOEXCEPT
check if another sparse vector has the same content and size
Definition: bmsparsevec.h:2246
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:229
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.
int sparse_vector_deserialize(SV &sv, const unsigned char *buf, bm::word_t *temp_block=0)
Deserialize sparse vector.
void print_svector2(const SV &sv, bool show_nulls=false)
Prints succinct vector using try_get() random access method.
Definition: rscsample01.cpp:89
void print_svector(const SV &sv, bool show_nulls=false)
Prints the vector using is_null() and get() Please note, that random access is not the fastest,...
Definition: rscsample01.cpp:61
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: rscsample01.cpp:53
int main(void)
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample01.cpp:54
layout class for serialization buffer structure
size_t size() const BMNOEXCEPT
return current serialized size
const unsigned char * buf() const BMNOEXCEPT
Return serialization buffer pointer.