| 1 |
/* |
| 2 |
* Copyright © 2020 Benjamin Otte |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Authors: Benjamin Otte <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include "eggbitset.h" |
| 23 |
|
| 24 |
#include "roaring.c" |
| 25 |
|
| 26 |
/** |
| 27 |
* EggBitset: (ref-func egg_bitset_ref) (unref-func egg_bitset_unref) |
| 28 |
* |
| 29 |
* A `EggBitset` represents a set of unsigned integers. |
| 30 |
* |
| 31 |
* Another name for this data structure is "bitmap". |
| 32 |
* |
| 33 |
* The current implementation is based on [roaring bitmaps](https://roaringbitmap.org/). |
| 34 |
* |
| 35 |
* A bitset allows adding a set of integers and provides support for set operations |
| 36 |
* like unions, intersections and checks for equality or if a value is contained |
| 37 |
* in the set. `EggBitset` also contains various functions to query metadata about |
| 38 |
* the bitset, such as the minimum or maximum values or its size. |
| 39 |
* |
| 40 |
* The fastest way to iterate values in a bitset is [[email protected]]. |
| 41 |
* |
| 42 |
* The main use case for `EggBitset` is implementing complex selections for |
| 43 |
* [[email protected]]. |
| 44 |
*/ |
| 45 |
|
| 46 |
struct _EggBitset |
| 47 |
{ |
| 48 |
int ref_count; |
| 49 |
roaring_bitmap_t roaring; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
G_DEFINE_BOXED_TYPE (EggBitset, egg_bitset, |
| 54 |
egg_bitset_ref, |
| 55 |
egg_bitset_unref) |
| 56 |
|
| 57 |
/** |
| 58 |
* egg_bitset_ref: |
| 59 |
* @self: (nullable): a `EggBitset` |
| 60 |
* |
| 61 |
* Acquires a reference on the given `EggBitset`. |
| 62 |
* |
| 63 |
* Returns: (transfer none): the `EggBitset` with an additional reference |
| 64 |
*/ |
| 65 |
EggBitset * |
| 66 |
egg_bitset_ref (EggBitset *self) |
| 67 |
{ |
| 68 |
g_return_val_if_fail (self != NULL, NULL); |
| 69 |
|
| 70 |
self->ref_count += 1; |
| 71 |
|
| 72 |
return self; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* egg_bitset_unref: |
| 77 |
* @self: (nullable): a `EggBitset` |
| 78 |
* |
| 79 |
* Releases a reference on the given `EggBitset`. |
| 80 |
* |
| 81 |
* If the reference was the last, the resources associated to the @self are |
| 82 |
* freed. |
| 83 |
*/ |
| 84 |
void |
| 85 |
egg_bitset_unref (EggBitset *self) |
| 86 |
{ |
| 87 |
g_return_if_fail (self != NULL); |
| 88 |
g_return_if_fail (self->ref_count > 0); |
| 89 |
|
| 90 |
self->ref_count -= 1; |
| 91 |
if (self->ref_count > 0) |
| 92 |
return; |
| 93 |
|
| 94 |
ra_clear (&self->roaring.high_low_container); |
| 95 |
g_free (self); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* egg_bitset_contains: |
| 100 |
* @self: a `EggBitset` |
| 101 |
* @value: the value to check |
| 102 |
* |
| 103 |
* Checks if the given @value has been added to @self |
| 104 |
* |
| 105 |
* Returns: %TRUE if @self contains @value |
| 106 |
**/ |
| 107 |
gboolean |
| 108 |
egg_bitset_contains (const EggBitset *self, |
| 109 |
guint value) |
| 110 |
{ |
| 111 |
g_return_val_if_fail (self != NULL, FALSE); |
| 112 |
|
| 113 |
return roaring_bitmap_contains (&self->roaring, value); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* egg_bitset_is_empty: |
| 118 |
* @self: a `EggBitset` |
| 119 |
* |
| 120 |
* Check if no value is contained in bitset. |
| 121 |
* |
| 122 |
* Returns: %TRUE if @self is empty |
| 123 |
**/ |
| 124 |
gboolean |
| 125 |
egg_bitset_is_empty (const EggBitset *self) |
| 126 |
{ |
| 127 |
g_return_val_if_fail (self != NULL, TRUE); |
| 128 |
|
| 129 |
return roaring_bitmap_is_empty (&self->roaring); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* egg_bitset_equals: |
| 134 |
* @self: a `EggBitset` |
| 135 |
* @other: another `EggBitset` |
| 136 |
* |
| 137 |
* Returns %TRUE if @self and @other contain the same values. |
| 138 |
* |
| 139 |
* Returns: %TRUE if @self and @other contain the same values |
| 140 |
**/ |
| 141 |
gboolean |
| 142 |
egg_bitset_equals (const EggBitset *self, |
| 143 |
const EggBitset *other) |
| 144 |
{ |
| 145 |
g_return_val_if_fail (self != NULL, other == NULL); |
| 146 |
g_return_val_if_fail (other != NULL, FALSE); |
| 147 |
|
| 148 |
if (self == other) |
| 149 |
return TRUE; |
| 150 |
|
| 151 |
return roaring_bitmap_equals (&self->roaring, &other->roaring); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* egg_bitset_get_minimum: |
| 156 |
* @self: a `EggBitset` |
| 157 |
* |
| 158 |
* Returns the smallest value in @self. |
| 159 |
* |
| 160 |
* If @self is empty, `G_MAXUINT` is returned. |
| 161 |
* |
| 162 |
* Returns: The smallest value in @self |
| 163 |
**/ |
| 164 |
guint |
| 165 |
egg_bitset_get_minimum (const EggBitset *self) |
| 166 |
{ |
| 167 |
g_return_val_if_fail (self != NULL, G_MAXUINT); |
| 168 |
|
| 169 |
return roaring_bitmap_minimum (&self->roaring); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* egg_bitset_get_maximum: |
| 174 |
* @self: a `EggBitset` |
| 175 |
* |
| 176 |
* Returns the largest value in @self. |
| 177 |
* |
| 178 |
* If @self is empty, 0 is returned. |
| 179 |
* |
| 180 |
* Returns: The largest value in @self |
| 181 |
**/ |
| 182 |
guint |
| 183 |
egg_bitset_get_maximum (const EggBitset *self) |
| 184 |
{ |
| 185 |
g_return_val_if_fail (self != NULL, 0); |
| 186 |
|
| 187 |
return roaring_bitmap_maximum (&self->roaring); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* egg_bitset_get_size: |
| 192 |
* @self: a `EggBitset` |
| 193 |
* |
| 194 |
* Gets the number of values that were added to the set. |
| 195 |
* |
| 196 |
* For example, if the set is empty, 0 is returned. |
| 197 |
* |
| 198 |
* Note that this function returns a `guint64`, because when all |
| 199 |
* values are set, the return value is `G_MAXUINT + 1`. Unless you |
| 200 |
* are sure this cannot happen (it can't with `GListModel`), be sure |
| 201 |
* to use a 64bit type. |
| 202 |
* |
| 203 |
* Returns: The number of values in the set. |
| 204 |
*/ |
| 205 |
guint64 |
| 206 |
egg_bitset_get_size (const EggBitset *self) |
| 207 |
{ |
| 208 |
g_return_val_if_fail (self != NULL, 0); |
| 209 |
|
| 210 |
return roaring_bitmap_get_cardinality (&self->roaring); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* egg_bitset_get_size_in_range: |
| 215 |
* @self: a `EggBitset` |
| 216 |
* @first: the first element to include |
| 217 |
* @last: the last element to include |
| 218 |
* |
| 219 |
* Gets the number of values that are part of the set from @first to @last |
| 220 |
* (inclusive). |
| 221 |
* |
| 222 |
* Note that this function returns a `guint64`, because when all values are |
| 223 |
* set, the return value is `G_MAXUINT + 1`. Unless you are sure this cannot |
| 224 |
* happen (it can't with `GListModel`), be sure to use a 64bit type. |
| 225 |
* |
| 226 |
* Returns: The number of values in the set from @first to @last. |
| 227 |
*/ |
| 228 |
guint64 |
| 229 |
egg_bitset_get_size_in_range (const EggBitset *self, |
| 230 |
guint first, |
| 231 |
guint last) |
| 232 |
{ |
| 233 |
g_return_val_if_fail (self != NULL, 0); |
| 234 |
g_return_val_if_fail (last >= first, 0); |
| 235 |
|
| 236 |
return roaring_bitmap_range_cardinality (&self->roaring, first, ((uint64_t) last) + 1); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* egg_bitset_get_nth: |
| 241 |
* @self: a `EggBitset` |
| 242 |
* @nth: index of the item to get |
| 243 |
* |
| 244 |
* Returns the value of the @nth item in self. |
| 245 |
* |
| 246 |
* If @nth is >= the size of @self, 0 is returned. |
| 247 |
* |
| 248 |
* Returns: the value of the @nth item in @self |
| 249 |
*/ |
| 250 |
guint |
| 251 |
egg_bitset_get_nth (const EggBitset *self, |
| 252 |
guint nth) |
| 253 |
{ |
| 254 |
uint32_t result; |
| 255 |
|
| 256 |
if (!roaring_bitmap_select (&self->roaring, nth, &result)) |
| 257 |
return 0; |
| 258 |
|
| 259 |
return result; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* egg_bitset_new_empty: |
| 264 |
* |
| 265 |
* Creates a new empty bitset. |
| 266 |
* |
| 267 |
* Returns: A new empty bitset |
| 268 |
*/ |
| 269 |
EggBitset * |
| 270 |
egg_bitset_new_empty (void) |
| 271 |
{ |
| 272 |
EggBitset *self; |
| 273 |
|
| 274 |
self = g_new0 (EggBitset, 1); |
| 275 |
|
| 276 |
self->ref_count = 1; |
| 277 |
|
| 278 |
ra_init (&self->roaring.high_low_container); |
| 279 |
|
| 280 |
return self; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* egg_bitset_new_range: |
| 285 |
* @start: first value to add |
| 286 |
* @n_items: number of consecutive values to add |
| 287 |
* |
| 288 |
* Creates a bitset with the given range set. |
| 289 |
* |
| 290 |
* Returns: A new bitset |
| 291 |
**/ |
| 292 |
EggBitset * |
| 293 |
egg_bitset_new_range (guint start, |
| 294 |
guint n_items) |
| 295 |
{ |
| 296 |
EggBitset *self; |
| 297 |
|
| 298 |
self = egg_bitset_new_empty (); |
| 299 |
|
| 300 |
egg_bitset_add_range (self, start, n_items); |
| 301 |
|
| 302 |
return self; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* egg_bitset_copy: |
| 307 |
* @self: a `EggBitset` |
| 308 |
* |
| 309 |
* Creates a copy of @self. |
| 310 |
* |
| 311 |
* Returns: (transfer full): A new bitset that contains the same |
| 312 |
* values as @self |
| 313 |
*/ |
| 314 |
EggBitset * |
| 315 |
egg_bitset_copy (const EggBitset *self) |
| 316 |
{ |
| 317 |
EggBitset *copy; |
| 318 |
|
| 319 |
g_return_val_if_fail (self != NULL, NULL); |
| 320 |
|
| 321 |
copy = egg_bitset_new_empty (); |
| 322 |
|
| 323 |
if (!egg_bitset_is_empty (self)) |
| 324 |
roaring_bitmap_overwrite (©->roaring, &self->roaring); |
| 325 |
|
| 326 |
return copy; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* egg_bitset_remove_all: |
| 331 |
* @self: a `EggBitset` |
| 332 |
* |
| 333 |
* Removes all values from the bitset so that it is empty again. |
| 334 |
*/ |
| 335 |
void |
| 336 |
egg_bitset_remove_all (EggBitset *self) |
| 337 |
{ |
| 338 |
g_return_if_fail (self != NULL); |
| 339 |
|
| 340 |
roaring_bitmap_clear (&self->roaring); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* egg_bitset_add: |
| 345 |
* @self: a `EggBitset` |
| 346 |
* @value: value to add |
| 347 |
* |
| 348 |
* Adds @value to @self if it wasn't part of it before. |
| 349 |
* |
| 350 |
* Returns: %TRUE if @value was not part of @self and @self |
| 351 |
* was changed |
| 352 |
*/ |
| 353 |
gboolean |
| 354 |
egg_bitset_add (EggBitset *self, |
| 355 |
guint value) |
| 356 |
{ |
| 357 |
g_return_val_if_fail (self != NULL, FALSE); |
| 358 |
|
| 359 |
return roaring_bitmap_add_checked (&self->roaring, value); |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* egg_bitset_remove: |
| 364 |
* @self: a `EggBitset` |
| 365 |
* @value: value to remove |
| 366 |
* |
| 367 |
* Removes @value from @self if it was part of it before. |
| 368 |
* |
| 369 |
* Returns: %TRUE if @value was part of @self and @self |
| 370 |
* was changed |
| 371 |
*/ |
| 372 |
gboolean |
| 373 |
egg_bitset_remove (EggBitset *self, |
| 374 |
guint value) |
| 375 |
{ |
| 376 |
g_return_val_if_fail (self != NULL, FALSE); |
| 377 |
|
| 378 |
return roaring_bitmap_remove_checked (&self->roaring, value); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* egg_bitset_add_range: |
| 383 |
* @self: a `EggBitset` |
| 384 |
* @start: first value to add |
| 385 |
* @n_items: number of consecutive values to add |
| 386 |
* |
| 387 |
* Adds all values from @start (inclusive) to @start + @n_items |
| 388 |
* (exclusive) in @self. |
| 389 |
*/ |
| 390 |
void |
| 391 |
egg_bitset_add_range (EggBitset *self, |
| 392 |
guint start, |
| 393 |
guint n_items) |
| 394 |
{ |
| 395 |
g_return_if_fail (self != NULL); |
| 396 |
|
| 397 |
if (n_items == 0) |
| 398 |
return; |
| 399 |
|
| 400 |
/* overflow check, the == 0 is to allow add_range(G_MAXUINT, 1); */ |
| 401 |
g_return_if_fail (start + n_items == 0 || start + n_items > start); |
| 402 |
|
| 403 |
roaring_bitmap_add_range_closed (&self->roaring, start, start + n_items - 1); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* egg_bitset_remove_range: |
| 408 |
* @self: a `EggBitset` |
| 409 |
* @start: first value to remove |
| 410 |
* @n_items: number of consecutive values to remove |
| 411 |
* |
| 412 |
* Removes all values from @start (inclusive) to @start + @n_items (exclusive) |
| 413 |
* in @self. |
| 414 |
*/ |
| 415 |
void |
| 416 |
egg_bitset_remove_range (EggBitset *self, |
| 417 |
guint start, |
| 418 |
guint n_items) |
| 419 |
{ |
| 420 |
g_return_if_fail (self != NULL); |
| 421 |
|
| 422 |
if (n_items == 0) |
| 423 |
return; |
| 424 |
|
| 425 |
/* overflow check, the == 0 is to allow add_range(G_MAXUINT, 1); */ |
| 426 |
g_return_if_fail (start + n_items == 0 || start + n_items > start); |
| 427 |
|
| 428 |
roaring_bitmap_remove_range_closed (&self->roaring, start, start + n_items - 1); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* egg_bitset_add_range_closed: |
| 433 |
* @self: a `EggBitset` |
| 434 |
* @first: first value to add |
| 435 |
* @last: last value to add |
| 436 |
* |
| 437 |
* Adds the closed range [@first, @last], so @first, @last and all |
| 438 |
* values in between. @first must be smaller than @last. |
| 439 |
*/ |
| 440 |
void |
| 441 |
egg_bitset_add_range_closed (EggBitset *self, |
| 442 |
guint first, |
| 443 |
guint last) |
| 444 |
{ |
| 445 |
g_return_if_fail (self != NULL); |
| 446 |
g_return_if_fail (first <= last); |
| 447 |
|
| 448 |
roaring_bitmap_add_range_closed (&self->roaring, first, last); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* egg_bitset_remove_range_closed: |
| 453 |
* @self: a `EggBitset` |
| 454 |
* @first: first value to remove |
| 455 |
* @last: last value to remove |
| 456 |
* |
| 457 |
* Removes the closed range [@first, @last], so @first, @last and all |
| 458 |
* values in between. @first must be smaller than @last. |
| 459 |
*/ |
| 460 |
void |
| 461 |
egg_bitset_remove_range_closed (EggBitset *self, |
| 462 |
guint first, |
| 463 |
guint last) |
| 464 |
{ |
| 465 |
g_return_if_fail (self != NULL); |
| 466 |
g_return_if_fail (first <= last); |
| 467 |
|
| 468 |
roaring_bitmap_remove_range_closed (&self->roaring, first, last); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* egg_bitset_add_rectangle: |
| 473 |
* @self: a `EggBitset` |
| 474 |
* @start: first value to add |
| 475 |
* @width: width of the rectangle |
| 476 |
* @height: height of the rectangle |
| 477 |
* @stride: row stride of the grid |
| 478 |
* |
| 479 |
* Interprets the values as a 2-dimensional boolean grid with the given @stride |
| 480 |
* and inside that grid, adds a rectangle with the given @width and @height. |
| 481 |
*/ |
| 482 |
void |
| 483 |
egg_bitset_add_rectangle (EggBitset *self, |
| 484 |
guint start, |
| 485 |
guint width, |
| 486 |
guint height, |
| 487 |
guint stride) |
| 488 |
{ |
| 489 |
guint i; |
| 490 |
|
| 491 |
g_return_if_fail (self != NULL); |
| 492 |
g_return_if_fail ((start % stride) + width <= stride); |
| 493 |
g_return_if_fail (G_MAXUINT - start >= height * stride); |
| 494 |
|
| 495 |
if (width == 0 || height == 0) |
| 496 |
return; |
| 497 |
|
| 498 |
for (i = 0; i < height; i++) |
| 499 |
egg_bitset_add_range (self, i * stride + start, width); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* egg_bitset_remove_rectangle: |
| 504 |
* @self: a `EggBitset` |
| 505 |
* @start: first value to remove |
| 506 |
* @width: width of the rectangle |
| 507 |
* @height: height of the rectangle |
| 508 |
* @stride: row stride of the grid |
| 509 |
* |
| 510 |
* Interprets the values as a 2-dimensional boolean grid with the given @stride |
| 511 |
* and inside that grid, removes a rectangle with the given @width and @height. |
| 512 |
*/ |
| 513 |
void |
| 514 |
egg_bitset_remove_rectangle (EggBitset *self, |
| 515 |
guint start, |
| 516 |
guint width, |
| 517 |
guint height, |
| 518 |
guint stride) |
| 519 |
{ |
| 520 |
guint i; |
| 521 |
|
| 522 |
g_return_if_fail (self != NULL); |
| 523 |
g_return_if_fail (width <= stride); |
| 524 |
g_return_if_fail (G_MAXUINT - start >= height * stride); |
| 525 |
|
| 526 |
if (width == 0 || height == 0) |
| 527 |
return; |
| 528 |
|
| 529 |
for (i = 0; i < height; i++) |
| 530 |
egg_bitset_remove_range (self, i * stride + start, width); |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* egg_bitset_union: |
| 535 |
* @self: a `EggBitset` |
| 536 |
* @other: the `EggBitset` to union with |
| 537 |
* |
| 538 |
* Sets @self to be the union of @self and @other. |
| 539 |
* |
| 540 |
* That is, add all values from @other into @self that weren't part of it. |
| 541 |
* |
| 542 |
* It is allowed for @self and @other to be the same bitset. Nothing will |
| 543 |
* happen in that case. |
| 544 |
*/ |
| 545 |
void |
| 546 |
egg_bitset_union (EggBitset *self, |
| 547 |
const EggBitset *other) |
| 548 |
{ |
| 549 |
g_return_if_fail (self != NULL); |
| 550 |
g_return_if_fail (other != NULL); |
| 551 |
|
| 552 |
if (self == other) |
| 553 |
return; |
| 554 |
|
| 555 |
roaring_bitmap_or_inplace (&self->roaring, &other->roaring); |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* egg_bitset_intersect: |
| 560 |
* @self: a `EggBitset` |
| 561 |
* @other: the `EggBitset` to intersect with |
| 562 |
* |
| 563 |
* Sets @self to be the intersection of @self and @other. |
| 564 |
* |
| 565 |
* In other words, remove all values from @self that are not part of @other. |
| 566 |
* |
| 567 |
* It is allowed for @self and @other to be the same bitset. Nothing will |
| 568 |
* happen in that case. |
| 569 |
*/ |
| 570 |
void |
| 571 |
egg_bitset_intersect (EggBitset *self, |
| 572 |
const EggBitset *other) |
| 573 |
{ |
| 574 |
g_return_if_fail (self != NULL); |
| 575 |
g_return_if_fail (other != NULL); |
| 576 |
|
| 577 |
if (self == other) |
| 578 |
return; |
| 579 |
|
| 580 |
roaring_bitmap_and_inplace (&self->roaring, &other->roaring); |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* egg_bitset_subtract: |
| 585 |
* @self: a `EggBitset` |
| 586 |
* @other: the `EggBitset` to subtract |
| 587 |
* |
| 588 |
* Sets @self to be the subtraction of @other from @self. |
| 589 |
* |
| 590 |
* In other words, remove all values from @self that are part of @other. |
| 591 |
* |
| 592 |
* It is allowed for @self and @other to be the same bitset. The bitset |
| 593 |
* will be emptied in that case. |
| 594 |
*/ |
| 595 |
void |
| 596 |
egg_bitset_subtract (EggBitset *self, |
| 597 |
const EggBitset *other) |
| 598 |
{ |
| 599 |
g_return_if_fail (self != NULL); |
| 600 |
g_return_if_fail (other != NULL); |
| 601 |
|
| 602 |
if (self == other) |
| 603 |
{ |
| 604 |
roaring_bitmap_clear (&self->roaring); |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
roaring_bitmap_andnot_inplace (&self->roaring, &other->roaring); |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* egg_bitset_difference: |
| 613 |
* @self: a `EggBitset` |
| 614 |
* @other: the `EggBitset` to compute the difference from |
| 615 |
* |
| 616 |
* Sets @self to be the symmetric difference of @self and @other. |
| 617 |
* |
| 618 |
* The symmetric difference is set @self to contain all values that |
| 619 |
* were either contained in @self or in @other, but not in both. |
| 620 |
* This operation is also called an XOR. |
| 621 |
* |
| 622 |
* It is allowed for @self and @other to be the same bitset. The bitset |
| 623 |
* will be emptied in that case. |
| 624 |
*/ |
| 625 |
void |
| 626 |
egg_bitset_difference (EggBitset *self, |
| 627 |
const EggBitset *other) |
| 628 |
{ |
| 629 |
g_return_if_fail (self != NULL); |
| 630 |
g_return_if_fail (other != NULL); |
| 631 |
|
| 632 |
if (self == other) |
| 633 |
{ |
| 634 |
roaring_bitmap_clear (&self->roaring); |
| 635 |
return; |
| 636 |
} |
| 637 |
|
| 638 |
roaring_bitmap_xor_inplace (&self->roaring, &other->roaring); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* egg_bitset_shift_left: |
| 643 |
* @self: a `EggBitset` |
| 644 |
* @amount: amount to shift all values to the left |
| 645 |
* |
| 646 |
* Shifts all values in @self to the left by @amount. |
| 647 |
* |
| 648 |
* Values smaller than @amount are discarded. |
| 649 |
*/ |
| 650 |
void |
| 651 |
egg_bitset_shift_left (EggBitset *self, |
| 652 |
guint amount) |
| 653 |
{ |
| 654 |
EggBitset *original; |
| 655 |
EggBitsetIter iter; |
| 656 |
guint value; |
| 657 |
gboolean loop; |
| 658 |
|
| 659 |
g_return_if_fail (self != NULL); |
| 660 |
|
| 661 |
if (amount == 0) |
| 662 |
return; |
| 663 |
|
| 664 |
original = egg_bitset_copy (self); |
| 665 |
egg_bitset_remove_all (self); |
| 666 |
|
| 667 |
for (loop = egg_bitset_iter_init_at (&iter, original, amount, &value); |
| 668 |
loop; |
| 669 |
loop = egg_bitset_iter_next (&iter, &value)) |
| 670 |
{ |
| 671 |
egg_bitset_add (self, value - amount); |
| 672 |
} |
| 673 |
|
| 674 |
egg_bitset_unref (original); |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* egg_bitset_shift_right: |
| 679 |
* @self: a `EggBitset` |
| 680 |
* @amount: amount to shift all values to the right |
| 681 |
* |
| 682 |
* Shifts all values in @self to the right by @amount. |
| 683 |
* |
| 684 |
* Values that end up too large to be held in a #guint are discarded. |
| 685 |
*/ |
| 686 |
void |
| 687 |
egg_bitset_shift_right (EggBitset *self, |
| 688 |
guint amount) |
| 689 |
{ |
| 690 |
EggBitset *original; |
| 691 |
EggBitsetIter iter; |
| 692 |
guint value; |
| 693 |
gboolean loop; |
| 694 |
|
| 695 |
g_return_if_fail (self != NULL); |
| 696 |
|
| 697 |
if (amount == 0) |
| 698 |
return; |
| 699 |
|
| 700 |
original = egg_bitset_copy (self); |
| 701 |
egg_bitset_remove_all (self); |
| 702 |
|
| 703 |
for (loop = egg_bitset_iter_init_first (&iter, original, &value); |
| 704 |
loop && value <= G_MAXUINT - amount; |
| 705 |
loop = egg_bitset_iter_next (&iter, &value)) |
| 706 |
{ |
| 707 |
egg_bitset_add (self, value + amount); |
| 708 |
} |
| 709 |
|
| 710 |
egg_bitset_unref (original); |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* egg_bitset_splice: |
| 715 |
* @self: a `EggBitset` |
| 716 |
* @position: position at which to slice |
| 717 |
* @removed: number of values to remove |
| 718 |
* @added: number of values to add |
| 719 |
* |
| 720 |
* This is a support function for `GListModel` handling, by mirroring |
| 721 |
* the `GlistModel::items-changed` signal. |
| 722 |
* |
| 723 |
* First, it "cuts" the values from @position to @removed from |
| 724 |
* the bitset. That is, it removes all those values and shifts |
| 725 |
* all larger values to the left by @removed places. |
| 726 |
* |
| 727 |
* Then, it "pastes" new room into the bitset by shifting all values |
| 728 |
* larger than @position by @added spaces to the right. This frees |
| 729 |
* up space that can then be filled. |
| 730 |
*/ |
| 731 |
void |
| 732 |
egg_bitset_splice (EggBitset *self, |
| 733 |
guint position, |
| 734 |
guint removed, |
| 735 |
guint added) |
| 736 |
{ |
| 737 |
g_return_if_fail (self != NULL); |
| 738 |
/* overflow */ |
| 739 |
g_return_if_fail (position + removed >= position); |
| 740 |
g_return_if_fail (position + added >= position); |
| 741 |
|
| 742 |
egg_bitset_remove_range (self, position, removed); |
| 743 |
|
| 744 |
if (removed != added) |
| 745 |
{ |
| 746 |
EggBitset *shift = egg_bitset_copy (self); |
| 747 |
|
| 748 |
egg_bitset_remove_range (shift, 0, position); |
| 749 |
egg_bitset_remove_range_closed (self, position, G_MAXUINT); |
| 750 |
if (added > removed) |
| 751 |
egg_bitset_shift_right (shift, added - removed); |
| 752 |
else |
| 753 |
egg_bitset_shift_left (shift, removed - added); |
| 754 |
egg_bitset_union (self, shift); |
| 755 |
egg_bitset_unref (shift); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
G_STATIC_ASSERT (sizeof (EggBitsetIter) >= sizeof (roaring_uint32_iterator_t)); |
| 760 |
|
| 761 |
static EggBitsetIter * |
| 762 |
egg_bitset_iter_copy (EggBitsetIter *iter) |
| 763 |
{ |
| 764 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 765 |
|
| 766 |
return (EggBitsetIter *) roaring_copy_uint32_iterator (riter); |
| 767 |
} |
| 768 |
|
| 769 |
static void |
| 770 |
egg_bitset_iter_free (EggBitsetIter *iter) |
| 771 |
{ |
| 772 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 773 |
|
| 774 |
roaring_free_uint32_iterator (riter); |
| 775 |
} |
| 776 |
|
| 777 |
G_DEFINE_BOXED_TYPE (EggBitsetIter, egg_bitset_iter, egg_bitset_iter_copy, egg_bitset_iter_free) |
| 778 |
|
| 779 |
/** |
| 780 |
* egg_bitset_iter_init_first: |
| 781 |
* @iter: (out): a pointer to an uninitialized `EggBitsetIter` |
| 782 |
* @set: a `EggBitset` |
| 783 |
* @value: (out) (optional): Set to the first value in @set |
| 784 |
* |
| 785 |
* Initializes an iterator for @set and points it to the first |
| 786 |
* value in @set. |
| 787 |
* |
| 788 |
* If @set is empty, %FALSE is returned and @value is set to %G_MAXUINT. |
| 789 |
* |
| 790 |
* Returns: %TRUE if @set isn't empty. |
| 791 |
*/ |
| 792 |
gboolean |
| 793 |
egg_bitset_iter_init_first (EggBitsetIter *iter, |
| 794 |
const EggBitset *set, |
| 795 |
guint *value) |
| 796 |
{ |
| 797 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 798 |
|
| 799 |
g_return_val_if_fail (iter != NULL, FALSE); |
| 800 |
g_return_val_if_fail (set != NULL, FALSE); |
| 801 |
|
| 802 |
roaring_init_iterator (&set->roaring, riter); |
| 803 |
|
| 804 |
if (value) |
| 805 |
*value = riter->has_value ? riter->current_value : 0; |
| 806 |
|
| 807 |
return riter->has_value; |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* egg_bitset_iter_init_last: |
| 812 |
* @iter: (out): a pointer to an uninitialized `EggBitsetIter` |
| 813 |
* @set: a `EggBitset` |
| 814 |
* @value: (out) (optional): Set to the last value in @set |
| 815 |
* |
| 816 |
* Initializes an iterator for @set and points it to the last |
| 817 |
* value in @set. |
| 818 |
* |
| 819 |
* If @set is empty, %FALSE is returned. |
| 820 |
* |
| 821 |
* Returns: %TRUE if @set isn't empty. |
| 822 |
**/ |
| 823 |
gboolean |
| 824 |
egg_bitset_iter_init_last (EggBitsetIter *iter, |
| 825 |
const EggBitset *set, |
| 826 |
guint *value) |
| 827 |
{ |
| 828 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 829 |
|
| 830 |
g_return_val_if_fail (iter != NULL, FALSE); |
| 831 |
g_return_val_if_fail (set != NULL, FALSE); |
| 832 |
|
| 833 |
roaring_init_iterator_last (&set->roaring, riter); |
| 834 |
|
| 835 |
if (value) |
| 836 |
*value = riter->has_value ? riter->current_value : 0; |
| 837 |
|
| 838 |
return riter->has_value; |
| 839 |
} |
| 840 |
|
| 841 |
/** |
| 842 |
* egg_bitset_iter_init_at: |
| 843 |
* @iter: (out): a pointer to an uninitialized `EggBitsetIter` |
| 844 |
* @set: a `EggBitset` |
| 845 |
* @target: target value to start iterating at |
| 846 |
* @value: (out) (optional): Set to the found value in @set |
| 847 |
* |
| 848 |
* Initializes @iter to point to @target. |
| 849 |
* |
| 850 |
* If @target is not found, finds the next value after it. |
| 851 |
* If no value >= @target exists in @set, this function returns %FALSE. |
| 852 |
* |
| 853 |
* Returns: %TRUE if a value was found. |
| 854 |
*/ |
| 855 |
gboolean |
| 856 |
egg_bitset_iter_init_at (EggBitsetIter *iter, |
| 857 |
const EggBitset *set, |
| 858 |
guint target, |
| 859 |
guint *value) |
| 860 |
{ |
| 861 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 862 |
|
| 863 |
g_return_val_if_fail (iter != NULL, FALSE); |
| 864 |
g_return_val_if_fail (set != NULL, FALSE); |
| 865 |
|
| 866 |
roaring_init_iterator (&set->roaring, riter); |
| 867 |
if (!roaring_move_uint32_iterator_equalorlarger (riter, target)) |
| 868 |
{ |
| 869 |
if (value) |
| 870 |
*value = 0; |
| 871 |
return FALSE; |
| 872 |
} |
| 873 |
|
| 874 |
if (value) |
| 875 |
*value = riter->current_value; |
| 876 |
|
| 877 |
return TRUE; |
| 878 |
} |
| 879 |
|
| 880 |
/** |
| 881 |
* egg_bitset_iter_next: |
| 882 |
* @iter: a pointer to a valid `EggBitsetIter` |
| 883 |
* @value: (out) (optional): Set to the next value |
| 884 |
* |
| 885 |
* Moves @iter to the next value in the set. |
| 886 |
* |
| 887 |
* If it was already pointing to the last value in the set, |
| 888 |
* %FALSE is returned and @iter is invalidated. |
| 889 |
* |
| 890 |
* Returns: %TRUE if a next value existed |
| 891 |
*/ |
| 892 |
gboolean |
| 893 |
egg_bitset_iter_next (EggBitsetIter *iter, |
| 894 |
guint *value) |
| 895 |
{ |
| 896 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 897 |
|
| 898 |
g_return_val_if_fail (iter != NULL, FALSE); |
| 899 |
|
| 900 |
if (!roaring_advance_uint32_iterator (riter)) |
| 901 |
{ |
| 902 |
if (value) |
| 903 |
*value = 0; |
| 904 |
return FALSE; |
| 905 |
} |
| 906 |
|
| 907 |
if (value) |
| 908 |
*value = riter->current_value; |
| 909 |
|
| 910 |
return TRUE; |
| 911 |
} |
| 912 |
|
| 913 |
/** |
| 914 |
* egg_bitset_iter_previous: |
| 915 |
* @iter: a pointer to a valid `EggBitsetIter` |
| 916 |
* @value: (out) (optional): Set to the previous value |
| 917 |
* |
| 918 |
* Moves @iter to the previous value in the set. |
| 919 |
* |
| 920 |
* If it was already pointing to the first value in the set, |
| 921 |
* %FALSE is returned and @iter is invalidated. |
| 922 |
* |
| 923 |
* Returns: %TRUE if a previous value existed |
| 924 |
*/ |
| 925 |
gboolean |
| 926 |
egg_bitset_iter_previous (EggBitsetIter *iter, |
| 927 |
guint *value) |
| 928 |
{ |
| 929 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 930 |
|
| 931 |
g_return_val_if_fail (iter != NULL, FALSE); |
| 932 |
|
| 933 |
if (!roaring_previous_uint32_iterator (riter)) |
| 934 |
{ |
| 935 |
if (value) |
| 936 |
*value = 0; |
| 937 |
return FALSE; |
| 938 |
} |
| 939 |
|
| 940 |
if (value) |
| 941 |
*value = riter->current_value; |
| 942 |
|
| 943 |
return TRUE; |
| 944 |
} |
| 945 |
|
| 946 |
/** |
| 947 |
* egg_bitset_iter_get_value: |
| 948 |
* @iter: a `EggBitsetIter` |
| 949 |
* |
| 950 |
* Gets the current value that @iter points to. |
| 951 |
* |
| 952 |
* If @iter is not valid and [[email protected]_valid] |
| 953 |
* returns %FALSE, this function returns 0. |
| 954 |
* |
| 955 |
* Returns: The current value pointer to by @iter |
| 956 |
*/ |
| 957 |
guint |
| 958 |
egg_bitset_iter_get_value (const EggBitsetIter *iter) |
| 959 |
{ |
| 960 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 961 |
|
| 962 |
g_return_val_if_fail (iter != NULL, 0); |
| 963 |
|
| 964 |
if (!riter->has_value) |
| 965 |
return 0; |
| 966 |
|
| 967 |
return riter->current_value; |
| 968 |
} |
| 969 |
|
| 970 |
/** |
| 971 |
* egg_bitset_iter_is_valid: |
| 972 |
* @iter: a `EggBitsetIter` |
| 973 |
* |
| 974 |
* Checks if @iter points to a valid value. |
| 975 |
* |
| 976 |
* Returns: %TRUE if @iter points to a valid value |
| 977 |
*/ |
| 978 |
gboolean |
| 979 |
egg_bitset_iter_is_valid (const EggBitsetIter *iter) |
| 980 |
{ |
| 981 |
roaring_uint32_iterator_t *riter = (roaring_uint32_iterator_t *) iter; |
| 982 |
|
| 983 |
g_return_val_if_fail (iter != NULL, FALSE); |
| 984 |
|
| 985 |
return riter->has_value; |
| 986 |
} |