Class PtrArray
-
Constructor Summary
ConstructorsConstructorDescriptionPtrArray()Allocate a new PtrArray.Allocate a new PtrArray.PtrArray(MemorySegment address) Create a PtrArray proxy instance for the provided memory address.PtrArray(MemorySegment pdata, int len) Allocate a new PtrArray with the fields set to the provided values.PtrArray(MemorySegment pdata, int len, Arena arena) Allocate a new PtrArray with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidadd(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data) Adds a pointer to the end of the pointer array.static MemorySegment[]copy(@Nullable MemorySegment @Nullable [] array, @Nullable CopyFunc func) Makes a full (deep) copy of aGPtrArray.static voidextend(@Nullable MemorySegment @Nullable [] arrayToExtend, @Nullable MemorySegment @Nullable [] array, @Nullable CopyFunc func) Adds all pointers ofarrayto the end of the arrayarrayToExtend.The array will grow in size automatically if needed.static voidextendAndSteal(@Nullable MemorySegment @Nullable [] arrayToExtend, @Nullable MemorySegment @Nullable [] array) Adds all the pointers inarrayto the end ofarrayToExtend,transferring ownership of each element fromarraytoarrayToExtendand modifyingarrayToExtendin-place.static booleanfind(@Nullable MemorySegment @Nullable [] haystack, @Nullable MemorySegment needle, @Nullable Out<Integer> index) Checks whetherneedleexists inhaystack.If the element is found, true is returned and the element’s index is returned inindex(if non-NULL).static booleanfindWithEqualFunc(@Nullable MemorySegment @Nullable [] haystack, @Nullable MemorySegment needle, @Nullable EqualFunc equalFunc, @Nullable Out<Integer> index) Checks whetherneedleexists inhaystack,using the givenequalFunc.If the element is found, true is returned and the element’s index is returned inindex(if non-NULL).static voidforeach(@Nullable MemorySegment @Nullable [] array, @Nullable Func func) Calls a function for each element of aGPtrArray.static @Nullable MemorySegment @Nullable []free(@Nullable MemorySegment @Nullable [] array, boolean freeSegment) Frees the memory allocated for theGPtrArray.static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the PtrArray classstatic voidinsert(@Nullable MemorySegment @Nullable [] array, int index, @Nullable MemorySegment data) Inserts an element into the pointer array at the given index.static booleanisNullTerminated(@Nullable MemorySegment @Nullable [] array) Checks whether thearraywas constructed asNULL-terminated.static MemorySegment[]new_()Creates a newGPtrArraywith a reference count of 1.static MemorySegment[]newFromArray(@Nullable MemorySegment @Nullable [] data, @Nullable CopyFunc copyFunc) Creates a newGPtrArray, copyinglenpointers fromdata,and setting the array’s reference count to 1.static MemorySegment[]newFromNullTerminatedArray(@Nullable MemorySegment @Nullable [] data, @Nullable CopyFunc copyFunc) Creates a newGPtrArraycopying the pointers fromdataafter having computed the length of it and with a reference count of 1.static MemorySegment[]newFull(int reservedSize) Creates a newGPtrArraywithreservedSizepointers preallocated and a reference count of 1.static MemorySegment[]newNullTerminated(int reservedSize, boolean nullTerminated) LikenewFull(int)but also allows to set the array to beNULLterminated.static MemorySegment[]newTake(@Nullable MemorySegment @Nullable [] data) Creates a newGPtrArraywithdataas pointers,lenas length and a reference count of 1.static MemorySegment[]newTakeNullTerminated(@Nullable MemorySegment @Nullable [] data) Creates a newGPtrArraywithdataas pointers, computing the length of it and setting the reference count to 1.static MemorySegment[]Creates a newGPtrArraywith a reference count of 1 and useelementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.intreadLen()Read the value of the fieldlen.Read the value of the fieldpdata.static MemorySegment[]ref(@Nullable MemorySegment @Nullable [] array) Atomically increments the reference count ofarrayby one.static booleanremove(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data) Removes the first occurrence of the given pointer from the pointer array.static booleanremoveFast(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data) Removes the first occurrence of the given pointer from the pointer array.static @Nullable MemorySegmentremoveIndex(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array.static @Nullable MemorySegmentremoveIndexFast(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array.static MemorySegment[]removeRange(@Nullable MemorySegment @Nullable [] array, int index, int length) Removes the given number of pointers starting at the given index from aGPtrArray.static voidsetFreeFunc(@Nullable MemorySegment @Nullable [] array) Sets a function for freeing each element whenarrayis destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.static voidsetSize(@Nullable MemorySegment @Nullable [] array, int length) Sets the size of the array.static MemorySegment[]sizedNew(int reservedSize) Creates a newGPtrArraywithreservedSizepointers preallocated and a reference count of 1.static voidsort(@Nullable MemorySegment @Nullable [] array, @Nullable CompareFunc compareFunc) Sorts the array, usingcompareFuncwhich should be aqsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).static voidsortValues(@Nullable MemorySegment @Nullable [] array, @Nullable CompareFunc compareFunc) Sorts the array, usingcompareFuncwhich should be aqsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).static voidsortValuesWithData(@Nullable MemorySegment @Nullable [] array, @Nullable CompareDataFunc compareFunc) LikesortValues(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc), but the comparison function has an extra user data argument.static voidsortWithData(@Nullable MemorySegment @Nullable [] array, @Nullable CompareDataFunc compareFunc) Likesort(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc), but the comparison function has an extra user data argument.static @Nullable MemorySegment @Nullable []steal(@Nullable MemorySegment @Nullable [] array) 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 @Nullable MemorySegmentstealIndex(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array.static @Nullable MemorySegmentstealIndexFast(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array.static voidunref(@Nullable MemorySegment @Nullable [] array) Atomically decrements the reference count ofarrayby one.voidwriteLen(int len) Write a value in the fieldlen.voidwritePdata(MemorySegment pdata) Write a value in the fieldpdata.Methods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
PtrArray
Create a PtrArray proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
PtrArray
Allocate a new PtrArray.- Parameters:
arena- to control the memory allocation scope
-
PtrArray
public PtrArray()Allocate a new PtrArray. The memory is allocated withArena.ofAuto(). -
PtrArray
Allocate a new PtrArray with the fields set to the provided values.- Parameters:
pdata- value for the fieldpdatalen- value for the fieldlenarena- to control the memory allocation scope
-
PtrArray
Allocate a new PtrArray with the fields set to the provided values. The memory is allocated withArena.ofAuto().- Parameters:
pdata- value for the fieldpdatalen- value for the fieldlen
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readPdata
Read the value of the fieldpdata.- Returns:
- The value of the field
pdata
-
writePdata
Write a value in the fieldpdata.- Parameters:
pdata- The new value for the fieldpdata
-
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
-
add
Adds a pointer to the end of the pointer array. The array will grow in size automatically if necessary.- Parameters:
array- a pointer arraydata- the pointer to add
-
copy
public static MemorySegment[] copy(@Nullable MemorySegment @Nullable [] array, @Nullable CopyFunc func) Makes a full (deep) copy of aGPtrArray.func,as aGLib.CopyFunc, takes two arguments, the data to be copied and auserDatapointer. On common processor architectures, it’s safe to passNULLasuserDataif the copy function takes only one argument. You may get compiler warnings from this though if compiling with GCC’s-Wcast-function-typewarning.If
funcisNULL, then only the pointers (and not what they are pointing to) are copied to the newGPtrArray.The copy of
arraywill have the sameGLib.DestroyNotifyfor its elements asarray.The copy will also beNULLterminated if (and only if) the source array is.- Parameters:
array- a pointer array to duplicatefunc- a copy function used to copy every element in the array- Returns:
- The deep copy of the initial
GPtrArray - Since:
- 2.62
-
extend
public static void extend(@Nullable MemorySegment @Nullable [] arrayToExtend, @Nullable MemorySegment @Nullable [] array, @Nullable CopyFunc func) Adds all pointers ofarrayto the end of the arrayarrayToExtend.The array will grow in size automatically if needed.arrayToExtendis modified in-place.func,as aGLib.CopyFunc, takes two arguments, the data to be copied and auserDatapointer. On common processor architectures, it’s safe to passNULLasuserDataif the copy function takes only one argument. You may get compiler warnings from this though if compiling with GCC’s-Wcast-function-typewarning.If
funcisNULL, then only the pointers (and not what they are pointing to) are copied to the newGPtrArray.Whether
arrayToExtendisNULLterminated stays unchanged by this function.- Parameters:
arrayToExtend- a pointer arrayarray- a pointer array to add to the end ofarrayToExtendfunc- a copy function used to copy every element in the array- Since:
- 2.62
-
extendAndSteal
public static void extendAndSteal(@Nullable MemorySegment @Nullable [] arrayToExtend, @Nullable MemorySegment @Nullable [] array) Adds all the pointers inarrayto the end ofarrayToExtend,transferring ownership of each element fromarraytoarrayToExtendand modifyingarrayToExtendin-place.arrayis then freed.As with
free(java.lang.foreign.MemorySegment[], boolean),arraywill be destroyed if its reference count is 1. If its reference count is higher, it will be decremented and the length ofarrayset to zero.- Parameters:
arrayToExtend- a pointer arrayarray- a pointer array to add to the end ofarrayToExtend- Since:
- 2.62
-
find
public static boolean find(@Nullable MemorySegment @Nullable [] haystack, @Nullable MemorySegment needle, @Nullable Out<Integer> index) Checks whetherneedleexists inhaystack.If the element is found, true is returned and the element’s index is returned inindex(if non-NULL). Otherwise, false is returned andindexis undefined. Ifneedleexists multiple times inhaystack,the index of the first instance is returned.This does pointer comparisons only. If you want to use more complex equality checks, such as string comparisons, use
findWithEqualFunc(java.lang.foreign.MemorySegment[], java.lang.foreign.MemorySegment, org.gnome.glib.EqualFunc, org.javagi.base.Out<java.lang.Integer>).- Parameters:
haystack- the pointer array to be searchedneedle- the pointer to look forindex- the return location for the index of the element, if found- Returns:
- true if
needleis one of the elements ofhaystack;false otherwise - Since:
- 2.54
-
findWithEqualFunc
public static boolean findWithEqualFunc(@Nullable MemorySegment @Nullable [] haystack, @Nullable MemorySegment needle, @Nullable EqualFunc equalFunc, @Nullable Out<Integer> index) Checks whetherneedleexists inhaystack,using the givenequalFunc.If the element is found, true is returned and the element’s index is returned inindex(if non-NULL). Otherwise, false is returned andindexis undefined. Ifneedleexists multiple times inhaystack,the index of the first instance is returned.equalFuncis called with the element from the array as its first parameter, andneedleas its second parameter. IfequalFuncisNULL, pointer equality is used.- Parameters:
haystack- the pointer array to be searchedneedle- the pointer to look forequalFunc- the function to call for each element, which should return true when the desired element is found; orNULLto use pointer equalityindex- the return location for the index of the element, if found- Returns:
- true if
needleis one of the elements ofhaystack;false otherwise - Since:
- 2.54
-
foreach
Calls a function for each element of aGPtrArray.funcmust not add elements to or remove elements from the array.- Parameters:
array- a pointer arrayfunc- the function to call for each array element- Since:
- 2.4
-
free
public static @Nullable MemorySegment @Nullable [] free(@Nullable MemorySegment @Nullable [] array, boolean freeSegment) Frees the memory allocated for theGPtrArray. IffreeSegmentis true it frees the memory block holding the elements as well. Pass false if you want to free theGPtrArraywrapper but preserve the underlying array for use elsewhere. If the reference count ofarrayis greater than one, theGPtrArraywrapper 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 noGLib.DestroyNotifyfunction has been set forarray.Note that if the array is
NULLterminated andfreeSegmentis false then this will always return an allocatedNULLterminated buffer. Ifpdatais previouslyNULL, a new buffer will be allocated.This function is not thread-safe. If using a
GPtrArrayfrom multiple threads, use only the atomicref(java.lang.foreign.MemorySegment[])andunref(java.lang.foreign.MemorySegment[])functions.- Parameters:
array- a pointer arrayfreeSegment- if true, the actual pointer array is freed as well- Returns:
- The allocated pointer array if
freeSegmentis false, otherwiseNULL.
-
insert
public static void insert(@Nullable MemorySegment @Nullable [] array, int index, @Nullable MemorySegment data) Inserts an element into the pointer array at the given index. The array will grow in size automatically if necessary.- Parameters:
array- a pointer arrayindex- the index to place the new element at, or -1 to appenddata- the pointer to add- Since:
- 2.40
-
isNullTerminated
Checks whether thearraywas constructed asNULL-terminated.This will only return true for arrays constructed by passing true to the
null_terminatedargument ofnewNullTerminated(int, boolean). It will not return true for normal arrays which have had aNULLelement appended to them.- Parameters:
array- a pointer array- Returns:
- true if the array is made to be
NULLterminated; false otherwise - Since:
- 2.74
-
new_
Creates a newGPtrArraywith a reference count of 1.- Returns:
- The new
GPtrArray
-
newFromArray
public static MemorySegment[] newFromArray(@Nullable MemorySegment @Nullable [] data, @Nullable CopyFunc copyFunc) Creates a newGPtrArray, copyinglenpointers fromdata,and setting the array’s reference count to 1.This avoids having to manually add each element one by one.
If
copyFuncis provided, then it is used to copy each element before adding them to the new array. If it isNULLthen the pointers are copied directly.It also sets
elementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.Do not use it if
lenis greater thanG_MAXUINT.GPtrArraystores the length of its data inguint, which may be shorter thangsize.- Parameters:
data- an array of pointerscopyFunc- a copy function used to copy every element in the array- Returns:
- The new
GPtrArray - Since:
- 2.76
-
newFromNullTerminatedArray
public static MemorySegment[] newFromNullTerminatedArray(@Nullable MemorySegment @Nullable [] data, @Nullable CopyFunc copyFunc) Creates a newGPtrArraycopying the pointers fromdataafter having computed the length of it and with a reference count of 1. This avoids having to manually add each element one by one. IfcopyFuncis provided, then it is used to copy the data in the new array. It also setselementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.Do not use it if the
datahas more thanG_MAXUINTelements.GPtrArraystores the length of its data inguint, which may be shorter thangsize.- Parameters:
data- an array of pointers,NULLterminatedcopyFunc- a copy function used to copy every element in the array- Returns:
- The new
GPtrArray - Since:
- 2.76
-
newFull
Creates a newGPtrArraywithreservedSizepointers preallocated and a reference count of 1. This avoids frequent reallocation, if you are going to add many pointers to the array. Note however that the size of the array is still 0. It also setselementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.- Parameters:
reservedSize- the number of pointers preallocated- Returns:
- The new
GPtrArray - Since:
- 2.30
-
newNullTerminated
LikenewFull(int)but also allows to set the array to beNULLterminated. ANULLterminated pointer array has an additionalNULLpointer after the last element, beyond the current length.GPtrArraycreated by other constructors are not automaticallyNULLterminated.Note that if the
array’slength is zero and currently no data array is allocated, thenpdatawill still beNULL.GPtrArraywill onlyNULLterminatepdata, if an actual array is allocated. It does not guarantee that an array is always allocated. In other words, if the length is zero, thenpdatamay either point to aNULLterminated array of length zero or beNULL.- Parameters:
reservedSize- the number of pointers preallocated. IfnullTerminatedisTRUE, the actually allocated buffer size isreservedSizeplus 1, unlessreservedSizeis zero, in which case no initial buffer gets allocated.nullTerminated- if true, make the arrayNULLterminated- Returns:
- The new
GPtrArray - Since:
- 2.74
-
newTake
Creates a newGPtrArraywithdataas pointers,lenas length and a reference count of 1.This avoids having to copy such data manually. After this call,
databelongs to theGPtrArrayand 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).It also sets
elementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.Do not use it if
lenis greater thanG_MAXUINT.GPtrArraystores the length of its data inguint, which may be shorter thangsize.- Parameters:
data- an array of pointers- Returns:
- The new
GPtrArray - Since:
- 2.76
-
newTakeNullTerminated
Creates a newGPtrArraywithdataas pointers, computing the length of it and setting the reference count to 1.This avoids having to copy such data manually. After this call,
databelongs to theGPtrArrayand 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.It also sets
elementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.Do not use it if the
datalength is greater thanG_MAXUINT.GPtrArraystores the length of its data inguint, which may be shorter thangsize.- Parameters:
data- an array of pointers,NULLterminated- Returns:
- The new
GPtrArray - Since:
- 2.76
-
newWithFreeFunc
Creates a newGPtrArraywith a reference count of 1 and useelementFreeFuncfor freeing each element when the array is destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.- Returns:
- The new
GPtrArray - Since:
- 2.22
-
ref
Atomically increments the reference count ofarrayby one. This function is thread-safe and may be called from any thread.- Parameters:
array- a pointer array- Returns:
- The passed in
GPtrArray - Since:
- 2.22
-
remove
public static boolean remove(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data) Removes the first occurrence of the given pointer from the pointer array. The following elements are moved down one place. Ifarrayhas a non-NULLGLib.DestroyNotifyfunction it is called for the removed element.It returns true if the pointer was removed, or false if the pointer was not found.
- Parameters:
array- a pointer arraydata- the pointer to remove- Returns:
- true if the pointer is found and removed; false otherwise
-
removeFast
public static boolean removeFast(@Nullable MemorySegment @Nullable [] array, @Nullable MemorySegment data) Removes the first occurrence of the given pointer from the pointer array. The last element in the array is used to fill in the space, so this function does not preserve the order of the array. But it is faster thanremove(java.lang.foreign.MemorySegment[], java.lang.foreign.MemorySegment). Ifarrayhas a non-NULLGLib.DestroyNotifyfunction it is called for the removed element.It returns true if the pointer was removed, or false if the pointer was not found.
- Parameters:
array- a pointer arraydata- the pointer to remove- Returns:
- true if the pointer is found and removed; false otherwise
-
removeIndex
public static @Nullable MemorySegment removeIndex(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array. The following elements are moved down one place. Ifarrayhas a non-NULLGLib.DestroyNotifyfunction it is called for the removed element. If so, the return value from this function will potentially point to freed memory (depending on theGLib.DestroyNotifyimplementation).- Parameters:
array- a pointer arrayindex- the index of the pointer to remove- Returns:
- The pointer which was removed
-
removeIndexFast
public static @Nullable MemorySegment removeIndexFast(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array. The last element in the array is used to fill in the space, so this function does not preserve the order of the array. But it is faster thanremoveIndex(java.lang.foreign.MemorySegment[], int). Ifarrayhas a non-NULLGLib.DestroyNotifyfunction it is called for the removed element. If so, the return value from this function will potentially point to freed memory (depending on theGLib.DestroyNotifyimplementation).- Parameters:
array- a pointer arrayindex- the index of the pointer to remove- Returns:
- The pointer which was removed
-
removeRange
public static MemorySegment[] removeRange(@Nullable MemorySegment @Nullable [] array, int index, int length) Removes the given number of pointers starting at the given index from aGPtrArray. The following elements are moved to close the gap. Ifarrayhas a non-NULLGLib.DestroyNotifyfunction it is called for the removed elements.- Parameters:
array- a pointer arrayindex- the index of the first pointer to removelength- the number of pointers to remove- Returns:
- The
array - Since:
- 2.4
-
setFreeFunc
Sets a function for freeing each element whenarrayis destroyed either viaunref(java.lang.foreign.MemorySegment[]), whenfree(java.lang.foreign.MemorySegment[], boolean)is called withfreeSegmentset to true or when removing elements.- Parameters:
array- a pointer array- Since:
- 2.22
-
setSize
Sets the size of the array. When making the array larger, newly-added elements will be set toNULL. When making it smaller, ifarrayhas a non-NULLGLib.DestroyNotifyfunction then it will be called for the removed elements.- Parameters:
array- a pointer arraylength- the new length of the pointer array
-
sizedNew
Creates a newGPtrArraywithreservedSizepointers preallocated and a reference count of 1. This avoids frequent reallocation, if you are going to add many pointers to the array. Note however that the size of the array is still 0.- Parameters:
reservedSize- the number of pointers preallocated- Returns:
- The new
GPtrArray
-
sort
public static void sort(@Nullable MemorySegment @Nullable [] array, @Nullable CompareFunc compareFunc) Sorts the array, usingcompareFuncwhich should be aqsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).Note that the comparison function for
sort(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc)doesn’t take the pointers from the array as arguments, it takes pointers to the pointers in the array.Use
sortValues(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc)if you want to use normalGLib.CompareFuncinstances, otherwise here is a full example of use:typedef struct { gchar *name; gint size; } FileListEntry; static gint sort_filelist (gconstpointer a, gconstpointer b) { const FileListEntry *entry1 = *((FileListEntry **) a); const FileListEntry *entry2 = *((FileListEntry **) b); return g_ascii_strcasecmp (entry1->name, entry2->name); } … g_autoptr (GPtrArray) file_list = NULL; // initialize file_list array and load with many FileListEntry entries ... // now sort it with g_ptr_array_sort (file_list, sort_filelist);This is guaranteed to be a stable sort since version 2.32.
- Parameters:
array- a pointer arraycompareFunc- a comparison function
-
sortValues
public static void sortValues(@Nullable MemorySegment @Nullable [] array, @Nullable CompareFunc compareFunc) Sorts the array, usingcompareFuncwhich should be aqsort()-style comparison function (returns less than zero for first arg is less than second arg, zero for equal, greater than zero if first arg is greater than second arg).This is guaranteed to be a stable sort.
- Parameters:
array- a pointer arraycompareFunc- a comparison function- Since:
- 2.76
-
sortValuesWithData
public static void sortValuesWithData(@Nullable MemorySegment @Nullable [] array, @Nullable CompareDataFunc compareFunc) LikesortValues(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareFunc), but the comparison function has an extra user data argument.This is guaranteed to be a stable sort.
- Parameters:
array- a pointer arraycompareFunc- a comparison function- Since:
- 2.76
-
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 has an extra user data argument.Note that the comparison function for
sortWithData(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareDataFunc)doesn’t take the pointers from the array as arguments, it takes pointers to the pointers in the array.Use
sortValuesWithData(java.lang.foreign.MemorySegment[], org.gnome.glib.CompareDataFunc)if you want to use normalGLib.CompareDataFuncinstances, otherwise here is a full example of use:typedef enum { SORT_NAME, SORT_SIZE } SortMode; typedef struct { gchar *name; gint size; } FileListEntry; static gint sort_filelist (gconstpointer a, gconstpointer b, gpointer user_data) { gint order; const SortMode sort_mode = GPOINTER_TO_INT (user_data); const FileListEntry *entry1 = *((FileListEntry **) a); const FileListEntry *entry2 = *((FileListEntry **) b); switch (sort_mode) { case SORT_NAME: order = g_ascii_strcasecmp (entry1->name, entry2->name); break; case SORT_SIZE: order = entry1->size - entry2->size; break; default: order = 0; break; } return order; } ... g_autoptr (GPtrArray) file_list = NULL; SortMode sort_mode; // initialize file_list array and load with many FileListEntry entries ... // now sort it with sort_mode = SORT_NAME; g_ptr_array_sort_with_data (file_list, sort_filelist, GINT_TO_POINTER (sort_mode));This is guaranteed to be a stable sort since version 2.32.
- Parameters:
array- a pointer arraycompareFunc- a comparison function
-
steal
public static @Nullable MemorySegment @Nullable [] steal(@Nullable MemorySegment @Nullable [] array) 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 is
NULLterminated this may still returnNULLif the length of the array was zero and pdata was not yet allocated.Even if set, the
GLib.DestroyNotifyfunction will never be called on the current contents of the array and the caller is responsible for freeing the array elements.An example of use:
g_autoptr(GPtrArray) chunk_buffer = g_ptr_array_new_with_free_func (g_bytes_unref); // Some part of your application appends a number of chunks to the pointer array. g_ptr_array_add (chunk_buffer, g_bytes_new_static ("hello", 5)); g_ptr_array_add (chunk_buffer, g_bytes_new_static ("world", 5)); … // Periodically, the chunks need to be sent as an array-and-length to some // other part of the program. GBytes **chunks; gsize n_chunks; chunks = g_ptr_array_steal (chunk_buffer, &n_chunks); for (gsize i = 0; i < n_chunks; i++) { // Do something with each chunk here, and then free them, since // g_ptr_array_steal() transfers ownership of all the elements and the // array to the caller. … g_bytes_unref (chunks[i]); } g_free (chunks); // After calling g_ptr_array_steal(), the pointer array can be reused for the // next set of chunks. g_assert (chunk_buffer->len == 0);- Parameters:
array- a pointer array- Returns:
- The allocated element data.
This may be
NULLif the array doesn’t have any elements (i.e. if*lenis zero). - Since:
- 2.64
-
stealIndex
public static @Nullable MemorySegment stealIndex(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array. The following elements are moved down one place. TheGLib.DestroyNotifyforarrayis not called on the removed element; ownership is transferred to the caller of this function.- Parameters:
array- a pointer arrayindex- the index of the pointer to steal- Returns:
- The pointer which was removed
- Since:
- 2.58
-
stealIndexFast
public static @Nullable MemorySegment stealIndexFast(@Nullable MemorySegment @Nullable [] array, int index) Removes the pointer at the given index from the pointer array. The last element in the array is used to fill in the space, so this function does not preserve the order of the array. But it is faster thanstealIndex(java.lang.foreign.MemorySegment[], int). TheGLib.DestroyNotifyforarrayis not called on the removed element; ownership is transferred to the caller of this function.- Parameters:
array- a pointer arrayindex- the index of the pointer to steal- Returns:
- The pointer which was removed
- Since:
- 2.58
-
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- a pointer array- Since:
- 2.22
-