Class Array

All Implemented Interfaces:
PointerInterface

public class Array extends Record
Contains the public fields of a GArray.

https://docs.gtk.org/glib/struct.Array.html

  • Field Details

    • DATA

      public static final String DATA
      a pointer to the element data. The data may be moved as
      elements are added to the #GArray.
      See Also:
    • LEN

      public static final String LEN
      the number of elements in the #GArray not including the
      possible terminating zero element.
      See Also:
  • Constructor Details

  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • setFieldData

      public void setFieldData(Str data)
      a pointer to the element data. The data may be moved as
      elements are added to the #GArray.
    • getFieldData

      public Str getFieldData()
      a pointer to the element data. The data may be moved as
      elements are added to the #GArray.
    • setFieldLen

      public void setFieldLen(int len)
      the number of elements in the #GArray not including the
      possible terminating zero element.
    • getFieldLen

      public int getFieldLen()
      the number of elements in the #GArray not including the
      possible terminating zero element.
    • appendVals

      public static Array appendVals(@Nonnull Array array, @Nonnull Pointer data, int len)
      Adds @len elements onto the end of the array.
      Parameters:
      array - a #GArray
      data - a pointer to the elements to append to the end of the array
      len - the number of elements to append
      Returns:
      the #GArray
    • copy

      public static Array copy(@Nonnull Array array)
      Create a shallow copy of a #GArray. If the array elements consist of
      pointers to data, the pointers are copied but the actual data is not.
      Parameters:
      array - A #GArray.
      Returns:
      A copy of @array.
    • free

      public static Str free(@Nonnull Array array, boolean free_segment)
      Frees the memory allocated for the #GArray. If @free_segment is
      %TRUE it frees the memory block holding the elements as well. Pass
      %FALSE if you want to free the #GArray wrapper but preserve the
      underlying array for use elsewhere. If the reference count of
      @array is greater than one, the #GArray wrapper is preserved but
      the size of @array will be set to zero.

      If array contents point to dynamically-allocated memory, they should
      be freed separately if @free_seg is %TRUE and no @clear_func
      function has been set for @array.

      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 - a #GArray
      free_segment - if %TRUE the actual element data is freed as well
      Returns:
      the element data if @free_segment is %FALSE, otherwise %NULL. The element data should be freed using g_free().
    • getElementSize

      public static int getElementSize(@Nonnull Array array)
      Gets the size of the elements in @array.
      Parameters:
      array - A #GArray
      Returns:
      Size of each element, in bytes
    • insertVals

      public static Array insertVals(@Nonnull Array array, int index_, @Nullable Pointer data, int len)
      Inserts @len elements into a #GArray 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 above @index_ will be moved
      upwards.

      @data may be %NULL if (and only if) @len is zero. If @len is zero, this
      function is a no-op.
      Parameters:
      array - a #GArray
      index_ - the index to place the elements at
      data - a pointer to the elements to insert
      len - the number of elements to insert
      Returns:
      the #GArray
    • _new

      public static Array _new(boolean zero_terminated, boolean clear_, int element_size)
      Creates a new #GArray with a reference count of 1.
      Parameters:
      zero_terminated - %TRUE if the array should have an extra element at the end which is set to 0
      clear_ - %TRUE if #GArray elements should be automatically cleared to 0 when they are allocated
      element_size - the size of each element in bytes
      Returns:
      the new #GArray
    • prependVals

      public static Array prependVals(@Nonnull Array array, @Nullable Pointer data, int len)
      Adds @len elements onto the start of the array.

      @data may be %NULL if (and only if) @len is zero. If @len 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 - a #GArray
      data - a pointer to the elements to prepend to the start of the array
      len - the number of elements to prepend, which may be zero
      Returns:
      the #GArray
    • ref

      public static Array ref(@Nonnull Array array)
      Atomically increments the reference count of @array by one.
      This function is thread-safe and may be called from any thread.
      Parameters:
      array - A #GArray
      Returns:
      The passed in #GArray
    • removeIndex

      public static Array removeIndex(@Nonnull Array array, int index_)
      Removes the element at the given index from a #GArray. The following
      elements are moved down one place.
      Parameters:
      array - a #GArray
      index_ - the index of the element to remove
      Returns:
      the #GArray
    • removeIndexFast

      public static Array removeIndexFast(@Nonnull Array array, int index_)
      Removes the element at the given index from a #GArray. The last
      element in the array is used to fill in the space, so this function
      does not preserve the order of the #GArray. But it is faster than
      g_array_remove_index().
      Parameters:
      array - a @GArray
      index_ - the index of the element to remove
      Returns:
      the #GArray
    • removeRange

      public static Array removeRange(@Nonnull Array array, int index_, int length)
      Removes the given number of elements starting at the given index
      from a #GArray. The following elements are moved to close the gap.
      Parameters:
      array - a @GArray
      index_ - the index of the first element to remove
      length - the number of elements to remove
      Returns:
      the #GArray
    • setClearFunc

      public static void setClearFunc(@Nonnull Array array, Array.OnDestroyNotify clear_func)
      Sets a function to clear an element of @array.

      The @clear_func 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. @clear_func 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, @clear_func is expected to clear the contents of
      the array element it is given, but not free the element itself.
      <!-- language="C" -->
       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 - A #GArray
      clear_func - a function to clear an element of @array
    • setSize

      public static Array setSize(@Nonnull Array array, int length)
      Sets the size of the array, expanding it if necessary. If the array
      was created with @clear_ set to %TRUE, the new elements are set to 0.
      Parameters:
      array - a #GArray
      length - the new size of the #GArray
      Returns:
      the #GArray
    • sizedNew

      public static Array sizedNew(boolean zero_terminated, boolean clear_, int element_size, int reserved_size)
      Creates a new #GArray with @reserved_size 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:
      zero_terminated - %TRUE if the array should have an extra element at the end with all bits cleared
      clear_ - %TRUE if all bits in the array should be cleared to 0 on allocation
      element_size - size of each element in the array
      reserved_size - number of elements preallocated
      Returns:
      the new #GArray
    • sort

      public static void sort(@Nonnull Array array, Array.OnCompareFunc compare_func)
      Sorts a #GArray using @compare_func 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 - a #GArray
      compare_func - comparison function
    • sortWithData

      public static void sortWithData(@Nonnull Array array, Array.OnCompareDataFunc compare_func, @Nullable Pointer user_data)
      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 - a #GArray
      compare_func - comparison function
      user_data - data to pass to @compare_func
    • steal

      public static Pointer steal(@Nonnull Array array, @Nullable Int64 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.

      If the array was created with the @zero_terminate property
      set to %TRUE, 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:
      <!-- language="C" -->
       ...
       gpointer data;
       gsize data_len;
       data = g_array_steal (some_array, &data_len);
       ...
       
      Parameters:
      array - a #GArray.
      len - pointer to retrieve the number of elements of the original array
      Returns:
      the element data, which should be freed using g_free().
    • unref

      public static void unref(@Nonnull Array array)
      Atomically decrements the reference count of @array 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 - A #GArray
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()