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 default initial string length for this container.
49// it is extended automatically based on actual use
50//
52
53
54// generate collection of strings from integers and shuffle it
55//
56static
57void generate_string_set(vector<string>& str_vec)
58{
59 const unsigned max_coll = 50000;
60
61 str_vec.resize(0);
62 string str;
63 for (unsigned i = 10; i < max_coll; i += unsigned(rand() % 3))
64 {
65 str = to_string(i);
66 str_vec.emplace_back(str);
67 } // for i
68
69 // shuffle the data set
70 //
71 std::random_device rd;
72 std::mt19937 g(rd());
73 std::shuffle(str_vec.begin(), str_vec.end(), g);
74}
75
76// insertion sort takes data from unsorted vector places it into sparse vector
77// maintaining correct sorted order (for fast search)
78//
79static
80void insertion_sort(str_sv_type& str_sv, const vector<string>& str_vec)
81{
82 // scanner object is re-used throught the processing
83 //
85
86 for (const string& s : str_vec)
87 {
88 const char* cs = s.c_str();
90 bool found = scanner.lower_bound_str(str_sv, cs, pos);
91 (void)found; // just to silence the unused variable warning
92
93 str_sv.insert(pos, cs);
94
95 } // for s
96}
97
98
99int main(void)
100{
101 try
102 {
103 str_sv_type str_sv;
104
105 vector<string> str_vec;
106 generate_string_set(str_vec);
107
108 insertion_sort(str_sv, str_vec);
109
110 {
112 str_sv.optimize(tb);
113 }
114
115 // validate the results to match STL sort
116 std::sort(str_vec.begin(), str_vec.end());
117 {
118 vector<string>::const_iterator sit = str_vec.begin();
119 str_sv_type::const_iterator it = str_sv.begin();
120 str_sv_type::const_iterator it_end = str_sv.end();
121 for (; it != it_end; ++it, ++sit)
122 {
123 string s = *it;
124 if (*sit != s)
125 {
126 cerr << "Mismatch at:" << s << "!=" << *sit << endl;
127 return 1;
128 }
129 } // for
130 }
131 cout << "Sort validation Ok." << endl;
132 }
133 catch(std::exception& ex)
134 {
135 std::cerr << ex.what() << std::endl;
136 return 1;
137 }
138
139
140 return 0;
141}
142
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
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.
bm::str_sparse_vector< char, bvector_type, 32 > str_sv_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)
bm::bvector bvector_type