|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /**
- * Custom modified version of Michael Lyashenko's BetterList
- **/
-
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace Svelto.DataStructures
- {
- public struct FasterListEnumerator<T> : IEnumerator<T>
- {
- public T Current
- {
- get { return _current; }
- }
-
- object IEnumerator.Current
- {
- get { return _current; }
- }
-
- T IEnumerator<T>.Current
- {
- get { return _current; }
- }
-
- public FasterListEnumerator(T[] buffer, int size)
- {
- _size = size;
- _counter = 0;
- _buffer = buffer;
- _current = default(T);
- }
-
- public void Dispose()
- {
- _buffer = null;
- }
-
- bool IEnumerator.MoveNext()
- {
- return MoveNext();
- }
-
- void IEnumerator.Reset()
- {
- Reset();
- }
-
- public bool MoveNext()
- {
- if (_counter < _size)
- {
- _current = _buffer[_counter++];
-
- return true;
- }
-
- _current = default(T);
-
- return false;
- }
-
- public void Reset()
- {
- _counter = 0;
- }
-
- T[] _buffer;
- int _counter;
- int _size;
- T _current;
- }
-
- public struct FasterReadOnlyList<T> : IList<T>
- {
- public FasterReadOnlyList(FasterList<T> list)
- {
- _list = list;
- }
-
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- public FasterListEnumerator<T> GetEnumerator()
- {
- return _list.GetEnumerator();
- }
-
- public void Add(T item)
- {
- throw new NotImplementedException();
- }
-
- public void Clear()
- {
- throw new NotImplementedException();
- }
-
- public bool Contains(T item)
- {
- return _list.Contains(item);
- }
-
- public void CopyTo(T[] array, int arrayIndex)
- {
- _list.CopyTo(array, arrayIndex);
- }
-
- public bool Remove(T item)
- {
- throw new NotImplementedException();
- }
-
- public int Count { get { return _list.Count; } }
- public bool IsReadOnly { get { return true; } }
-
- public int IndexOf(T item)
- {
- return _list.IndexOf(item);
- }
-
- public void Insert(int index, T item)
- {
- throw new NotImplementedException();
- }
-
- public void RemoveAt(int index)
- {
- throw new NotImplementedException();
- }
-
- public T this[int index] { get { return _list[index]; } set { throw new NotImplementedException(); } }
-
- readonly FasterList<T> _list;
- }
-
- public class FasterList<T> : IList<T>
- {
- public int Count
- {
- get { return _count; }
- }
-
- public bool IsReadOnly
- {
- get { return false; }
- }
-
- public FasterList()
- {
- _count = 0;
-
- _buffer = new T[MIN_SIZE];
- }
-
- public FasterList(int initialSize)
- {
- _count = 0;
-
- _buffer = new T[initialSize];
- }
-
- public FasterList(ICollection<T> collection)
- {
- _buffer = new T[collection.Count];
-
- collection.CopyTo(_buffer, 0);
-
- _count = _buffer.Length;
- }
-
- public FasterList(FasterList<T> listCopy)
- {
- _buffer = new T[listCopy.Count];
-
- listCopy.CopyTo(_buffer, 0);
-
- _count = listCopy.Count;
- }
-
- public void Add(T item)
- {
- if (_count == _buffer.Length)
- AllocateMore();
-
- _buffer[_count++] = item;
- }
-
- public void AddRange(IEnumerable<T> items, int count)
- {
- if (_count + count >= _buffer.Length)
- AllocateMore(_count + count);
-
- for (var i = items.GetEnumerator(); i.MoveNext();)
- {
- var item = i.Current;
- _buffer[_count++] = item;
- }
- }
-
- public void AddRange(ICollection<T> items)
- {
- var count = items.Count;
- if (_count + count >= _buffer.Length)
- AllocateMore(_count + count);
-
- for (var i = items.GetEnumerator(); i.MoveNext();)
- {
- var item = i.Current;
- _buffer[_count++] = item;
- }
- }
-
- public void AddRange(FasterList<T> items)
- {
- var count = items.Count;
- if (_count + count >= _buffer.Length)
- AllocateMore(_count + count);
-
- Array.Copy(items._buffer, 0, _buffer, _count, count);
- _count += count;
- }
-
- public void AddRange(T[] items)
- {
- var count = items.Length;
- if (_count + count >= _buffer.Length)
- AllocateMore(_count + count);
-
- Array.Copy(items, 0, _buffer, _count, count);
- _count += count;
- }
-
- public FasterReadOnlyList<T> AsReadOnly()
- {
- return new FasterReadOnlyList<T>(this);
- }
-
- public void Clear()
- {
- _count = 0;
- }
-
- public bool Contains(T item)
- {
- var index = IndexOf(item);
-
- return index != -1;
- }
-
- public void CopyTo(T[] array, int arrayIndex)
- {
- Array.Copy(_buffer, 0, array, arrayIndex, Count);
- }
-
- public FasterListEnumerator<T> GetEnumerator()
- {
- return new FasterListEnumerator<T>(_buffer, Count);
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- return GetEnumerator();
- }
-
- public int IndexOf(T item)
- {
- var comp = EqualityComparer<T>.Default;
-
- for (var index = _count - 1; index >= 0; --index)
- if (comp.Equals(_buffer[index], item))
- return index;
-
- return -1;
- }
-
- public void Insert(int index, T item)
- {
- DesignByContract.Check.Require(index < _count, "out of bound index");
-
- if (_count == _buffer.Length) AllocateMore();
-
- Array.Copy(_buffer, index, _buffer, index + 1, _count - index);
-
- _buffer[index] = item;
- ++_count;
- }
-
- public void Release()
- {
- _count = 0;
- _buffer = null;
- }
-
- public bool Remove(T item)
- {
- var index = IndexOf(item);
-
- if (index == -1)
- return false;
-
- RemoveAt(index);
-
- return true;
- }
-
- public void RemoveAt(int index)
- {
- DesignByContract.Check.Require(index < _count, "out of bound index");
-
- --_count;
-
- if (index == _count)
- return;
-
- Array.Copy(_buffer, index + 1, _buffer, index, _count - index);
- }
-
- public void Resize(int newSize)
- {
- Array.Resize(ref _buffer, newSize);
-
- _count = newSize;
- }
-
- public void SetAt(int index, T value)
- {
- if (index >= _buffer.Length)
- AllocateMore(index + 1);
-
- if (_count <= index)
- _count = index + 1;
-
- this[index] = value;
- }
-
- public void Sort(Comparison<T> comparer)
- {
- Trim();
- Array.Sort(_buffer, comparer);
- }
-
- public T[] ToArray()
- {
- T[] destinationArray = new T[_count];
-
- Array.Copy(_buffer, 0, destinationArray, 0, _count);
-
- return destinationArray;
- }
-
- /// <summary>
- /// This function exists to allow fast iterations. The size of the array returned cannot be
- /// used. The list count must be used instead.
- /// </summary>
- /// <returns></returns>
- public T[] ToArrayFast()
- {
- return _buffer;
- }
-
- public bool UnorderredRemove(T item)
- {
- var index = IndexOf(item);
-
- if (index == -1)
- return false;
-
- UnorderredRemoveAt(index);
-
- return true;
- }
-
- public void UnorderredRemoveAt(int index)
- {
- DesignByContract.Check.Require(index < _count, "out of bound index");
-
- _buffer[index] = _buffer[--_count];
- }
-
- private void AllocateMore()
- {
- var newList = new T[Mathf.Max(_buffer.Length << 1, MIN_SIZE)];
- if (_count > 0) _buffer.CopyTo(newList, 0);
- _buffer = newList;
- }
-
- private void AllocateMore(int newSize)
- {
- var oldLength = _buffer.Length;
-
- while (oldLength < newSize)
- oldLength <<= 1;
-
- var newList = new T[oldLength];
- if (_count > 0) _buffer.CopyTo(newList, 0);
- _buffer = newList;
- }
-
- private void Trim()
- {
- if (_count < _buffer.Length)
- Resize(_count);
- }
-
- public T this[int i]
- {
- get { DesignByContract.Check.Require(i < _count, "out of bound index"); return _buffer[i]; }
- set { DesignByContract.Check.Require(i < _count, "out of bound index"); _buffer[i] = value; }
- }
-
- private const int MIN_SIZE = 32;
- private T[] _buffer;
- private int _count;
- }
- }
|