/// <summary> /// Sorts a <see cref="List{T}"/> using the given comparison function. The algorithm /// used is a stable sort. /// </summary> /// <param name="compareFunc"> /// the comparison function used to sort the <see cref="List{T}"/>. /// This function is passed the data from 2 elements of the <see cref="List{T}"/> /// and should return 0 if they are equal, a negative value if the /// first element comes before the second, or a positive value if /// the first element comes after the second. /// </param> protected void Sort(UnmanagedCompareFunc compareFunc) { if (compareFunc == null) { throw new ArgumentNullException(nameof(compareFunc)); } handle = g_list_sort(handle, compareFunc); GC.KeepAlive(compareFunc); }
/// <summary> /// Sorts a <see cref="Array{T}"/> using <paramref name="compareFunc"/> /// 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). /// </summary> /// <remarks> /// This is guaranteed to be a stable sort since version 2.32. /// </remarks> /// <param name="compareFunc"> /// comparison function /// </param> protected void Sort <T> (Comparison <T> compareFunc) where T : struct { AssertNotDisposed(); if (compareFunc == null) { throw new ArgumentNullException(nameof(compareFunc)); } UnmanagedCompareFunc compareFunc_ = (a, b) => { var x = Marshal.PtrToStructure <T> (a); var y = Marshal.PtrToStructure <T> (b); return(compareFunc(x, y)); }; g_array_sort(handle, compareFunc_); GC.KeepAlive(compareFunc_); }
/// <summary> /// Sorts the array, using <paramref name="compareFunc"/> which should be a qsort()-style /// comparison function (returns less than zero for first arg is less /// than second arg, zero for equal, greater than zero if irst arg is /// greater than second arg). /// </summary> /// <remarks> /// This is guaranteed to be a stable sort since version 2.32. /// </remarks> /// <param name="compareFunc"> /// comparison function /// </param> protected void Sort(Comparison <IntPtr> compareFunc) { AssertNotDisposed(); if (compareFunc == null) { throw new ArgumentNullException(nameof(compareFunc)); } UnmanagedCompareFunc compareFunc_ = (a, b) => { var x = Marshal.ReadIntPtr(a); var y = Marshal.ReadIntPtr(b); var compareFuncRet = compareFunc(x, y); return(compareFuncRet); }; g_ptr_array_sort(handle, compareFunc_); GC.KeepAlive(compareFunc_); }
/// <summary> /// Sorts a byte array, using @compareFunc which should be a /// qsort()-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). /// </summary> /// <remarks> /// If two array elements compare equal, their order in the sorted array /// is undefined. If you want equal elements to keep their order (i.e. /// you want a stable sort) you can write a comparison function that, /// if two elements would otherwise compare equal, compares them by /// their addresses. /// </remarks> /// <param name="compareFunc"> /// comparison function /// </param> public void Sort(Comparison <byte> compareFunc) { AssertNotDisposed(); if (compareFunc == null) { throw new ArgumentNullException(nameof(compareFunc)); } UnmanagedCompareFunc compareFunc_ = (a, b) => { var x = Marshal.ReadByte(a); var y = Marshal.ReadByte(b); return(compareFunc(x, y)); }; g_byte_array_sort(handle, compareFunc_); GC.KeepAlive(compareFunc_); }
static extern IntPtr g_list_sort( IntPtr list, UnmanagedCompareFunc compareFunc);
/// <summary> /// Inserts a new element into the list, using the given comparison /// function to determine its position. /// </summary> /// <remarks> /// If you are adding many new elements to a list, and the number of /// new elements is much larger than the length of the list, use /// g_list_prepend() to add the new items and sort the list afterwards /// with g_list_sort(). /// </remarks> /// <param name="data"> /// the data for the new element /// </param> /// <param name="func"> /// the function to compare elements in the list. It should /// return a number > 0 if the first parameter comes after the /// second parameter in the sort order. /// </param> protected void InsertSorted(IntPtr data, UnmanagedCompareFunc func) { handle = g_list_insert_sorted(handle, data, func); GC.KeepAlive(func); }
static extern IntPtr g_list_insert_sorted( IntPtr list, IntPtr data, UnmanagedCompareFunc func);
static extern IntPtr g_list_find_custom( IntPtr list, IntPtr data, UnmanagedCompareFunc func);
static extern void g_array_sort( IntPtr array, UnmanagedCompareFunc compareFunc);