BitMagic-C++
svsample02.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 svsample02.cpp
20 Example of how to serialize bm::sparse_vector<> template class
21
22 \sa bm::sparse_vector
23 \sa bm::sparse_vector<>::push_back
24 \sa bm::sparse_vector<>::equal
25 \sa bm::sparse_vector_serialize
26 \sa bm::sparse_vector_deserialize
27 \sa bm::sparse_vector_serializer
28 \sa bm::sparse_vector_deserializer
29
30 \sa rscsample05.cpp
31*/
32
33/*! \file svsample02.cpp
34 \brief Example: sparse_vector<> serialization
35
36*/
37
38#include <iostream>
39#include <vector>
40#include <assert.h>
41
42#include "bm.h"
43#include "bmsparsevec.h"
44#include "bmsparsevec_serial.h"
45#include "bmundef.h" /* clear the pre-proc defines from BM */
46
47using namespace std;
48
51
54
55/// Demo 1
56/// Simple one function call serialization
57///
58static
59void SDemo1()
60{
63
64 for (unsigned i = 0; i < 128000; ++i)
65 {
66 sv1.push_back(8);
67 }
68
69 // optimize memory allocation of sparse vector
70 sv1.optimize();
71
73 bm::sparse_vector_serialize(sv1, sv_lay);
74
75 // copy serialization buffer to some other location
76 // to simulate data-base storage or network transaction
77 //
78 const unsigned char* buf = sv_lay.buf();
79 size_t buf_size = sv_lay.size();
80 cout << buf_size << endl;
81
82 vector<unsigned char> tmp_buf(buf_size);
83 ::memcpy(&tmp_buf[0], buf, buf_size);
84
85 int res = bm::sparse_vector_deserialize(sv2, &tmp_buf[0]);
86 if (res != 0)
87 {
88 cerr << "De-Serialization error!" << endl;
89 return;
90 }
91 if (!sv1.equal(sv2) )
92 {
93 cerr << "Error! Please report a bug to BitMagic project support." << endl;
94 return;
95 }
96}
97
98/// Demo 2
99/// - Reuseable serializer/deserilaizer classes
100/// - Shows serializarion with XOR compression enabled
101///
102/// Reusable serializer is better (works faster) when we need to serialize/deserialize a bunch of vectors
103/// use of serializer also offers a better control on serialization options (like XOR compression)
104///
105static
106void SDemo2()
107{
108 sparse_vector_u32 sv1(bm::use_null); // NULL-able vector
110
111 // not the fastest way to init the vector but it will do for an example
112 //
113 for (unsigned i = 0; i < 128000; i+=2)
114 sv1.set(i, 8);
115 for (unsigned i = 128000; i < 128000*4; i+=256)
116 sv1.set(i, 7);
117
118
120 sv1.optimize(tb);
121 sv2 = sv1; // copy sv1
122
123 sv_serializer_type sv_ser;
124 sv_deserializer_type sv_dser;
126
127 // the data pattern will allow XOR compression
128 // lets try to enable it!
129
130 sv_ser.enable_xor_compression();
131 assert(sv_ser.is_xor_ref());
132 sv_ser.serialize(sv1, sv_lay0);
133
134 // Get BLOB pointer and size
135 const unsigned char* buf = sv_lay0.data();
136 size_t sz = sv_lay0.size();
137 cout << "XOR compression enabled size=" << sz << endl;
138
139 // deserialize from the memory pointer
140 //
141 {
143 sv_dser.deserialize(sv3, buf);
144 assert(sv3.equal(sv1));
145
146 // here we do read-only deserialization
147 // RO vector is immutable (which is ok in many cases)
148 //
149 // Immutable vectors drops the over-allocation overhead
150 // necessary for fast modifications which makes it more succinct.
151 //
152 // Another advantage or RO vectors is that it may be a bit faster
153 // due to reduced memory fragmentation.
154 //
155 // Please note that RO deserialization will be a bit slower
156 //
157
159 {
160 sv_deserializer_type sv_dser_ro;
162 sv_dser_ro.deserialize(sv4, buf);
163 }
164
165 assert(sv4.is_ro());
166 bool eq = sv3.equal(sv4);
167 assert(eq); (void)eq;
168
169 // compute memory profile for RO and RW vectors
170 // to illustrate the case when RO takes less RAM
171 //
172 sparse_vector_u32::statistics st3, st4;
173 sv3.calc_stat(&st3);
174 sv4.calc_stat(&st4);
175
176 cout << "RW vector mem=" << st3.memory_used << endl;
177 cout << "RO vector mem=" << st4.memory_used << endl;
178 }
179
180
181 // disbale XOR compression
182 // please note that we re-use serializer and deserializer instances
183 // to save construction costs (memory allocations, etc)
184 //
185
187 assert(!sv_ser.is_xor_ref());
188
189 sv_ser.serialize(sv2, sv_lay0);
190
191 buf = sv_lay0.data();
192 sz = sv_lay0.size();
193 cout << "XOR compression disabled size=" << sz << endl;
194
195 // deserialize from the memory pointer
196 //
197 {
199 sv_dser.deserialize(sv3, buf);
200 assert(sv3.equal(sv1));
201 }
202
203}
204
205int main(void)
206{
207 try
208 {
209 cout << "Demo 1" << endl;
210 SDemo1();
211
212 cout << "Demo 2" << endl;
213 SDemo2();
214 }
215 catch(std::exception& ex)
216 {
217 std::cerr << ex.what() << std::endl;
218 return 1;
219 }
220
221 return 0;
222}
223
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.
Serialization for sparse_vector<>.
pre-processor un-defines to avoid global space pollution (internal)
sparse vector de-serializer
void deserialize(SV &sv, const unsigned char *buf, bool clear_sv=true)
void set_finalization(bm::finalization is_final)
Set deserialization finalization to force deserialized vectors into READONLY (or READWRITE) mode.
void serialize(const SV &sv, sparse_vector_serial_layout< SV > &sv_layout)
Serialize sparse vector into a memory buffer(s) structure.
bool is_xor_ref() const BMNOEXCEPT
Returns the XOR reference compression status (enabled/disabled).
void disable_xor_compression() BMNOEXCEPT
Disable XOR compression on serialization.
void enable_xor_compression() BMNOEXCEPT
Enable XOR compression on vector serialization.
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
void push_back(value_type v)
push value back into vector
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
void calc_stat(struct sparse_vector< Val, BV >::statistics *st) const BMNOEXCEPT
Calculates memory statistics.
bool is_ro() const BMNOEXCEPT
Returns true if vector is read-only.
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
@ use_null
support "non-assigned" or "NULL" logic
Definition bmconst.h:230
@ READONLY
immutable (read-only object)
Definition bmconst.h:158
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.
layout class for serialization buffer structure
const unsigned char * buf() const BMNOEXCEPT
Return serialization buffer pointer.
size_t size() const BMNOEXCEPT
return current serialized size
const unsigned char * data() const BMNOEXCEPT
Return serialization buffer pointer.
bm::sparse_vector< int, bm::bvector<> > sparse_vector_i32
static void SDemo2()
Demo 2.
int main(void)
bm::sparse_vector_serializer< sparse_vector_u32 > sv_serializer_type
bm::sparse_vector_deserializer< sparse_vector_u32 > sv_deserializer_type
static void SDemo1()
Demo 1 Simple one function call serialization.
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition xsample02.cpp:68