Example #1
0
        /// <summary>
        /// Finds all occurrence indexes of elements matching the specified predicate condition(s) in an array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="haystack">The array to search.</param>
        /// <param name="match">The expression for the elements to match.</param>
        /// <param name="count">The number of elements to find, 0 for all.</param>
        /// <param name="index">The index to begin searching from.</param>
        /// <returns><see cref="System.Int32[]"/></returns>
        private static int[] SafeCall <T>(T[] haystack, Func <T, bool> match, int count = 0, int index = 0)
        {
            int[] results = new int[100];
            int   size    = 0;

            for (int i = index; i < haystack.Length; i++)
            {
                if (match(haystack[i]))
                {
                    results[size] = i;
                    if (results.Length == count && count != 0)
                    {
                        break;
                    }
                    size++;
                    if (size > results.Length)
                    {
                        Resize.Call(ref results, size + 100);
                    }
                }
            }

            Resize.Call(ref results, size);
            return(results);
        }
Example #2
0
        /// <summary>
        /// Appends a collection of elements to an array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array">The array to append to.</param>
        /// <param name="collection">The collection to append.</param>
        public static void SafeCall <T>(ref T[] array, T[] collection)
        {
            Resize.Call(ref array, array.Length + collection.Length);

            for (int i = 0; i < collection.Length; i++)
            {
                array[(array.Length - collection.Length) + i] = collection[i];
            }
        }
Example #3
0
        /// <summary>
        /// Finds all occurrence indexes of elements matching the specified predicate condition(s) in an array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="haystack">The array to search.</param>
        /// <param name="needle">The collection to find.</param>
        /// <param name="count">The number of elements to find, 0 for all.</param>
        /// <param name="index">The index to begin searching from.</param>
        /// <returns><see cref="System.Int32[]"/></returns>
        private static int[] SafeCall <T>(T[] haystack, T[] needle, int count = 0, int index = 0)
        {
            int[] results = new int[100];
            int   szResults = 0;
            int   nLast = needle.Length - 1;
            int   nMiddle = nLast / 2;
            int   hLast, hMiddle;
            bool  match = true;

            for (int i = index; i < haystack.Length; i++)
            {
                hLast   = i + nLast;
                hMiddle = i + nMiddle;

                if (hLast < haystack.Length)
                {
                    if (haystack[i].Equals(needle[0]) &&
                        haystack[hMiddle].Equals(needle[nMiddle]) &&
                        haystack[hLast].Equals(needle[nLast]))
                    {
                        match = true;

                        for (int j = 0; j < needle.Length; j++)
                        {
                            if (!haystack[i + j].Equals(needle[j]))
                            {
                                match = false;
                                break;
                            }
                        }

                        if (match)
                        {
                            results[szResults] = i;
                            if (results.Length == count && count != 0)
                            {
                                break;
                            }
                            i += needle.Length - 1;
                            szResults++;
                            if (szResults > results.Length)
                            {
                                Resize.Call(ref results, (szResults - 1) + 100);
                            }
                        }
                    }
                }
                else
                {
                    break;
                }
            }

            Resize.Call(ref results, szResults);
            return(results);
        }
Example #4
0
        /// <summary>
        /// Removes a collection of elements from an array
        /// </summary>
        /// <typeparam name="T">The element type of the array</typeparam>
        /// <param name="array">The array to manipulate</param>
        /// <param name="count">The number of elements to remove</param>
        /// <param name="index">The index of the first element in the desired collection to be removed</param>
        private static void SafeCall <T>(ref T[] array, int count, int index)
        {
            for (int i = index; i < array.Length; i++)
            {
                if (i == index)
                {
                    i += count - 1;
                }
                else
                {
                    array[i - count] = array[i];
                }
            }

            Resize.Call(ref array, array.Length - count);
        }
Example #5
0
        /// <summary>
        /// Inserts a collection of elements into an array
        /// </summary>
        /// <typeparam name="T">The element type of the array and collection</typeparam>
        /// <param name="array">The array to insert into</param>
        /// <param name="collection">The collection to insert</param>
        /// <param name="index">The index to insert at</param>
        private static void SafeCall <T>(ref T[] array, T[] collection, int index)
        {
            Resize.Call(ref array, array.Length + collection.Length);

            for (int i = array.Length - 1; i >= index; i--)
            {
                if (i == index)
                {
                    for (int j = 0; j < collection.Length; j++)
                    {
                        array[i + j] = collection[j];
                    }
                }
                else
                {
                    if (!(i < collection.Length))
                    {
                        array[i] = array[i - collection.Length];
                    }
                }
            }
        }