BitMagic-C++: svsample08.cpp Source File

BitMagic-C++

svsample08.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2019 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 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
43 using namespace std;
44
45 /// Print sparse vector content
46 template<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
61 typedef bm::sparse_vector<unsigned, bm::bvector<> > svector_u32;
62
63 int main(void)
64 {
65 try
66 {
67 svector_u32 sv1(bm::use_null);
68 svector_u32 sv2(bm::use_null);
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
86 bm::sparse_vector_serial_layout<svector_u32> sv_lay;
87 {
88 BM_DECLARE_TEMP_BLOCK(tb)
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
109 bm::sparse_vector_serializer<svector_u32> sv_serializer;
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 //
122 bm::sparse_vector_deserializer<svector_u32> sv_deserial;
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 {
130 svector_u32::bvector_type bv_mask;
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
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_deserializer
sparse vector de-serializer
Definition: bmsparsevec_serial.h:223
bm::sparse_vector
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
PrintSV
void PrintSV(const SV &sv)
Print sparse vector content.
Definition: svsample08.cpp:46
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_serial_layout
layout class for serialization buffer structure
Definition: bmsparsevec_serial.h:57
bm::bvector<>
bm::use_null
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:215
bm::bvector::set
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
bm::sparse_vector_serializer::serialize
void serialize(const SV &sv, sparse_vector_serial_layout< SV > &sv_layout)
Serialize sparse vector into a memory buffer(s) structure.
Definition: bmsparsevec_serial.h:628
bm::sparse_vector_deserializer::deserialize
void deserialize(SV &sv, const unsigned char *buf)
Definition: bmsparsevec_serial.h:243
main
int main(void)
Definition: svsample08.cpp:63
bm::sparse_vector_serializer::set_bookmarks
void set_bookmarks(bool enable, unsigned bm_interval=256)
Add skip-markers for faster range deserialization.
Definition: bmsparsevec_serial.h:183
bm::sparse_vector::get_back_inserter
back_insert_iterator get_back_inserter()
Provide back insert iterator Back insert iterator implements buffered insertion, which is faster,...
Definition: bmsparsevec.h:506
svector_u32
bm::sparse_vector< unsigned, bm::bvector<> > svector_u32
Definition: svsample08.cpp:61
bm::sparse_vector_deserializer::deserialize_range
void deserialize_range(SV &sv, const unsigned char *buf, size_type from, size_type to)
Definition: bmsparsevec_serial.h:788
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::sparse_vector_serializer
Definition: bmsparsevec_serial.h:160