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#include "bmundef.h" /* clear the pre-proc defines from BM */
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.bfind(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();
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.
pre-processor un-defines to avoid global space pollution (internal)
algorithms for sparse_vector scan/search
bool bfind(const SV &sv, const value_type val, size_type &pos)
binary search for position in the sorted sparse vector
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
Definition: bmsparsevec.h:559
void insert(size_type idx, value_type v)
insert specified element into container
Definition: bmsparsevec.h:1860
const_iterator begin() const BMNOEXCEPT
Provide const iterator access to container content
Definition: bmsparsevec.h:2256
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
Definition: bmsparsevec.h:2094
static void insertion_sort(sparse_vector_u32 &sv, const vector< unsigned > &vec)
Definition: svsample07.cpp:72
bm::sparse_vector< bm::id_t, bm::bvector<> > sparse_vector_u32
Definition: svsample07.cpp:46
static void generate_set(vector< unsigned > &vec)
Definition: svsample07.cpp:51
int main(void)
Definition: svsample07.cpp:90