PageRenderTime 14ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Debugger/Debugger.Core/ArrayDimensions.cs

http://github.com/icsharpcode/ILSpy
C# | 109 lines | 83 code | 15 blank | 11 comment | 9 complexity | 8d28865fb7c13be6346a9a837b6cc907 MD5 | raw file
  1// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
  3
  4using System;
  5using System.Collections;
  6using System.Collections.Generic;
  7
  8namespace Debugger
  9{
 10	/// <summary>
 11	/// Specifies the range of valid indicies for all array dimensions
 12	/// </summary>
 13	public class ArrayDimensions: IEnumerable<ArrayDimension>
 14	{
 15		List<ArrayDimension> dimensions = new List<ArrayDimension>();
 16		
 17		public IEnumerator<ArrayDimension> GetEnumerator()
 18		{
 19			return dimensions.GetEnumerator();
 20		}
 21		
 22		IEnumerator IEnumerable.GetEnumerator()
 23		{
 24			return ((IEnumerable)dimensions).GetEnumerator();
 25		}
 26		
 27		/// <summary> Gets a given dimension </summary>
 28		public ArrayDimension this[int index] {
 29			get {
 30				return dimensions[index];
 31			}
 32		}
 33		
 34		/// <summary> Get the number of dimensions of the array </summary>
 35		public int Count {
 36			get {
 37				return dimensions.Count;
 38			}
 39		}
 40		
 41		/// <summary> Get the total number of elements within the bounds
 42		/// of an array specified by these dimensions. </summary>
 43		public int TotalElementCount {
 44			get {
 45				int totalCount = 1;
 46				foreach(ArrayDimension dim in this) {
 47					totalCount *= dim.Count;
 48				}
 49				return totalCount;
 50			}
 51		}
 52		
 53		/// <summary> Enumerate all vaild indicies in the array </summary>
 54		public IEnumerable<int[]> Indices {
 55			get {
 56				foreach(ArrayDimension dim in this) {
 57					if (dim.Count == 0) yield break;
 58				}
 59				
 60				int rank = this.Count;
 61				int[] indices = new int[rank];
 62				for(int i = 0; i < rank; i++) {
 63					indices[i] = this[i].LowerBound;
 64				}
 65				
 66				while(true) { // Go thought all combinations
 67					for (int i = rank - 1; i >= 1; i--) {
 68						if (indices[i] > this[i].UpperBound) {
 69							indices[i] = this[i].LowerBound;
 70							indices[i - 1]++;
 71						}
 72					}
 73					if (indices[0] > this[0].UpperBound) yield break; // We are done
 74					
 75					yield return (int[])indices.Clone();
 76					
 77					indices[rank - 1]++;
 78				}
 79			}
 80		}
 81		
 82		/// <summary> Determines whether the given index is a valid index for the array </summary>
 83		public bool IsIndexValid(int[] indices)
 84		{
 85			for (int i = 0; i < this.Count; i++) {
 86				if (!this[i].IsIndexValid(indices[i])) return false;
 87			}
 88			return true;
 89		}
 90		
 91		public ArrayDimensions(List<ArrayDimension> dimensions)
 92		{
 93			this.dimensions = dimensions;
 94		}
 95		
 96		public override string ToString()
 97		{
 98			string result = "[";
 99			bool isFirst = true;
100			foreach(ArrayDimension dim in this) {
101				if (!isFirst) result += ", ";
102				result += dim.ToString();
103				isFirst = false;
104			}
105			result += "]";
106			return result;
107		}
108	}
109}