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
41using namespace std;
42
43/// Print sparse vector content
44template<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
62
63
64int main(void)
65{
66 try
67 {
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
90 {
92 // optimize memory allocation of sparse vector
93 csv1.optimize(tb);
94
95 // configure serializer, set bookmarking option for faster
96 // range deserialization
97 //
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 //
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 {
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
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<>
void add_null() BMNOEXCEPT
add NULL (no-value) to the container
Rank-Select compressed sparse vector.
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 plains
void sync(bool force)
Re-calculate prefix sum table used for rank search.
SV::bvector_type bvector_type
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
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
int main(void)
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
void PrintSV(const SV &sv)
Print sparse vector content.
layout class for serialization buffer structure
const unsigned char * buf() const
Return serialization buffer pointer.