BitMagic-C++
svsample07a.cpp

Example of how to search bm::sparse_vector_scanner<> with sparse vector scanner and inspect search results using bm::interval_enumerator<> to find special values: unique, co-locvated together in the results or dispersed.

Example of how to search bm::sparse_vector_scanner<> with sparse vector scanner and inspect search results using bm::interval_enumerator<> to find special values: unique, co-locvated together in the results or dispersed.

See also
bm::sparse_vector
bm::sparse_vector_scanner
bm::interval_enumerator
Algorithms for bit intervals
sample23
/*
Copyright(c) 2002-2019 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For more information please visit: http://bitmagic.io
*/
/** \example svsample07a.cpp
Example of how to search bm::sparse_vector_scanner<> with sparse vector scanner
and inspect search results using bm::interval_enumerator<>
to find special values: unique, co-locvated together in the results or dispersed.
\sa bm::sparse_vector
\sa bm::sparse_vector_scanner
\sa bm::interval_enumerator
\sa bvintervals
\sa sample23
*/
/*! \file svsample07a.cpp
\brief Example: sparse_vector<> search
*/
#include <assert.h>
#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm>
#include <random>
#include <algorithm>
#include <stdexcept>
#include "bm.h"
#include "bmsparsevec.h"
#include "bmintervals.h"
#include "bmundef.h" /* clear the pre-proc defines from BM */
using namespace std;
int main(void)
{
try
{
// use back inserter to add values to the vector
{
sparse_vector_u32::back_insert_iterator bit(&sv);
bit = 0;
bit = bm::id_max;
bit = 17;
bit = 17;
bit = 5;
bit = 18;
bit = 178;
bit = 178;
bit = 17;
bit = 0;
bit = bm::id_max;
bit.flush();
}
sv.optimize(); // mmemory optimization after active editing
// just a print-out
{
cout << "Sparse vector:" << endl;
std::for_each(sv.begin(), sv.end(), [](unsigned v) {
cout << v << ",";
});
cout << endl << endl;
}
scanner.bind(sv, false);
bvector_type bv_res;
// connect bv_res with allocation pool scanner for better
// this is optional but known to improve performance on repeated searches
typename bvector_type::mem_pool_guard mp_guard;
mp_guard.assign_if_not_set(scanner.get_bvector_alloc_pool(), bv_res);
sparse_vector_u32::const_iterator it = sv.begin();
sparse_vector_u32::const_iterator it_end = sv.end();
// setup two bit-vectors for positive and negative values we seen
// if values are signed - maintain two separatevectors
// for positives and negatives
bvector_type bv_seen_values(bm::BM_GAP);
bool id_max_seen(false); // bit-vector cannot take bm::id_max value
for (;it != it_end; ++it)
{
auto v = *it;
{
if (id_max_seen)
continue;
id_max_seen = true;
}
else
{
// scanner search is an expensive operation do avoid duplicate lookups
// maintain bit-vectors of already seen values
//
if (bv_seen_values.test(bvector_type::size_type(v)))
continue;
bv_seen_values.set(bvector_type::size_type(v));
}
scanner.find_eq(sv, v, bv_res);
// we have our results, we can now inspect the result bit-vector
// using interval enumerator to find which values are co-located
// in one bunch as 000011000...0000
// or unique as 000010000...00
// or form non-unique, multi-occurence patterns as 000111000..011..
//
assert(ien.valid()); // as we took value from the search vector
bvector_type::size_type range_cnt = ien.end() - ien.start() + 1;
// try to advance
if (!ien.advance()) // only one interval, cannot advance more
{
if (range_cnt == 1)
{
cout << "Value = " << v << " is unique" << endl;
continue;
}
cout << "Value = " << v << " is colocated" << endl;
continue;
}
cout << "Value = " << v << " is not colocated" << endl;
} // for it
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
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
int main(void)
Definition sample1.cpp:43
bm::interval_enumerator< bm::bvector<> > interval_enumerator_type
Definition sample23.cpp:52
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition xsample02.cpp:68
bm::bvector bvector_type