Vector`1.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SmartCoalApplication.Base
  7. {
  8. public sealed class Vector<T>
  9. {
  10. private int count = 0;
  11. private T[] array;
  12. public Vector()
  13. : this(10)
  14. {
  15. }
  16. public Vector(int capacity)
  17. {
  18. this.array = new T[capacity];
  19. }
  20. public Vector(IEnumerable<T> copyMe)
  21. {
  22. foreach (T t in copyMe)
  23. {
  24. Add(t);
  25. }
  26. }
  27. public void Add(T pt)
  28. {
  29. if (this.count >= this.array.Length)
  30. {
  31. Grow(this.count + 1);
  32. }
  33. this.array[this.count] = pt;
  34. ++this.count;
  35. }
  36. public void Insert(int index, T item)
  37. {
  38. if (this.count >= this.array.Length)
  39. {
  40. Grow(this.count + 1);
  41. }
  42. ++this.count;
  43. for (int i = this.count - 1; i >= index + 1; --i)
  44. {
  45. this.array[i] = this.array[i - 1];
  46. }
  47. this.array[index] = item;
  48. }
  49. public void Clear()
  50. {
  51. this.count = 0;
  52. }
  53. public T this[int index]
  54. {
  55. get
  56. {
  57. return Get(index);
  58. }
  59. set
  60. {
  61. Set(index, value);
  62. }
  63. }
  64. public T Get(int index)
  65. {
  66. if (index < 0 || index >= this.count)
  67. {
  68. throw new ArgumentOutOfRangeException("index", index, "0 <= index < count");
  69. }
  70. return this.array[index];
  71. }
  72. public unsafe T GetUnchecked(int index)
  73. {
  74. return this.array[index];
  75. }
  76. public void Set(int index, T pt)
  77. {
  78. if (index < 0)
  79. {
  80. throw new ArgumentOutOfRangeException("index", index, "0 <= index");
  81. }
  82. if (index >= this.array.Length)
  83. {
  84. Grow(index + 1);
  85. }
  86. this.array[index] = pt;
  87. }
  88. public int Count
  89. {
  90. get
  91. {
  92. return this.count;
  93. }
  94. }
  95. private void Grow(int min)
  96. {
  97. int newSize = this.array.Length;
  98. if (newSize <= 0)
  99. {
  100. newSize = 1;
  101. }
  102. while (newSize < min)
  103. {
  104. newSize = 1 + ((newSize * 10) / 8);
  105. }
  106. T[] replacement = new T[newSize];
  107. for (int i = 0; i < this.count; i++)
  108. {
  109. replacement[i] = this.array[i];
  110. }
  111. this.array = replacement;
  112. }
  113. public T[] ToArray()
  114. {
  115. T[] ret = new T[this.count];
  116. for (int i = 0; i < this.count; i++)
  117. {
  118. ret[i] = this.array[i];
  119. }
  120. return ret;
  121. }
  122. public unsafe T[] UnsafeArray
  123. {
  124. get
  125. {
  126. return this.array;
  127. }
  128. }
  129. /// <summary>
  130. /// Gets direct access to the array held by the Vector.
  131. /// The caller must not modify the array.
  132. /// </summary>
  133. /// <param name="array">The array.</param>
  134. /// <param name="length">The actual number of items stored in the array. This number will be less than or equal to array.Length.</param>
  135. /// <remarks>This method is supplied strictly for performance-critical purposes.</remarks>
  136. public unsafe void GetArrayReadOnly(out T[] arrayResult, out int lengthResult)
  137. {
  138. arrayResult = this.array;
  139. lengthResult = this.count;
  140. }
  141. }
  142. }