28 #define GETENTRY(vec,pos) ((TRGM *) DatumGetPointer((vec)->vector[(pos)].key))
44 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
45 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
46 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
47 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
48 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
49 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
50 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
51 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
52 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
53 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
54 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
55 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
56 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
57 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
58 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
59 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
87 for (k = 0; k < len; k++)
89 CPTRGM(((
char *) &tmp), ptr + k);
121 if ((sign[i] & 0xff) != 0xff)
171 for (k = 0; k < len; k++)
173 CPTRGM(((
char *) &tmp), ptr + k);
213 memcmp((
char *) cache->
query, (
char *) query, querysize) != 0)
228 elog(
ERROR,
"cannot handle ~~* with case-sensitive trigrams");
237 elog(
ERROR,
"cannot handle ~* with case-sensitive trigrams");
242 &graph, fcinfo->flinfo->fn_mcxt);
251 elog(
ERROR,
"unrecognized strategy number: %d", strategy);
256 qtrgsize = qtrg ?
VARSIZE(qtrg) : 0;
267 memcpy((
char *) newcache->
query, (
char *) query, querysize);
272 memcpy((
char *) newcache->
trigrams, (
char *) qtrg, qtrgsize);
278 newcache->
graph = graph;
282 fcinfo->flinfo->fn_extra = (
void *) newcache;
307 res = (*(
int *) &tmpsml == *(
int *) &nlimit || tmpsml > nlimit);
321 res = (((((
float8) count) / ((
float8) len))) >= nlimit);
326 elog(
ERROR,
"cannot handle ~~* with case-sensitive trigrams");
354 for (k = 0; k < len; k++)
356 CPTRGM(((
char *) &tmp), ptr + k);
367 elog(
ERROR,
"cannot handle ~* with case-sensitive trigrams");
406 check = (
bool *)
palloc(len *
sizeof(
bool));
407 for (k = 0; k < len; k++)
409 CPTRGM(((
char *) &tmp), ptr + k);
423 elog(
ERROR,
"unrecognized strategy number: %d", strategy);
444 char *cache = (
char *) fcinfo->flinfo->fn_extra;
451 memcmp(cache, query, querysize) != 0)
461 memcpy(newcache, query, querysize);
466 fcinfo->flinfo->fn_extra = newcache;
496 res = (len == 0) ? -1.0 : 1.0 - ((
float8) count) / ((
float8) len);
500 elog(
ERROR,
"unrecognized strategy number: %d", strategy);
530 CPTRGM(((
char *) &tmp), ptr + i);
550 for (i = 0; i < len; i++)
565 memcpy((
void *)
GETSIGN(result), (
void *) base,
sizeof(
BITVEC));
617 for (i = 0; i < lena; i++)
618 if (
CMPTRGM(ptra + i, ptrb + i))
649 diff = (
unsigned char) (a[i] ^ b[i]);
685 char *cache = (
char *) fcinfo->flinfo->fn_extra;
694 VARSIZE(cachedVal) != newvalsize ||
695 memcmp(cachedVal, newval, newvalsize) != 0)
706 memcpy(cachedVal, newval, newvalsize);
710 fcinfo->flinfo->fn_extra = newcache;
722 *penalty =
hemdist(origval, newval);
744 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
814 if (size_waste > waste)
824 if (seed_1 == 0 || seed_2 == 0)
838 if (cache[seed_1].allistrue)
851 if (cache[seed_2].allistrue)
873 costvector[j - 1].
pos = j;
874 size_alpha =
hemdistcache(&(cache[seed_1]), &(cache[j]));
875 size_beta =
hemdistcache(&(cache[seed_2]), &(cache[j]));
876 costvector[j - 1].
cost = abs(size_alpha - size_beta);
880 for (k = 0; k < maxoff; k++)
882 j = costvector[k].
pos;
889 else if (j == seed_2)
896 if (
ISALLTRUE(datum_l) || cache[j].allistrue)
898 if (
ISALLTRUE(datum_l) && cache[j].allistrue)
908 if (
ISALLTRUE(datum_r) || cache[j].allistrue)
910 if (
ISALLTRUE(datum_r) && cache[j].allistrue)
922 if (
ISALLTRUE(datum_l) || cache[j].allistrue)
931 union_l[
i] |= ptr[
i];
938 if (
ISALLTRUE(datum_r) || cache[j].allistrue)
947 union_r[
i] |= ptr[
i];
#define PG_RETURN_POINTER(x)
bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check)
static int hemdist(TRGM *a, TRGM *b)
#define RegExpStrategyNumber
static int32 cnt_sml_sign_common(TRGM *qtrg, BITVECP sign)
TRGM * generate_wildcard_trgm(const char *str, int slen)
#define SimilarityStrategyNumber
#define PointerGetDatum(X)
#define DistanceStrategyNumber
PG_FUNCTION_INFO_V1(gtrgm_in)
TRGM * createTrgmNFA(text *text_re, Oid collation, TrgmPackedGraph **graph, MemoryContext rcontext)
#define RegExpICaseStrategyNumber
#define WordDistanceStrategyNumber
#define PG_RETURN_FLOAT8(x)
double similarity_threshold
static int hemdistcache(CACHESIGN *a, CACHESIGN *b)
Datum gtrgm_union(PG_FUNCTION_ARGS)
static int comparecost(const void *a, const void *b)
float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact)
#define MemSet(start, val, len)
static void fillcache(CACHESIGN *item, TRGM *key)
#define PG_GETARG_POINTER(n)
double word_similarity_threshold
bool * trgm_presence_map(TRGM *query, TRGM *key)
Datum gtrgm_same(PG_FUNCTION_ARGS)
static const uint8 number_of_ones[256]
#define PG_GET_COLLATION()
static int32 unionkey(BITVECP sbase, TRGM *add)
Datum gtrgm_distance(PG_FUNCTION_ARGS)
void pfree(void *pointer)
Datum gtrgm_consistent(PG_FUNCTION_ARGS)
static void makesign(BITVECP sign, TRGM *a)
#define LikeStrategyNumber
#define FirstOffsetNumber
bool trgm_contained_by(TRGM *trg1, TRGM *trg2)
Datum gtrgm_out(PG_FUNCTION_ARGS)
#define PG_RETURN_BOOL(x)
#define GETENTRY(vec, pos)
#define PG_RETURN_DATUM(x)
Datum gtrgm_penalty(PG_FUNCTION_ARGS)
Datum gtrgm_picksplit(PG_FUNCTION_ARGS)
#define WordSimilarityStrategyNumber
static int hemdistsign(BITVECP a, BITVECP b)
#define gistentryinit(e, k, r, pg, o, l)
Datum gtrgm_decompress(PG_FUNCTION_ARGS)
TRGM * generate_trgm(char *str, int slen)
#define OffsetNumberNext(offsetNumber)
#define PG_GETARG_UINT16(n)
#define DatumGetPointer(X)
#define PG_GETARG_TEXT_P(n)
void * MemoryContextAlloc(MemoryContext context, Size size)
#define SET_VARSIZE(PTR, len)
#define ILikeStrategyNumber
#define qsort(a, b, c, d)
Datum gtrgm_in(PG_FUNCTION_ARGS)
static int32 sizebitvec(BITVECP sign)
Datum gtrgm_compress(PG_FUNCTION_ARGS)