BitMagic-C++
svsample07a.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2019 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 svsample07a.cpp
20 Example of how to search bm::sparse_vector_scanner<> with sparse vector scanner
21 and inspect search results using bm::interval_enumerator<>
22 to find special values: unique, co-locvated together in the results or dispersed.
23
24 \sa bm::sparse_vector
25 \sa bm::sparse_vector_scanner
26 \sa bm::interval_enumerator
27 \sa bvintervals
28 \sa sample23
29
30*/
31
32/*! \file svsample07a.cpp
33 \brief Example: sparse_vector<> search
34*/
35
36#include <assert.h>
37#include <iostream>
38#include <vector>
39#include <chrono>
40#include <algorithm>
41#include <random>
42#include <algorithm>
43#include <stdexcept>
44
45#include "bm.h"
46#include "bmsparsevec.h"
47#include "bmsparsevec_algo.h"
48#include "bmintervals.h"
49#include "bmundef.h" /* clear the pre-proc defines from BM */
50
51using namespace std;
52
56
57
58
59int main(void)
60{
61 try
62 {
64
65 // use back inserter to add values to the vector
66 {
67 sparse_vector_u32::back_insert_iterator bit(&sv);
68 bit = 0;
69 bit = bm::id_max;
70 bit = 17;
71 bit = 17;
72 bit = 5;
73 bit = 18;
74 bit = 178;
75 bit = 178;
76 bit = 17;
77 bit = 0;
78 bit = bm::id_max;
79 bit.flush();
80 }
81 sv.optimize(); // mmemory optimization after active editing
82
83 // just a print-out
84 {
85 cout << "Sparse vector:" << endl;
86 std::for_each(sv.begin(), sv.end(), [](unsigned v) {
87 cout << v << ",";
88 });
89 cout << endl << endl;
90 }
91
93
94 scanner.bind(sv, false);
95
96 bvector_type bv_res;
97 // connect bv_res with allocation pool scanner for better
98 // this is optional but known to improve performance on repeated searches
99 typename bvector_type::mem_pool_guard mp_guard;
100 mp_guard.assign_if_not_set(scanner.get_bvector_alloc_pool(), bv_res);
101
102 sparse_vector_u32::const_iterator it = sv.begin();
103 sparse_vector_u32::const_iterator it_end = sv.end();
104
105 // setup two bit-vectors for positive and negative values we seen
106 // if values are signed - maintain two separatevectors
107 // for positives and negatives
108 bvector_type bv_seen_values(bm::BM_GAP);
109 bool id_max_seen(false); // bit-vector cannot take bm::id_max value
110 for (;it != it_end; ++it)
111 {
112 auto v = *it;
114 {
115 if (id_max_seen)
116 continue;
117 id_max_seen = true;
118 }
119 else
120 {
121 // scanner search is an expensive operation do avoid duplicate lookups
122 // maintain bit-vectors of already seen values
123 //
124 if (bv_seen_values.test(bvector_type::size_type(v)))
125 continue;
126 bv_seen_values.set(bvector_type::size_type(v));
127 }
128
129 scanner.find_eq(sv, v, bv_res);
130
131 // we have our results, we can now inspect the result bit-vector
132 // using interval enumerator to find which values are co-located
133 // in one bunch as 000011000...0000
134 // or unique as 000010000...00
135 // or form non-unique, multi-occurence patterns as 000111000..011..
136 //
137
138 interval_enumerator_type ien(bv_res);
139 assert(ien.valid()); // as we took value from the search vector
140 bvector_type::size_type range_cnt = ien.end() - ien.start() + 1;
141
142 // try to advance
143 if (!ien.advance()) // only one interval, cannot advance more
144 {
145 if (range_cnt == 1)
146 {
147 cout << "Value = " << v << " is unique" << endl;
148 continue;
149 }
150 cout << "Value = " << v << " is colocated" << endl;
151 continue;
152 }
153 cout << "Value = " << v << " is not colocated" << endl;
154 } // for it
155 }
156 catch(std::exception& ex)
157 {
158 std::cerr << ex.what() << std::endl;
159 return 1;
160 }
161
162 return 0;
163}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Algorithms for bit ranges and intervals.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
Algorithms for bm::sparse_vector.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:115
bool test(size_type n) const BMNOEXCEPT
returns true if bit n is set and false is bit n is 0.
Definition bm.h:1502
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition bm.h:4188
bvector_size_type size_type
Definition bm.h:121
forward iterator class to traverse bit-vector as ranges
Definition bmintervals.h:53
size_type end() const BMNOEXCEPT
Return interval end/right as bit-vector coordinate 011110 [left..right].
bool valid() const BMNOEXCEPT
Returns true if enumerator is valid (false if traversal is done).
size_type start() const BMNOEXCEPT
Return interval start/left as bit-vector coordinate 011110 [left..right].
algorithms for sparse_vector scan/search
allocator_pool_type & get_bvector_alloc_pool() BMNOEXCEPT
Return allocator pool for blocks (Can be used to improve performance of repeated searches with the sa...
void find_eq(const SV &sv, value_type value, bvector_type &bv_out)
find all sparse vector elements EQ to search value
void bind(const SV &sv, bool sorted)
bind sparse vector for all searches
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition bmsparsevec.h:87
const_iterator end() const BMNOEXCEPT
Provide const iterator access to the end.
const_iterator begin() const BMNOEXCEPT
Provide const iterator access to container content.
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename sparse_vector< Val, BV >::statistics *stat=0)
run memory optimization for all vector planes
bm::alloc_pool_guard< allocator_pool_type, bvector< Alloc > > mem_pool_guard
Definition bm.h:790
@ BM_GAP
GAP compression is ON.
Definition bmconst.h:148
const unsigned id_max
Definition bmconst.h:109
bm::interval_enumerator< bm::bvector<> > interval_enumerator_type
Definition sample23.cpp:52
int main(void)
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition xsample02.cpp:68
bm::bvector bvector_type