1 module roaring.c; 2 3 import core.stdc.inttypes: uint8_t, uint16_t, int32_t, uint32_t, uint64_t; 4 5 extern(C): 6 7 struct roaring_array_t { 8 int32_t size; 9 int32_t allocation_size; 10 void **containers; 11 uint16_t *keys; 12 uint8_t *typecodes; 13 } 14 15 struct roaring_bitmap_t { 16 roaring_array_t high_low_container; 17 bool copy_on_write; 18 } 19 20 struct roaring_uint32_iterator_t { 21 const roaring_bitmap_t *parent; // owner 22 int32_t container_index; // point to the current container index 23 int32_t in_container_index; // for bitset and array container, this is out 24 // index 25 int32_t run_index; // for run container, this points at the run 26 uint32_t in_run_index; // within a run, this is our index (points at the 27 // end of the current run) 28 29 uint32_t current_value; 30 bool has_value; 31 32 const void *container; // should be: 33 // parent->high_low_container.containers[container_index]; 34 uint8_t typecode; // should be: 35 // parent->high_low_container.typecodes[container_index]; 36 uint32_t highbits; // should be: 37 // parent->high_low_container.keys[container_index]) << 38 // 16; 39 } 40 41 extern(C) alias roaring_iterator = bool function(uint32_t value, void* param); 42 43 @nogc @safe 44 void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t x); 45 46 /** 47 * Add value n_args from pointer vals, faster than repeatedly calling 48 * roaring_bitmap_add 49 * 50 */ 51 void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args, const uint32_t *vals); 52 53 /** 54 * Computes the intersection between two bitmaps and returns new bitmap. The 55 * caller is 56 * responsible for memory management. 57 * 58 */ 59 @nogc @safe 60 roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1, const roaring_bitmap_t *x2); 61 62 /** 63 * Computes the union between two bitmaps and returns new bitmap. The caller is 64 * responsible for memory management. 65 */ 66 @nogc @safe 67 roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *x1, const roaring_bitmap_t *x2); 68 69 /** 70 * Computes the symmetric difference (xor) between two bitmaps 71 * and returns new bitmap. The caller is responsible for memory management. 72 */ 73 @nogc @safe 74 roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *x1, const roaring_bitmap_t *x2); 75 76 77 @nogc @safe 78 bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) pure; 79 80 @nogc 81 roaring_bitmap_t *roaring_bitmap_create(); 82 83 @nogc 84 roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap); 85 86 /** 87 * Return true if all the elements of ra1 are also in ra2. 88 */ 89 bool roaring_bitmap_is_subset(const roaring_bitmap_t *ra1, const roaring_bitmap_t *ra2); 90 91 92 /** use with roaring_bitmap_serialize 93 * see roaring_bitmap_portable_deserialize if you want a format that's 94 * compatible with Java and Go implementations 95 */ 96 roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf); 97 98 /** 99 * read a bitmap from a serialized version. This is meant to be compatible with 100 * the Java and Go versions. See format specification at 101 * https://github.com/RoaringBitmap/RoaringFormatSpec 102 * In case of failure, a null pointer is returned. 103 * This function is unsafe in the sense that if there is no valid serialized 104 * bitmap at the pointer, then many bytes could be read, possibly causing a buffer 105 * overflow. For a safer approach, 106 * call roaring_bitmap_portable_deserialize_safe. 107 */ 108 roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf); 109 110 /** 111 * read a bitmap from a serialized version in a safe manner (reading up to maxbytes). 112 * This is meant to be compatible with 113 * the Java and Go versions. See format specification at 114 * https://github.com/RoaringBitmap/RoaringFormatSpec 115 * In case of failure, a null pointer is returned. 116 */ 117 roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf, size_t maxbytes); 118 119 /** 120 * Check how many bytes would be read (up to maxbytes) at this pointer if there 121 * is a bitmap, returns zero if there is no valid bitmap. 122 * This is meant to be compatible with 123 * the Java and Go versions. See format specification at 124 * https://github.com/RoaringBitmap/RoaringFormatSpec 125 */ 126 size_t roaring_bitmap_portable_deserialize_size(const char *buf, size_t maxbytes); 127 128 /** 129 * Return true if the two bitmaps contain the same elements. 130 */ 131 bool roaring_bitmap_equals(const roaring_bitmap_t *ra1, const roaring_bitmap_t *ra2); 132 133 void roaring_bitmap_free(roaring_bitmap_t *r); 134 roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max, uint32_t step); 135 136 @nogc @safe 137 uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *ra) pure; 138 139 @nogc @safe 140 uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *bm) pure; 141 142 @nogc @safe 143 uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *bm) pure; 144 145 /** 146 * roaring_bitmap_rank returns the number of integers that are smaller or equal 147 * to x. 148 */ 149 @nogc @safe 150 uint64_t roaring_bitmap_rank(const roaring_bitmap_t *bm, uint32_t x) pure; 151 152 /** 153 * write a bitmap to a char buffer. The output buffer should refer to at least 154 * roaring_bitmap_portable_size_in_bytes(ra) bytes of allocated memory. 155 * This is meant to be compatible with 156 * the 157 * Java and Go versions. Returns how many bytes were written which should be 158 * roaring_bitmap_portable_size_in_bytes(ra). See format specification at 159 * https://github.com/RoaringBitmap/RoaringFormatSpec 160 */ 161 size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *ra, char *buf); 162 163 /** 164 * How many bytes are required to serialize this bitmap (meant to be compatible 165 * with Java and Go versions). See format specification at 166 * https://github.com/RoaringBitmap/RoaringFormatSpec 167 */ 168 @nogc @safe 169 size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *ra) pure; 170 171 @nogc @safe 172 void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x); 173 174 @nogc @safe 175 bool roaring_bitmap_run_optimize(roaring_bitmap_t *r); 176 177 bool roaring_bitmap_select(const roaring_bitmap_t *bm, uint32_t rank, uint32_t *element); 178 179 /** 180 * write the bitmap to an output pointer, this output buffer should refer to 181 * at least roaring_bitmap_size_in_bytes(ra) allocated bytes. 182 * 183 * see roaring_bitmap_portable_serialize if you want a format that's compatible 184 * with Java and Go implementations 185 * 186 * this format has the benefit of being sometimes more space efficient than 187 * roaring_bitmap_portable_serialize 188 * e.g., when the data is sparse. 189 * 190 * Returns how many bytes were written which should be 191 * roaring_bitmap_size_in_bytes(ra). 192 */ 193 size_t roaring_bitmap_serialize(const roaring_bitmap_t *ra, char *buf); 194 195 /** 196 * How many bytes are required to serialize this bitmap (NOT compatible 197 * with Java and Go versions) 198 */ 199 @nogc @safe 200 size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *ra) pure; 201 202 /** 203 * Convert the bitmap to an array. Write the output to "ans", 204 * caller is responsible to ensure that there is enough memory 205 * allocated 206 * (e.g., ans = malloc(roaring_bitmap_get_cardinality(mybitmap) 207 * * sizeof(uint32_t)) ) 208 */ 209 void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *ra, uint32_t *ans); 210 211 /** 212 * Advance the iterator. If there is a new value, then it->has_value is true. 213 * The new value is in it->current_value. Values are traversed in increasing 214 * orders. For convenience, returns it->has_value. 215 */ 216 @nogc 217 bool roaring_advance_uint32_iterator(roaring_uint32_iterator_t *it); 218 219 /** 220 * Create an iterator object that can be used to iterate through the 221 * values. Caller is responsible for calling roaring_free_iterator. 222 * The iterator is initialized. If there is a value, then it->has_value is true. 223 * The first value is in it->current_value. The iterator traverses the values 224 * in increasing order. 225 * 226 * This function calls roaring_init_iterator. 227 */ 228 @nogc 229 roaring_uint32_iterator_t *roaring_create_iterator(const roaring_bitmap_t *ra); 230 231 /** 232 * Free memory following roaring_create_iterator 233 */ 234 @nogc 235 void roaring_free_uint32_iterator(roaring_uint32_iterator_t *it);