Class Bitset
- All Implemented Interfaces:
Proxy
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 Gtk.BitsetIter
.
The main use case for GtkBitset
is implementing complex selections for
SelectionModel
.
-
Constructor Summary
ConstructorDescriptionBitset
(MemorySegment address) Create a Bitset proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(int value) Addsvalue
to this Bitset if it wasn't part of it before.void
addRange
(int start, int nItems) Adds all values fromstart
(inclusive) tostart
+nItems
(exclusive) in this Bitset.void
addRangeClosed
(int first, int last) Adds the closed rangelast
, sofirst
,last
and all values in between.void
addRectangle
(int start, int width, int height, int stride) Interprets the values as a 2-dimensional boolean grid with the givenstride
and inside that grid, adds a rectangle with the givenwidth
andheight
.boolean
contains
(int value) Checks if the givenvalue
has been added to this Bitsetcopy()
Creates a copy of this Bitset.void
difference
(Bitset other) Sets this Bitset to be the symmetric difference of this Bitset andother
.static Bitset
empty()
Creates a new empty bitset.boolean
Returnstrue
if this Bitset andother
contain the same values.int
Returns the largest value in this Bitset.int
Returns the smallest value in this Bitset.int
getNth
(int nth) Returns the value of thenth
item in self.long
getSize()
Gets the number of values that were added to the set.long
getSizeInRange
(int first, int last) Gets the number of values that are part of the set fromfirst
tolast
(inclusive).static Type
getType()
Get the GType of the Bitset classvoid
Sets this Bitset to be the intersection of this Bitset andother
.boolean
isEmpty()
Check if no value is contained in bitset.static Bitset
range
(int start, int nItems) Creates a bitset with the given range set.ref()
Acquires a reference on the givenGtkBitset
.boolean
remove
(int value) Removesvalue
from this Bitset if it was part of it before.void
Removes all values from the bitset so that it is empty again.void
removeRange
(int start, int nItems) Removes all values fromstart
(inclusive) tostart
+nItems
(exclusive) in this Bitset.void
removeRangeClosed
(int first, int last) Removes the closed rangelast
, sofirst
,last
and all values in between.void
removeRectangle
(int start, int width, int height, int stride) Interprets the values as a 2-dimensional boolean grid with the givenstride
and inside that grid, removes a rectangle with the givenwidth
andheight
.void
shiftLeft
(int amount) Shifts all values in this Bitset to the left byamount
.void
shiftRight
(int amount) Shifts all values in this Bitset to the right byamount
.void
splice
(int position, int removed, int added) This is a support function forGListModel
handling, by mirroring theGlistModel::items-changed
signal.void
Sets this Bitset to be the subtraction ofother
from this Bitset.void
Sets this Bitset to be the union of this Bitset andother
.void
unref()
Releases a reference on the givenGtkBitset
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Bitset
Create a Bitset proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
-
Method Details
-
getType
-
empty
-
range
Creates a bitset with the given range set.- Parameters:
start
- first value to addnItems
- number of consecutive values to add- Returns:
- A new bitset
-
add
public boolean add(int value) Addsvalue
to this Bitset if it wasn't part of it before.- Parameters:
value
- value to add- Returns:
true
ifvalue
was not part of this Bitset and this Bitset was changed
-
addRange
public void addRange(int start, int nItems) Adds all values fromstart
(inclusive) tostart
+nItems
(exclusive) in this Bitset.- Parameters:
start
- first value to addnItems
- number of consecutive values to add
-
addRangeClosed
public void addRangeClosed(int first, int last) Adds the closed rangelast
, sofirst
,last
and all values in between.first
must be smaller thanlast
.- Parameters:
first
- first value to addlast
- last value to add
-
addRectangle
public void addRectangle(int start, int width, int height, int stride) Interprets the values as a 2-dimensional boolean grid with the givenstride
and inside that grid, adds a rectangle with the givenwidth
andheight
.- Parameters:
start
- first value to addwidth
- width of the rectangleheight
- height of the rectanglestride
- row stride of the grid
-
contains
public boolean contains(int value) Checks if the givenvalue
has been added to this Bitset- Parameters:
value
- the value to check- Returns:
true
if this Bitset containsvalue
-
copy
Creates a copy of this Bitset.- Returns:
- A new bitset that contains the same values as this Bitset
-
difference
Sets this Bitset to be the symmetric difference of this Bitset andother
.The symmetric difference is set this Bitset to contain all values that were either contained in this Bitset or in
other
, but not in both. This operation is also called an XOR.It is allowed for this Bitset and
other
to be the same bitset. The bitset will be emptied in that case.- Parameters:
other
- theGtkBitset
to compute the difference from
-
equals
Returnstrue
if this Bitset andother
contain the same values.- Parameters:
other
- anotherGtkBitset
- Returns:
true
if this Bitset andother
contain the same values
-
getMaximum
public int getMaximum()Returns the largest value in this Bitset.If this Bitset is empty, 0 is returned.
- Returns:
- The largest value in this Bitset
-
getMinimum
public int getMinimum()Returns the smallest value in this Bitset.If this Bitset is empty,
G_MAXUINT
is returned.- Returns:
- The smallest value in this Bitset
-
getNth
public int getNth(int nth) Returns the value of thenth
item in self.If
nth
is >= the size of this Bitset, 0 is returned.- Parameters:
nth
- index of the item to get- Returns:
- the value of the
nth
item in this Bitset
-
getSize
public long getSize()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.- Returns:
- The number of values in the set.
-
getSizeInRange
public long getSizeInRange(int first, int last) Gets the number of values that are part of the set fromfirst
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 includelast
- the last element to include- Returns:
- The number of values in the set from
first
tolast
.
-
intersect
Sets this Bitset to be the intersection of this Bitset andother
.In other words, remove all values from this Bitset that are not part of
other
.It is allowed for this Bitset and
other
to be the same bitset. Nothing will happen in that case.- Parameters:
other
- theGtkBitset
to intersect with
-
isEmpty
public boolean isEmpty()Check if no value is contained in bitset.- Returns:
true
if this Bitset is empty
-
ref
Acquires a reference on the givenGtkBitset
.- Returns:
- the
GtkBitset
with an additional reference
-
remove
public boolean remove(int value) Removesvalue
from this Bitset if it was part of it before.- Parameters:
value
- value to remove- Returns:
true
ifvalue
was part of this Bitset and this Bitset was changed
-
removeAll
public void removeAll()Removes all values from the bitset so that it is empty again. -
removeRange
public void removeRange(int start, int nItems) Removes all values fromstart
(inclusive) tostart
+nItems
(exclusive) in this Bitset.- Parameters:
start
- first value to removenItems
- number of consecutive values to remove
-
removeRangeClosed
public void removeRangeClosed(int first, int last) Removes the closed rangelast
, sofirst
,last
and all values in between.first
must be smaller thanlast
.- Parameters:
first
- first value to removelast
- last value to remove
-
removeRectangle
public void removeRectangle(int start, int width, int height, int stride) Interprets the values as a 2-dimensional boolean grid with the givenstride
and inside that grid, removes a rectangle with the givenwidth
andheight
.- Parameters:
start
- first value to removewidth
- width of the rectangleheight
- height of the rectanglestride
- row stride of the grid
-
shiftLeft
public void shiftLeft(int amount) Shifts all values in this Bitset to the left byamount
.Values smaller than
amount
are discarded.- Parameters:
amount
- amount to shift all values to the left
-
shiftRight
public void shiftRight(int amount) Shifts all values in this Bitset 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
public void splice(int position, int removed, int added) This is a support function forGListModel
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 sliceremoved
- number of values to removeadded
- number of values to add
-
subtract
Sets this Bitset to be the subtraction ofother
from this Bitset.In other words, remove all values from this Bitset that are part of
other
.It is allowed for this Bitset and
other
to be the same bitset. The bitset will be emptied in that case.- Parameters:
other
- theGtkBitset
to subtract
-
union
Sets this Bitset to be the union of this Bitset andother
.That is, add all values from
other
into this Bitset that weren't part of it.It is allowed for this Bitset and
other
to be the same bitset. Nothing will happen in that case.- Parameters:
other
- theGtkBitset
to union with
-
unref
public void unref()Releases a reference on the givenGtkBitset
.If the reference was the last, the resources associated to the this Bitset are freed.
-