GtkBitset

GtkBitset — Sets of integers

Types and Values

Object Hierarchy

    GBoxed
    ╰── GtkBitset

Includes

#include <gtk/gtk.h>

Description

GtkBitset is a data structure for representing a set of unsigned integers. Another name for this data structure is "bitmap".

The current implementation is based on roaring bitmaps.

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 GtkBitsetIter.

The main use case for GtkBitset is implementing complex selections for GtkSelectionModel.

Functions

gtk_bitset_ref ()

GtkBitset *
gtk_bitset_ref (GtkBitset *self);

Acquires a reference on the given GtkBitset.

Parameters

self

a GtkBitset.

[allow-none]

Returns

the GtkBitset with an additional reference.

[transfer none]

gtk_bitset_unref ()

void
gtk_bitset_unref (GtkBitset *self);

Releases a reference on the given GtkBitset.

If the reference was the last, the resources associated to the self are freed.

Parameters

self

a GtkBitset.

[allow-none]

gtk_bitset_new_empty ()

GtkBitset *
gtk_bitset_new_empty (void);

Creates a new empty bitset.

Returns

A new empty bitset

gtk_bitset_new_range ()

GtkBitset *
gtk_bitset_new_range (guint start,
                      guint n_items);

Creates a bitset with the given range set.

Parameters

start

first value to add

n_items

number of consecutive values to add

Returns

A new bitset

gtk_bitset_copy ()

GtkBitset *
gtk_bitset_copy (const GtkBitset *self);

Creates a copy of self .

Parameters

self

a GtkBitset

Returns

A new bitset that contains the same values as self .

[transfer full]

gtk_bitset_contains ()

gboolean
gtk_bitset_contains (const GtkBitset *self,
                     guint value);

Checks if the given value has been added to bitset

Parameters

self

a GtkBitset

value

the value to check

Returns

TRUE if self contains value

gtk_bitset_is_empty ()

gboolean
gtk_bitset_is_empty (const GtkBitset *self);

Check if no value is contained in bitset.

Parameters

self

a GtkBitset

Returns

TRUE if self is empty

gtk_bitset_equals ()

gboolean
gtk_bitset_equals (const GtkBitset *self,
                   const GtkBitset *other);

Returns TRUE if self and other contain the same values.

Parameters

self

a GtkBitset

other

another GtkBitset

Returns

TRUE if self and other contain the same values

gtk_bitset_get_minimum ()

guint
gtk_bitset_get_minimum (const GtkBitset *self);

Returns the smallest value in self . If self is empty, G_MAXUINT is returned.

Parameters

self

a GtkBitset

Returns

The smallest value in self

gtk_bitset_get_maximum ()

guint
gtk_bitset_get_maximum (const GtkBitset *self);

Returns the largest value in self . If self is empty, 0 is returned.

Parameters

self

a GtkBitset

Returns

The largest value in self

gtk_bitset_get_size ()

guint64
gtk_bitset_get_size (const GtkBitset *self);

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.

Parameters

self

a GtkBitset

Returns

The number of values in the set.

gtk_bitset_get_size_in_range ()

guint64
gtk_bitset_get_size_in_range (const GtkBitset *self,
                              guint first,
                              guint last);

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.

Parameters

self

a GtkBitset

first

the first element to include

last

the last element to include

Returns

The number of values in the set from first to last .

gtk_bitset_get_nth ()

guint
gtk_bitset_get_nth (const GtkBitset *self,
                    guint nth);

Returns the value of the nth item in self.

If nth is >= the size of self , 0 is returned.

Parameters

self

a GtkBitset

nth

index of the item to get

Returns

the value of the nth item in self

gtk_bitset_remove_all ()

void
gtk_bitset_remove_all (GtkBitset *self);

Removes all values from the bitset so that it is empty again.

Parameters

self

a GtkBitset

gtk_bitset_add ()

gboolean
gtk_bitset_add (GtkBitset *self,
                guint value);

Adds value to self if it wasn't part of it before.

Parameters

self

a GtkBitset

value

value to add

Returns

TRUE if value was not part of self and self was changed.

gtk_bitset_remove ()

gboolean
gtk_bitset_remove (GtkBitset *self,
                   guint value);

Removes value from self if it was part of it before.

Parameters

self

a GtkBitset

value

value to add

Returns

TRUE if value was part of self and self was changed.

gtk_bitset_add_range ()

void
gtk_bitset_add_range (GtkBitset *self,
                      guint start,
                      guint n_items);

Adds all values from start (inclusive) to start + n_items (exclusive) in self .

Parameters

self

a GtkBitset

start

first value to add

n_items

number of consecutive values to add

gtk_bitset_remove_range ()

void
gtk_bitset_remove_range (GtkBitset *self,
                         guint start,
                         guint n_items);

Removes all values from start (inclusive) to start + n_items (exclusive) in self .

Parameters

self

a GtkBitset

start

first value to remove

n_items

number of consecutive values to remove

gtk_bitset_add_range_closed ()

void
gtk_bitset_add_range_closed (GtkBitset *self,
                             guint first,
                             guint last);

Adds the closed range [first , last ], so first , last and all values in between. first must be smaller than last .

Parameters

self

a GtkBitset

first

first value to add

last

last value to add

gtk_bitset_remove_range_closed ()

void
gtk_bitset_remove_range_closed (GtkBitset *self,
                                guint first,
                                guint last);

Removes the closed range [first , last ], so first , last and all values in between. first must be smaller than last .

Parameters

self

a GtkBitset

first

first value to remove

last

last value to remove

