123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using UnityEngine;
- public class BetterList<T>
- {
- public IEnumerator<T> GetEnumerator()
- {
- if (this.buffer != null)
- {
- for (int i = 0; i < this.size; i++)
- {
- yield return this.buffer[i];
- }
- }
- yield break;
- }
- [DebuggerHidden]
- public T this[int i]
- {
- get
- {
- return this.buffer[i];
- }
- set
- {
- this.buffer[i] = value;
- }
- }
- private void AllocateMore()
- {
- T[] array = (this.buffer == null) ? new T[32] : new T[Mathf.Max(this.buffer.Length << 1, 32)];
- if (this.buffer != null && this.size > 0)
- {
- this.buffer.CopyTo(array, 0);
- }
- this.buffer = array;
- }
- private void Trim()
- {
- if (this.size > 0)
- {
- if (this.size < this.buffer.Length)
- {
- T[] array = new T[this.size];
- for (int i = 0; i < this.size; i++)
- {
- array[i] = this.buffer[i];
- }
- this.buffer = array;
- }
- }
- else
- {
- this.buffer = null;
- }
- }
- public void Clear()
- {
- this.size = 0;
- }
- public void Release()
- {
- this.size = 0;
- this.buffer = null;
- }
- public void Add(T item)
- {
- if (this.buffer == null || this.size == this.buffer.Length)
- {
- this.AllocateMore();
- }
- this.buffer[this.size++] = item;
- }
- public void Insert(int index, T item)
- {
- if (this.buffer == null || this.size == this.buffer.Length)
- {
- this.AllocateMore();
- }
- if (index > -1 && index < this.size)
- {
- for (int i = this.size; i > index; i--)
- {
- this.buffer[i] = this.buffer[i - 1];
- }
- this.buffer[index] = item;
- this.size++;
- }
- else
- {
- this.Add(item);
- }
- }
- public bool Contains(T item)
- {
- if (this.buffer == null)
- {
- return false;
- }
- for (int i = 0; i < this.size; i++)
- {
- if (this.buffer[i].Equals(item))
- {
- return true;
- }
- }
- return false;
- }
- public int IndexOf(T item)
- {
- if (this.buffer == null)
- {
- return -1;
- }
- for (int i = 0; i < this.size; i++)
- {
- if (this.buffer[i].Equals(item))
- {
- return i;
- }
- }
- return -1;
- }
- public bool Remove(T item)
- {
- if (this.buffer != null)
- {
- EqualityComparer<T> @default = EqualityComparer<T>.Default;
- for (int i = 0; i < this.size; i++)
- {
- if (@default.Equals(this.buffer[i], item))
- {
- this.size--;
- this.buffer[i] = default(T);
- for (int j = i; j < this.size; j++)
- {
- this.buffer[j] = this.buffer[j + 1];
- }
- this.buffer[this.size] = default(T);
- return true;
- }
- }
- }
- return false;
- }
- public void RemoveAt(int index)
- {
- if (this.buffer != null && index > -1 && index < this.size)
- {
- this.size--;
- this.buffer[index] = default(T);
- for (int i = index; i < this.size; i++)
- {
- this.buffer[i] = this.buffer[i + 1];
- }
- this.buffer[this.size] = default(T);
- }
- }
- public T Pop()
- {
- if (this.buffer != null && this.size != 0)
- {
- T result = this.buffer[--this.size];
- this.buffer[this.size] = default(T);
- return result;
- }
- return default(T);
- }
- public T[] ToArray()
- {
- this.Trim();
- return this.buffer;
- }
- [DebuggerHidden]
- [DebuggerStepThrough]
- public void Sort(BetterList<T>.CompareFunc comparer)
- {
- int num = 0;
- int num2 = this.size - 1;
- bool flag = true;
- while (flag)
- {
- flag = false;
- for (int i = num; i < num2; i++)
- {
- if (comparer(this.buffer[i], this.buffer[i + 1]) > 0)
- {
- T t = this.buffer[i];
- this.buffer[i] = this.buffer[i + 1];
- this.buffer[i + 1] = t;
- flag = true;
- }
- else if (!flag)
- {
- num = ((i != 0) ? (i - 1) : 0);
- }
- }
- }
- }
- public T[] buffer;
- public int size;
- public delegate int CompareFunc(T left, T right);
- }
|