BitMagic-C++
svsample08.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2019 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 svsample08.cpp
20 Example of how to serialize bm::sparse_vector<> container
21
22 \sa bm::sparse_vector
23 \sa bm::sparse_vector_deserializer
24 \sa bm::sparse_vector_serial_layout
25 \sa bm::sparse_vector_serializer
26
27*/
28
29/*! \file svsample08.cpp
30 \brief Example: sparse_vector<> selective de-serialization (gather)
31 and range deserialization
32
33 @sa strsvsample05.cpp
34*/
35
36#include <iostream>
37#include <vector>
38
39#include "bm.h"
40#include "bmsparsevec.h"
41#include "bmsparsevec_serial.h"
42#include "bmundef.h" /* clear the pre-proc defines from BM */
43
44using namespace std;
45
46/// Print sparse vector content
47template<typename SV> void PrintSV(const SV& sv)
48{
49 cout << "size() = " << sv.size() << " : ";
50 auto it = sv.begin(); auto it_end = sv.end();
51 for (; it != it_end; ++it)
52 {
53 if (it.is_null())
54 cout << "NULL";
55 else
56 cout << *it;
57 cout << ", ";
58 }
59 cout << endl;
60}
61
63
64int main(void)
65{
66 try
67 {
70
71 // add sample data using back insert iterator
72 //
73 {
74 auto bit = sv1.get_back_inserter();
75 bit = 10;
76 bit = 11;
77 bit.add_null();
78 bit = 13;
79 bit = 14;
80 bit.add_null(2);
81 bit = 256;
82 bit.flush();
83 }
84 PrintSV(sv1); // size() = 8 : 10, 11, NULL, 13, 14, NULL, NULL, 256,
85
86 // serialize sparse vector
88 {
90 // optimize memory allocation of sparse vector
91 sv1.optimize(tb);
92
93
94 // construct a serializer utility class, setup serialization parameters
95 //
96 // please note, use of "set_bookmarks()" to enable fast range
97 // deserialization. Bookmarks somewhat increase the BLOB size but allow
98 // more effeiciently skip parts which we would not need (paging) and
99 // avoid decompression of blocks we would never need
100 //
101 // This example sets "64" as a bookmarks parameter, but you have to
102 // experiment with what works for you, between 4 and 512
103 //
104 // Each block corresponds to 64K vector element
105 // making bookmarks after each block does not make much sense
106 // because decode is reasonably fast and some residual throw away
107 // is usually ok.
108 //
109
111 sv_serializer.set_bookmarks(true, 64);
112
113 // serialization, creates a compressed BLOB
114 sv_serializer.serialize(sv1, sv_lay);
115 }
116
117 // get serialization pointer
118 const unsigned char* buf = sv_lay.buf();
119
120 // instantiate the deserializer object to do all multiple
121 // deserialization operations (faster)
122 //
124
125 // Case 1:
126 // Here we define de-serialization indexes as a bit-vector (mask)
127 // so deserializer gathers only elements selected by the bit-vector
128 // all vector elements not set in the gather vector will be 0 (or NULL)
129 //
130 {
132 bv_mask.set(0);
133 bv_mask.set(1);
134 sv_deserial.deserialize(sv2, buf, bv_mask);
135 }
136 PrintSV(sv2); // size() = 8 : 10, 11, NULL, 0, 0, NULL, NULL, 0,
137
138 // Case 2:
139 // Here we define de-serialization indexes as a closed range [1..4]
140 // It is an equivalent of setting selection bits in the mask vector
141 // but defines the intent more clearly and works faster
142 {
143 sv_deserial.deserialize_range(sv2, buf, 1, 4);
144 }
145 PrintSV(sv2); // size() = 8 : 0, 11, NULL, 13, 14, NULL, NULL, 0,
146
147 }
148 catch(std::exception& ex)
149 {
150 std::cerr << ex.what() << std::endl;
151 return 1;
152 }
153
154
155
156 return 0;
157}
158
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)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:4153
sparse vector de-serializer
void deserialize(SV &sv, const unsigned char *buf, bool clear_sv=true)
void deserialize_range(SV &sv, const unsigned char *buf, size_type from, size_type to, bool clear_sv=true)
void set_bookmarks(bool enable, unsigned bm_interval=256) BMNOEXCEPT
Add skip-markers for faster range deserialization.
void serialize(const SV &sv, sparse_vector_serial_layout< SV > &sv_layout)
Serialize sparse vector into a memory buffer(s) structure.
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
back_insert_iterator get_back_inserter()
Provide back insert iterator Back insert iterator implements buffered insertion, which is faster,...
Definition: bmsparsevec.h:572
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
layout class for serialization buffer structure
const unsigned char * buf() const BMNOEXCEPT
Return serialization buffer pointer.
bm::sparse_vector< unsigned, bm::bvector<> > svector_u32
Definition: svsample08.cpp:62
int main(void)
Definition: svsample08.cpp:64
void PrintSV(const SV &sv)
Print sparse vector content.
Definition: svsample08.cpp:47