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 for (unsigned i = 0; i < 128000; i+=2)
112 {
113 sv1.set(i, 8);
114 }
115 sv1.optimize();
116 sv2 = sv1; // copy sv1
117
118 sv_serializer_type sv_ser;
119 sv_deserializer_type sv_dser;
121
122 // the data pattern will allow XOR compression
123 // lets try to enable it!
124
125 sv_ser.enable_xor_compression();
126 assert(sv_ser.is_xor_ref());
127 sv_ser.serialize(sv1, sv_lay0);
128
129 // Get BLOB pointer and size
130 const unsigned char* buf = sv_lay0.data();
131 size_t sz = sv_lay0.size();
132 cout << "XOR compression enabled size=" << sz << endl;
133
134 // deserialize from the memory pointer
135 //
136 {
138 sv_dser.deserialize(sv3, buf);
139 assert(sv3.equal(sv1));
140 }
141
142
143 // disbale XOR compression
144 // please note that we re-use serializer and deserializer instances
145 // to save construction costs (memory allocations, etc)
146 //
147
149 assert(!sv_ser.is_xor_ref());
150
151 sv_ser.serialize(sv2, sv_lay0);
152
153 buf = sv_lay0.data();
154 sz = sv_lay0.size();
155 cout << "XOR compression disabled size=" << sz << endl;
156
157 // deserialize from the memory pointer
158 //
159 {
161 sv_dser.deserialize(sv3, buf);
162 assert(sv3.equal(sv1));
163 }
164
165}
166
167int main(void)
168{
169 try
170 {
171 cout << "Demo 1" << endl;
172 SDemo1();
173
174 cout << "Demo 2" << endl;
175 SDemo2();
176 }
177 catch(std::exception& ex)
178 {
179 std::cerr << ex.what() << std::endl;
180 return 1;
181 }
182
183 return 0;
184}
185
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
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 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
Definition: bmsparsevec.h:2246
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1838
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1804
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
@ 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.
layout class for serialization buffer structure
size_t size() const BMNOEXCEPT
return current serialized size
const unsigned char * data() const BMNOEXCEPT
Return serialization buffer pointer.
const unsigned char * buf() const BMNOEXCEPT
Return serialization buffer pointer.
static void SDemo2()
Demo 2.
Definition: svsample02.cpp:106
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: svsample02.cpp:49
int main(void)
Definition: svsample02.cpp:167
bm::sparse_vector_serializer< sparse_vector_u32 > sv_serializer_type
Definition: svsample02.cpp:52
bm::sparse_vector< int, bm::bvector<> > sparse_vector_i32
Definition: svsample02.cpp:50
bm::sparse_vector_deserializer< sparse_vector_u32 > sv_deserializer_type
Definition: svsample02.cpp:53
static void SDemo1()
Demo 1 Simple one function call serialization.
Definition: svsample02.cpp:59