pgbitmap
Postgres extension providing a bitmap type
pgbitmap.h File Reference

Define bitmap datatypes. More...

#include "postgres.h"
#include "funcapi.h"
Include dependency graph for pgbitmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Bitmap
 

Macros

#define BITMAP_DATATYPES   1
 
#define USE_64_BIT   1
 
#define ELEMBITS   64
 
#define DBG_ELEMS   0
 
#define SETCANARY(b)
 
#define CHKCANARY(b)   true
 
#define CANARYELEM
 
#define BITZERO(x)   ((x) & 0xffffffffffffffc0)
 
#define BITSET_ELEM(x)   ((x) >> 6)
 
#define BITSET_BIT(x)   (x & 0x3f)
 
#define ARRAYELEMS(min, max)   (((max - BITZERO(min)) >> 6) + 1)
 
#define MIN(a, b)   ((a < b)? a: b)
 
#define MAX(a, b)   ((a > b)? a: b)
 
#define DatumGetBitmap(x)   ((Bitmap *) PG_DETOAST_DATUM(DatumGetPointer(x)))
 
#define PG_GETARG_BITMAP(x)
 
#define PG_RETURN_BITMAP(x)   PG_RETURN_POINTER(x)
 

Typedefs

typedef uint64 bm_int
 
typedef struct Bitmap Bitmap
 
typedef unsigned char boolean
 

Functions

bool bitmapTestbit (Bitmap *bitmap, int32 bit)
 
BitmapbitmapCopy (Bitmap *bitmap)
 
Datum bitmap_in (FunctionCallInfo fcinfo)
 
Datum bitmap_out (FunctionCallInfo fcinfo)
 
Datum bitmap_is_empty (FunctionCallInfo fcinfo)
 
Datum bitmap_bits (FunctionCallInfo fcinfo)
 
Datum bitmap_new_empty (FunctionCallInfo fcinfo)
 
Datum bitmap_new (FunctionCallInfo fcinfo)
 
Datum bitmap_bitmin (FunctionCallInfo fcinfo)
 
Datum bitmap_bitmax (FunctionCallInfo fcinfo)
 
Datum bitmap_setbit (FunctionCallInfo fcinfo)
 
Datum bitmap_testbit (FunctionCallInfo fcinfo)
 
Datum bitmap_setmin (FunctionCallInfo fcinfo)
 
Datum bitmap_setmax (FunctionCallInfo fcinfo)
 
Datum bitmap_equal (FunctionCallInfo fcinfo)
 
Datum bitmap_nequal (FunctionCallInfo fcinfo)
 
Datum bitmap_union (FunctionCallInfo fcinfo)
 
Datum bitmap_clearbit (FunctionCallInfo fcinfo)
 
Datum bitmap_intersection (FunctionCallInfo fcinfo)
 
Datum bitmap_minus (FunctionCallInfo fcinfo)
 
Datum bitmap_lt (FunctionCallInfo fcinfo)
 
Datum bitmap_le (FunctionCallInfo fcinfo)
 
Datum bitmap_gt (FunctionCallInfo fcinfo)
 
Datum bitmap_ge (FunctionCallInfo fcinfo)
 
Datum bitmap_cmp (FunctionCallInfo fcinfo)
 

Detailed Description

Define bitmap datatypes.

Author: Marc Munro
Copyright (c) 2020 Marc Munro
License: BSD

Macro Definition Documentation

◆ ARRAYELEMS

#define ARRAYELEMS (   min,
  max 
)    (((max - BITZERO(min)) >> 6) + 1)

Gives the number of array elements in a Bitmap that runs from element min to element max.

Parameters
min
max
Returns
The number of elements in the bitmap.

◆ BITMAP_DATATYPES

#define BITMAP_DATATYPES   1

Prevent this header from being included multiple times.

◆ BITSET_BIT

#define BITSET_BIT (   x)    (x & 0x3f)

Gives the index into bitmasks for the bit specified in x.

