BitMagic-C++
rscsample04.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2020 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 rscsample04.cpp
20 Example of how to use bm::rsc_sparse_vector<> pre-defined set construction
21
22 1. Lets assume we know which elements of the vector are not NULL
23 2. bm::rsc_sparse_vector<> can be constructed for this
24 3. call to bm::rsc_sparse_vector<>::sync will re-calculate Rank-Select index
25 to enable fast access to elements (usually read only mode)
26 4. In this example we modify not NULL vector elements so it would use
27 Rank-Select index without breaking its consistency
28
29
30 \sa bm::rsc_sparse_vector
31 \sa bm::rsc_sparse_vector::sync
32 \sa bm::rsc_sparse_vector::inc
33
34 @sa rscsample02.cpp
35*/
36
37/*! \file rscsample04.cpp
38 \brief Example: bm::rsc_sparse_vector<> known NULL elements access
39
40*/
41
42#include <iostream>
43#include <vector>
44#include <cassert>
45
46#include "bm.h"
47#include "bmsparsevec.h"
48#include "bmsparsevec_compr.h"
49#include "bmundef.h" /* clear the pre-proc defines from BM */
50
51using namespace std;
52
53/// Print sparse vector not NULL elements
54template<typename SV> void PrintSV(const SV& sv)
55{
56 typename SV::const_iterator it = sv.begin();
57 typename SV::const_iterator it_end = sv.end();
58
59 for (size_t i = 0; it != it_end; ++it, ++i)
60 {
61 if (!it.is_null())
62 cout << i << ": " << *it << ", ";
63 }
64 cout << endl;
65}
66
69
70
71int main(void)
72{
73 try
74 {
75 // vector of not NULL elements
76 bm::bvector<> bv1 { 10, 20, 100, 200, 65536 };
77 rsc_sparse_vector_u32 csv1(bv1);
78
79 csv1.sync(); // build Rank-Select index for faster access
80 bv1.clear(); // we don't need bv1 anymore
81
82 // modify vector elements but only touch the not NULL elements
83 // for fast access
84 //
85 csv1.set(100, 1);
86 csv1.set(20, 1);
87 csv1.inc(10);
88 csv1.inc(65536, 1);
89
90 assert(csv1.in_sync()); // make sure Rank-Select index is still alive
91
92 PrintSV(csv1); // 10: 1, 20: 1, 100: 1, 200: 0, 65536: 1
93 }
94 catch(std::exception& ex)
95 {
96 std::cerr << ex.what() << std::endl;
97 return 1;
98 }
99
100
101
102 return 0;
103}
104
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
Compressed sparse container rsc_sparse_vector<> for integer types.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
void sync(bool force)
Re-calculate rank-select index for faster access to vector.
bool in_sync() const BMNOEXCEPT
returns true if prefix sum table is in sync with the vector
void inc(size_type idx)
increment specified element by one
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: rscsample04.cpp:67
int main(void)
Definition: rscsample04.cpp:71
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample04.cpp:68
void PrintSV(const SV &sv)
Print sparse vector not NULL elements.
Definition: rscsample04.cpp:54