Class Array
-
Constructor Summary
ConstructorDescriptionArray()
Allocate a new Array.Allocate a new Array.Array
(MemorySegment address) Create a Array proxy instance for the provided memory address.Allocate a new Array with the fields set to the provided values.Allocate a new Array with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegment[]
appendVals
(MemorySegment[] array, MemorySegment data, int len) Addslen
elements onto the end of the array.static boolean
binarySearch
(MemorySegment[] array, @Nullable MemorySegment target, CompareFunc compareFunc, @Nullable Out<Integer> outMatchIndex) Checks whethertarget
exists inarray
by performing a binary search based on the given comparison functioncompareFunc
which get pointers to items as arguments.static MemorySegment[]
copy
(MemorySegment[] array) Create a shallow copy of aGArray
.static String
free
(MemorySegment[] array, boolean freeSegment) Frees the memory allocated for theGArray
.static int
getElementSize
(MemorySegment[] array) Gets the size of the elements inarray
.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the Array classstatic MemorySegment[]
insertVals
(MemorySegment[] array, int index, @Nullable MemorySegment data, int len) Insertslen
elements into aGArray
at the given index.static MemorySegment[]
new_
(boolean zeroTerminated, boolean clear, int elementSize) Creates a newGArray
with a reference count of 1.static MemorySegment[]
newTake
(@Nullable MemorySegment[] data, boolean clear, long elementSize) Creates a newGArray
withdata
as array data,len
as length and a reference count of 1.static MemorySegment[]
newTakeZeroTerminated
(MemorySegment[] data, boolean clear, long elementSize) Creates a newGArray
withdata
as array data, computing the length of it and setting the reference count to 1.static MemorySegment[]
prependVals
(MemorySegment[] array, @Nullable MemorySegment data, int len) Addslen
elements onto the start of the array.readData()
Read the value of the fielddata
.int
readLen()
Read the value of the fieldlen
.static MemorySegment[]
ref
(MemorySegment[] array) Atomically increments the reference count ofarray
by one.static MemorySegment[]
removeIndex
(MemorySegment[] array, int index) Removes the element at the given index from aGArray
.static MemorySegment[]
removeIndexFast
(MemorySegment[] array, int index) Removes the element at the given index from aGArray
.static MemorySegment[]
removeRange
(MemorySegment[] array, int index, int length) Removes the given number of elements starting at the given index from aGArray
.static void
setClearFunc
(MemorySegment[] array) Sets a function to clear an element ofarray
.static MemorySegment[]
setSize
(MemorySegment[] array, int length) Sets the size of the array, expanding it if necessary.static MemorySegment[]
sizedNew
(boolean zeroTerminated, boolean clear, int elementSize, int reservedSize) Creates a newGArray
withreservedSize
elements preallocated and a reference count of 1.static void
sort
(MemorySegment[] array, CompareFunc compareFunc) Sorts aGArray
usingcompareFunc
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater zero if first arg is greater than second arg).static void
sortWithData
(MemorySegment[] array, CompareDataFunc compareFunc) Like g_array_sort(), but the comparison function receives an extra user data argument.static MemorySegment
steal
(MemorySegment[] array, @Nullable Out<Long> len) Frees the data in the array and resets the size to zero, while the underlying array is preserved for use elsewhere and returned to the caller.static void
unref
(MemorySegment[] array) Atomically decrements the reference count ofarray
by one.void
Write a value in the fielddata
.void
writeLen
(int len) Write a value in the fieldlen
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Array
Create a Array proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Array
Allocate a new Array.- Parameters:
arena
- to control the memory allocation scope
-
Array
public Array()Allocate a new Array. The memory is allocated withArena.ofAuto()
. -
Array
-
Array
Allocate a new Array with the fields set to the provided values. The memory is allocated withArena.ofAuto()
.- Parameters:
data
- value for the fielddata
len
- value for the fieldlen
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readData
-
writeData
-
readLen
public int readLen()Read the value of the fieldlen
.- Returns:
- The value of the field
len
-
writeLen
public void writeLen(int len) Write a value in the fieldlen
.- Parameters:
len
- The new value for the fieldlen
-
appendVals
Addslen
elements onto the end of the array.- Parameters:
array
- aGArray
data
- a pointer to the elements to append to the end of the arraylen
- the number of elements to append- Returns:
- the
GArray
-
binarySearch
public static boolean binarySearch(MemorySegment[] array, @Nullable @Nullable MemorySegment target, CompareFunc compareFunc, @Nullable @Nullable Out<Integer> outMatchIndex) Checks whethertarget
exists inarray
by performing a binary search based on the given comparison functioncompareFunc
which get pointers to items as arguments. If the element is found,true
is returned and the element’s index is returned inoutMatchIndex
(if non-null
). Otherwise,false
is returned andoutMatchIndex
is undefined. Iftarget
exists multiple times inarray
, the index of the first instance is returned. This search is using a binary search, so thearray
must absolutely be sorted to return a correct result (if not, the function may produce false-negative).This example defines a comparison function and search an element in a
GArray
:static gint cmpint (gconstpointer a, gconstpointer b) { const gint *_a = a; const gint *_b = b; return *_a - *_b; } ... gint i = 424242; guint matched_index; gboolean result = g_array_binary_search (garray, &i, cmpint, &matched_index); ...
- Parameters:
array
- aGArray
.target
- a pointer to the item to look up.compareFunc
- AGCompareFunc
used to locatetarget
.outMatchIndex
- return location for the index of the element, if found.- Returns:
true
iftarget
is one of the elements ofarray
,false
otherwise.
-
copy
Create a shallow copy of aGArray
. If the array elements consist of pointers to data, the pointers are copied but the actual data is not.- Parameters:
array
- AGArray
.- Returns:
- A copy of
array
.
-
free
Frees the memory allocated for theGArray
. IffreeSegment
istrue
it frees the memory block holding the elements as well. Passfalse
if you want to free theGArray
wrapper but preserve the underlying array for use elsewhere. If the reference count ofarray
is greater than one, theGArray
wrapper is preserved but the size ofarray
will be set to zero.If array contents point to dynamically-allocated memory, they should be freed separately if
freeSeg
istrue
and noclearFunc
function has been set forarray
.This function is not thread-safe. If using a
GArray
from multiple threads, use only the atomic g_array_ref() and g_array_unref() functions.- Parameters:
array
- aGArray
freeSegment
- iftrue
the actual element data is freed as well- Returns:
- the element data if
freeSegment
isfalse
, otherwisenull
. The element data should be freed using g_free().
-
getElementSize
Gets the size of the elements inarray
.- Parameters:
array
- AGArray
- Returns:
- Size of each element, in bytes
-
insertVals
public static MemorySegment[] insertVals(MemorySegment[] array, int index, @Nullable @Nullable MemorySegment data, int len) Insertslen
elements into aGArray
at the given index.If
index
is greater than the array’s current length, the array is expanded. The elements between the old end of the array and the newly inserted elements will be initialised to zero if the array was configured to clear elements; otherwise their values will be undefined.If
index
is less than the array’s current length, new entries will be inserted into the array, and the existing entries aboveindex
will be moved upwards.data
may benull
if (and only if)len
is zero. Iflen
is zero, this function is a no-op.- Parameters:
array
- aGArray
index
- the index to place the elements atdata
- a pointer to the elements to insertlen
- the number of elements to insert- Returns:
- the
GArray
-
new_
Creates a newGArray
with a reference count of 1.- Parameters:
zeroTerminated
-true
if the array should have an extra element at the end which is set to 0clear
-true
ifGArray
elements should be automatically cleared to 0 when they are allocatedelementSize
- the size of each element in bytes- Returns:
- the new
GArray
-
newTake
public static MemorySegment[] newTake(@Nullable @Nullable MemorySegment[] data, boolean clear, long elementSize) Creates a newGArray
withdata
as array data,len
as length and a reference count of 1.This avoids having to copy the data manually, when it can just be inherited. After this call,
data
belongs to theGArray
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed with g_free().In case the elements need to be cleared when the array is freed, use g_array_set_clear_func() to set a
GDestroyNotify
function to perform such task.Do not use it if
len
orelementSize
are greater thanG_MAXUINT
.GArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- an array of elements ofelementSize
, ornull
for an empty arrayclear
-true
ifGArray
elements should be automatically cleared to 0 when they are allocatedelementSize
- the size of each element in bytes- Returns:
- A new
GArray
-
newTakeZeroTerminated
public static MemorySegment[] newTakeZeroTerminated(MemorySegment[] data, boolean clear, long elementSize) Creates a newGArray
withdata
as array data, computing the length of it and setting the reference count to 1.This avoids having to copy the data manually, when it can just be inherited. After this call,
data
belongs to theGArray
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed with g_free().The length is calculated by iterating through
data
until the firstnull
element is found.In case the elements need to be cleared when the array is freed, use g_array_set_clear_func() to set a
GDestroyNotify
function to perform such task.Do not use it if
data
length orelementSize
are greater thanG_MAXUINT
.GArray
stores the length of its data inguint
, which may be shorter thangsize
.- Parameters:
data
- an array of elements ofelementSize
clear
-true
ifGArray
elements should be automatically cleared to 0 when they are allocatedelementSize
- the size of each element in bytes- Returns:
- A new
GArray
-
prependVals
public static MemorySegment[] prependVals(MemorySegment[] array, @Nullable @Nullable MemorySegment data, int len) Addslen
elements onto the start of the array.data
may benull
if (and only if)len
is zero. Iflen
is zero, this function is a no-op.This operation is slower than g_array_append_vals() since the existing elements in the array have to be moved to make space for the new elements.
- Parameters:
array
- aGArray
data
- a pointer to the elements to prepend to the start of the arraylen
- the number of elements to prepend, which may be zero- Returns:
- the
GArray
-
ref
Atomically increments the reference count ofarray
by one. This function is thread-safe and may be called from any thread.- Parameters:
array
- AGArray
- Returns:
- The passed in
GArray
-
removeIndex
Removes the element at the given index from aGArray
. The following elements are moved down one place.- Parameters:
array
- aGArray
index
- the index of the element to remove- Returns:
- the
GArray
-
removeIndexFast
Removes the element at the given index from aGArray
. The last element in the array is used to fill in the space, so this function does not preserve the order of theGArray
. But it is faster than g_array_remove_index().- Parameters:
array
- aGArray
index
- the index of the element to remove- Returns:
- the
GArray
-
removeRange
Removes the given number of elements starting at the given index from aGArray
. The following elements are moved to close the gap.- Parameters:
array
- aGArray
index
- the index of the first element to removelength
- the number of elements to remove- Returns:
- the
GArray
-
setClearFunc
Sets a function to clear an element ofarray
.The
clearFunc
will be called when an element in the array data segment is removed and when the array is freed and data segment is deallocated as well.clearFunc
will be passed a pointer to the element to clear, rather than the element itself.Note that in contrast with other uses of
GDestroyNotify
functions,clearFunc
is expected to clear the contents of the array element it is given, but not free the element itself.typedef struct { gchar *str; GObject *obj; } ArrayElement; static void array_element_clear (ArrayElement *element) { g_clear_pointer (&element->str, g_free); g_clear_object (&element->obj); } // main code GArray *garray = g_array_new (FALSE, FALSE, sizeof (ArrayElement)); g_array_set_clear_func (garray, (GDestroyNotify) array_element_clear); // assign data to the structure g_array_free (garray, TRUE);
- Parameters:
array
- AGArray
-
setSize
Sets the size of the array, expanding it if necessary. If the array was created withclear
set totrue
, the new elements are set to 0.- Parameters:
array
- aGArray
length
- the new size of theGArray
- Returns:
- the
GArray
-
sizedNew
public static MemorySegment[] sizedNew(boolean zeroTerminated, boolean clear, int elementSize, int reservedSize) Creates a newGArray
withreservedSize
elements preallocated and a reference count of 1. This avoids frequent reallocation, if you are going to add many elements to the array. Note however that the size of the array is still 0.- Parameters:
zeroTerminated
-true
if the array should have an extra element at the end with all bits clearedclear
-true
if all bits in the array should be cleared to 0 on allocationelementSize
- size of each element in the arrayreservedSize
- number of elements preallocated- Returns:
- the new
GArray
-
sort
Sorts aGArray
usingcompareFunc
which should be a qsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater zero if first arg is greater than second arg).This is guaranteed to be a stable sort since version 2.32.
- Parameters:
array
- aGArray
compareFunc
- comparison function
-
sortWithData
Like g_array_sort(), but the comparison function receives an extra user data argument.This is guaranteed to be a stable sort since version 2.32.
There used to be a comment here about making the sort stable by using the addresses of the elements in the comparison function. This did not actually work, so any such code should be removed.
- Parameters:
array
- aGArray
compareFunc
- comparison function
-
steal
Frees the data in the array and resets the size to zero, while the underlying array is preserved for use elsewhere and returned to the caller.If the array was created with the
zeroTerminate
property set totrue
, the returned data is zero terminated too.If array elements contain dynamically-allocated memory, the array elements should also be freed by the caller.
A short example of use:
... gpointer data; gsize data_len; data = g_array_steal (some_array, &data_len); ...
- Parameters:
array
- aGArray
.len
- pointer to retrieve the number of elements of the original array- Returns:
- the element data, which should be freed using g_free().
-
unref
Atomically decrements the reference count ofarray
by one. If the reference count drops to 0, all memory allocated by the array is released. This function is thread-safe and may be called from any thread.- Parameters:
array
- AGArray
-