BitMagic-C++
rscsample03.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 rscsample03.cpp
20 Example of how to use bm::rsc_sparse_vector<>::const_iterator
21
22 \sa bm::rsc_sparse_vector
23 \sa bm::rsc_sparse_vector::const_iterator
24 \sa bm::rsc_sparse_vector::const_iterator::go_to
25
26*/
27
28/*! \file rscsample03.cpp
29 \brief Example: bm::rsc_sparse_vector<>::const_iterator
30
31 @sa rscsample02.cpp
32*/
33
34#include <iostream>
35#include <vector>
36
37#include "bm.h"
38#include "bmsparsevec.h"
39#include "bmsparsevec_compr.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 typename SV::const_iterator it = sv.begin();
48 typename SV::const_iterator it_end = sv.end();
49
50 for (; it != it_end; ++it)
51 {
52 if (it.is_null())
53 cout << "NULL";
54 else
55 cout << *it;
56 cout << ", ";
57 }
58 cout << endl;
59}
60
63
64
65int main(void)
66{
67 try
68 {
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
85 // sync call updates rank-select index of the succinct container and
86 // it is necessary for the correct work of the const_iterator
87 // and bm::rsc_sparse_vector<>::decode()
88 // and bm::rsc_sparse_vector<>::decode_buf() methods
89 //
90 csv1.sync();
91
92 PrintSV(csv1); // 10, 11, NULL, 13, 14, NULL, NULL, 256,
93
94
95 // another way to use iterator at a random position
96 // is to use rsc_sparse_vector<>::get_const_iterator()
97 {
98 rsc_sparse_vector_u32::const_iterator it =
99 csv1.get_const_iterator(2);
100 if (it.valid()) // check if iterator is avlid and not at the end
101 {
102 do
103 {
104 auto v = it.value();
105 if (it.is_null())
106 cout << "NULL";
107 else
108 cout << v;
109 cout << ", ";
110 } while (it.advance()); // while it is able to advance
111 cout << endl;
112
113 // iterator can be re-positioned
114 //
115 it.go_to(3); // position iterator at 3
116 cout << it.value() << endl; // 13
117 }
118 }
119
120 }
121 catch(std::exception& ex)
122 {
123 std::cerr << ex.what() << std::endl;
124 return 1;
125 }
126
127
128
129 return 0;
130}
131
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)
back_insert_iterator get_back_inserter()
void sync(bool force)
Re-calculate rank-select index for faster access to vector.
const_iterator get_const_iterator(size_type idx) const BMNOEXCEPT
Get const_itertor re-positioned to specific element.
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: rscsample03.cpp:61
int main(void)
Definition: rscsample03.cpp:65
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample03.cpp:62
void PrintSV(const SV &sv)
Print sparse vector content.
Definition: rscsample03.cpp:45