|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <sdsl/suffix_arrays.hpp> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +using namespace sdsl; |
| 8 | + |
| 9 | +//routine to save a vector in different formats, see lower implementations |
| 10 | +template<class INT_VECTOR, uint8_t num_byte> |
| 11 | +void saveVector(const INT_VECTOR &v, const char *dest); |
| 12 | + |
| 13 | +//main function to generate MTF of BWT of an integer vector. |
| 14 | +// CSA_WT: used wavelet - tree - based suffix array implementation |
| 15 | +// INT_VECTOR: used integer vector for extracting BWT |
| 16 | +// num_byte: value indicating how result has to be opened / saved |
| 17 | +// srcfile: file from which to generate |
| 18 | +// destfile: file where to save result |
| 19 | +// tmpdir: directory used for temporary results |
| 20 | +// conf_bwt_key: key what is able to fetch bwt after suffix array construction |
| 21 | +template<class CSA_WT, class INT_VECTOR, uint8_t num_byte> |
| 22 | +void gen_bwt_mtf(const char *srcfile, const char *destfile, const char *tmpdir, |
| 23 | + const char *conf_bwt_key) { |
| 24 | + //utility for CSA generation |
| 25 | + cache_config cc(false, tmpdir, "gen_bwt_mtf_"); |
| 26 | + INT_VECTOR bwt; |
| 27 | + |
| 28 | + //create suffix array |
| 29 | + CSA_WT wt; |
| 30 | + construct(wt, srcfile, cc, num_byte); |
| 31 | + |
| 32 | + //compute alphabet table from suffix array |
| 33 | + std::vector<uint64_t> alph_tbl( wt.sigma ); |
| 34 | + for (uint64_t i = 0; i < wt.sigma; i++) { |
| 35 | + alph_tbl.push_back( wt.comp2char[i] ); |
| 36 | + } |
| 37 | + |
| 38 | + //fetch bwt |
| 39 | + load_from_file(bwt, cache_file_name(conf_bwt_key, cc)); |
| 40 | + |
| 41 | + //create mtf |
| 42 | + for (uint64_t i = 0; i < bwt.size(); i++) { |
| 43 | + uint64_t c = bwt[i]; |
| 44 | + //find c in alphabet table and move it to front |
| 45 | + uint64_t j = 0; |
| 46 | + do { |
| 47 | + uint64_t tmp = alph_tbl[j]; |
| 48 | + alph_tbl[j++] = c; |
| 49 | + c = tmp; |
| 50 | + } while (c != alph_tbl.front()); |
| 51 | + //and write it's index to mtf transform of bwt |
| 52 | + bwt[i] = j-1; |
| 53 | + } |
| 54 | + |
| 55 | + //save everything |
| 56 | + saveVector<INT_VECTOR, num_byte>( bwt, destfile ); |
| 57 | + |
| 58 | + //and free resources |
| 59 | + util::delete_all_files(cc.file_map); |
| 60 | +} |
| 61 | + |
| 62 | +//functions for saving an integer vector in different formats |
| 63 | +//generic version (raw output) |
| 64 | +template<class INT_VECTOR, uint8_t num_byte> |
| 65 | +void saveVector(const INT_VECTOR &v, const char *dest) { |
| 66 | + std::ofstream out(dest); |
| 67 | + out.write((char *)v.data(), num_byte * v.size()); |
| 68 | +} |
| 69 | +//serialization of integer vector |
| 70 | +template<> |
| 71 | +void saveVector<int_vector<>, 0>(const int_vector<> &v, const char *dest) { |
| 72 | + store_to_file(v, dest); |
| 73 | +} |
| 74 | +//decimal digits |
| 75 | +template<> |
| 76 | +void saveVector<int_vector<>, 'd'>(const int_vector<> &v, const char *dest) { |
| 77 | + std::ofstream out(dest); |
| 78 | + if (v.size()) out << v[0]; |
| 79 | + for (uint64_t i = 1; i < v.size(); i++) { |
| 80 | + out << " " << v[i]; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +//main function |
| 85 | +int main(int argc, char* argv[]) { |
| 86 | + if (argc != 5) { |
| 87 | + std::cout<<"Usage: input_file output_file temp_dir num_byte" << std::endl; |
| 88 | + return 1; |
| 89 | + } |
| 90 | + std::cout << "Calculate MTF Transform of BWT of " << argv[1] |
| 91 | + << " and store it to " << argv[2] << std::endl; |
| 92 | + |
| 93 | + typedef csa_wt<> csa_wt_byte; |
| 94 | + typedef csa_wt<wt_int<>, 64, 64, sa_order_sa_sampling<>, int_vector<>, int_alphabet<>> csa_wt_int; |
| 95 | + |
| 96 | + switch (argv[4][0]) { |
| 97 | + case 'd': //decimal digits |
| 98 | + gen_bwt_mtf<csa_wt_int, int_vector<>, 'd'>(argv[1], argv[2], argv[3], conf::KEY_BWT_INT); |
| 99 | + return 0; |
| 100 | + case '0': //serialized integer vector |
| 101 | + gen_bwt_mtf<csa_wt_int, int_vector<>, 0>(argv[1], argv[2], argv[3], conf::KEY_BWT_INT); |
| 102 | + return 0; |
| 103 | + case '1': //byte integer vector |
| 104 | + gen_bwt_mtf<csa_wt_byte, int_vector<8>, 1>(argv[1], argv[2], argv[3], conf::KEY_BWT); |
| 105 | + return 0; |
| 106 | + case '2': //2 byte integer vector |
| 107 | + gen_bwt_mtf<csa_wt_int, int_vector<>, 2>(argv[1], argv[2], argv[3], conf::KEY_BWT_INT); |
| 108 | + return 0; |
| 109 | + case '4': //4 byte integer vector |
| 110 | + gen_bwt_mtf<csa_wt_int, int_vector<>, 4>(argv[1], argv[2], argv[3], conf::KEY_BWT_INT); |
| 111 | + return 0; |
| 112 | + case '8': //8 byte integer vector |
| 113 | + gen_bwt_mtf<csa_wt_int, int_vector<>, 8>(argv[1], argv[2], argv[3], conf::KEY_BWT_INT); |
| 114 | + return 0; |
| 115 | + default: |
| 116 | + std::cout << "Illegal num_byte, allowed are 'd', 0, 1, 2, 4, 8" << std::endl; |
| 117 | + return 1; |
| 118 | + } |
| 119 | +} |
0 commit comments