uint-bset.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef HEADER_CURL_UINT_BSET_H
  2. #define HEADER_CURL_UINT_BSET_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <curl/curl.h>
  28. /* A bitset for unsigned int values.
  29. * It can hold the numbers from 0 - (nmax - 1),
  30. * rounded to the next 64 multiple.
  31. *
  32. * Optimized for high efficiency in adding/removing numbers.
  33. * Efficient storage when the set is (often) relatively full.
  34. *
  35. * If the set's cardinality is only expected to be a fraction of nmax,
  36. * uint_spbset offers a "sparse" variant with more memory efficiency at
  37. * the price of slightly slower operations.
  38. */
  39. struct uint_bset {
  40. curl_uint64_t *slots;
  41. unsigned int nslots;
  42. unsigned int first_slot_used;
  43. #ifdef DEBUGBUILD
  44. int init;
  45. #endif
  46. };
  47. /* Initialize the bitset with capacity 0. */
  48. void Curl_uint_bset_init(struct uint_bset *bset);
  49. /* Resize the bitset capacity to hold numbers from 0 to `nmax`,
  50. * which rounds up `nmax` to the next multiple of 64. */
  51. CURLcode Curl_uint_bset_resize(struct uint_bset *bset, unsigned int nmax);
  52. /* Destroy the bitset, freeing all resources. */
  53. void Curl_uint_bset_destroy(struct uint_bset *bset);
  54. /* Get the bitset capacity, e.g. can hold numbers from 0 to capacity - 1. */
  55. unsigned int Curl_uint_bset_capacity(struct uint_bset *bset);
  56. /* Get the cardinality of the bitset, e.g. numbers present in the set. */
  57. unsigned int Curl_uint_bset_count(struct uint_bset *bset);
  58. /* TRUE of bitset is empty */
  59. bool Curl_uint_bset_empty(struct uint_bset *bset);
  60. /* Clear the bitset, making it empty. */
  61. void Curl_uint_bset_clear(struct uint_bset *bset);
  62. /* Add the number `i` to the bitset. Return FALSE if the number is
  63. * outside the set's capacity.
  64. * Numbers can be added more than once, without making a difference. */
  65. bool Curl_uint_bset_add(struct uint_bset *bset, unsigned int i);
  66. /* Remove the number `i` from the bitset. */
  67. void Curl_uint_bset_remove(struct uint_bset *bset, unsigned int i);
  68. /* Return TRUE if the bitset contains number `i`. */
  69. bool Curl_uint_bset_contains(struct uint_bset *bset, unsigned int i);
  70. /* Get the first number in the bitset, e.g. the smallest.
  71. * Returns FALSE when the bitset is empty. */
  72. bool Curl_uint_bset_first(struct uint_bset *bset, unsigned int *pfirst);
  73. /* Get the next number in the bitset, following `last` in natural order.
  74. * Put another way, this is the smallest number greater than `last` in
  75. * the bitset. `last` does not have to be present in the set.
  76. *
  77. * Returns FALSE when no such number is in the set.
  78. *
  79. * This allows to iterate the set while being modified:
  80. * - added numbers higher than 'last' will be picked up by the iteration.
  81. * - added numbers lower than 'last' will not show up.
  82. * - removed numbers lower or equal to 'last' will not show up.
  83. * - removed numbers higher than 'last' will not be visited. */
  84. bool Curl_uint_bset_next(struct uint_bset *bset, unsigned int last,
  85. unsigned int *pnext);
  86. #ifndef CURL_POPCOUNT64
  87. #define CURL_POPCOUNT64(x) Curl_popcount64(x)
  88. #define CURL_POPCOUNT64_IMPLEMENT
  89. unsigned int Curl_popcount64(curl_uint64_t x);
  90. #endif /* !CURL_POPCOUNT64 */
  91. #ifndef CURL_CTZ64
  92. #define CURL_CTZ64(x) Curl_ctz64(x)
  93. #define CURL_CTZ64_IMPLEMENT
  94. unsigned int Curl_ctz64(curl_uint64_t x);
  95. #endif /* !CURL_CTZ64 */
  96. #endif /* HEADER_CURL_UINT_BSET_H */