BitMagic-C++
strsvsample02.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 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 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 
41 
42 using 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 //
57 static
58 void 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 += 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 //
80 static
81 void 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 
100 int 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();
120  str_sv_type::const_iterator it = str_sv.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 
bmsparsevec_algo.h
Algorithms for bm::sparse_vector.
BM_DECLARE_TEMP_BLOCK
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
bm::str_sparse_vector::begin
const_iterator begin() const BMNOEXCEPT
Provide const iterator access to container content
Definition: bmstrsparsevec.h:1700
bm::sparse_vector_scanner
algorithms for sparse_vector scan/search
Definition: bmsparsevec_algo.h:481
main
int main(void)
Definition: strsvsample02.cpp:100
generate_string_set
static void generate_string_set(vector< string > &str_vec)
Definition: strsvsample02.cpp:58
bm::bvector<>
bm::sparse_vector_scanner::lower_bound_str
bool lower_bound_str(const SV &sv, const typename SV::value_type *str, typename SV::size_type &pos)
lower bound search for an array position
Definition: bmsparsevec_algo.h:1841
bm::str_sparse_vector
sparse vector for strings with compression using bit transposition method
Definition: bmstrsparsevec.h:56
str_sv_type
bm::str_sparse_vector< char, bvector_type, 32 > str_sv_type
Definition: strsvsample02.cpp:52
bmstrsparsevec.h
string sparse vector based on bit-transposed matrix
bm::str_sparse_vector::end
const_iterator end() const BMNOEXCEPT
Provide const iterator access to the end
Definition: bmstrsparsevec.h:709
bm::str_sparse_vector::size_type
bvector_type::size_type size_type
Definition: bmstrsparsevec.h:63
bm::str_sparse_vector::const_iterator
Const iterator to do quick traverse of the sparse vector.
Definition: bmstrsparsevec.h:167
bvector_type
bm::bvector bvector_type
Definition: strsvsample02.cpp:44
bm::str_sparse_vector::insert
void insert(size_type idx, const value_type *str)
insert the specified element
Definition: bmstrsparsevec.h:1169
bm::str_sparse_vector::optimize
void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename str_sparse_vector< CharType, BV, MAX_STR_SIZE >::statistics *stat=0)
run memory optimization for all vector plains
Definition: bmstrsparsevec.h:1323
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
insertion_sort
static void insertion_sort(str_sv_type &str_sv, const vector< string > &str_vec)
Definition: strsvsample02.cpp:81