BitMagic-C++
rscsample02.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 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#include "bmundef.h" /* clear the pre-proc defines from BM */
41
42using namespace std;
43
44/// Print sparse vector content
45template<typename SV> void PrintSV(const SV& sv)
46{
47 auto sz = sv.size();
48 cout << "size() = " << sz << " : ";
49
50 for (typename SV::size_type i = 0; i < sz; ++i)
51 {
52 if (sv.is_null(i))
53 cout << "NULL";
54 else
55 cout << sv.get(i);
56 cout << ", ";
57 }
58 cout << endl;
59}
60
63
64
65int main(void)
66{
67 try
68 {
71
72 // add sample data using back insert iterator
73 //
74 {
75 auto bit = csv1.get_back_inserter();
76 bit = 10;
77 bit = 11;
78 bit.add_null();
79 bit = 13;
80 bit = 14;
81 bit.add_null(2);
82 bit = 256;
83 bit.flush();
84
85 csv1.sync();
86 }
87 PrintSV(csv1); // size() = 8 : 10, 11, NULL, 13, 14, NULL, NULL, 256,
88
89 // serialize sparse vector
91 {
93 // optimize memory allocation of sparse vector
94 csv1.optimize(tb);
95
96 // configure serializer, set bookmarking option for faster
97 // range deserialization
98 //
100 sv_serializer.set_bookmarks(true, 64);
101
102 sv_serializer.serialize(csv1, sv_lay); // serialization
103 }
104
105 // get serialization pointer
106 const unsigned char* buf = sv_lay.buf();
107
108 // instantiate the deserializer object to do all multiple
109 // deserialization operations (faster)
110 //
112
113 // Case 1:
114 // Here we define de-serialization indexes as a bit-vector (mask)
115 // so deserializer gathers only elements selected by the bit-vector
116 // all vector elements not set in the gather vector will be 0 (or NULL)
117 //
118 {
120 bv_mask.set(0);
121 bv_mask.set(1);
122 sv_deserial.deserialize(csv2, buf, bv_mask);
123 }
124 PrintSV(csv2); // size() = 8 : 10, 11, NULL, 0, 0, NULL, NULL, 0,
125
126 // Case 2:
127 // Here we define de-serialization indexes as a closed range [1..4]
128 // It is an equivalent of setting selection bits in the mask vector
129 // but defines the intent more clearly and works faster
130 {
131 sv_deserial.deserialize_range(csv2, buf, 1, 4);
132 }
133 PrintSV(csv2); // size() = 8 : 0, 11, NULL, 13, 14, NULL, NULL, 0,
134
135 }
136 catch(std::exception& ex)
137 {
138 std::cerr << ex.what() << std::endl;
139 return 1;
140 }
141
142
143
144 return 0;
145}
146
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.
Compressed sparse container rsc_sparse_vector<> for integer types.
Serialization for sparse_vector<>
pre-processor un-defines to avoid global space pollution (internal)
back_insert_iterator get_back_inserter()
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 slices
void sync(bool force)
Re-calculate rank-select index for faster access to vector.
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
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: rscsample02.cpp:61
int main(void)
Definition: rscsample02.cpp:65
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample02.cpp:62
void PrintSV(const SV &sv)
Print sparse vector content.
Definition: rscsample02.cpp:45
layout class for serialization buffer structure
const unsigned char * buf() const BMNOEXCEPT
Return serialization buffer pointer.