BitMagic-C++: svsample02.cpp Source File

BitMagic-C++

svsample02.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 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
28 */
29
30 /*! \file svsample02.cpp
31 \brief Example: sparse_vector<> serialization
32 */
33
34 #include <iostream>
35 #include <vector>
36
37 #include "bm.h"
38 #include "bmsparsevec.h"
39 #include "bmsparsevec_serial.h"
40
41 using namespace std;
42
43 typedef bm::sparse_vector<unsigned, bm::bvector<> > svector;
44
45 int main(void)
46 {
47 try
48 {
49 svector sv1;
50 svector sv2;
51
52 // temp buffer to avoid unnecessary re-allocations
53 BM_DECLARE_TEMP_BLOCK(tb)
54
55
56 for (unsigned i = 0; i < 128000; ++i)
57 {
58 sv1.push_back(8);
59 }
60
61 // optimize memory allocation of sparse vector
62 sv1.optimize(tb);
63
64 bm::sparse_vector_serial_layout<svector> sv_lay;
65 bm::sparse_vector_serialize(sv1, sv_lay, tb);
66
67 // copy serialization buffer to some other location
68 // to simulate data-base storage or network transaction
69 //
70 const unsigned char* buf = sv_lay.buf();
71 size_t buf_size = sv_lay.size();
72
73 vector<unsigned char> tmp_buf(buf_size);
74 ::memcpy(&tmp_buf[0], buf, buf_size);
75
76 int res = bm::sparse_vector_deserialize(sv2, &tmp_buf[0], tb);
77 if (res != 0)
78 {
79 cerr << "De-Serialization error!" << endl;
80 return 1;
81 }
82
83
84 if (!sv1.equal(sv2) )
85 {
86 cerr << "Error! Please report a bug to BitMagic project support." << endl;
87 return 1;
88 }
89
90 cout << sv2.size() << endl;
91 }
92 catch(std::exception& ex)
93 {
94 std::cerr << ex.what() << std::endl;
95 return 1;
96 }
97
98
99
100 return 0;
101 }
102
bm::sparse_vector::optimize
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:1728
bm::sparse_vector
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
BM_DECLARE_TEMP_BLOCK
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
bm::sparse_vector_serial_layout::buf
const unsigned char * buf() const
Return serialization buffer pointer.
Definition: bmsparsevec_serial.h:106
bmsparsevec.h
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
bmsparsevec_serial.h
Serialization for sparse_vector<>
bm::sparse_vector::equal
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:1890
bm::sparse_vector_deserialize
int sparse_vector_deserialize(SV &sv, const unsigned char *buf, bm::word_t *temp_block=0)
Deserialize sparse vector.
Definition: bmsparsevec_serial.h:380
bm::sparse_vector_serialize
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.
Definition: bmsparsevec_serial.h:356
bm::sparse_vector_serial_layout
layout class for serialization buffer structure
Definition: bmsparsevec_serial.h:57
bm::sparse_vector::size
size_type size() const BMNOEXCEPT
return size of the vector
Definition: bmsparsevec.h:644
bm::sparse_vector::push_back
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1498
bm::sparse_vector_serial_layout::size
size_t size() const
return current serialized size
Definition: bmsparsevec_serial.h:84
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
svector
bm::sparse_vector< unsigned, bm::bvector<> > svector
Definition: svsample02.cpp:43
main
int main(void)
Definition: svsample02.cpp:45