From ece7e6bf34facb67c7a938b2862f3d3af06aefc0 Mon Sep 17 00:00:00 2001
From: pgaddict <jian.universality@gmail.com>
Date: Thu, 29 Jun 2023 14:27:02 +0800
Subject: [PATCH 2/2] marco SET_DATA_PTR to quicly access hashset data region

---
 hashset-api.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/hashset-api.c b/hashset-api.c
index 28eb2387..2d856b47 100644
--- a/hashset-api.c
+++ b/hashset-api.c
@@ -194,7 +194,7 @@ int4hashset_out(PG_FUNCTION_ARGS)
 
 	/* Calculate the pointer to the bitmap and values array */
 	bitmap = set->data;
-	values = (int32 *) (set->data + CEIL_DIV(set->capacity, 8));
+	values = (int32 *) SET_DATA_PTR(set);
 
 	/* Initialize the StringInfo buffer */
 	initStringInfo(&str);
@@ -411,7 +411,7 @@ int4hashset_union(PG_FUNCTION_ARGS)
 	int4hashset_t  *seta = PG_GETARG_INT4HASHSET_COPY(0);
 	int4hashset_t  *setb = PG_GETARG_INT4HASHSET(1);
 	char		   *bitmap = setb->data;
-	int32		   *values = (int32 *) (bitmap + CEIL_DIV(setb->capacity, 8));
+	int32		   *values = (int32 *) SET_DATA_PTR(seta);
 
 	for (i = 0; i < setb->capacity; i++)
 	{
@@ -598,7 +598,7 @@ int4hashset_agg_add_set(PG_FUNCTION_ARGS)
 		value = PG_GETARG_INT4HASHSET(1);
 
 		bitmap = value->data;
-		values = (int32 *) (value->data + CEIL_DIV(value->capacity, 8));
+		values = (int32 *) SET_DATA_PTR(value);
 
 		for (i = 0; i < value->capacity; i++)
 		{
@@ -665,7 +665,7 @@ int4hashset_agg_combine(PG_FUNCTION_ARGS)
 	dst = (int4hashset_t *) PG_GETARG_POINTER(0);
 
 	bitmap = src->data;
-	values = (int32 *) (src->data + CEIL_DIV(src->capacity, 8));
+	values = (int32 *) SET_DATA_PTR(src);
 
 	for (i = 0; i < src->capacity; i++)
 	{
@@ -698,7 +698,7 @@ int4hashset_to_array(PG_FUNCTION_ARGS)
 		PG_RETURN_ARRAYTYPE_P(construct_empty_array(INT4OID));
 
 	sbitmap = set->data;
-	svalues = (int32 *) (set->data + CEIL_DIV(set->capacity, 8));
+	svalues = (int32 *) (int32 *) SET_DATA_PTR(set);
 
 	/* number of values to store in the array */
 	nvalues = set->nelements;
@@ -757,7 +757,7 @@ int4hashset_eq(PG_FUNCTION_ARGS)
 		PG_RETURN_BOOL(false);
 
 	bitmap_a = a->data;
-	values_a = (int32 *)(a->data + CEIL_DIV(a->capacity, 8));
+	values_a = (int32 *) SET_DATA_PTR(a);
 
 	/*
 	 * Check if every element in a is also in b
@@ -935,7 +935,8 @@ int4hashset_intersection(PG_FUNCTION_ARGS)
 	int4hashset_t  *seta = PG_GETARG_INT4HASHSET(0);
 	int4hashset_t  *setb = PG_GETARG_INT4HASHSET(1);
 	char		   *bitmap = setb->data;
-	int32		   *values = (int32 *)(bitmap + CEIL_DIV(setb->capacity, 8));
+	int32		   *values = (int32 *) SET_DATA_PTR(setb);
+	
 	int4hashset_t  *intersection;
 
 	intersection = int4hashset_allocate(
@@ -971,7 +972,7 @@ int4hashset_difference(PG_FUNCTION_ARGS)
 	int4hashset_t	*setb = PG_GETARG_INT4HASHSET(1);
 	int4hashset_t	*difference;
 	char			*bitmap = seta->data;
-	int32			*values = (int32 *)(bitmap + CEIL_DIV(seta->capacity, 8));
+	int32			*values = (int32 *) SET_DATA_PTR(seta);
 
 	difference = int4hashset_allocate(
 		seta->capacity,
@@ -1007,8 +1008,8 @@ int4hashset_symmetric_difference(PG_FUNCTION_ARGS)
 	int4hashset_t  *result;
 	char		   *bitmapa = seta->data;
 	char		   *bitmapb = setb->data;
-	int32		   *valuesa = (int32 *) (bitmapa + CEIL_DIV(seta->capacity, 8));
-	int32		   *valuesb = (int32 *) (bitmapb + CEIL_DIV(setb->capacity, 8));
+	int32		   *valuesa = (int32 *) SET_DATA_PTR(seta);
+	int32		   *valuesb = (int32 *) SET_DATA_PTR(setb);
 
 	result = int4hashset_allocate(
 		seta->nelements + setb->nelements,
-- 
2.34.1

