uint-spbset.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef HEADER_CURL_UINT_SPBSET_H
  2. #define HEADER_CURL_UINT_SPBSET_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 "sparse" bitset for unsigned int values.
  29. * It can hold any unsigned int value.
  30. *
  31. * Optimized for the case where only a small set of numbers need
  32. * to be kept, especially when "close" together. Then storage space
  33. * is most efficient, deteriorating when many number are far apart.
  34. */
  35. /* 4 slots = 256 bits, keep this a 2^n value. */
  36. #define CURL_UINT_SPBSET_CH_SLOTS 4
  37. #define CURL_UINT_SPBSET_CH_MASK ((CURL_UINT_SPBSET_CH_SLOTS * 64) - 1)
  38. /* store the uint value from offset to
  39. * (offset + (CURL_UINT_SPBSET_CHUNK_SLOTS * 64) - 1 */
  40. struct uint_spbset_chunk {
  41. struct uint_spbset_chunk *next;
  42. curl_uint64_t slots[CURL_UINT_SPBSET_CH_SLOTS];
  43. unsigned int offset;
  44. };
  45. struct uint_spbset {
  46. struct uint_spbset_chunk head;
  47. #ifdef DEBUGBUILD
  48. int init;
  49. #endif
  50. };
  51. void Curl_uint_spbset_init(struct uint_spbset *bset);
  52. void Curl_uint_spbset_destroy(struct uint_spbset *bset);
  53. /* Get the cardinality of the bitset, e.g. numbers present in the set. */
  54. unsigned int Curl_uint_spbset_count(struct uint_spbset *bset);
  55. /* TRUE of bitset is empty */
  56. bool Curl_uint_spbset_empty(struct uint_spbset *bset);
  57. /* Add the number `i` to the bitset.
  58. * Numbers can be added more than once, without making a difference.
  59. * Returns FALSE if allocations failed. */
  60. bool Curl_uint_spbset_add(struct uint_spbset *bset, unsigned int i);
  61. /* Remove the number `i` from the bitset. */
  62. void Curl_uint_spbset_remove(struct uint_spbset *bset, unsigned int i);
  63. /* Return TRUE if the bitset contains number `i`. */
  64. bool Curl_uint_spbset_contains(struct uint_spbset *bset, unsigned int i);
  65. /* Get the first number in the bitset, e.g. the smallest.
  66. * Returns FALSE when the bitset is empty. */
  67. bool Curl_uint_spbset_first(struct uint_spbset *bset, unsigned int *pfirst);
  68. /* Get the next number in the bitset, following `last` in natural order.
  69. * Put another way, this is the smallest number greater than `last` in
  70. * the bitset. `last` does not have to be present in the set.
  71. *
  72. * Returns FALSE when no such number is in the set.
  73. *
  74. * This allows to iterate the set while being modified:
  75. * - added numbers higher than 'last' will be picked up by the iteration.
  76. * - added numbers lower than 'last' will not show up.
  77. * - removed numbers lower or equal to 'last' will not show up.
  78. * - removed numbers higher than 'last' will not be visited. */
  79. bool Curl_uint_spbset_next(struct uint_spbset *bset, unsigned int last,
  80. unsigned int *pnext);
  81. #endif /* HEADER_CURL_UINT_SPBSET_H */