public static Array CreateArray <T>(this RegularArrayInfo arrayInfo, ICollection <T> coll) { if (arrayInfo == null) { throw new ArgumentNullException("arrayInfo"); } if (coll == null) { throw new ArgumentNullException("coll"); } if (coll.Count != arrayInfo.Length) { throw new ArgumentException("Invalid collection length", "coll"); } var indices = new int[arrayInfo.Rank]; var array = arrayInfo.CreateArray <T>(); coll.ForEach((t, i) => { arrayInfo.CalcDimIndices(i, indices); arrayInfo.SetValue(array, t, indices); }); return(array); }
public static int TransformFlatIndex(this RegularArrayInfo arrayInfo, int flatIndex, params Range[] ranges) { arrayInfo.ValidateRanges(ranges); int[] dimIndices = new int[arrayInfo.Rank]; RegularArrayInfo ai = new RegularArrayInfo(ranges.Select(r => new ArrayDimension(r.Count, r.Index)).ToArray()); ai.CalcDimIndices(flatIndex, dimIndices); return(arrayInfo.CalcFlatIndex(dimIndices)); }
public static IEnumerable <int> EnumerateFlatIndex(this RegularArrayInfo arrayInfo, params Range[] ranges) { arrayInfo.ValidateRanges(ranges); int[] dimIndices = new int[arrayInfo.Rank]; for (ArrayIndex ai = new ArrayIndex(new RegularArrayInfo(ranges.Select(r => new ArrayDimension(r.Count, r.Index)).ToArray())); ai.Carry == 0; ai++) { ai.GetDimIndices(dimIndices); yield return(arrayInfo.CalcFlatIndex(dimIndices)); } }
public static void ValidateRanges(this RegularArrayInfo arrayInfo, params Range[] ranges) { if (arrayInfo == null) { throw new ArgumentNullException("arrayInfo"); } if (ranges == null) { throw new ArgumentNullException("ranges"); } if (ranges.Length != arrayInfo.Rank) { throw new ArgumentException("Invalid array length", "ranges"); } if (Enumerable.Range(0, ranges.Length) .Any(i => ranges[i].Index <arrayInfo.LowerBounds[i] || ranges[i].Index + ranges[i].Count> arrayInfo.LowerBounds[i] + arrayInfo.Lengths[i])) { throw new ArgumentException("Out of range dimension exists"); } //return new RegularArrayInfo(ranges.Select(r => new ArrayDimension(r.Count, r.Index)).ToArray()); }