BitMagic-C++: rscsample03.cpp Source File

BitMagic-C++

rscsample03.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2020 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 For 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
41 using namespace std;
42
43 /// Print sparse vector content
44 template<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
60 typedef bm::sparse_vector<unsigned, bm::bvector<> > sparse_vector_u32;
61 typedef bm::rsc_sparse_vector<unsigned, sparse_vector_u32 > rsc_sparse_vector_u32;
62
63
64 int main(void)
65 {
66 try
67 {
68 rsc_sparse_vector_u32 csv1;
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 {
97 rsc_sparse_vector_u32::const_iterator it =
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
bm::sparse_vector
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
bm::rsc_sparse_vector::const_iterator::go_to
void go_to(size_type pos) BMNOEXCEPT
re-position to a specified position
Definition: bmsparsevec_compr.h:1441
bm::rsc_sparse_vector::const_iterator
Const iterator to traverse the rsc sparse vector.
Definition: bmsparsevec_compr.h:124
bmsparsevec.h
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
bm::rsc_sparse_vector::back_insert_iterator::add_null
void add_null() BMNOEXCEPT
add NULL (no-value) to the container
Definition: bmsparsevec_compr.h:1370
bmsparsevec_compr.h
Compressed sparse container rsc_sparse_vector<> for integer types.
bm::rsc_sparse_vector::const_iterator::value
value_type value() const
Get current position (value)
Definition: bmsparsevec_compr.h:1473
sparse_vector_u32
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: rscsample03.cpp:60
bm::rsc_sparse_vector::const_iterator::is_null
bool is_null() const BMNOEXCEPT
Get NULL status.
Definition: bmsparsevec_compr.h:1522
bm::rsc_sparse_vector::const_iterator::valid
bool valid() const BMNOEXCEPT
Returns true if iterator is at a valid position.
Definition: bmsparsevec_compr.h:184
main
int main(void)
Definition: rscsample03.cpp:64
bm::rsc_sparse_vector::get_back_inserter
back_insert_iterator get_back_inserter()
Definition: bmsparsevec_compr.h:547
rsc_sparse_vector_u32
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample03.cpp:61
bm::rsc_sparse_vector
Rank-Select compressed sparse vector.
Definition: bmsparsevec_compr.h:58
bm::rsc_sparse_vector::const_iterator::advance
bool advance() BMNOEXCEPT
advance iterator forward by one
Definition: bmsparsevec_compr.h:1450
bm::rsc_sparse_vector::sync
void sync(bool force)
Re-calculate prefix sum table used for rank search.
Definition: bmsparsevec_compr.h:963
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
PrintSV
void PrintSV(const SV &sv)
Print sparse vector content.
Definition: rscsample03.cpp:44
bm::rsc_sparse_vector::get_const_iterator
const_iterator get_const_iterator(size_type idx) const BMNOEXCEPT
Get const_itertor re-positioned to specific element.
Definition: bmsparsevec_compr.h:544