pgbitmap
Postgres extension providing a bitmap type
pgbitmap.h
Go to the documentation of this file.
1 
14 #include "postgres.h"
15 #include "funcapi.h"
16 
17 #ifndef BITMAP_DATATYPES
18 
21 #define BITMAP_DATATYPES 1
22 
23 /* Bitmaps will be based on 64-bit integers if pointer types are 64-bit,
24  * unless FORCE_32_BIT is defined.
25  */
26 #ifdef FORCE_32_BIT
27 #undef USE_64_BIT
28 #else
29 #if (SIZEOF_VOID_P == 8)
30 
33 #define USE_64_BIT 1
34 #else
35 #undef USE_64_BIT
36 #endif
37 #endif
38 
39 #ifdef USE_64_BIT
40 
43 #define ELEMBITS 64
44 #else
45 
48 #define ELEMBITS 32
49 #endif
50 
51 //#define BITMAP_DEBUG 1
52 #ifdef BITMAP_DEBUG
53 #define DBG_ELEMS 1
54 #define SETCANARY(b) do { \
55  b->bitset[ARRAYELEMS(b->bitmin, b->bitmax)] = 0; \
56  b->canary = 0; \
57  } while (false)
58 
59 #define CHKCANARY(b) \
60  ((b->bitset[ARRAYELEMS(b->bitmin, b->bitmax)] == 0) && \
61  (b->canary == 0))
62 
63 #ifdef USE_64_BIT
64 #define CANARYELEM uint64 canary;
65 #else
66 #define CANARYELEM uint32 canary;
67 #endif
68 #else
69 #define DBG_ELEMS 0
70 #define SETCANARY(b)
71 #define CHKCANARY(b) true
72 #define CANARYELEM
73 #endif
74 
75 
76 
87 #ifdef USE_64_BIT
88 #define BITZERO(x) ((x) & 0xffffffffffffffc0)
89 #else
90 #define BITZERO(x) ((x) & 0xffffffe0)
91 #endif
92 
100 #ifdef USE_64_BIT
101 #define BITSET_ELEM(x) ((x) >> 6)
102 #else
103 #define BITSET_ELEM(x) ((x) >> 5)
104 #endif
105 
113 #ifdef USE_64_BIT
114 #define BITSET_BIT(x) (x & 0x3f)
115 #else
116 #define BITSET_BIT(x) (x & 0x1f)
117 #endif
118 
128 #ifdef USE_64_BIT
129 #define ARRAYELEMS(min,max) (((max - BITZERO(min)) >> 6) + 1)
130 #else
131 #define ARRAYELEMS(min,max) (((max - BITZERO(min)) >> 5) + 1)
132 #endif
133 
134 
144 #define MIN(a,b) ((a < b)? a: b)
145 
155 #define MAX(a,b) ((a > b)? a: b)
156 
157 #ifdef USE_64_BIT
158 
161 typedef uint64 bm_int;
162 #else
163 
166 typedef uint32 bm_int;
167 #endif
168 
174 typedef struct Bitmap {
175  char vl_len[4];
176  int32 bitmin;
178  int32 bitmax;
180  CANARYELEM
183 } Bitmap;
184 
188 typedef unsigned char boolean;
189 
193 #define DatumGetBitmap(x) ((Bitmap *) PG_DETOAST_DATUM(DatumGetPointer(x)))
194 
198 #define PG_GETARG_BITMAP(x) DatumGetBitmap( \
199  PG_DETOAST_DATUM(PG_GETARG_DATUM(x)))
200 
203 #define PG_RETURN_BITMAP(x) PG_RETURN_POINTER(x)
204 
205 extern bool bitmapTestbit(Bitmap *bitmap, int32 bit);
206 extern Bitmap *bitmapCopy(Bitmap *bitmap);
207 
208 extern Datum bitmap_in(PG_FUNCTION_ARGS);
209 extern Datum bitmap_out(PG_FUNCTION_ARGS);
210 extern Datum bitmap_is_empty(PG_FUNCTION_ARGS);
211 extern Datum bitmap_bits(PG_FUNCTION_ARGS);
212 extern Datum bitmap_new_empty(PG_FUNCTION_ARGS);
213 extern Datum bitmap_new(PG_FUNCTION_ARGS);
214 extern Datum bitmap_bitmin(PG_FUNCTION_ARGS);
215 extern Datum bitmap_bitmax(PG_FUNCTION_ARGS);
216 extern Datum bitmap_setbit(PG_FUNCTION_ARGS);
217 extern Datum bitmap_testbit(PG_FUNCTION_ARGS);
218 extern Datum bitmap_setmin(PG_FUNCTION_ARGS);
219 extern Datum bitmap_setmax(PG_FUNCTION_ARGS);
220 extern Datum bitmap_equal(PG_FUNCTION_ARGS);
221 extern Datum bitmap_nequal(PG_FUNCTION_ARGS);
222 extern Datum bitmap_union(PG_FUNCTION_ARGS);
223 extern Datum bitmap_clearbit(PG_FUNCTION_ARGS);
224 extern Datum bitmap_union(PG_FUNCTION_ARGS);
225 extern Datum bitmap_intersection(PG_FUNCTION_ARGS);
226 extern Datum bitmap_minus(PG_FUNCTION_ARGS);
227 extern Datum bitmap_lt(PG_FUNCTION_ARGS);
228 extern Datum bitmap_le(PG_FUNCTION_ARGS);
229 extern Datum bitmap_gt(PG_FUNCTION_ARGS);
230 extern Datum bitmap_ge(PG_FUNCTION_ARGS);
231 extern Datum bitmap_cmp(PG_FUNCTION_ARGS);
232 
233 
234 #endif
Datum bitmap_ge(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1605
Datum bitmap_in(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1073
Definition: pgbitmap.h:174
Datum bitmap_union(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1635
bm_int bitset[0]
Definition: pgbitmap.h:181
int32 bitmax
Definition: pgbitmap.h:178
Datum bitmap_new_empty(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1252
Datum bitmap_bitmax(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1173
Bitmap * bitmapCopy(Bitmap *bitmap)
Definition: pgbitmap.c:348
Datum bitmap_nequal(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1460
bool bitmapTestbit(Bitmap *bitmap, int32 bit)
Definition: pgbitmap.c:290
Datum bitmap_out(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1096
Datum bitmap_clearbit(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1675
unsigned char boolean
Definition: pgbitmap.h:188
char vl_len[4]
Definition: pgbitmap.h:175
Datum bitmap_le(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1547
Datum bitmap_cmp(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1489
Datum bitmap_bitmin(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1141
Datum bitmap_setmin(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1374
int32 bitmin
Definition: pgbitmap.h:176
Datum bitmap_bits(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1203
Datum bitmap_testbit(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1346
uint64 bm_int
Definition: pgbitmap.h:161
Datum bitmap_new(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1274
Datum bitmap_gt(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1576
Datum bitmap_minus(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1745
Datum bitmap_setbit(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1305
Datum bitmap_intersection(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1707
Datum bitmap_is_empty(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1119
Datum bitmap_equal(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1432
Datum bitmap_lt(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1518
Datum bitmap_setmax(FunctionCallInfo fcinfo)
Definition: pgbitmap.c:1403
struct Bitmap Bitmap