예제 #1
0
 /// <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);
 }
예제 #2
0
        /// <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_);
        }
예제 #3
0
        /// <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_);
        }
예제 #4
0
        /// <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_);
        }
예제 #5
0
 static extern IntPtr g_list_sort(
     IntPtr list,
     UnmanagedCompareFunc compareFunc);
예제 #6
0
 /// <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 &gt; 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);
 }
예제 #7
0
 static extern IntPtr g_list_insert_sorted(
     IntPtr list,
     IntPtr data,
     UnmanagedCompareFunc func);
예제 #8
0
 static extern IntPtr g_list_find_custom(
     IntPtr list,
     IntPtr data,
     UnmanagedCompareFunc func);
예제 #9
0
 static extern void g_array_sort(
     IntPtr array,
     UnmanagedCompareFunc compareFunc);