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