コード例 #1
0
        /// <summary>
        /// Performs a binary search to locate the specified value.
        /// </summary>
        /// <typeparam name="T">Type of the elements</typeparam>
        /// <param name="array">The array.</param>
        /// <param name="start">The start index.</param>
        /// <param name="end">The end index.</param>
        /// <param name="value">The value to find.</param>
        /// <param name="comparer">The comparer used to find the elements.</param>
        /// <returns>The index of the value or the index which the value should be as ~index.</returns>
        /// <exception cref="ArgumentException">If the array is invalid</exception>
        /// <exception cref="ArgumentOutOfRangeException">If the specified range is invalid</exception>
        public static int BinarySearch <T>(this NativeArray <T> array, int start, int end, T value, IComparer <T> comparer) where T : unmanaged
        {
            if (!array.IsValid)
            {
                throw new ArgumentException("NativeArray is invalid");
            }

            if (start > end || start < 0 || end < 0 || start > array.Length || end > array.Length)
            {
                throw new ArgumentOutOfRangeException($"Invalid range, start: {start}, end: {end}");
            }

            return(UnsafeUtilities.BinarySearch(array._buffer, start, end, value, comparer));
        }