Vector`1.cs 3.7 KB

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