uint-bset.h (4303B)
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, <daniel@haxx.se>, 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 unsigned int first_slot_used; 46 #ifdef DEBUGBUILD 47 int init; 48 #endif 49 }; 50 51 /* Initialize the bitset with capacity 0. */ 52 void Curl_uint_bset_init(struct uint_bset *bset); 53 54 /* Resize the bitset capacity to hold numbers from 0 to `nmax`, 55 * which rounds up `nmax` to the next multiple of 64. */ 56 CURLcode Curl_uint_bset_resize(struct uint_bset *bset, unsigned int nmax); 57 58 /* Destroy the bitset, freeing all resources. */ 59 void Curl_uint_bset_destroy(struct uint_bset *bset); 60 61 /* Get the bitset capacity, e.g. can hold numbers from 0 to capacity - 1. */ 62 unsigned int Curl_uint_bset_capacity(struct uint_bset *bset); 63 64 /* Get the cardinality of the bitset, e.g. numbers present in the set. */ 65 unsigned int Curl_uint_bset_count(struct uint_bset *bset); 66 67 /* TRUE of bitset is empty */ 68 bool Curl_uint_bset_empty(struct uint_bset *bset); 69 70 /* Clear the bitset, making it empty. */ 71 void Curl_uint_bset_clear(struct uint_bset *bset); 72 73 /* Add the number `i` to the bitset. Return FALSE if the number is 74 * outside the set's capacity. 75 * Numbers can be added more than once, without making a difference. */ 76 bool Curl_uint_bset_add(struct uint_bset *bset, unsigned int i); 77 78 /* Remove the number `i` from the bitset. */ 79 void Curl_uint_bset_remove(struct uint_bset *bset, unsigned int i); 80 81 /* Return TRUE if the bitset contains number `i`. */ 82 bool Curl_uint_bset_contains(struct uint_bset *bset, unsigned int i); 83 84 /* Get the first number in the bitset, e.g. the smallest. 85 * Returns FALSE when the bitset is empty. */ 86 bool Curl_uint_bset_first(struct uint_bset *bset, unsigned int *pfirst); 87 88 /* Get the next number in the bitset, following `last` in natural order. 89 * Put another way, this is the smallest number greater than `last` in 90 * the bitset. `last` does not have to be present in the set. 91 * 92 * Returns FALSE when no such number is in the set. 93 * 94 * This allows to iterate the set while being modified: 95 * - added numbers higher than 'last' will be picked up by the iteration. 96 * - added numbers lower than 'last' will not show up. 97 * - removed numbers lower or equal to 'last' will not show up. 98 * - removed numbers higher than 'last' will not be visited. */ 99 bool Curl_uint_bset_next(struct uint_bset *bset, unsigned int last, 100 unsigned int *pnext); 101 102 103 #ifndef CURL_POPCOUNT64 104 #define CURL_POPCOUNT64(x) Curl_popcount64(x) 105 #define CURL_POPCOUNT64_IMPLEMENT 106 unsigned int Curl_popcount64(curl_uint64_t x); 107 #endif /* !CURL_POPCOUNT64 */ 108 109 #ifndef CURL_CTZ64 110 #define CURL_CTZ64(x) Curl_ctz64(x) 111 #define CURL_CTZ64_IMPLEMENT 112 unsigned int Curl_ctz64(curl_uint64_t x); 113 #endif /* !CURL_CTZ64 */ 114 115 #endif /* HEADER_CURL_UINT_BSET_H */