BitMagic-C++
rscsample06.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 rscsample06.cpp
20 Example of how to use bm::rsc_sparse_vector<>::gather to extract values in random order
21
22
23 \sa bm::rsc_sparse_vector
24 \sa bm::rsc_sparse_vector::sync
25 \sa bm::rsc_sparse_vector::gather
26*/
27
28/*! \file rscsample06.cpp
29 \brief Example: bm::rsc_sparse_vector<>::gather
30
31*/
32
33#include <iostream>
34#include <vector>
35#include <cassert>
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 not NULL elements
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 (size_t i = 0; it != it_end; ++it, ++i)
51 {
52 if (!it.is_null())
53 cout << i << ": " << *it << ", ";
54 }
55 cout << endl;
56}
57
60
61
62int main(void)
63{
64 try
65 {
66 // vector of not NULL elements
67 bm::bvector<> bv1 { 10, 20, 100, 200, 65536 };
68 rsc_sparse_vector_u32 csv1(bv1);
69
70 csv1.sync(); // build Rank-Select index for faster access
71 bv1.clear(); // we don't need bv1 anymore
72
73 // modify vector elements but only touch the not NULL elements
74 // for fast access
75 //
76 csv1.set(100, 1);
77 csv1.set(20, 1);
78 csv1.inc(10);
79 csv1.inc(65536, 1);
80
81 assert(csv1.in_sync()); // make sure Rank-Select index is still alive
82
83 // gather some of the values in random order
84 //
85
86 // idx vector to request RSC vector elements
87 bm::bvector<>::size_type idx[] = { 20, 0, 10, 65536 };
88
89 // scratch buffer for Rank-Select coordinates recalculation
90 // MUST be the same size as index buffer
91 //
92 bm::bvector<>::size_type idx_buf[sizeof(idx)/sizeof(idx[0])];
93
94
95 rsc_sparse_vector_u32::value_type values[sizeof(idx)/sizeof(idx[0])];
96
97
98 bm::bvector<>::size_type req_size = 4;
99
100 // gather using bm::BM_UNKNOWM sort order (most universal).
101 // if sort order is ascending use BM_SORTED for faster operation
102 //
103 csv1.gather(&values[0], &idx[0], &idx_buf[0], req_size, bm::BM_UNKNOWN);
104
105
106 // it is possible to use gather() to request NULL values, in this case
107 // the secondary array buffer will contain the magic value "bm::id_max"
108 //
109 for (size_t i = 0; i < req_size; ++i)
110 {
111 auto ix = idx[i];
112 if (idx_buf[i] == bm::id_max)
113 cout << "[" << ix << "] = " << "NULL" << endl;
114 else
115 cout << "[" << ix << "] = " << values[i] << endl;
116 } // for
117
118 PrintSV(csv1); // 10: 1, 20: 1, 100: 1, 200: 0, 65536: 1
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.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bvector_size_type size_type
Definition: bm.h:121
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
size_type gather(value_type *arr, const size_type *idx, size_type *idx_tmp_buf, size_type size, bm::sort_order sorted_idx) const
Gather elements to a C-style array.
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
@ BM_UNKNOWN
sort order unknown
Definition: bmconst.h:207
const unsigned id_max
Definition: bmconst.h:109
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition: rscsample06.cpp:58
int main(void)
Definition: rscsample06.cpp:62
bm::rsc_sparse_vector< unsigned, sparse_vector_u32 > rsc_sparse_vector_u32
Definition: rscsample06.cpp:59
void PrintSV(const SV &sv)
Print sparse vector not NULL elements.
Definition: rscsample06.cpp:45