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
41using namespace std;
42
43/// Print sparse vector content
44template<typename SV> void PrintSV(const SV& sv)
45{
46 typename SV::const_iterator it = sv.begin();
47 typename SV::const_iterator it_end = sv.end();
48
49 for (; it != it_end; ++it)
50 {
51 if (it.is_null())
52 cout << "NULL";
53 else
54 cout << *it;
55 cout << ", ";
56 }
57 cout << endl;
58}
59
62
63
64int main(void)
65{
66 try
67 {
69
70 // add sample data using back insert iterator
71 //
72 {
73 auto bit = csv1.get_back_inserter();
74 bit = 10;
75 bit = 11;
76 bit.add_null();
77 bit = 13;
78 bit = 14;
79 bit.add_null(2);
80 bit = 256;
81 bit.flush();
82 }
83
84 // sync call updates rank-select index of the succinct container and
85 // it is necessary for the correct work of the const_iterator
86 // and bm::rsc_sparse_vector<>::decode()
87 // and bm::rsc_sparse_vector<>::decode_buf() methods
88 //
89 csv1.sync();
90
91 PrintSV(csv1); // 10, 11, NULL, 13, 14, NULL, NULL, 256,
92
93
94 // another way to use iterator at a random position
95 // is to use rsc_sparse_vector<>::get_const_iterator()
96 {
98 csv1.get_const_iterator(2);
99 if (it.valid()) // check if iterator is avlid and not at the end
100 {
101 do
102 {
103 auto v = it.value();
104 if (it.is_null())
105 cout << "NULL";
106 else
107 cout << v;
108 cout << ", ";
109 } while (it.advance()); // while it is able to advance
110 cout << endl;
111
112 // iterator can be re-positioned
113 //
114 it.go_to(3); // position iterator at 3
115 cout << it.value() << endl; // 13
116 }
117 }
118
119 }
120 catch(std::exception& ex)
121 {
122 std::cerr << ex.what() << std::endl;
123 return 1;
124 }
125
126
127
128 return 0;
129}
130
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.
void add_null() BMNOEXCEPT
add NULL (no-value) to the container
Const iterator to traverse the rsc sparse vector.
bool advance() BMNOEXCEPT
advance iterator forward by one
void go_to(size_type pos) BMNOEXCEPT
re-position to a specified position
bool is_null() const BMNOEXCEPT
Get NULL status.
bool valid() const BMNOEXCEPT
Returns true if iterator is at a valid position.
value_type value() const
Get current position (value)
Rank-Select compressed sparse vector.
back_insert_iterator get_back_inserter()
void sync(bool force)
Re-calculate prefix sum table used for rank search.
const_iterator get_const_iterator(size_type idx) const BMNOEXCEPT
Get const_itertor re-positioned to specific element.
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.