Пример #1
0
        public static int IndexOf <T>(T[] array, T value, int startIndex, int count)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }

            if ((uint)startIndex > (uint)array.Length)
            {
                ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index();
            }

            if ((uint)count > (uint)(array.Length - startIndex))
            {
                ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
            }

            if (RuntimeHelpers.IsBitwiseEquatable <T>())
            {
                if (Unsafe.SizeOf <T>() == sizeof(byte))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref array.GetRawSzArrayData(), startIndex),
                        Unsafe.As <T, byte>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(char))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, char>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, char>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(int))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, int>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, int>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
                else if (Unsafe.SizeOf <T>() == sizeof(long))
                {
                    int result = SpanHelpers.IndexOf(
                        ref Unsafe.Add(ref Unsafe.As <byte, long>(ref array.GetRawSzArrayData()), startIndex),
                        Unsafe.As <T, long>(ref value),
                        count);
                    return((result >= 0 ? startIndex : 0) + result);
                }
            }

            return(EqualityComparer <T> .Default.IndexOf(array, value, startIndex, count));
        }
Пример #2
0
        public static int IndexOf <T>(ref T searchSpace, int searchSpaceLength, ref T value, int valueLength) where T : IEquatable <T>
        {
            // The optimized implementation should be used for these types
            Debug.Assert(!RuntimeHelpers.IsBitwiseEquatable <T>() || !(Unsafe.SizeOf <T>() == sizeof(byte) || Unsafe.SizeOf <T>() == sizeof(char)));

            Debug.Assert(searchSpaceLength >= 0);
            Debug.Assert(valueLength >= 0);

            if (valueLength == 0)
            {
                return(0);  // A zero-length sequence is always treated as "found" at the start of the search space.
            }
            T     valueHead       = value;
            ref T valueTail       = ref Unsafe.Add(ref value, 1);