| 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 | |
| 28 | #include <curl/curl.h> |
| 29 | |
| 30 | /* A bitset for unsigned int values. |
| 31 | * It can hold the numbers from 0 - (nmax - 1), |
| 32 | * rounded to the next 64 multiple. |
| 33 | * |
| 34 | * Optimized for high efficiency in adding/removing numbers. |
| 35 | * Efficient storage when the set is (often) relatively full. |
| 36 | * |
| 37 | * If the set's cardinality is only expected to be a fraction of nmax, |
| 38 | * uint_spbset offers a "sparse" variant with more memory efficiency at |
| 39 | * the price of slightly slower operations. |
| 40 | */ |
| 41 | |
| 42 | struct uint_bset { |
| 43 | curl_uint64_t *slots; |
| 44 | unsigned int nslots; |
| 45 | #ifdef DEBUGBUILD |
| 46 | int init; |
| 47 | #endif |
| 48 | }; |
| 49 | |
| 50 | /* Initialize the bitset with capacity 0. */ |
| 51 | void Curl_uint_bset_init(struct uint_bset *bset); |
| 52 | |
| 53 | /* Resize the bitset capacity to hold numbers from 0 to `nmax`, |
| 54 | * which rounds up `nmax` to the next multiple of 64. */ |
| 55 | CURLcode Curl_uint_bset_resize(struct uint_bset *bset, unsigned int nmax); |
| 56 | |
| 57 | /* Destroy the bitset, freeing all resources. */ |
| 58 | void Curl_uint_bset_destroy(struct uint_bset *bset); |
| 59 | |
| 60 | /* Get the bitset capacity, e.g. can hold numbers from 0 to capacity - 1. */ |
| 61 | unsigned int Curl_uint_bset_capacity(struct uint_bset *bset); |
| 62 | |
| 63 | /* Get the cardinality of the bitset, e.g. numbers present in the set. */ |
| 64 | unsigned int Curl_uint_bset_count(struct uint_bset *bset); |
| 65 | |
| 66 | /* TRUE of bitset is empty */ |
| 67 | bool Curl_uint_bset_empty(struct uint_bset *bset); |
| 68 | |
| 69 | /* Clear the bitset, making it empty. */ |
| 70 | void Curl_uint_bset_clear(struct uint_bset *bset); |
| 71 | |
| 72 | /* Add the number `i` to the bitset. Return FALSE if the number is |
| 73 | * outside the set's capacity. |
| 74 | * Numbers can be added more than once, without making a difference. */ |
| 75 | bool Curl_uint_bset_add(struct uint_bset *bset, unsigned int i); |
| 76 | |
| 77 | /* Remove the number `i` from the bitset. */ |
| 78 | void Curl_uint_bset_remove(struct uint_bset *bset, unsigned int i); |
| 79 | |
| 80 | /* Return TRUE if the bitset contains number `i`. */ |
| 81 | bool Curl_uint_bset_contains(struct uint_bset *bset, unsigned int i); |
| 82 | |
| 83 | /* Get the first number in the bitset, e.g. the smallest. |
| 84 | * Returns FALSE when the bitset is empty. */ |
| 85 | bool Curl_uint_bset_first(struct uint_bset *bset, unsigned int *pfirst); |
| 86 | |
| 87 | /* Get the next number in the bitset, following `last` in natural order. |
| 88 | * Put another way, this is the smallest number greater than `last` in |
| 89 | * the bitset. `last` does not have to be present in the set. |
| 90 | * |
| 91 | * Returns FALSE when no such number is in the set. |
| 92 | * |
| 93 | * This allows to iterate the set while being modified: |
| 94 | * - added numbers higher than 'last' will be picked up by the iteration. |
| 95 | * - added numbers lower than 'last' will not show up. |
| 96 | * - removed numbers lower or equal to 'last' will not show up. |
| 97 | * - removed numbers higher than 'last' will not be visited. */ |
| 98 | bool Curl_uint_bset_next(struct uint_bset *bset, unsigned int last, |
| 99 | unsigned int *pnext); |
| 100 | |
| 101 | |
| 102 | #ifndef CURL_POPCOUNT64 |
| 103 | #define CURL_POPCOUNT64(x) Curl_popcount64(x) |
| 104 | #define CURL_POPCOUNT64_IMPLEMENT |
| 105 | unsigned int Curl_popcount64(curl_uint64_t x); |
| 106 | #endif /* !CURL_POPCOUNT64 */ |
| 107 | |
| 108 | #ifndef CURL_CTZ64 |
| 109 | #define CURL_CTZ64(x) Curl_ctz64(x) |
| 110 | #define CURL_CTZ64_IMPLEMENT |
| 111 | unsigned int Curl_ctz64(curl_uint64_t x); |
| 112 | #endif /* !CURL_CTZ64 */ |
| 113 | |
| 114 | #endif /* HEADER_CURL_UINT_BSET_H */ |