Class Array
-
Constructor Summary
ConstructorsConstructorDescriptionArray()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(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data, int len) Addslenelements onto the end of the array.static booleanbinarySearch(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment target, @Nullable CompareFunc compareFunc, @Nullable Out<Integer> outMatchIndex) Checks whethertargetexists inarrayby performing a binary search based on the given comparison functioncompareFuncwhich gets pointers to items as arguments.static MemorySegment[]copy(@Nullable MemorySegment @Nullable [] array) Creates a shallow copy of aGArray.static Stringfree(@Nullable MemorySegment @Nullable [] array, boolean freeSegment) Frees the memory allocated for theGArray.static intgetElementSize(@Nullable MemorySegment @Nullable [] array) Gets the size of the elements inarray.static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the Array classstatic MemorySegment[]insertVals(@Nullable MemorySegment @Nullable [] array, int index, @Nullable MemorySegment data, int len) Insertslenelements into aGArrayat the given index.static MemorySegment[]new_(boolean zeroTerminated, boolean clear, int elementSize) Creates a newGArraywith a reference count of 1.static MemorySegment[]newTake(@Nullable MemorySegment @Nullable [] data, boolean clear, long elementSize) Creates a newGArraywithdataas array data,lenas length and a reference count of 1.static MemorySegment[]newTakeZeroTerminated(@Nullable MemorySegment @Nullable [] data, boolean clear, long elementSize) Creates a newGArraywithdataas array data, computing the length of it and setting the reference count to 1.static MemorySegment[]prependVals(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data, int len) Addslenelements onto the start of the array.readData()Read the value of the fielddata.intreadLen()Read the value of the fieldlen.static MemorySegment[]ref(@Nullable MemorySegment @Nullable [] array) Atomically increments the reference count ofarrayby one.static MemorySegment[]removeIndex(@Nullable MemorySegment @Nullable [] array, int index) Removes the element at the given index from aGArray.static MemorySegment[]removeIndexFast(@Nullable MemorySegment @Nullable [] array, int index) Removes the element at the given index from aGArray.static MemorySegment[]removeRange(@Nullable MemorySegment @Nullable [] array, int index, int length) Removes the given number of elements starting at the given index from aGArray.static voidsetClearFunc(@Nullable MemorySegment @Nullable [] array) Sets a function to clear an element ofarray.static MemorySegment[]setSize(@Nullable MemorySegment @Nullable [] 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 newGArraywithreservedSizeelements preallocated and a reference count of 1.static voidsort(@Nullable MemorySegment @Nullable [] array, @Nullable CompareFunc compareFunc) Sorts aGArrayusingcompareFuncwhich should be aqsort()-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 voidsortWithData(@Nullable MemorySegment @Nullable [] array, @Nullable CompareDataFunc compareFunc) Likesort(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc), but the comparison function receives an extra user data argument.static @Nullable MemorySegmentsteal(@Nullable MemorySegment @Nullable [] 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 voidunref(@Nullable MemorySegment @Nullable [] array) Atomically decrements the reference count ofarrayby one.voidWrite a value in the fielddata.voidwriteLen(int len) Write a value in the fieldlen.Methods inherited from class org.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 fielddatalen- 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
public static MemorySegment[] appendVals(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data, int len) Addslenelements onto the end of the array.datamay beNULLif (and only if)lenis zero. Iflenis zero, this function is a no-op.- Parameters:
array- an arraydata- 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(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment target, @Nullable CompareFunc compareFunc, @Nullable Out<Integer> outMatchIndex) Checks whethertargetexists inarrayby performing a binary search based on the given comparison functioncompareFuncwhich gets 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 andoutMatchIndexis undefined. This search is using a binary search, so thearraymust absolutely be sorted to return a correct result (if not, the function may produce false-negative).This example defines a comparison function and searches 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- an arraytarget- a pointer to the item to look upcompareFunc- a comparison function to locatetargetoutMatchIndex- the return location for the index of the element, if found- Returns:
- true if
targetis one of the elements ofarray;false otherwise - Since:
- 2.62
-
copy
Creates 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- an array- Returns:
- The copy of
array - Since:
- 2.62
-
free
Frees the memory allocated for theGArray. IffreeSegmentis true it frees the memory block holding the elements as well. Pass false if you want to free theGArraywrapper but preserve the underlying array for use elsewhere. If the reference count ofarrayis greater than one, theGArraywrapper is preserved but the size ofarraywill be set to zero.If array contents point to dynamically-allocated memory, they should be freed separately if
freeSegmentis true and noclearFuncfunction has been set forarray.This function is not thread-safe. If using a
GArrayfrom multiple threads, use only the atomicref(java.lang.foreign.MemorySegment[])andunref(java.lang.foreign.MemorySegment[])functions.- Parameters:
array- an arrayfreeSegment- if true, the actual element data is freed as well- Returns:
- The allocated element data if
freeSegmentis false, otherwiseNULL
-
getElementSize
Gets the size of the elements inarray.- Parameters:
array- an array- Returns:
- The size of each element, in bytes
- Since:
- 2.22
-
insertVals
public static MemorySegment[] insertVals(@Nullable MemorySegment @Nullable [] array, int index, @Nullable MemorySegment data, int len) Insertslenelements into aGArrayat the given index.If
indexis 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
indexis less than the array’s current length, new entries will be inserted into the array, and the existing entries aboveindexwill be moved upwards.datamay beNULLif (and only if)lenis zero. Iflenis zero, this function is a no-op.- Parameters:
array- an arrayindex- 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 newGArraywith a reference count of 1.- Parameters:
zeroTerminated- if true, the array should have an extra element at the end which is set to 0clear- if true,GArrayelements 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 MemorySegment @Nullable [] data, boolean clear, long elementSize) Creates a newGArraywithdataas array data,lenas length and a reference count of 1.This avoids having to copy the data manually, when it can just be inherited. After this call,
databelongs to theGArrayand may no longer be modified by the caller. The memory ofdatahas to be dynamically allocated and will eventually be freed withGLib.free(java.lang.foreign.MemorySegment).In case the elements need to be cleared when the array is freed, use
setClearFunc(java.lang.foreign.MemorySegment[])to set aGLib.DestroyNotifyfunction to perform such task.Do not use it if
lenorelementSizeare greater thanG_MAXUINT.GArraystores the length of its data inguint, which may be shorter thangsize.- Parameters:
data- an array of elements ofelementSizeclear- if true,GArrayelements should be automatically cleared to 0 when they are allocatedelementSize- the size of each element in bytes- Returns:
- The new
GArray - Since:
- 2.76
-
newTakeZeroTerminated
public static MemorySegment[] newTakeZeroTerminated(@Nullable MemorySegment @Nullable [] data, boolean clear, long elementSize) Creates a newGArraywithdataas 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,
databelongs to theGArrayand may no longer be modified by the caller. The memory ofdatahas to be dynamically allocated and will eventually be freed withGLib.free(java.lang.foreign.MemorySegment).The length is calculated by iterating through
datauntil the firstNULLelement is found.In case the elements need to be cleared when the array is freed, use
setClearFunc(java.lang.foreign.MemorySegment[])to set aGLib.DestroyNotifyfunction to perform such task.Do not use it if
datalength orelementSizeare greater thanG_MAXUINT.GArraystores the length of its data inguint, which may be shorter thangsize.- Parameters:
data- an array of elements ofelementSize,NULLterminatedclear- if true,GArrayelements should be automatically cleared to 0 when they are allocatedelementSize- the size of each element in bytes- Returns:
- The new
GArray - Since:
- 2.76
-
prependVals
public static MemorySegment[] prependVals(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data, int len) Addslenelements onto the start of the array.datamay beNULLif (and only if)lenis zero. Iflenis zero, this function is a no-op.This operation is slower than
appendVals(java.lang.foreign.MemorySegment[], java.lang.foreign.MemorySegment, int)since the existing elements in the array have to be moved to make space for the new elements.- Parameters:
array- an arraydata- 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 ofarrayby one. This function is thread-safe and may be called from any thread.- Parameters:
array- an array- Returns:
- The passed in
GArray - Since:
- 2.22
-
removeIndex
Removes the element at the given index from aGArray. The following elements are moved down one place.- Parameters:
array- an arrayindex- the index of the element to remove- Returns:
- The
GArray
-
removeIndexFast
public static MemorySegment[] removeIndexFast(@Nullable MemorySegment @Nullable [] array, int index) 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 thanremoveIndex(java.lang.foreign.MemorySegment[], int).- Parameters:
array- an arrayindex- the index of the element to remove- Returns:
- The
GArray
-
removeRange
public static MemorySegment[] removeRange(@Nullable MemorySegment @Nullable [] array, int index, int length) Removes the given number of elements starting at the given index from aGArray. The following elements are moved to close the gap.- Parameters:
array- an arrayindex- the index of the first element to removelength- the number of elements to remove- Returns:
- The
GArray - Since:
- 2.4
-
setClearFunc
Sets a function to clear an element ofarray.The
clearFuncwill 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.clearFuncwill be passed a pointer to the element to clear, rather than the element itself.Note that in contrast with other uses of
GLib.DestroyNotifyfunctions,clearFuncis 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- an array- Since:
- 2.32
-
setSize
Sets the size of the array, expanding it if necessary. If the array was created withclearset to true, the new elements are set to 0.- Parameters:
array- an arraylength- the new size of theGArray- Returns:
- The
GArray
-
sizedNew
public static MemorySegment[] sizedNew(boolean zeroTerminated, boolean clear, int elementSize, int reservedSize) Creates a newGArraywithreservedSizeelements 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- if true, the array should have an extra element at the end with all bits clearedclear- if true, all bits in the array should be cleared to 0 on allocationelementSize- the size of each element in the arrayreservedSize- the number of elements preallocated- Returns:
- The new
GArray
-
sort
public static void sort(@Nullable MemorySegment @Nullable [] array, @Nullable CompareFunc compareFunc) Sorts aGArrayusingcompareFuncwhich should be aqsort()-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- an arraycompareFunc- a comparison function
-
sortWithData
public static void sortWithData(@Nullable MemorySegment @Nullable [] array, @Nullable CompareDataFunc compareFunc) Likesort(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc), 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- an arraycompareFunc- a comparison function
-
steal
public static @Nullable MemorySegment steal(@Nullable MemorySegment @Nullable [] 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.Note that if the array was created with the
zeroTerminateproperty set to true, this may still returnNULLif the length of the array was zero and data was not yet allocated.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- an arraylen- a pointer to retrieve the number of elements of the original array- Returns:
- The allocated element data
- Since:
- 2.64
-
unref
Atomically decrements the reference count ofarrayby one. If the reference count drops to 0, the effect is the same as callingfree(java.lang.foreign.MemorySegment[], boolean)withfreeSegmentset to true. This function is thread-safe and may be called from any thread.- Parameters:
array- an array- Since:
- 2.22
-