2
0

bitmapset.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*-------------------------------------------------------------------------
  2. *
  3. * bitmapset.h
  4. * PostgreSQL generic bitmap set package
  5. *
  6. * A bitmap set can represent any set of nonnegative integers, although
  7. * it is mainly intended for sets where the maximum value is not large,
  8. * say at most a few hundred. By convention, a NULL pointer is always
  9. * accepted by all operations to represent the empty set. (But beware
  10. * that this is not the only representation of the empty set. Use
  11. * bms_is_empty() in preference to testing for NULL.)
  12. *
  13. *
  14. * Copyright (c) 2003-2022, PostgreSQL Global Development Group
  15. *
  16. * src/include/nodes/bitmapset.h
  17. *
  18. *-------------------------------------------------------------------------
  19. */
  20. #ifndef BITMAPSET_H
  21. #define BITMAPSET_H
  22. /*
  23. * Forward decl to save including pg_list.h
  24. */
  25. struct List;
  26. /*
  27. * Data representation
  28. *
  29. * Larger bitmap word sizes generally give better performance, so long as
  30. * they're not wider than the processor can handle efficiently. We use
  31. * 64-bit words if pointers are that large, else 32-bit words.
  32. */
  33. #if SIZEOF_VOID_P >= 8
  34. #define BITS_PER_BITMAPWORD 64
  35. typedef uint64 bitmapword; /* must be an unsigned type */
  36. typedef int64 signedbitmapword; /* must be the matching signed type */
  37. #else
  38. #define BITS_PER_BITMAPWORD 32
  39. typedef uint32 bitmapword; /* must be an unsigned type */
  40. typedef int32 signedbitmapword; /* must be the matching signed type */
  41. #endif
  42. typedef struct Bitmapset
  43. {
  44. int nwords; /* number of words in array */
  45. bitmapword words[FLEXIBLE_ARRAY_MEMBER]; /* really [nwords] */
  46. } Bitmapset;
  47. /* result of bms_subset_compare */
  48. typedef enum
  49. {
  50. BMS_EQUAL, /* sets are equal */
  51. BMS_SUBSET1, /* first set is a subset of the second */
  52. BMS_SUBSET2, /* second set is a subset of the first */
  53. BMS_DIFFERENT /* neither set is a subset of the other */
  54. } BMS_Comparison;
  55. /* result of bms_membership */
  56. typedef enum
  57. {
  58. BMS_EMPTY_SET, /* 0 members */
  59. BMS_SINGLETON, /* 1 member */
  60. BMS_MULTIPLE /* >1 member */
  61. } BMS_Membership;
  62. /*
  63. * function prototypes in nodes/bitmapset.c
  64. */
  65. extern Bitmapset *bms_copy(const Bitmapset *a);
  66. extern bool bms_equal(const Bitmapset *a, const Bitmapset *b);
  67. extern int bms_compare(const Bitmapset *a, const Bitmapset *b);
  68. extern Bitmapset *bms_make_singleton(int x);
  69. extern void bms_free(Bitmapset *a);
  70. extern Bitmapset *bms_union(const Bitmapset *a, const Bitmapset *b);
  71. extern Bitmapset *bms_intersect(const Bitmapset *a, const Bitmapset *b);
  72. extern Bitmapset *bms_difference(const Bitmapset *a, const Bitmapset *b);
  73. extern bool bms_is_subset(const Bitmapset *a, const Bitmapset *b);
  74. extern BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b);
  75. extern bool bms_is_member(int x, const Bitmapset *a);
  76. extern int bms_member_index(Bitmapset *a, int x);
  77. extern bool bms_overlap(const Bitmapset *a, const Bitmapset *b);
  78. extern bool bms_overlap_list(const Bitmapset *a, const struct List *b);
  79. extern bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b);
  80. extern int bms_singleton_member(const Bitmapset *a);
  81. extern bool bms_get_singleton_member(const Bitmapset *a, int *member);
  82. extern int bms_num_members(const Bitmapset *a);
  83. /* optimized tests when we don't need to know exact membership count: */
  84. extern BMS_Membership bms_membership(const Bitmapset *a);
  85. extern bool bms_is_empty(const Bitmapset *a);
  86. /* these routines recycle (modify or free) their non-const inputs: */
  87. extern Bitmapset *bms_add_member(Bitmapset *a, int x);
  88. extern Bitmapset *bms_del_member(Bitmapset *a, int x);
  89. extern Bitmapset *bms_add_members(Bitmapset *a, const Bitmapset *b);
  90. extern Bitmapset *bms_add_range(Bitmapset *a, int lower, int upper);
  91. extern Bitmapset *bms_int_members(Bitmapset *a, const Bitmapset *b);
  92. extern Bitmapset *bms_del_members(Bitmapset *a, const Bitmapset *b);
  93. extern Bitmapset *bms_join(Bitmapset *a, Bitmapset *b);
  94. /* support for iterating through the integer elements of a set: */
  95. extern int bms_first_member(Bitmapset *a);
  96. extern int bms_next_member(const Bitmapset *a, int prevbit);
  97. extern int bms_prev_member(const Bitmapset *a, int prevbit);
  98. /* support for hashtables using Bitmapsets as keys: */
  99. extern uint32 bms_hash_value(const Bitmapset *a);
  100. extern uint32 bitmap_hash(const void *key, Size keysize);
  101. extern int bitmap_match(const void *key1, const void *key2, Size keysize);
  102. #endif /* BITMAPSET_H */