rscsample02.cpp
Go to the documentation of this file.
/*
Copyright(c) 2002-2019 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For more information please visit: http://bitmagic.io
*/
/** \example rscsample02.cpp
Example of how to partially de-serialize bm::rsc_sparse_vector<> container
\sa bm::rsc_sparse_vector
\sa bm::rsc_sparse_vector<>::back_insert_iterator
*/
/*! \file rscsample02.cpp
\brief Example: rsc_sparse_vector<> selective and range de-serialization
@sa strsvsample05.cpp
*/
#include <iostream>
#include <vector>
#include "bm.h"
#include "bmsparsevec.h"
#include "bmsparsevec_compr.h"
#include "bmsparsevec_serial.h"
#include "bmundef.h" /* clear the pre-proc defines from BM */
using namespace std;
/// Print sparse vector content
template<typename SV> void PrintSV(const SV& sv)
{
auto sz = sv.size();
cout << "size() = " << sz << " : ";
for (typename SV::size_type i = 0; i < sz; ++i)
{
if (sv.is_null(i))
cout << "NULL";
else
cout << sv.get(i);
cout << ", ";
}
cout << endl;
}
typedef bm::sparse_vector<unsigned, bm::bvector<> > sparse_vector_u32;
typedef bm::rsc_sparse_vector<unsigned, sparse_vector_u32 > rsc_sparse_vector_u32;
int main(void)
{
try
{
rsc_sparse_vector_u32 csv1;
rsc_sparse_vector_u32 csv2;
// add sample data using back insert iterator
//
{
auto bit = csv1.get_back_inserter();
bit = 10;
bit = 11;
bit.add_null();
bit = 13;
bit = 14;
bit.add_null(2);
bit = 256;
bit.flush();
csv1.sync();
}
PrintSV(csv1); // size() = 8 : 10, 11, NULL, 13, 14, NULL, NULL, 256,
// serialize sparse vector
bm::sparse_vector_serial_layout<rsc_sparse_vector_u32> sv_lay;
{
BM_DECLARE_TEMP_BLOCK(tb)
// optimize memory allocation of sparse vector
csv1.optimize(tb);
// configure serializer, set bookmarking option for faster
// range deserialization
//
bm::sparse_vector_serializer<rsc_sparse_vector_u32> sv_serializer;
sv_serializer.set_bookmarks(true, 64);
sv_serializer.serialize(csv1, sv_lay); // serialization
}
// get serialization pointer
const unsigned char* buf = sv_lay.buf();
// instantiate the deserializer object to do all multiple
// deserialization operations (faster)
//
bm::sparse_vector_deserializer<rsc_sparse_vector_u32> sv_deserial;
// Case 1:
// Here we define de-serialization indexes as a bit-vector (mask)
// so deserializer gathers only elements selected by the bit-vector
// all vector elements not set in the gather vector will be 0 (or NULL)
//
{
rsc_sparse_vector_u32::bvector_type bv_mask;
bv_mask.set(0);
bv_mask.set(1);
sv_deserial.deserialize(csv2, buf, bv_mask);
}
PrintSV(csv2); // size() = 8 : 10, 11, NULL, 0, 0, NULL, NULL, 0,
// Case 2:
// Here we define de-serialization indexes as a closed range [1..4]
// It is an equivalent of setting selection bits in the mask vector
// but defines the intent more clearly and works faster
{
sv_deserial.deserialize_range(csv2, buf, 1, 4);
}
PrintSV(csv2); // size() = 8 : 0, 11, NULL, 13, 14, NULL, NULL, 0,
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
BM_DECLARE_TEMP_BLOCK
#define BM_DECLARE_TEMP_BLOCK(x)
Definition bm.h:47
bmsparsevec.h
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
bmsparsevec_compr.h
Compressed sparse container rsc_sparse_vector<> for integer types.
bmsparsevec_serial.h
Serialization for sparse_vector<>.
bmundef.h
pre-processor un-defines to avoid global space pollution (internal)
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:4188
bm::rsc_sparse_vector
Rank-Select compressed sparse vector.
Definition bmsparsevec_compr.h:59
bm::rsc_sparse_vector::get_back_inserter
back_insert_iterator get_back_inserter()
Definition bmsparsevec_compr.h:714
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 slices
Definition bmsparsevec_compr.h:1644
bm::rsc_sparse_vector::sync
void sync(bool force)
Re-calculate rank-select index for faster access to vector.
Definition bmsparsevec_compr.h:1442
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 >::bvector_type
sparse_vector_u32::bvector_type bvector_type
Definition bmsparsevec_compr.h:72
bm::sparse_vector_deserializer
sparse vector de-serializer
Definition bmsparsevec_serial.h:327
bm::sparse_vector_deserializer::deserialize
void deserialize(SV &sv, const unsigned char *buf, bool clear_sv=true)
Definition bmsparsevec_serial.h:1222
bm::sparse_vector_deserializer::deserialize_range
void deserialize_range(SV &sv, const unsigned char *buf, size_type from, size_type to, bool clear_sv=true)
Definition bmsparsevec_serial.h:1261
bm::sparse_vector_serializer
Definition bmsparsevec_serial.h:176
bm::sparse_vector_serializer::set_bookmarks
void set_bookmarks(bool enable, unsigned bm_interval=256) BMNOEXCEPT
Add skip-markers for faster range deserialization.
Definition bmsparsevec_serial.h:207
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:941
bm::sparse_vector
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition bmsparsevec.h:87
main
int main(void)
Definition rscsample02.cpp:65
PrintSV
void PrintSV(const SV &sv)
Print sparse vector content.
Definition rscsample02.cpp:45
bm::sparse_vector_serial_layout
layout class for serialization buffer structure
Definition bmsparsevec_serial.h:58
bm::sparse_vector_serial_layout::buf
const unsigned char * buf() const BMNOEXCEPT
Return serialization buffer pointer.
Definition bmsparsevec_serial.h:108
sparse_vector_u32
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition xsample02.cpp:68
rsc_sparse_vector_u32
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition xsample03.cpp:205