123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SmartCoalApplication.Base
- {
- public sealed class Vector<T>
- {
- private int count = 0;
- private T[] array;
- public Vector()
- : this(10)
- {
- }
- public Vector(int capacity)
- {
- this.array = new T[capacity];
- }
- public Vector(IEnumerable<T> copyMe)
- {
- foreach (T t in copyMe)
- {
- Add(t);
- }
- }
- public void Add(T pt)
- {
- if (this.count >= this.array.Length)
- {
- Grow(this.count + 1);
- }
- this.array[this.count] = pt;
- ++this.count;
- }
- public void Insert(int index, T item)
- {
- if (this.count >= this.array.Length)
- {
- Grow(this.count + 1);
- }
- ++this.count;
- for (int i = this.count - 1; i >= index + 1; --i)
- {
- this.array[i] = this.array[i - 1];
- }
- this.array[index] = item;
- }
- public void Clear()
- {
- this.count = 0;
- }
- public T this[int index]
- {
- get
- {
- return Get(index);
- }
- set
- {
- Set(index, value);
- }
- }
- public T Get(int index)
- {
- if (index < 0 || index >= this.count)
- {
- throw new ArgumentOutOfRangeException("index", index, "0 <= index < count");
- }
- return this.array[index];
- }
- public unsafe T GetUnchecked(int index)
- {
- return this.array[index];
- }
- public void Set(int index, T pt)
- {
- if (index < 0)
- {
- throw new ArgumentOutOfRangeException("index", index, "0 <= index");
- }
- if (index >= this.array.Length)
- {
- Grow(index + 1);
- }
- this.array[index] = pt;
- }
- public int Count
- {
- get
- {
- return this.count;
- }
- }
- private void Grow(int min)
- {
- int newSize = this.array.Length;
- if (newSize <= 0)
- {
- newSize = 1;
- }
- while (newSize < min)
- {
- newSize = 1 + ((newSize * 10) / 8);
- }
- T[] replacement = new T[newSize];
- for (int i = 0; i < this.count; i++)
- {
- replacement[i] = this.array[i];
- }
- this.array = replacement;
- }
- public T[] ToArray()
- {
- T[] ret = new T[this.count];
- for (int i = 0; i < this.count; i++)
- {
- ret[i] = this.array[i];
- }
- return ret;
- }
- public unsafe T[] UnsafeArray
- {
- get
- {
- return this.array;
- }
- }
- /// <summary>
- /// Gets direct access to the array held by the Vector.
- /// The caller must not modify the array.
- /// </summary>
- /// <param name="array">The array.</param>
- /// <param name="length">The actual number of items stored in the array. This number will be less than or equal to array.Length.</param>
- /// <remarks>This method is supplied strictly for performance-critical purposes.</remarks>
- public unsafe void GetArrayReadOnly(out T[] arrayResult, out int lengthResult)
- {
- arrayResult = this.array;
- lengthResult = this.count;
- }
- }
- }
|