BitMagic-C++
svsample07.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 svsample07.cpp
20 Example of how to use bm::str_sparse_vector<> - succinct container for
21 sorted bit-transposed collection of integers
22
23 \sa bm::sparse_vector
24 \sa bm::sparse_vector_scanner<>::lower_bound
25*/
26
27/*! \file svsample07.cpp
28 \brief Example: sparse_vector<> lower bound search
29*/
30
31#include <iostream>
32#include <vector>
33#include <chrono>
34#include <algorithm>
35#include <random>
36#include <algorithm>
37#include <stdexcept>
38
39#include "bm.h"
40#include "bmsparsevec.h"
41#include "bmsparsevec_algo.h"
42
43
44using namespace std;
45
47
48// generate collection of strings from integers and shuffle it
49//
50static
51void generate_set(vector<unsigned>& vec)
52{
53 const unsigned max_coll = 50000;
54
55 vec.resize(0);
56 for (unsigned i = 10; i < max_coll; i += rand() % 3)
57 {
58 vec.emplace_back(i);
59 } // for i
60
61 // shuffle the data set
62 //
63 std::random_device rd;
64 std::mt19937 g(rd());
65 std::shuffle(vec.begin(), vec.end(), g);
66}
67
68// insertion sort takes data from unsorted vector places it into sparse vector
69// maintaining correct sorted order (for fast search)
70//
71static
72void insertion_sort(sparse_vector_u32& sv, const vector<unsigned>& vec)
73{
74 // scanner object is re-used throught the processing
75 //
78
79 for (const unsigned u : vec)
80 {
81 bool found = scanner.lower_bound(sv, u, pos);
82 (void)found; // just to silence the unused variable warning
83
84 sv.insert(pos, u);
85
86 } // for u
87}
88
89
90int main(void)
91{
92 try
93 {
95 vector<unsigned> vec;
96 generate_set(vec);
97
98 insertion_sort(sv, vec);
99
100 sv.optimize(); // mmemory optimization after active editing
101
102 // validate the results to match STL sort
103 std::sort(vec.begin(), vec.end());
104 {
105 vector<unsigned>::const_iterator vit = vec.begin();
106 sparse_vector_u32::const_iterator it = sv.begin();
107 sparse_vector_u32::const_iterator it_end = sv.end();
108 for (; it != it_end; ++it, ++vit)
109 {
110 unsigned u = *it;
111 if (*vit != u)
112 {
113 cerr << "Mismatch at:" << u << "!=" << *vit << endl;
114 return 1;
115 }
116 } // for
117 }
118 cout << "Sort validation Ok." << endl;
119 }
120 catch(std::exception& ex)
121 {
122 std::cerr << ex.what() << std::endl;
123 return 1;
124 }
125
126 return 0;
127}
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.
algorithms for sparse_vector scan/search
bool lower_bound(const SV &sv, const typename SV::value_type val, typename SV::size_type &pos)
lower bound search for an array position
sparse vector with runtime compression using bit transposition method
Definition bmsparsevec.h:82
const_iterator end() const BMNOEXCEPT
Provide const iterator access to the end
void insert(size_type idx, value_type v)
insert specified element into container
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 plains
static void insertion_sort(sparse_vector_u32 &sv, const vector< unsigned > &vec)
bm::sparse_vector< bm::id_t, bm::bvector<> > sparse_vector_u32
static void generate_set(vector< unsigned > &vec)
int main(void)