BitMagic-C++
strsvsample02.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 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 strsvsample02.cpp
20 Example of how to use bm::str_sparse_vector<> - succinct container for
21 bit-transposed string collections
22
23 \sa bm::str_sparse_vector
24 \sa bm::sparse_vector_scanner
25*/
26
27/*! \file strsvsample02.cpp
28 \brief Example: str_sparse_vector<> insertion sort example
29*/
30
31#include <iostream>
32#include <string>
33#include <vector>
34#include <random>
35#include <algorithm>
36
37#include "bm.h"
38#include "bmstrsparsevec.h"
39#include "bmsparsevec_algo.h"
40#include "bmundef.h" /* clear the pre-proc defines from BM */
41
42using namespace std;
43
45
46// define the sparse vector type for 'char' type using bvector as
47// a container of bits for bit-transposed planes
48// 32 - is maximum string length for this container.
49// Memory allocation is dynamic using sparse techniques, so this number
50// just defines the max capacity.
51//
53
54
55// generate collection of strings from integers and shuffle it
56//
57static
58void generate_string_set(vector<string>& str_vec)
59{
60 const unsigned max_coll = 50000;
61
62 str_vec.resize(0);
63 string str;
64 for (unsigned i = 10; i < max_coll; i += unsigned(rand() % 3))
65 {
66 str = to_string(i);
67 str_vec.emplace_back(str);
68 } // for i
69
70 // shuffle the data set
71 //
72 std::random_device rd;
73 std::mt19937 g(rd());
74 std::shuffle(str_vec.begin(), str_vec.end(), g);
75}
76
77// insertion sort takes data from unsorted vector places it into sparse vector
78// maintaining correct sorted order (for fast search)
79//
80static
81void insertion_sort(str_sv_type& str_sv, const vector<string>& str_vec)
82{
83 // scanner object is re-used throught the processing
84 //
86
87 for (const string& s : str_vec)
88 {
89 const char* cs = s.c_str();
91 bool found = scanner.lower_bound_str(str_sv, cs, pos);
92 (void)found; // just to silence the unused variable warning
93
94 str_sv.insert(pos, cs);
95
96 } // for s
97}
98
99
100int main(void)
101{
102 try
103 {
104 str_sv_type str_sv;
105
106 vector<string> str_vec;
107 generate_string_set(str_vec);
108
109 insertion_sort(str_sv, str_vec);
110
111 {
113 str_sv.optimize(tb);
114 }
115
116 // validate the results to match STL sort
117 std::sort(str_vec.begin(), str_vec.end());
118 {
119 vector<string>::const_iterator sit = str_vec.begin();
121 str_sv_type::const_iterator it_end = str_sv.end();
122 for (; it != it_end; ++it, ++sit)
123 {
124 string s = *it;
125 if (*sit != s)
126 {
127 cerr << "Mismatch at:" << s << "!=" << *sit << endl;
128 return 1;
129 }
130 } // for
131 }
132 cout << "Sort validation Ok." << endl;
133 }
134 catch(std::exception& ex)
135 {
136 std::cerr << ex.what() << std::endl;
137 return 1;
138 }
139
140
141 return 0;
142}
143
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
Algorithms for bm::sparse_vector.
string sparse vector based on bit-transposed matrix
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
algorithms for sparse_vector scan/search
bool lower_bound_str(const SV &sv, const value_type *str, size_type &pos)
lower bound search for an array position
Const iterator to do quick traverse of the sparse vector.
succinct sparse vector for strings with compression using bit-slicing ( transposition) method
void insert(size_type idx, const value_type *str)
insert the specified element
const_iterator end() const BMNOEXCEPT
Provide const iterator access to the end
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename str_sparse_vector< CharType, BV, STR_SIZE >::statistics *stat=0)
run memory optimization for all vector planes
const_iterator begin() const BMNOEXCEPT
Provide const iterator access to container content
bvector_type::size_type size_type
bm::str_sparse_vector< char, bvector_type, 32 > str_sv_type
bm::bvector bvector_type
int main(void)
static void generate_string_set(vector< string > &str_vec)
static void insertion_sort(str_sv_type &str_sv, const vector< string > &str_vec)