Esempio n. 1
0
        static public int[] GetStartingIndex(this IArrayDefinition array)
        {
            var indexes = new int[array.Rank];

            array.GetStartingIndex(indexes);
            return(indexes);
        }
Esempio n. 2
0
 static public void GetStartingIndex(this IArrayDefinition array, int[] indexes)
 {
     for (int i = 0; i < array.Rank; i++)
     {
         indexes[i] = array.GetLowerBound(i);
     }
 }
Esempio n. 3
0
        static public int[] Lengths(this IArrayDefinition array)
        {
            var lengths = new int[array.Rank];

            for (int i = 0; i < array.Rank; i++)
            {
                lengths[i] = array.GetLength(i);
            }

            return(lengths);
        }
Esempio n. 4
0
        static public bool MoveIndex(this IArrayDefinition array, ref int x, int dimension)
        {
            x++;
            if (x < array.GetOutOfBound(dimension))
            {
                return(true);
            }

            x = array.GetLowerBound(dimension);
            return(false);
        }
Esempio n. 5
0
        static public bool ContainsIndex(this IArrayDefinition array, params int[] indexes)
        {
            for (int r = 0; r < array.Rank; r++)
            {
                if (indexes[r] < array.GetLowerBound(r) || indexes[r] > array.GetUpperBound(r))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 6
0
        static public bool MoveIndex(this IArrayDefinition array, ref int i, ref int j)
        {
            j++;
            if (j < array.GetOutOfBound(1))
            {
                return(true);
            }

            j = 0;
            i++;
            return(i < array.GetOutOfBound(0));
        }
Esempio n. 7
0
        static public bool IndexesIntersects(this IArrayDefinition first, IArrayDefinition second, out IArrayDefinition intersection)
        {
            int[] minimums = first.GetStartingIndex().Zip(second.GetStartingIndex(), Math.Max).ToArray();
            int[] maximums = first.GetEndingIndex().Zip(second.GetEndingIndex(), Math.Min).ToArray();

            if (Enumerable.Range(0, first.Rank).Any(x => minimums[x] > maximums[x]))
            {
                intersection = new IndexRange();
                return(false);
            }

            intersection = new IndexRange(minimums, maximums.Zip(minimums, (x, y) => x - y + 1).ToArray());
            return(true);
        }
Esempio n. 8
0
        static public bool MoveIndex(this IArrayDefinition array, int[] indexes)
        {
            indexes[array.Rank - 1]++;

            for (int r = array.Rank - 1; r >= 0; r--)
            {
                if (indexes[r] < array.GetOutOfBound(r))
                {
                    break;
                }

                if (r - 1 < 0)
                {
                    return(false);
                }

                indexes[r] = array.GetLowerBound(r);
                indexes[r - 1]++;
            }

            return(true);
        }
Esempio n. 9
0
        static public Array ToResizedArray <T>(this Array array, int[] newLengths, bool keepValues = true, Func <T, int[], T> defaultValueFactory = null, int[] rangeToFill = null)
        {
            var newArray = Array.CreateInstance(typeof(T), newLengths);

            int[] fillLengths = rangeToFill ?? newArray.Lengths();

            IArrayDefinition excludingMask = null;

            if (keepValues)
            {
                int[] copyLengths = array.Lengths().Zip(fillLengths, Math.Min).ToArray();

                int copyRowsCount     = copyLengths[0];
                int copyRowLength     = copyLengths.Skip(1).Sum();
                int arrayRowLength    = array.Lengths().Skip(1).Sum();
                int newArrayRowLength = newArray.Lengths().Skip(1).Sum();

                for (int i = 0; i < copyRowsCount; ++i)
                {
                    Array.Copy(array, i * arrayRowLength, newArray, i * newArrayRowLength, copyRowLength);
                }

                excludingMask = new IndexRange(copyLengths);
            }

            defaultValueFactory = defaultValueFactory ?? ((_, __) => default);

            IEnumerable <int[]> fillIndexes = new IndexRange(fillLengths).Indexes();

            if (excludingMask != null)
            {
                fillIndexes = fillIndexes.Where(x => !excludingMask.ContainsIndex(x));
            }

            newArray.Fill(defaultValueFactory, fillIndexes);

            return(newArray);
        }
Esempio n. 10
0
 static public bool MoveIndex(this IArrayDefinition array, int[] indexes, int dimension)
 {
     return(MoveIndex(array, ref indexes[dimension], dimension));
 }
Esempio n. 11
0
 static public void GetResetIndex(this IArrayDefinition array, int[] indexes)
 {
     array.GetStartingIndex(indexes);
     indexes[array.Rank - 1]--;
 }
Esempio n. 12
0
        static public TwoDimensionIndexSequenceCollection ToSequences <T>(this IEnumerable <int[]> indexes, IArrayDefinition array)
        {
            var sequences = new List <TwoDimensionIndexSequence>();
            int sequenceI = 0;
            int sequenceJ = 0;
            int length    = 0;

            int[] predictedIndex = null;
            foreach (int[] index in indexes)
            {
                if (predictedIndex != null && !predictedIndex.Equals(index))
                {
                    sequences.Add(new TwoDimensionIndexSequence(sequenceI, sequenceJ, length));
                    length = 0;
                }

                if (length == 0)
                {
                    sequenceI = index[0];
                    sequenceJ = index[1];
                }

                length++;

                if (predictedIndex == null)
                {
                    predictedIndex = new int[2];
                }

                Array.Copy(index, predictedIndex, index.Length);
                array.MoveIndex(predictedIndex, 0);
            }

            return(new TwoDimensionIndexSequenceCollection(sequences));
        }
Esempio n. 13
0
 static public void GetResetIndex(this IArrayDefinition array, out int i, out int j)
 {
     GetStartingIndex(array, out i, out j);
     j--;
 }
Esempio n. 14
0
 static public IEnumerable <int[]> Indexes(this IArrayDefinition array)
 {
     return(new IndexEnumerable(array));
 }
Esempio n. 15
0
 static public int[] GetResetIndex(this IArrayDefinition array)
 {
     int[] indexes = array.GetStartingIndex();
     indexes[array.Rank - 1]--;
     return(indexes);
 }
Esempio n. 16
0
 static public void GetStartingIndex(this IArrayDefinition array, out int i)
 {
     i = array.GetLowerBound(0);
 }
Esempio n. 17
0
 static public int GetOutOfBound(this IArrayDefinition array, int dimension)
 {
     return(array.GetLowerBound(dimension) + array.GetLength(dimension));
 }
Esempio n. 18
0
 static public bool MoveIndex(this IArrayDefinition array, ref int i)
 {
     i++;
     return(i < array.GetOutOfBound(0));
 }
Esempio n. 19
0
 public IndexEnumerable(IArrayDefinition array) => _array = array;
Esempio n. 20
0
 public IndexEnumerator(IArrayDefinition array)
 {
     _array  = array;
     Current = _array.GetResetIndex();
 }