Parameters
xThe bit in question
Returns
The bitmask index

◆ BITSET_ELEM

#define BITSET_ELEM (   x)    ((x) >> 6)

Gives the index of the word for a given bit, assuming bitmin is zero.

Parameters
xThe bit in question
Returns
The array index of the bit.

◆ BITZERO

#define BITZERO (   x)    ((x) & 0xffffffffffffffc0)

Gives the bitmask index for the bitzero value of a Bitmap. This is part of the "normalisation" process for bitmap ranges. This process allows unlike bitmaps to be more easily compared by forcing bitmap indexes to be normalised around 32 or 64 bit word boundaries.

Parameters
xThe bitzero value of a bitmap
Returns
The bitmask index representing x.

◆ DatumGetBitmap

#define DatumGetBitmap (   x)    ((Bitmap *) PG_DETOAST_DATUM(DatumGetPointer(x)))

Provide a macro for getting a bitmap datum.

◆ ELEMBITS

#define ELEMBITS   64

Use 64-bit words.

◆ MAX

#define MAX (   a,
 
)    ((a > b)? a: b)

Return the larger of a or b. Note that expressions a and b may be evaluated more than once.

Parameters
a
b
Returns
The smaller value of a or b.

◆ MIN

#define MIN (   a,
 
)    ((a < b)? a: b)

Return the smaller of a or b. Note that expressions a and b may be evaluated more than once.

Parameters
a
b
Returns
The smaller value of a or b.

◆ PG_GETARG_BITMAP

#define PG_GETARG_BITMAP (   x)
Value:
PG_DETOAST_DATUM(PG_GETARG_DATUM(x)))
#define DatumGetBitmap(x)
Definition: pgbitmap.h:193

Provide a macro for dealing with bitmap arguments.

◆ PG_RETURN_BITMAP

#define PG_RETURN_BITMAP (   x)    PG_RETURN_POINTER(x)

Provide a macro for returning bitmap results.

◆ USE_64_BIT

#define USE_64_BIT   1

Use 64-bit word definitions throughout.

Typedef Documentation

◆ Bitmap

typedef struct Bitmap Bitmap

A bitmap is stored as an array of integer values. Note that the size of a Bitmap structure is determined dynamically at run time as the size of the array is only known then.

◆ bm_int

typedef uint64 bm_int

bm_int is the bitmap integer type (a 64-bit value).

◆ boolean

typedef unsigned char boolean

Defines a boolean type to make our code more readable.

Function Documentation

◆ bitmap_bitmax()

Datum bitmap_bitmax ( FunctionCallInfo  fcinfo)

bitmap_bitmax(bitmap bitmap) returns boolean Return the highest bit set in the bitmap, or NULL if none are set. This relies on bitmap->bitmax always identifying the highest numbered bit in the bitset, unless the bitmap is empty.

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap being examined.
Returns
integer The lowest bit set in the bitmap or NULL if there are no bits set.
Here is the call graph for this function:

◆ bitmap_bitmin()

Datum bitmap_bitmin ( FunctionCallInfo  fcinfo)

bitmap_bitmin(bitmap bitmap) returns boolean Return the lowest bit set in the bitmap, or NULL if none are set. This relies on bitmap->bitmin always identifying the lowest numbered bit in the bitset, unless the bitmap is empty.

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap being examined.
Returns
integer The lowest bit set in the bitmap or NULL if there are no bits set.
Here is the call graph for this function:

◆ bitmap_bits()

Datum bitmap_bits ( FunctionCallInfo  fcinfo)

bitmap_bits(name text) returns setof int4 Return the set of all bits set in the specified Bitmap.

Parameters
fcinfoname text The name of the bitmap.
Returns
setof int4The set of bits that are set in the bitmap.
Here is the call graph for this function:

◆ bitmap_clearbit()

Datum bitmap_clearbit ( FunctionCallInfo  fcinfo)

