Field.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // $Id$
  2. using System;
  3. using System.Collections.Generic;
  4. namespace DataStructures
  5. {
  6. /// <summary>This class represents a dynamic 2-dimensional array</summary>
  7. public class Field<T>
  8. {
  9. protected List<T> Elements = new List<T>();
  10. protected uint width;
  11. protected uint height;
  12. public Field()
  13. {
  14. }
  15. public Field(uint Width, uint Height, T FillValue)
  16. {
  17. this.width = Width;
  18. this.height = Height;
  19. for(uint i = 0; i < Width * Height; ++i)
  20. Elements.Add(FillValue);
  21. }
  22. public Field(List<T> Values, uint Width, uint Height)
  23. {
  24. Assign(Values, Width, Height);
  25. }
  26. /// <summary>
  27. /// Clone Subset of other field
  28. /// </summary>
  29. public Field(Field<T> Other, int startX, int startY, uint width, uint height) {
  30. this.width = width;
  31. this.height = height;
  32. if (startX < 0) throw new ArgumentOutOfRangeException("startX");
  33. if (startY < 0) throw new ArgumentOutOfRangeException("startY");
  34. if (startX + width > Other.Width) throw new ArgumentOutOfRangeException("startX");
  35. if (startY + height > Other.Height) throw new ArgumentOutOfRangeException("startY");
  36. for (int y = 0; y < height; y++) {
  37. for (int x = 0; x < width; x++) {
  38. Elements.Add(Other[x + startX, y + startY]);
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// Width of array
  44. /// </summary>
  45. public uint Width
  46. {
  47. get
  48. {
  49. return width;
  50. }
  51. }
  52. /// <summary>
  53. /// Height of array
  54. /// </summary>
  55. public uint Height
  56. {
  57. get
  58. {
  59. return height;
  60. }
  61. }
  62. public T this[uint X, uint Y]
  63. {
  64. get
  65. {
  66. return Elements[(int) (Y * width + X)];
  67. }
  68. set
  69. {
  70. Elements[(int) (Y * width + X)] = value;
  71. }
  72. }
  73. public T this[int X, int Y]
  74. {
  75. get
  76. {
  77. return Elements[Y * (int) width + X];
  78. }
  79. set
  80. {
  81. Elements[Y * (int) width + X] = value;
  82. }
  83. }
  84. public T this[FieldPos Pos]
  85. {
  86. get
  87. {
  88. return this[(uint) Pos.X, (uint) Pos.Y];
  89. }
  90. set
  91. {
  92. this[(uint) Pos.X, (uint) Pos.Y] = value;
  93. }
  94. }
  95. public void Assign(List<T> Values, uint Width, uint Height)
  96. {
  97. if(Values.Count != Width * Height)
  98. throw new Exception("invalid size of value list for field");
  99. this.width = Width;
  100. this.height = Height;
  101. Elements.Clear();
  102. foreach(T val in Values) {
  103. Elements.Add(val);
  104. }
  105. }
  106. public void Resize(uint NewWidth, uint NewHeight, T FillValue)
  107. {
  108. List<T> NewElements = new List<T>();
  109. for(uint y = 0; y < NewHeight; ++y) {
  110. for(uint x = 0; x < NewWidth; ++x) {
  111. if(x < Width && y < Height)
  112. NewElements.Add(this[x, y]);
  113. else
  114. NewElements.Add(FillValue);
  115. }
  116. }
  117. Elements = NewElements;
  118. width = NewWidth;
  119. height = NewHeight;
  120. }
  121. public List<T> GetContentsArray()
  122. {
  123. List<T> Result = new List<T>(Elements);
  124. return Result;
  125. }
  126. public bool InBounds(FieldPos pos) {
  127. if (pos.X < 0) return false;
  128. if (pos.Y < 0) return false;
  129. if (pos.X >= Width) return false;
  130. if (pos.Y >= Height) return false;
  131. return true;
  132. }
  133. public bool EqualContents(object obj) {
  134. if (!(obj is Field<T>)) return false;
  135. Field<T> other = (Field<T>)obj;
  136. if (this.width != other.width) return false;
  137. if (this.height != other.height) return false;
  138. if (this.Elements.Count != other.Elements.Count) return false;
  139. for (int i = 0; i < this.Elements.Count; i++) {
  140. if (!this.Elements[i].Equals(other.Elements[i])) return false;
  141. }
  142. return true;
  143. }
  144. }
  145. }