gtk_bitset_add_rectangle ()

void
gtk_bitset_add_rectangle (GtkBitset *self,
                          guint start,
                          guint width,
                          guint height,
                          guint stride);

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 .

Parameters

self

a GtkBitset

start

first value to add

width

width of the rectangle

height

height of the rectangle

stride

row stride of the grid

gtk_bitset_remove_rectangle ()

void
gtk_bitset_remove_rectangle (GtkBitset *self,
                             guint start,
                             guint width,
                             guint height,
                             guint stride);

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 .

Parameters

self

a GtkBitset

start

first value to remove

width

width of the rectangle

height

height of the rectangle

stride

row stride of the grid

gtk_bitset_union ()

void
gtk_bitset_union (GtkBitset *self,
                  const GtkBitset *other);

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.

Parameters

self

a GtkBitset

other

the GtkBitset to union with

gtk_bitset_intersect ()

void
gtk_bitset_intersect (GtkBitset *self,
                      const GtkBitset *other);

Sets self to be the intersection of self and other , that is 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.

Parameters

self

a GtkBitset

other

the GtkBitset to intersect with

gtk_bitset_subtract ()

void
gtk_bitset_subtract (GtkBitset *self,
                     const GtkBitset *other);

Sets self to be the subtraction of other from self , that is 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.

Parameters

self

a GtkBitset

other

the GtkBitset to subtract

gtk_bitset_difference ()

void
gtk_bitset_difference (GtkBitset *self,
                       const GtkBitset *other);

Sets self to be the symmetric difference of self and other , that 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.

Parameters

self

a GtkBitset

other

the GtkBitset to compute the difference from

gtk_bitset_shift_left ()

void
gtk_bitset_shift_left (GtkBitset *self,
                       guint amount);

Shifts all values in self to the left by amount . Values smaller than amount are discarded.

Parameters

self

a $GtkBitset

amount

amount to shift all values to the left

gtk_bitset_shift_right ()

void
gtk_bitset_shift_right (GtkBitset *self,
                        guint amount);

Shifts all values in self to the right by amount . Values that end up too large to be held in a guint are discarded.

Parameters

self

a $GtkBitset

amount

amount to shift all values to the right

gtk_bitset_splice ()

void
gtk_bitset_splice (GtkBitset *self,
                   guint position,
                   guint removed,
                   guint added);

This is a support function for GListModel handling, by mirroring the “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.

Parameters

self

a GtkBitset

position

position at which to slice

removed

number of values to remove

added

number of values to add

gtk_bitset_iter_init_first ()

gboolean
gtk_bitset_iter_init_first (GtkBitsetIter *iter,
                            const GtkBitset *set,
                            guint *value);

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.

Parameters

iter

a pointer to an uninitialized GtkBitsetIter.

[out]

set

a GtkBitset

value

Set to the first value in set .

[out][optional]

Returns

TRUE if set isn't empty.

gtk_bitset_iter_init_last ()

gboolean
gtk_bitset_iter_init_last (GtkBitsetIter *iter,
                           const GtkBitset *set,
                           guint *value);

Initializes an iterator for set and points it to the last value in set . If set is empty, FALSE is returned.

Parameters

iter

a pointer to an uninitialized GtkBitsetIter.

[out]

set

a GtkBitset

value

Set to the last value in set .

[out][optional]

Returns

TRUE if set isn't empty.

gtk_bitset_iter_init_at ()

gboolean
gtk_bitset_iter_init_at (GtkBitsetIter *iter,
                         const GtkBitset *set,
                         guint target,
                         guint *value);

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.

Parameters

iter

a pointer to an uninitialized GtkBitsetIter.

[out]

set

a GtkBitset

target

target value to start iterating at

value

Set to the found value in set .

[out][optional]

Returns

TRUE if a value was found.

gtk_bitset_iter_next ()

gboolean
gtk_bitset_iter_next (GtkBitsetIter *iter,
                      guint *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.

Parameters

iter

a pointer to a valid GtkBitsetIter

value

Set to the next value.

[out][optional]

Returns

TRUE if a next value existed

gtk_bitset_iter_previous ()

gboolean
gtk_bitset_iter_previous (GtkBitsetIter *iter,
                          guint *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.

Parameters

iter

a pointer to a valid GtkBitsetIter

value

Set to the previous value.

[out][optional]

Returns

TRUE if a previous value existed

gtk_bitset_iter_get_value ()

guint
gtk_bitset_iter_get_value (const GtkBitsetIter *iter);

Gets the current value that iter points to.

If iter is not valid and gtk_bitset_iter_is_valid() returns FALSE, this function returns 0.

Parameters

iter

a GtkBitsetIter

Returns

The current value pointer to by iter

gtk_bitset_iter_is_valid ()

gboolean
gtk_bitset_iter_is_valid (const GtkBitsetIter *iter);

Checks if iter points to a valid value.

Parameters

iter

a GtkBitsetIter

Returns

TRUE if iter points to a valid value

Types and Values

GtkBitset

typedef struct _GtkBitset GtkBitset;

The GtkBitset structure contains only private data.

struct GtkBitsetIter

struct GtkBitsetIter {
};

An opaque, stack-allocated struct for iterating over the elements of a GtkBitset. Before a GtkBitsetIter can be used, it needs to be initialized with gtk_bitset_iter_init_first(), gtk_bitset_iter_init_last() or gtk_bitset_iter_init_at().

© 2005–2020 The GNOME Project
Licensed under the GNU Lesser General Public License version 2.1 or later.
https://developer.gnome.org/gtk4/4.0/GtkBitset.html