bitmap_clearbit(bitmap bitmap, bit int4) returns bool Clear the given bit in the bitmap, returning FALSE.

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap to be manipulated.
bitno int4 The bitnumber to be set.
Returns
bool TRUE
Here is the call graph for this function:

◆ bitmap_cmp()

Datum bitmap_cmp ( FunctionCallInfo  fcinfo)

bitmap_cmp(bitmap1 bitmap, bitmap2 bitmap) returns bool Return result of comparison of bitmap1's string representation with bitmap2's.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
int4 result of comparison
Here is the call graph for this function:

◆ bitmap_equal()

Datum bitmap_equal ( FunctionCallInfo  fcinfo)

bitmap_equal(bitmap1 bitmap, bitmap2 bitmap) returns bool Return true if the bitmaps are equivalent,

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bool true if the bitmaps contain the same bits.
Here is the call graph for this function:

◆ bitmap_ge()

Datum bitmap_ge ( FunctionCallInfo  fcinfo)

bitmap_ge(bitmap1 bitmap, bitmap2 bitmap) returns bool Return true if bitmap1's string representation should be sorted later than, or the same as, bitmap2's.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bool true unless the bitmaps contain the same bits.
Here is the call graph for this function:

◆ bitmap_gt()

Datum bitmap_gt ( FunctionCallInfo  fcinfo)

bitmap_gt(bitmap1 bitmap, bitmap2 bitmap) returns bool Return true if bitmap1's string representation should be sorted later than bitmap2's.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bool true unless the bitmaps contain the same bits.
Here is the call graph for this function:

◆ bitmap_in()

Datum bitmap_in ( FunctionCallInfo  fcinfo)

bitmap_in(serialised_bitmap text) returns bitmap Create a Bitmap initialised from a (base64) text value.

Parameters
fcinfoParams as described_below
serialised_bitmap text A base64 serialiastion of a bitmap value.
Returns
Bitmap the newly created bitmap
Here is the call graph for this function:

◆ bitmap_intersection()

Datum bitmap_intersection ( FunctionCallInfo  fcinfo)

bitmap_intersection(bitmap1 bitmap, bitmap2 bitmap) returns bitmap Return the intersection of 2 bitmaps. This can be used as an aggregate function in which case the bitmap1 parameter will be null for the first call. In this case simply return the second argument.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bitmap the intersection of the two bitmaps.
Here is the call graph for this function:

◆ bitmap_is_empty()

Datum bitmap_is_empty ( FunctionCallInfo  fcinfo)

bitmap_is_empty(bitmap bitmap) returns boolean Predicate to identify whether a bitmap is empty or not.

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap being examined. bitmap value.
Returns
boolean true, if the bitmap has no bits.
Here is the call graph for this function:

◆ bitmap_le()

Datum bitmap_le ( FunctionCallInfo  fcinfo)

bitmap_le(bitmap1 bitmap, bitmap2 bitmap) returns bool Return true if bitmap1's string representation should be sorted earlier than, or the same as, bitmap2's.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bool true unless the bitmaps contain the same bits.
Here is the call graph for this function:

◆ bitmap_lt()

Datum bitmap_lt ( FunctionCallInfo  fcinfo)

bitmap_lt(bitmap1 bitmap, bitmap2 bitmap) returns bool Return true if bitmap1's string representation should be sorted earlier than bitmap2's.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bool true unless the bitmaps contain the same bits.
Here is the call graph for this function:

◆ bitmap_minus()

Datum bitmap_minus ( FunctionCallInfo  fcinfo)

bitmap_minus(bitmap1 bitmap, bitmap2 bitmap) returns bitmap Return the bitmap1 with all bits from bitmap2 subtracted (cleared) from it.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bitmap the subtraction of the two bitmaps.
Here is the call graph for this function:

◆ bitmap_nequal()

Datum bitmap_nequal ( FunctionCallInfo  fcinfo)

bitmap_nequal(bitmap1 bitmap, bitmap2 bitmap) returns bool Return true if the bitmaps are not equivalent,

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bool true unless the bitmaps contain the same bits.
Here is the call graph for this function:

