/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. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace Debugger
  7. {
  8. /// <summary>
  9. /// Specifies the range of valid indicies for all array dimensions
  10. /// </summary>
  11. public class ArrayDimensions: IEnumerable<ArrayDimension>
  12. {
  13. List<ArrayDimension> dimensions = new List<ArrayDimension>();
  14. public IEnumerator<ArrayDimension> GetEnumerator()
  15. {
  16. return dimensions.GetEnumerator();
  17. }
  18. IEnumerator IEnumerable.GetEnumerator()
  19. {
  20. return ((IEnumerable)dimensions).GetEnumerator();
  21. }
  22. /// <summary> Gets a given dimension </summary>
  23. public ArrayDimension this[int index] {
  24. get {
  25. return dimensions[index];
  26. }
  27. }
  28. /// <summary> Get the number of dimensions of the array </summary>
  29. public int Count {
  30. get {
  31. return dimensions.Count;
  32. }
  33. }
  34. /// <summary> Get the total number of elements within the bounds
  35. /// of an array specified by these dimensions. </summary>
  36. public int TotalElementCount {
  37. get {
  38. int totalCount = 1;
  39. foreach(ArrayDimension dim in this) {
  40. totalCount *= dim.Count;
  41. }
  42. return totalCount;
  43. }
  44. }
  45. /// <summary> Enumerate all vaild indicies in the array </summary>
  46. public IEnumerable<int[]> Indices {
  47. get {
  48. foreach(ArrayDimension dim in this) {
  49. if (dim.Count == 0) yield break;
  50. }
  51. int rank = this.Count;
  52. int[] indices = new int[rank];
  53. for(int i = 0; i < rank; i++) {
  54. indices[i] = this[i].LowerBound;
  55. }
  56. while(true) { // Go thought all combinations
  57. for (int i = rank - 1; i >= 1; i--) {
  58. if (indices[i] > this[i].UpperBound) {
  59. indices[i] = this[i].LowerBound;
  60. indices[i - 1]++;
  61. }
  62. }
  63. if (indices[0] > this[0].UpperBound) yield break; // We are done
  64. yield return (int[])indices.Clone();
  65. indices[rank - 1]++;
  66. }
  67. }
  68. }
  69. /// <summary> Determines whether the given index is a valid index for the array </summary>
  70. public bool IsIndexValid(int[] indices)
  71. {
  72. for (int i = 0; i < this.Count; i++) {
  73. if (!this[i].IsIndexValid(indices[i])) return false;
  74. }
  75. return true;
  76. }
  77. public ArrayDimensions(List<ArrayDimension> dimensions)
  78. {
  79. this.dimensions = dimensions;
  80. }
  81. public override string ToString()
  82. {
  83. string result = "[";
  84. bool isFirst = true;
  85. foreach(ArrayDimension dim in this) {
  86. if (!isFirst) result += ", ";
  87. result += dim.ToString();
  88. isFirst = false;
  89. }
  90. result += "]";
  91. return result;
  92. }
  93. }
  94. }