Lolly 1.4.27
Loading...
Searching...
No Matches
Functions
fast_search.cpp File Reference
#include "fast_search.hpp"
#include "analyze.hpp"
#include "iterator.hpp"
Include dependency graph for fast_search.cpp:

Go to the source code of this file.

Functions

static int hash_combine (int c1, int c2, int l)
 
static int fast_hash (string s)
 
void get_longest_common (string s1, string s2, int &b1, int &e1, int &b2, int &e2)
 

Function Documentation

◆ hash_combine()

static int hash_combine ( int c1,
int c2,
int l )
static

Definition at line 21 of file fast_search.cpp.

21 {
22 // FIXME: we should rather compute modulo 2^32 - 1 instead of 2^32,
23 // or modulo some prime number < 2^32, and avoid the cyclicity of period 32
24 int rot= (9 * l) & 31;
25 if (rot == 0) return c1 ^ c2;
26 unsigned int u1= c1;
27 unsigned int u2= c2;
28 unsigned int r2=
29 ((u2 << rot) & 0xffffffff) ^ ((u2 >> (32 - rot)) & 0xffffffff);
30 return (int) (u1 ^ r2);
31}
The list class represents a linked list.
Definition list.hpp:48

◆ fast_hash()

static int fast_hash ( string s)
static

Definition at line 34 of file fast_search.cpp.

34 {
35 unsigned int h= 0;
36 for (int i= N (s) - 1; i >= 0; i--) {
37 h= ((h << 9) & 0xffffffff) ^ ((h >> 23) & 0xffffffff);
38 h= h ^ ((unsigned int) (unsigned char) s[i]);
39 }
40 return (int) h;
41}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170

◆ get_longest_common()

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

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