Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "search.h"
- #include <iostream>
- void searchIndex(const vector<string> &patterns, const string &txt,
- const vector<int> &sa, function<void(const string &)> reporter)
- {
- // auto lambda1 = [txt](const int &i, const string &pat)
- // {
- // cout << "1 " << i << endl;
- // return txt.compare(i, pat.size(), pat) < 0;
- // };
- // auto lambda2 = [txt](const string &pat, const int &i)
- // {
- // cout << "2 " << i << endl;
- // return txt.compare(i, pat.size(), pat) > 0;
- // };
- // set<int> occurrences;
- // for (const string &pattern : patterns)
- // {
- // auto lower = lower_bound(sa.begin(), sa.end(), pattern, lambda1);
- // auto upper = upper_bound(lower, sa.end(), pattern, lambda2);
- // for (auto i = lower; i != upper; ++i)
- // occurrences.insert(*i);
- // }
- set<int> occ;
- for(int i = 0; i < patterns.size(); i++){
- for(int j = 0; j < sa.size(); j++){
- cout << "sa " << sa[j];
- cout << " slice ";
- for(int k=sa[j]; k<sa[j]+patterns[i].size();k++) cout << txt[k];
- cout << endl;
- if (txt.compare(sa[j], patterns[i].size(), patterns[i]) == 0){
- occ.insert(sa[j]);
- cout << "patS " << patterns[i].size() << endl;
- cout << "occ " << sa[j] << endl;
- }
- }
- }
- for(int i=0; i<patterns.size();i++) cout << endl << patterns[i] << endl << endl;
- const int size = txt.size();
- int previousLine = -1;
- for (const int &pos : occ)
- {
- int start, end;
- for (start = pos; start > 0 && txt[start - 1] != '\n'; --start)
- ;
- if (start == previousLine)
- continue;
- for (end = pos; end < size && txt[end] != '\n'; ++end)
- ;
- reporter(txt.substr(start, end - start));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement