uint-spbset.h (3666B)
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, <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 "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 /* Add the number `i` to the bitset. 68 * Numbers can be added more than once, without making a difference. 69 * Returns FALSE if allocations failed. */ 70 bool Curl_uint_spbset_add(struct uint_spbset *bset, unsigned int i); 71 72 /* Remove the number `i` from the bitset. */ 73 void Curl_uint_spbset_remove(struct uint_spbset *bset, unsigned int i); 74 75 /* Return TRUE if the bitset contains number `i`. */ 76 bool Curl_uint_spbset_contains(struct uint_spbset *bset, unsigned int i); 77 78 /* Get the first number in the bitset, e.g. the smallest. 79 * Returns FALSE when the bitset is empty. */ 80 bool Curl_uint_spbset_first(struct uint_spbset *bset, unsigned int *pfirst); 81 82 /* Get the next number in the bitset, following `last` in natural order. 83 * Put another way, this is the smallest number greater than `last` in 84 * the bitset. `last` does not have to be present in the set. 85 * 86 * Returns FALSE when no such number is in the set. 87 * 88 * This allows to iterate the set while being modified: 89 * - added numbers higher than 'last' will be picked up by the iteration. 90 * - added numbers lower than 'last' will not show up. 91 * - removed numbers lower or equal to 'last' will not show up. 92 * - removed numbers higher than 'last' will not be visited. */ 93 bool Curl_uint_spbset_next(struct uint_spbset *bset, unsigned int last, 94 unsigned int *pnext); 95 96 #endif /* HEADER_CURL_UINT_SPBSET_H */