Bitset#
- class Bitset(*args, **kwargs)#
A GtkBitset
represents 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 BitsetIter
.
The main use case for GtkBitset
is implementing complex selections for
SelectionModel
.
Constructors#
Methods#
- class Bitset
- add(value: int) bool #
Adds
value
toself
if it wasn’t part of it before.- Parameters:
value – value to add
- add_range(start: int, n_items: int) None #
Adds all values from
start
(inclusive) tostart
+n_items
(exclusive) inself
.- Parameters:
start – first value to add
n_items – number of consecutive values to add
- add_range_closed(first: int, last: int) None #
Adds the closed range [
first
,last
], sofirst
,last
and all values in between.first
must be smaller thanlast
.- Parameters:
first – first value to add
last – last value to add
- add_rectangle(start: int, width: int, height: int, stride: int) None #
Interprets the values as a 2-dimensional boolean grid with the given
stride
and inside that grid, adds a rectangle with the givenwidth
andheight
.- Parameters:
start – first value to add
width – width of the rectangle
height – height of the rectangle
stride – row stride of the grid
- contains(value: int) bool #
Checks if the given
value
has been added toself
- Parameters:
value – the value to check
- difference(other: Bitset) None #
Sets
self
to be the symmetric difference ofself
andother
.The symmetric difference is set
self
to contain all values that were either contained inself
or inother
, but not in both. This operation is also called an XOR.It is allowed for
self
andother
to be the same bitset. The bitset will be emptied in that case.- Parameters:
other – the
GtkBitset
to compute the difference from
- equals(other: Bitset) bool #
Returns
True
ifself
andother
contain the same values.- Parameters:
other – another
GtkBitset
- get_nth(nth: int) int #
Returns the value of the
nth
item in self.If
nth
is >= the size ofself
, 0 is returned.- Parameters:
nth – index of the item to get
- get_size() int #
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 isG_MAXUINT + 1
. Unless you are sure this cannot happen (it can’t withGListModel
), be sure to use a 64bit type.
- get_size_in_range(first: int, last: int) int #
Gets the number of values that are part of the set from
first
tolast
(inclusive).Note that this function returns a
guint64
, because when all values are set, the return value isG_MAXUINT + 1
. Unless you are sure this cannot happen (it can’t withGListModel
), be sure to use a 64bit type.- Parameters:
first – the first element to include
last – the last element to include
- intersect(other: Bitset) None #
Sets
self
to be the intersection ofself
andother
.In other words, remove all values from
self
that are not part ofother
.It is allowed for
self
andother
to be the same bitset. Nothing will happen in that case.- Parameters:
other – the
GtkBitset
to intersect with
- remove(value: int) bool #
Removes
value
fromself
if it was part of it before.- Parameters:
value – value to remove
- remove_range(start: int, n_items: int) None #
Removes all values from
start
(inclusive) tostart
+n_items
(exclusive) inself
.- Parameters:
start – first value to remove
n_items – number of consecutive values to remove
- remove_range_closed(first: int, last: int) None #
Removes the closed range [
first
,last
], sofirst
,last
and all values in between.first
must be smaller thanlast
.- Parameters:
first – first value to remove
last – last value to remove
- remove_rectangle(start: int, width: int, height: int, stride: int) None #
Interprets the values as a 2-dimensional boolean grid with the given
stride
and inside that grid, removes a rectangle with the givenwidth
andheight
.- Parameters:
start – first value to remove
width – width of the rectangle
height – height of the rectangle
stride – row stride of the grid
- shift_left(amount: int) None #
Shifts all values in
self
to the left byamount
.Values smaller than
amount
are discarded.- Parameters:
amount – amount to shift all values to the left
- shift_right(amount: int) None #
Shifts all values in
self
to the right byamount
.Values that end up too large to be held in a
guint
are discarded.- Parameters:
amount – amount to shift all values to the right
- splice(position: int, removed: int, added: int) None #
This is a support function for
GListModel
handling, by mirroring theGlistModel::items-changed
signal.First, it “cuts” the values from
position
toremoved
from the bitset. That is, it removes all those values and shifts all larger values to the left byremoved
places.Then, it “pastes” new room into the bitset by shifting all values larger than
position
byadded
spaces to the right. This frees up space that can then be filled.- Parameters:
position – position at which to slice
removed – number of values to remove
added – number of values to add