Lolly 1.4.28
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
string_searcher_rep Class Reference

#include <fast_search.hpp>

Inheritance diagram for string_searcher_rep:
Inheritance graph
[legend]
Collaboration diagram for string_searcher_rep:
Collaboration graph
[legend]

Public Member Functions

 string_searcher_rep (string s)
 
string get_string ()
 
int search_next (string what, int pos)
 
array< int > search_all (string what)
 

Private Member Functions

array< int > search_sub (string what)
 
- Private Member Functions inherited from concrete_struct
 concrete_struct ()
 Default constructor for the concrete object. Increments the reference count.
 
virtual ~concrete_struct ()
 Virtual destructor for the concrete object. Decrements the reference count.
 

Private Attributes

string s
 
array< hashmap< int, array< int > > > a
 
- Private Attributes inherited from concrete_struct
int ref_count
 The reference count for the concrete object.
 

Friends

class string_searcher
 
void get_longest_common (string s1, string s2, int &b1, int &e1, int &b2, int &e2)
 

Detailed Description

Definition at line 20 of file fast_search.hpp.

Constructor & Destructor Documentation

◆ string_searcher_rep()

string_searcher_rep::string_searcher_rep ( string s)

Definition at line 47 of file fast_search.cpp.

47 : s (s2), a () {
48 // NOTE: a[i] contains a hashmap with all associations c :-> j,
49 // where c is the hash code of the substring s (j, j + 2^i).
50 int i, n= N (s);
51 array<int> codes (n);
52 for (i= 0; i < n; i++)
53 codes[i]= (int) (unsigned int) (unsigned char) s[i];
54 int l= 1;
55 while (l <= n) {
57 for (i= 0; i + l <= n; i++) {
58 int c= codes[i];
59 if (!h->contains (c)) h (c)= array<int> ();
60 h (c) << i;
61 }
62 a << h;
63 int d= 2 * l;
64 for (i= 0; i + d <= n; i++)
65 codes[i]= hash_combine (codes[i], codes[i + l], l);
66 l= d;
67 }
68}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
The list class represents a linked list.
Definition list.hpp:48
array< hashmap< int, array< int > > > a
static int hash_combine(int c1, int c2, int l)

Member Function Documentation

◆ search_sub()

array< int > string_searcher_rep::search_sub ( string what)
private

Definition at line 80 of file fast_search.cpp.

80 {
81 if (N (what) == 0) {
83 for (int i= 0; i <= N (s); i++)
84 r << i;
85 return r;
86 }
87 int k= 1, l= 0;
88 while ((k << 1) <= N (what)) {
89 k<<= 1;
90 l++;
91 }
92 int code= fast_hash (what (0, k));
93 if (!a[l]->contains (code)) return array<int> ();
94 else return a[l][code];
95}
bool contains(T a, array< T > b)
Check if an array contains a specified element.
static int fast_hash(string s)

◆ get_string()

string string_searcher_rep::get_string ( )

Definition at line 71 of file fast_search.cpp.

71 {
72 return s;
73}

◆ search_next()

int string_searcher_rep::search_next ( string what,
int pos )

Definition at line 98 of file fast_search.cpp.

98 {
100 for (int i= 0; i < N (ps); i++) {
101 int next= ps[i];
102 if (next >= pos && test (s, next, what)) return next;
103 }
104 return -1;
105}
bool test(string s, int i, const char *test)
Definition analyze.cpp:542
array< int > search_sub(string what)

◆ search_all()

array< int > string_searcher_rep::search_all ( string what)

Definition at line 108 of file fast_search.cpp.

108 {
109 // NOTE: There are better algorithms based on generalized suffix trees.
110 // However, in practice, our approach based on hashtables might be faster
113 for (int i= 0; i < N (ps); i++) {
114 int pos= ps[i];
115 if (test (s, pos, what)) r << pos;
116 }
117 return r;
118}

Friends And Related Symbol Documentation

◆ string_searcher

Definition at line 30 of file fast_search.hpp.

◆ get_longest_common

void get_longest_common ( string s1,
string s2,
int & b1,
int & e1,
int & b2,
int & e2 )
friend

Definition at line 125 of file fast_search.cpp.

125 {
126 // NOTE: There are better algorithms based on generalized suffix trees.
127 b1= e1= b2= e2 = 0;
128 int bestl= 0;
131 for (int i= min (N (ss1->a), N (ss2->a)) - 1; i >= 0; i--) {
135 while (it->busy ()) {
136 int h = it->next ();
137 array<int> ps1= a1[h];
138 array<int> ps2= a2[h];
139 if (N (ps1) == 0 || N (ps2) == 0) continue;
140 for (int j1= 0; j1 < N (ps1); j1++) {
141 int pos1= ps1[j1];
142 if (b1 <= pos1 && pos1 < e1) continue;
143 for (int j2= 0; j2 < N (ps2); j2++) {
144 int pos2= ps2[j2];
145 int bb1= pos1, ee1= pos1, bb2= pos2, ee2= pos2;
146 while (bb1 > 0 && bb2 > 0 && s1[bb1 - 1] == s2[bb2 - 1]) {
147 bb1--;
148 bb2--;
149 }
150 while (ee1 < N (s1) && ee2 < N (s2) && s1[ee1] == s2[ee2]) {
151 ee1++;
152 ee2++;
153 }
154 if (ee1 - bb1 > bestl ||
155 (ee1 - bb1 == bestl && (bb1 < b1 || (bb1 == b1 && bb2 < b2)))) {
156 b1 = bb1;
157 e1 = ee1;
158 b2 = bb2;
159 e2 = ee2;
160 bestl= e1 - b1;
161 }
162 }
163 }
164 }
165 }
166}
blackbox b1
blackbox b2
iterator< T > iterate(hashmap< T, U > h)
Generates an iterator for a container of type hashmap<T, U>.
Definition iterator.ipp:166
SI min(SI i, SI j)
Returns the minimum of two signed integers.
Definition minmax.hpp:27

Member Data Documentation

◆ s

string string_searcher_rep::s
private

Definition at line 21 of file fast_search.hpp.

◆ a

array<hashmap<int, array<int> > > string_searcher_rep::a
private

Definition at line 22 of file fast_search.hpp.


The documentation for this class was generated from the following files: