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
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
41using namespace std;
42
44
45int main(void)
46{
47 try
48 {
49 svector sv1;
50 svector sv2;
51
52 // temp buffer to avoid unnecessary re-allocations
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
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
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<>
sparse vector with runtime compression using bit transposition method
Definition bmsparsevec.h:82
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
size_type size() const BMNOEXCEPT
return size of the vector
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
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
return current serialized size
const unsigned char * buf() const
Return serialization buffer pointer.
int main(void)
bm::sparse_vector< unsigned, bm::bvector<> > svector