static public int[] GetStartingIndex(this IArrayDefinition array) { var indexes = new int[array.Rank]; array.GetStartingIndex(indexes); return(indexes); }
static public void GetStartingIndex(this IArrayDefinition array, int[] indexes) { for (int i = 0; i < array.Rank; i++) { indexes[i] = array.GetLowerBound(i); } }
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); }
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); }
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); }
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)); }
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); }
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); }
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); }
static public bool MoveIndex(this IArrayDefinition array, int[] indexes, int dimension) { return(MoveIndex(array, ref indexes[dimension], dimension)); }
static public void GetResetIndex(this IArrayDefinition array, int[] indexes) { array.GetStartingIndex(indexes); indexes[array.Rank - 1]--; }
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)); }
static public void GetResetIndex(this IArrayDefinition array, out int i, out int j) { GetStartingIndex(array, out i, out j); j--; }
static public IEnumerable <int[]> Indexes(this IArrayDefinition array) { return(new IndexEnumerable(array)); }
static public int[] GetResetIndex(this IArrayDefinition array) { int[] indexes = array.GetStartingIndex(); indexes[array.Rank - 1]--; return(indexes); }
static public void GetStartingIndex(this IArrayDefinition array, out int i) { i = array.GetLowerBound(0); }
static public int GetOutOfBound(this IArrayDefinition array, int dimension) { return(array.GetLowerBound(dimension) + array.GetLength(dimension)); }
static public bool MoveIndex(this IArrayDefinition array, ref int i) { i++; return(i < array.GetOutOfBound(0)); }
public IndexEnumerable(IArrayDefinition array) => _array = array;
public IndexEnumerator(IArrayDefinition array) { _array = array; Current = _array.GetResetIndex(); }