123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using UnityEngine;
- namespace SRF
- {
- [Serializable]
- public class SRList<T> : IEnumerable<T>, IList<T>, ICollection<T>, IEnumerable, ISerializationCallbackReceiver
- {
- public SRList()
- {
- }
- public SRList(int capacity)
- {
- this.Buffer = new T[capacity];
- }
- public SRList(IEnumerable<T> source)
- {
- this.AddRange(source);
- }
- public T[] Buffer
- {
- get
- {
- return this._buffer;
- }
- private set
- {
- this._buffer = value;
- }
- }
- private EqualityComparer<T> EqualityComparer
- {
- get
- {
- if (this._equalityComparer == null)
- {
- this._equalityComparer = EqualityComparer<T>.Default;
- }
- return this._equalityComparer;
- }
- }
- public int Count
- {
- get
- {
- return this._count;
- }
- private set
- {
- this._count = value;
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- if (this.Buffer != null)
- {
- for (int i = 0; i < this.Count; i++)
- {
- yield return this.Buffer[i];
- }
- }
- yield break;
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- public void Add(T item)
- {
- if (this.Buffer == null || this.Count == this.Buffer.Length)
- {
- this.Expand();
- }
- this.Buffer[this.Count++] = item;
- }
- public void Clear()
- {
- this.Count = 0;
- }
- public bool Contains(T item)
- {
- if (this.Buffer == null)
- {
- return false;
- }
- for (int i = 0; i < this.Count; i++)
- {
- if (this.EqualityComparer.Equals(this.Buffer[i], item))
- {
- return true;
- }
- }
- return false;
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- this.Trim();
- this.Buffer.CopyTo(array, arrayIndex);
- }
- public bool Remove(T item)
- {
- if (this.Buffer == null)
- {
- return false;
- }
- int num = this.IndexOf(item);
- if (num < 0)
- {
- return false;
- }
- this.RemoveAt(num);
- return true;
- }
- public bool IsReadOnly
- {
- get
- {
- return false;
- }
- }
- public int IndexOf(T item)
- {
- if (this.Buffer == null)
- {
- return -1;
- }
- for (int i = 0; i < this.Count; i++)
- {
- if (this.EqualityComparer.Equals(this.Buffer[i], item))
- {
- return i;
- }
- }
- return -1;
- }
- public void Insert(int index, T item)
- {
- if (this.Buffer == null || this.Count == this.Buffer.Length)
- {
- this.Expand();
- }
- if (index < this.Count)
- {
- for (int i = this.Count; i > index; i--)
- {
- this.Buffer[i] = this.Buffer[i - 1];
- }
- this.Buffer[index] = item;
- this.Count++;
- }
- else
- {
- this.Add(item);
- }
- }
- public void RemoveAt(int index)
- {
- if (this.Buffer != null && index < this.Count)
- {
- this.Count--;
- this.Buffer[index] = default(T);
- for (int i = index; i < this.Count; i++)
- {
- this.Buffer[i] = this.Buffer[i + 1];
- }
- }
- }
- public T this[int index]
- {
- get
- {
- if (this.Buffer == null)
- {
- throw new IndexOutOfRangeException();
- }
- return this.Buffer[index];
- }
- set
- {
- if (this.Buffer == null)
- {
- throw new IndexOutOfRangeException();
- }
- this.Buffer[index] = value;
- }
- }
- public void OnBeforeSerialize()
- {
- UnityEngine.Debug.Log("[OnBeforeSerialize] Count: {0}".Fmt(new object[]
- {
- this._count
- }));
- this.Clean();
- }
- public void OnAfterDeserialize()
- {
- UnityEngine.Debug.Log("[OnAfterDeserialize] Count: {0}".Fmt(new object[]
- {
- this._count
- }));
- }
- public void AddRange(IEnumerable<T> range)
- {
- foreach (T item in range)
- {
- this.Add(item);
- }
- }
- public void Clear(bool clean)
- {
- this.Clear();
- if (!clean)
- {
- return;
- }
- this.Clean();
- }
- public void Clean()
- {
- if (this.Buffer == null)
- {
- return;
- }
- for (int i = this.Count; i < this._buffer.Length; i++)
- {
- this._buffer[i] = default(T);
- }
- }
- public ReadOnlyCollection<T> AsReadOnly()
- {
- if (this._readOnlyWrapper == null)
- {
- this._readOnlyWrapper = new ReadOnlyCollection<T>(this);
- }
- return this._readOnlyWrapper;
- }
- private void Expand()
- {
- T[] array = (this.Buffer == null) ? new T[32] : new T[Mathf.Max(this.Buffer.Length << 1, 32)];
- if (this.Buffer != null && this.Count > 0)
- {
- this.Buffer.CopyTo(array, 0);
- }
- this.Buffer = array;
- }
- public void Trim()
- {
- if (this.Count > 0)
- {
- if (this.Count >= this.Buffer.Length)
- {
- return;
- }
- T[] array = new T[this.Count];
- for (int i = 0; i < this.Count; i++)
- {
- array[i] = this.Buffer[i];
- }
- this.Buffer = array;
- }
- else
- {
- this.Buffer = new T[0];
- }
- }
- public void Sort(Comparison<T> comparer)
- {
- bool flag = true;
- while (flag)
- {
- flag = false;
- for (int i = 1; i < this.Count; i++)
- {
- if (comparer(this.Buffer[i - 1], this.Buffer[i]) > 0)
- {
- T t = this.Buffer[i];
- this.Buffer[i] = this.Buffer[i - 1];
- this.Buffer[i - 1] = t;
- flag = true;
- }
- }
- }
- }
- [SerializeField]
- private T[] _buffer;
- [SerializeField]
- private int _count;
- private EqualityComparer<T> _equalityComparer;
- private ReadOnlyCollection<T> _readOnlyWrapper;
- }
- }
|