BitMagic-C++: svsample07.cpp Source File

BitMagic-C++

svsample07.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2019 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 For 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
44 using namespace std;
45
46 typedef bm::sparse_vector<bm::id_t, bm::bvector<> > sparse_vector_u32;
47
48 // generate collection of strings from integers and shuffle it
49 //
50 static
51 void 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 //
71 static
72 void insertion_sort(sparse_vector_u32& sv, const vector<unsigned>& vec)
73 {
74 // scanner object is re-used throught the processing
75 //
76 bm::sparse_vector_scanner<sparse_vector_u32> scanner;
77 sparse_vector_u32::size_type pos;
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
90 int main(void)
91 {
92 try
93 {
94 sparse_vector_u32 sv;
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 }
bmsparsevec_algo.h
Algorithms for bm::sparse_vector.
bm::sparse_vector::optimize
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
Definition: bmsparsevec.h:1728
bm::sparse_vector::const_iterator
friend const_iterator
Definition: bmsparsevec.h:348
bm::sparse_vector
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
bmsparsevec.h
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
bm::sparse_vector_scanner
algorithms for sparse_vector scan/search
Definition: bmsparsevec_algo.h:481
bm::sparse_vector::end
const_iterator end() const BMNOEXCEPT
Provide const iterator access to the end
Definition: bmsparsevec.h:493
generate_set
static void generate_set(vector< unsigned > &vec)
Definition: svsample07.cpp:51
insertion_sort
static void insertion_sort(sparse_vector_u32 &sv, const vector< unsigned > &vec)
Definition: svsample07.cpp:72
bm::sparse_vector::begin
const_iterator begin() const BMNOEXCEPT
Provide const iterator access to container content
Definition: bmsparsevec.h:1900
sparse_vector_u32
bm::sparse_vector< bm::id_t, bm::bvector<> > sparse_vector_u32
Definition: svsample07.cpp:46
bm::sparse_vector::insert
void insert(size_type idx, value_type v)
insert specified element into container
Definition: bmsparsevec.h:1520
main
int main(void)
Definition: svsample07.cpp:90
bm::sparse_vector_scanner::lower_bound
bool lower_bound(const SV &sv, const typename SV::value_type val, typename SV::size_type &pos)
lower bound search for an array position
Definition: bmsparsevec_algo.h:1721
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::sparse_vector< unsigned, bm::bvector<> >::size_type
bvector_type::size_type size_type
Definition: bmsparsevec.h:87