BitMagic-C++
svsample10.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 svsample10.cpp
20 Scanner searches: GT, GE, LT, LE, RANGE[from..to]
21
22 \sa bm::sparse_vector
23 \sa bm::sparse_vector_scanner<>::find_gt
24 \sa bm::sparse_vector_scanner<>::find_ge
25 \sa bm::sparse_vector_scanner<>::find_lt
26 \sa bm::sparse_vector_scanner<>::find_le
27 \sa bm::sparse_vector_scanner<>::find_range
28
29*/
30
31/*! \file svsample10.cpp
32 \brief Example: Succinct vector searches: GT, GE, LT, LE, RANGE[from..to]
33*/
34
35#include <assert.h>
36#include <stdlib.h>
37
38#include <iostream>
39#include <vector>
40#include <utility>
41
42#include "bm.h"
43#include "bmsparsevec.h"
44#include "bmsparsevec_algo.h"
45#include "bmundef.h" /* clear the pre-proc defines from BM */
46
47using namespace std;
48
49
52
53template<typename SV, typename BV>
54void PrintResults(const SV& sv, const BV& bv)
55{
56 auto en = bv.get_enumerator(0);
57 if (!en.valid())
58 {
59 cout << "<EMPTY>" << endl;
60 return;
61 }
62 auto cnt = bv.count();
63 cout << "size=" << cnt << " ";
64 for (; en.valid(); ++en)
65 {
66 auto idx = *en;
67 auto v = sv.get(idx);
68 cout << idx << ":" << v << ", ";
69 } // for
70 cout << endl;
71}
72
73
74int main(void)
75{
76 try
77 {
78 svector_u32 sv1;
80
81 // fill in vectors with some test data
82 //
83 {
84 auto bit = sv1.get_back_inserter();
85 bit = 1;
86 bit = 2;
87 bit = 20;
88 bit = 0;
89
90 bit.flush();
91 }
92 {
93 auto bit = sv2.get_back_inserter();
94 bit = 1;
95 bit = -2;
96 bit = 0;
97 bit.add_null();
98 bit = 30;
99
100 bit.flush();
101 }
102
105
106 // Here we perform scanner searches of various types
107 // Please note that NULL values are never included into
108 // the search results.
109 // Any comparison with NULL results as false.
110
111 // bm::sparse_vector_scanner performs searches without reverse
112 // transformation, using logical ops, returning a result-set
113 // bvector<>
114 //
115
116 // GT search
117 {
118 bm::bvector<> bv_res;
119 scanner_u32.find_gt(sv1, 1, bv_res);
120 PrintResults(sv1, bv_res); // size=2 1:2, 2:20,
121 }
122
123 // LT search
124 {
125 bm::bvector<> bv_res;
126 scanner_u32.find_lt(sv1, 0, bv_res);
127 PrintResults(sv1, bv_res); // <EMPTY>
128 }
129
130 // GE search
131 {
132 bm::bvector<> bv_res;
133 scanner_i32.find_ge(sv2, -1, bv_res);
134 PrintResults(sv2, bv_res); // size=3 0:1, 2:0, 4:30,
135 }
136
137 // LE search
138 {
139 bm::bvector<> bv_res;
140 scanner_i32.find_le(sv2, 30, bv_res);
141 PrintResults(sv2, bv_res); // size=4 0:1, 1:-2, 2:0, 4:30,
142 }
143
144 // range search [from..to] closed range
145 {
146 bm::bvector<> bv_res;
147 scanner_i32.find_range(sv2, -1, 30, bv_res);
148 PrintResults(sv2, bv_res); // size=3 0:1, 2:0, 4:30,
149 }
150
151 // a bit more complex search expression
152 // (> 10) OR (<= -1)
153 //
154 // (> 10) AND (<= -1)
155 //
156 {
157 bm::bvector<> bv_res_gt;
158 bm::bvector<> bv_res_le;
159
160 scanner_i32.find_gt(sv2, 10, bv_res_gt); // > 10
161 scanner_i32.find_le(sv2, -1, bv_res_le); // <= -1
162
163 bm::bvector<> bv_res;
164 bv_res.bit_or(bv_res_gt, bv_res_le); // OR two together
165
166 PrintResults(sv2, bv_res); // size=2 1:-2, 4:30,
167
168 bv_res.bit_and(bv_res_gt, bv_res_le); // AND two together - empty set
169 PrintResults(sv2, bv_res); // <EMPTY>
170
171 }
172
173 }
174 catch(std::exception& ex)
175 {
176 std::cerr << ex.what() << std::endl;
177 return 1;
178 }
179
180
181
182 return 0;
183}
184
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
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
bm::bvector< Alloc > & bit_or(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand OR : this := bv1 OR bv2
Definition: bm.h:5668
bm::bvector< Alloc > & bit_and(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand AND : this := bv1 AND bv2
Definition: bm.h:5880
void add_null()
add NULL (no-value) to the container
Definition: bmsparsevec.h:2513
algorithms for sparse_vector scan/search
void find_lt(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element less (<) than value
void find_ge(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element greater or equal (>=) than value
void find_le(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element less or equal (<=) than value
void find_gt(const SV &sv, value_type val, bvector_type &bv_out)
find all elements sparse vector element greater (>) than value
void find_range(const SV &sv, value_type from, value_type to, bvector_type &bv_out)
find all elements sparse vector element in closed range [left..right] interval
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
back_insert_iterator get_back_inserter()
Provide back insert iterator Back insert iterator implements buffered insertion, which is faster,...
Definition: bmsparsevec.h:572
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:229
bm::sparse_vector< unsigned, bm::bvector<> > svector_u32
Definition: svsample10.cpp:50
bm::sparse_vector< int, bm::bvector<> > svector_i32
Definition: svsample10.cpp:51
void PrintResults(const SV &sv, const BV &bv)
Definition: svsample10.cpp:54
int main(void)
Definition: svsample10.cpp:74