◆ bitmap_new()

Datum bitmap_new ( FunctionCallInfo  fcinfo)

bitmap_new() returns bitmap; Create or re-initialise a Bitmap, for dealing with a named range of values.

Parameters
fcinfoParams as described_below
bitno int32 The first integer value to be recorded in the bitmap
Returns
Bitmap the newly allocated bitmap
Here is the call graph for this function:

◆ bitmap_new_empty()

Datum bitmap_new_empty ( FunctionCallInfo  fcinfo)

bitmap_new_empty() returns bitmap; Create an empty Bitmap, for dealing with a named range of values.

Parameters
fcinfoParams as described_below
Returns
Bitmap the newly allocated bitmap
Here is the call graph for this function:

◆ bitmap_out()

Datum bitmap_out ( FunctionCallInfo  fcinfo)

bitmap_out(bitmap bitmap) returns text Create a (base64) text representation of a bitmap.

Parameters
fcinfoParams as described_below
bitmap bitmap A base64 serialiastion of a bitmap value.
Returns
cstring the newly serialised text stream.
Here is the call graph for this function:

◆ bitmap_setbit()

Datum bitmap_setbit ( FunctionCallInfo  fcinfo)

bitmap_setbit(bitmap bitmap, bit int4) returns bool Set the given bit in the bitmap, returning TRUE. This can be used as an aggregate function in which case the bitmap parameter will be null for the first call. In this case simply create a bitmap from the second argument.

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap to be manipulated.
bitno int4 The bitnumber to be set.
Returns
bitmap The result.
Here is the call graph for this function:

◆ bitmap_setmax()

Datum bitmap_setmax ( FunctionCallInfo  fcinfo)

bitmap_setmax(bitmap bitmap, bitmax int4) returns bitmap Return a new bitmap having no bits greater than bitmax

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap to be manipulated.
bitmax int4 The new maximum bit.
Returns
bitmap The new bitmap.
Here is the call graph for this function:

◆ bitmap_setmin()

Datum bitmap_setmin ( FunctionCallInfo  fcinfo)

bitmap_setmin(bitmap bitmap, bitmin int4) returns bitmap Return a new bitmap having no bits less than bitmin

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap to be manipulated.
bitmin int4 The new minimum bit.
Returns
bitmap The new bitmap.
Here is the call graph for this function:

◆ bitmap_testbit()

Datum bitmap_testbit ( FunctionCallInfo  fcinfo)

bitmap_testbit(bitmap bitmap, bit int4) returns bool Test the given bit in the bitmap, returning TRUE if it is 1.

Parameters
fcinfoParams as described_below
bitmap bitmap The bitmap to be manipulated.
bitno int4 The bitnumber to be tested.
Returns
bool the truth value of the bit.
Here is the call graph for this function:

◆ bitmap_union()

Datum bitmap_union ( FunctionCallInfo  fcinfo)

bitmap_union(bitmap1 bitmap, bitmap2 bitmap) returns bitmap Return the union of 2 bitmaps. This can be used as an aggregate function in which case the bitmap1 parameter will be null for the first call. In this case simply return the second argument.

Parameters
fcinfoParams as described_below
bitmap1 bitmap The first bitmap
bitmap2 bitmap The second bitmap
Returns
bitmap the union of the two bitmaps.
Here is the call graph for this function:

◆ bitmapCopy()

Bitmap* bitmapCopy ( Bitmap bitmap)

Create a copy of a bitmap.

Parameters
bitmapThe original bitmap
Returns
Copy of bitmap.
Here is the call graph for this function:

◆ bitmapTestbit()

bool bitmapTestbit ( Bitmap bitmap,
int32  bit 
)

Test a bit within a Bitmap. If the bit is outside of the acceptable range return false.

Parameters
bitmapThe Bitmap within which the bit is to be set.
bitThe bit to be tested.
Returns
True if the bit is set, false otherwise.