You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.9KB

  1. /*
  2. * contrib/intarray/_int.h
  3. */
  4. #ifndef ___INT_H__
  5. #define ___INT_H__
  6. #include "utils/array.h"
  7. #include "utils/memutils.h"
  8. /* number ranges for compression */
  9. #define MAXNUMRANGE 100
  10. /* useful macros for accessing int4 arrays */
  11. #define ARRPTR(x) ( (int32 *) ARR_DATA_PTR(x) )
  12. #define ARRNELEMS(x) ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
  13. /* reject arrays we can't handle; to wit, those containing nulls */
  14. #define CHECKARRVALID(x) \
  15. do { \
  16. if (ARR_HASNULL(x) && array_contains_nulls(x)) \
  17. ereport(ERROR, \
  18. (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \
  19. errmsg("array must not contain nulls"))); \
  20. } while(0)
  21. #define ARRISEMPTY(x) (ARRNELEMS(x) == 0)
  22. /* sort the elements of the array */
  23. #define SORT(x) \
  24. do { \
  25. int _nelems_ = ARRNELEMS(x); \
  26. if (_nelems_ > 1) \
  27. isort(ARRPTR(x), _nelems_); \
  28. } while(0)
  29. /* sort the elements of the array and remove duplicates */
  30. #define PREPAREARR(x) \
  31. do { \
  32. int _nelems_ = ARRNELEMS(x); \
  33. if (_nelems_ > 1) \
  34. if (isort(ARRPTR(x), _nelems_)) \
  35. (x) = _int_unique(x); \
  36. } while(0)
  37. /* "wish" function */
  38. #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
  39. /* bigint defines */
  40. #define SIGLENINT 63 /* >122 => key will toast, so very slow!!! */
  41. #define SIGLEN ( sizeof(int)*SIGLENINT )
  42. #define SIGLENBIT (SIGLEN*BITS_PER_BYTE)
  43. typedef char BITVEC[SIGLEN];
  44. typedef char *BITVECP;
  45. #define LOOPBYTE \
  46. for(i=0;i<SIGLEN;i++)
  47. /* beware of multiple evaluation of arguments to these macros! */
  48. #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
  49. #define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
  50. #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
  51. #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
  52. #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
  53. #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
  54. #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
  55. /*
  56. * type of index key
  57. */
  58. typedef struct
  59. {
  60. int32 vl_len_; /* varlena header (do not touch directly!) */
  61. int32 flag;
  62. char data[FLEXIBLE_ARRAY_MEMBER];
  63. } GISTTYPE;
  64. #define ALLISTRUE 0x04
  65. #define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
  66. #define GTHDRSIZE (VARHDRSZ + sizeof(int32))
  67. #define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
  68. #define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
  69. /*
  70. * useful functions
  71. */
  72. bool isort(int32 *a, int len);
  73. ArrayType *new_intArrayType(int num);
  74. ArrayType *copy_intArrayType(ArrayType *a);
  75. ArrayType *resize_intArrayType(ArrayType *a, int num);
  76. int internal_size(int *a, int len);
  77. ArrayType *_int_unique(ArrayType *a);
  78. int32 intarray_match_first(ArrayType *a, int32 elem);
  79. ArrayType *intarray_add_elem(ArrayType *a, int32 elem);
  80. ArrayType *intarray_concat_arrays(ArrayType *a, ArrayType *b);
  81. ArrayType *int_to_intset(int32 elem);
  82. bool inner_int_overlap(ArrayType *a, ArrayType *b);
  83. bool inner_int_contains(ArrayType *a, ArrayType *b);
  84. ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
  85. ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
  86. void rt__int_size(ArrayType *a, float *size);
  87. void gensign(BITVEC sign, int *a, int len);
  88. /*****************************************************************************
  89. * Boolean Search
  90. *****************************************************************************/
  91. #define BooleanSearchStrategy 20
  92. /*
  93. * item in polish notation with back link
  94. * to left operand
  95. */
  96. typedef struct ITEM
  97. {
  98. int16 type;
  99. int16 left;
  100. int32 val;
  101. } ITEM;
  102. typedef struct QUERYTYPE
  103. {
  104. int32 vl_len_; /* varlena header (do not touch directly!) */
  105. int32 size; /* number of ITEMs */
  106. ITEM items[FLEXIBLE_ARRAY_MEMBER];
  107. } QUERYTYPE;
  108. #define HDRSIZEQT offsetof(QUERYTYPE, items)
  109. #define COMPUTESIZE(size) ( HDRSIZEQT + (size) * sizeof(ITEM) )
  110. #define QUERYTYPEMAXITEMS ((MaxAllocSize - HDRSIZEQT) / sizeof(ITEM))
  111. #define GETQUERY(x) ( (x)->items )
  112. /* "type" codes for ITEM */
  113. #define END 0
  114. #define ERR 1
  115. #define VAL 2
  116. #define OPR 3
  117. #define OPEN 4
  118. #define CLOSE 5
  119. /* fmgr macros for QUERYTYPE objects */
  120. #define DatumGetQueryTypeP(X) ((QUERYTYPE *) PG_DETOAST_DATUM(X))
  121. #define DatumGetQueryTypePCopy(X) ((QUERYTYPE *) PG_DETOAST_DATUM_COPY(X))
  122. #define PG_GETARG_QUERYTYPE_P(n) DatumGetQueryTypeP(PG_GETARG_DATUM(n))
  123. #define PG_GETARG_QUERYTYPE_P_COPY(n) DatumGetQueryTypePCopy(PG_GETARG_DATUM(n))
  124. bool signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot);
  125. bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot);
  126. bool gin_bool_consistent(QUERYTYPE *query, bool *check);
  127. bool query_has_required_values(QUERYTYPE *query);
  128. int compASC(const void *a, const void *b);
  129. int compDESC(const void *a, const void *b);
  130. /* sort, either ascending or descending */
  131. #define QSORT(a, direction) \
  132. do { \
  133. int _nelems_ = ARRNELEMS(a); \
  134. if (_nelems_ > 1) \
  135. qsort((void*) ARRPTR(a), _nelems_, sizeof(int32), \
  136. (direction) ? compASC : compDESC ); \
  137. } while(0)
  138. #endif /* ___INT_H__ */