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
43using namespace std;
44
45/// Print sparse vector content
46template<typename SV> void PrintSV(const SV& sv)
47{
48 cout << "size() = " << sv.size() << " : ";
49 auto it = sv.begin(); auto it_end = sv.end();
50 for (; it != it_end; ++it)
51 {
52 if (it.is_null())
53 cout << "NULL";
54 else
55 cout << *it;
56 cout << ", ";
57 }
58 cout << endl;
59}
60
62
63int main(void)
64{
65 try
66 {
69
70 // add sample data using back insert iterator
71 //
72 {
73 auto bit = sv1.get_back_inserter();
74 bit = 10;
75 bit = 11;
76 bit.add_null();
77 bit = 13;
78 bit = 14;
79 bit.add_null(2);
80 bit = 256;
81 bit.flush();
82 }
83 PrintSV(sv1); // size() = 8 : 10, 11, NULL, 13, 14, NULL, NULL, 256,
84
85 // serialize sparse vector
87 {
89 // optimize memory allocation of sparse vector
90 sv1.optimize(tb);
91
92
93 // construct a serializer utility class, setup serialization parameters
94 //
95 // please note, use of "set_bookmarks()" to enable fast range
96 // deserialization. Bookmarks somewhat increase the BLOB size but allow
97 // more effeiciently skip parts which we would not need (paging) and
98 // avoid decompression of blocks we would never need
99 //
100 // This example sets "64" as a bookmarks parameter, but you have to
101 // experiment with what works for you, between 4 and 512
102 //
103 // Each block corresponds to 64K vector element
104 // making bookmarks after each block does not make much sense
105 // because decode is reasonably fast and some residual throw away
106 // is usually ok.
107 //
108
110 sv_serializer.set_bookmarks(true, 64);
111
112 // serialization, creates a compressed BLOB
113 sv_serializer.serialize(sv1, sv_lay);
114 }
115
116 // get serialization pointer
117 const unsigned char* buf = sv_lay.buf();
118
119 // instantiate the deserializer object to do all multiple
120 // deserialization operations (faster)
121 //
123
124 // Case 1:
125 // Here we define de-serialization indexes as a bit-vector (mask)
126 // so deserializer gathers only elements selected by the bit-vector
127 // all vector elements not set in the gather vector will be 0 (or NULL)
128 //
129 {
131 bv_mask.set(0);
132 bv_mask.set(1);
133 sv_deserial.deserialize(sv2, buf, bv_mask);
134 }
135 PrintSV(sv2); // size() = 8 : 10, 11, NULL, 0, 0, NULL, NULL, 0,
136
137 // Case 2:
138 // Here we define de-serialization indexes as a closed range [1..4]
139 // It is an equivalent of setting selection bits in the mask vector
140 // but defines the intent more clearly and works faster
141 {
142 sv_deserial.deserialize_range(sv2, buf, 1, 4);
143 }
144 PrintSV(sv2); // size() = 8 : 0, 11, NULL, 13, 14, NULL, NULL, 0,
145
146 }
147 catch(std::exception& ex)
148 {
149 std::cerr << ex.what() << std::endl;
150 return 1;
151 }
152
153
154
155 return 0;
156}
157
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<>
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
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:3581
sparse vector de-serializer
void deserialize_range(SV &sv, const unsigned char *buf, size_type from, size_type to)
void deserialize(SV &sv, const unsigned char *buf)
void set_bookmarks(bool enable, unsigned bm_interval=256)
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.
sparse vector with runtime compression using bit transposition method
Definition bmsparsevec.h:82
back_insert_iterator get_back_inserter()
Provide back insert iterator Back insert iterator implements buffered insertion, which is faster,...
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
@ use_null
support "non-assigned" or "NULL" logic
Definition bmconst.h:215
layout class for serialization buffer structure
const unsigned char * buf() const
Return serialization buffer pointer.
bm::sparse_vector< unsigned, bm::bvector<> > svector_u32
int main(void)
void PrintSV(const SV &sv)
Print sparse vector content.