BitMagic-C++: rscsample02.cpp Source File

BitMagic-C++

rscsample02.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 rscsample02.cpp
20 Example of how to partially de-serialize bm::rsc_sparse_vector<> container
21
22 \sa bm::rsc_sparse_vector
23 \sa bm::rsc_sparse_vector<>::back_insert_iterator
24
25 */
26
27 /*! \file rscsample02.cpp
28 \brief Example: rsc_sparse_vector<> selective and range de-serialization
29
30 @sa strsvsample05.cpp
31 */
32
33 #include <iostream>
34 #include <vector>
35
36 #include "bm.h"
37 #include "bmsparsevec.h"
38 #include "bmsparsevec_compr.h"
39 #include "bmsparsevec_serial.h"
40
41 using namespace std;
42
43 /// Print sparse vector content
44 template<typename SV> void PrintSV(const SV& sv)
45 {
46 auto sz = sv.size();
47 cout << "size() = " << sz << " : ";
48
49 for (typename SV::size_type i = 0; i < sz; ++i)
50 {
51 if (sv.is_null(i))
52 cout << "NULL";
53 else
54 cout << sv.get(i);
55 cout << ", ";
56 }
57 cout << endl;
58 }
59
60 typedef bm::sparse_vector<unsigned, bm::bvector<> > sparse_vector_u32;
61 typedef bm::rsc_sparse_vector<unsigned, sparse_vector_u32 > rsc_sparse_vector_u32;
62
63
64 int main(void)
65 {
66 try
67 {
68 rsc_sparse_vector_u32 csv1;
69 rsc_sparse_vector_u32 csv2;
70
71 // add sample data using back insert iterator
72 //
73 {
74 auto bit = csv1.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 csv1.sync();
85 }
86 PrintSV(csv1); // size() = 8 : 10, 11, NULL, 13, 14, NULL, NULL, 256,
87
88 // serialize sparse vector
89 bm::sparse_vector_serial_layout<rsc_sparse_vector_u32> sv_lay;
90 {
91 BM_DECLARE_TEMP_BLOCK(tb)
92 // optimize memory allocation of sparse vector
93 csv1.optimize(tb);
94
95 // configure serializer, set bookmarking option for faster
96 // range deserialization
97 //
98 bm::sparse_vector_serializer<rsc_sparse_vector_u32> sv_serializer;
99 sv_serializer.set_bookmarks(true, 64);
100
101 sv_serializer.serialize(csv1, sv_lay); // serialization
102 }
103
104 // get serialization pointer
105 const unsigned char* buf = sv_lay.buf();
106
107 // instantiate the deserializer object to do all multiple
108 // deserialization operations (faster)
109 //
110 bm::sparse_vector_deserializer<rsc_sparse_vector_u32> sv_deserial;
111
112 // Case 1:
113 // Here we define de-serialization indexes as a bit-vector (mask)
114 // so deserializer gathers only elements selected by the bit-vector
115 // all vector elements not set in the gather vector will be 0 (or NULL)
116 //
117 {
118 rsc_sparse_vector_u32::bvector_type bv_mask;
119 bv_mask.set(0);
120 bv_mask.set(1);
121 sv_deserial.deserialize(csv2, buf, bv_mask);
122 }
123 PrintSV(csv2); // size() = 8 : 10, 11, NULL, 0, 0, NULL, NULL, 0,
124
125 // Case 2:
126 // Here we define de-serialization indexes as a closed range [1..4]
127 // It is an equivalent of setting selection bits in the mask vector
128 // but defines the intent more clearly and works faster
129 {
130 sv_deserial.deserialize_range(csv2, buf, 1, 4);
131 }
132 PrintSV(csv2); // size() = 8 : 0, 11, NULL, 13, 14, NULL, NULL, 0,
133
134 }
135 catch(std::exception& ex)
136 {
137 std::cerr << ex.what() << std::endl;
138 return 1;
139 }
140
141
142
143 return 0;
144 }
145
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
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.
bm::rsc_sparse_vector::back_insert_iterator::add_null
void add_null() BMNOEXCEPT
add NULL (no-value) to the container
Definition: bmsparsevec_compr.h:1370
bmsparsevec_serial.h
Serialization for sparse_vector<>
bmsparsevec_compr.h
Compressed sparse container rsc_sparse_vector<> for integer types.
bm::sparse_vector_serial_layout
layout class for serialization buffer structure
Definition: bmsparsevec_serial.h:57
sparse_vector_u32
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: rscsample02.cpp:60
main
int main(void)
Definition: rscsample02.cpp:64
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::rsc_sparse_vector::get_back_inserter
back_insert_iterator get_back_inserter()
Definition: bmsparsevec_compr.h:547
bm::sparse_vector_deserializer::deserialize
void deserialize(SV &sv, const unsigned char *buf)
Definition: bmsparsevec_serial.h:243
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::rsc_sparse_vector::bvector_type
SV::bvector_type bvector_type
Definition: bmsparsevec_compr.h:72
bm::rsc_sparse_vector
Rank-Select compressed sparse vector.
Definition: bmsparsevec_compr.h:58
bm::rsc_sparse_vector::optimize
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, statistics *stat=0)
run memory optimization for all vector plains
Definition: bmsparsevec_compr.h:1081
rsc_sparse_vector_u32
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample02.cpp:61
bm::rsc_sparse_vector::sync
void sync(bool force)
Re-calculate prefix sum table used for rank search.
Definition: bmsparsevec_compr.h:963
PrintSV
void PrintSV(const SV &sv)
Print sparse vector content.
Definition: rscsample02.cpp:44
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