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