]> granicus.if.org Git - postgresql/blob - contrib/pg_trgm/trgm.h
Modify LOOPBYTE/LOOPBIT macros to be more logical; rather than have the
[postgresql] / contrib / pg_trgm / trgm.h
1 #ifndef __TRGM_H__
2 #define __TRGM_H__
3
4 #include "postgres.h"
5
6 #include "access/gist.h"
7 #include "access/itup.h"
8 #include "utils/builtins.h"
9 #include "storage/bufpage.h"
10
11 /* options */
12 #define LPADDING                2
13 #define RPADDING                1
14 #define KEEPONLYALNUM
15 #define IGNORECASE
16 #define DIVUNION
17
18
19 typedef char trgm[3];
20
21 #define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
22 #define CMPPCHAR(a,b,i)  CMPCHAR( *(((char*)(a))+i), *(((char*)(b))+i) )
23 #define CMPTRGM(a,b) ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
24
25 #define CPTRGM(a,b) do {                                \
26         *(((char*)(a))+0) = *(((char*)(b))+0);  \
27         *(((char*)(a))+1) = *(((char*)(b))+1);  \
28         *(((char*)(a))+2) = *(((char*)(b))+2);  \
29 } while(0);
30
31 #define TRGMINT(a) ( (*(((char*)(a))+2)<<16)+(*(((char*)(a))+1)<<8)+*(((char*)(a))+0) )
32
33 typedef struct
34 {
35         int32           vl_len_;                /* varlena header (do not touch directly!) */
36         uint8           flag;
37         char            data[1];
38 }       TRGM;
39
40 #define TRGMHDRSIZE               (VARHDRSZ + sizeof(uint8))
41
42 /* gist */
43 #define BITBYTE 8
44 #define SIGLENINT  3                    /* >122 => key will toast, so very slow!!! */
45 #define SIGLEN  ( sizeof(int)*SIGLENINT )
46
47 #define SIGLENBIT (SIGLEN*BITBYTE - 1)  /* see makesign */
48
49 typedef char BITVEC[SIGLEN];
50 typedef char *BITVECP;
51
52 #define LOOPBYTE \
53                         for(i=0;i<SIGLEN;i++)
54
55 #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
56 #define GETBITBYTE(x,i) ( ((char)(x)) >> i & 0x01 )
57 #define CLRBIT(x,i)   GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
58 #define SETBIT(x,i)   GETBYTE(x,i) |=  ( 0x01 << ( (i) % BITBYTE ) )
59 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
60
61 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
62 #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
63
64 #define ARRKEY                  0x01
65 #define SIGNKEY                 0x02
66 #define ALLISTRUE               0x04
67
68 #define ISARRKEY(x) ( ((TRGM*)x)->flag & ARRKEY )
69 #define ISSIGNKEY(x)    ( ((TRGM*)x)->flag & SIGNKEY )
70 #define ISALLTRUE(x)    ( ((TRGM*)x)->flag & ALLISTRUE )
71
72 #define CALCGTSIZE(flag, len) ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
73 #define GETSIGN(x)              ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )
74 #define GETARR(x)               ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
75 #define ARRNELEM(x) ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
76
77 extern float4 trgm_limit;
78
79 TRGM       *generate_trgm(char *str, int slen);
80 float4          cnt_sml(TRGM * trg1, TRGM * trg2);
81
82 #endif