├── BiDictionary.cs ├── BiDictionary.cs.meta ├── FastBucket.cs ├── FastBucket.cs.meta ├── FastDictionary.cs ├── FastDictionary.cs.meta ├── FastList.cs ├── FastList.cs.meta ├── FastQueue.cs ├── FastQueue.cs.meta ├── FastSorter.cs ├── FastSorter.cs.meta ├── FastStack.cs ├── FastStack.cs.meta ├── LICENSE ├── LICENSE.meta ├── LSEnumerable.cs ├── LSEnumerable.cs.meta ├── README.md ├── README.md.meta ├── Shortcuts.cs ├── Shortcuts.cs.meta ├── Tests.meta └── Tests ├── Array2DTester.cs └── Array2DTester.cs.meta /BiDictionary.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace FastCollections 6 | { 7 | public class BiDictionary : Dictionary 8 | { 9 | private Dictionary _reverseMap = new Dictionary(); 10 | public new void Add (T1 item1, T2 item2) { 11 | base.Add(item1,item2); 12 | _reverseMap.Add(item2,item1); 13 | } 14 | public void Remove (T1 item1, T2 item2) { 15 | base.Remove(item1); 16 | _reverseMap.Remove (item2); 17 | } 18 | 19 | public T1 GetReversed (T2 key) { 20 | return _reverseMap[key]; 21 | } 22 | public bool TryGetValueReversed (T2 key, out T1 value) { 23 | return _reverseMap.TryGetValue(key,out value); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /BiDictionary.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2fcbc8ec9391a4a4294899edfcfe1d5a 3 | timeCreated: 1451430327 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /FastBucket.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace FastCollections 7 | { 8 | public class FastBucket : FastEnumerable 9 | { 10 | public T[] innerArray; 11 | 12 | public BitArray arrayAllocation { get; private set; } 13 | 14 | private int Capacity = 8; 15 | 16 | public int Count { get; private set; } 17 | 18 | public int PeakCount { get; private set; } 19 | 20 | private FastStack OpenSlots = new FastStack(); 21 | 22 | public FastBucket() 23 | { 24 | Initialize(); 25 | } 26 | public FastBucket (int capacity) { 27 | this.Capacity = capacity; 28 | Initialize (); 29 | } 30 | 31 | private void Initialize() 32 | { 33 | innerArray = new T[Capacity]; 34 | arrayAllocation = new BitArray(Capacity); 35 | Count = 0; 36 | PeakCount = 0; 37 | } 38 | 39 | 40 | public int Add(T item) 41 | { 42 | int index = OpenSlots.Count > 0 ? OpenSlots.Pop() : PeakCount++; 43 | this._AddAt(item, index); 44 | return index; 45 | } 46 | public void InsertAt(T item, int index) 47 | { 48 | //Public API for adding at a specific index. 49 | //Note: Has linear performance 50 | if (index < arrayAllocation.Length && arrayAllocation.Get(index)) 51 | { 52 | //this.innerArray[index] = item; //If something's already there, just replace it 53 | } 54 | else { 55 | CheckCapacity(index + 1); 56 | if (index < PeakCount) 57 | { 58 | int indexIndex = Array.BinarySearch(OpenSlots.innerArray, index); 59 | Shortcuts.Shift(OpenSlots.innerArray,indexIndex, OpenSlots.innerArray.Length, -1); 60 | } 61 | else if (index >= PeakCount) 62 | { 63 | for (; PeakCount < index; PeakCount++) 64 | { 65 | OpenSlots.Add(PeakCount); 66 | } 67 | PeakCount++; 68 | } 69 | 70 | Count++; 71 | } 72 | this.innerArray[index] = item; 73 | arrayAllocation[index] = true; 74 | } 75 | 76 | public void _AddAt(T item, int index) 77 | { 78 | CheckCapacity(index + 1); 79 | arrayAllocation.Set(index, true); 80 | innerArray[index] = item; 81 | Count++; 82 | } 83 | 84 | private void CheckCapacity(int min) 85 | { 86 | if (min >= Capacity) 87 | { 88 | Capacity *= 2; 89 | if (Capacity < min) 90 | Capacity = min; 91 | Array.Resize(ref innerArray, Capacity); 92 | arrayAllocation.Length = 93 | arrayAllocation.Length >= Capacity ? 94 | arrayAllocation.Length : Capacity; 95 | } 96 | } 97 | 98 | public bool Remove(T item) 99 | { 100 | int index = Array.IndexOf(innerArray, item); 101 | if (index >= 0 && arrayAllocation[index]) 102 | { 103 | RemoveAt(index); 104 | return true; 105 | } 106 | return false; 107 | } 108 | 109 | public void RemoveAt(int index) 110 | { 111 | OpenSlots.Add(index); 112 | arrayAllocation.Set(index, false); 113 | this.innerArray[index] = default(T); 114 | Count--; 115 | } 116 | 117 | public bool SafeRemoveAt(int index, T item) 118 | { 119 | if (ContainsAt(index, item)) 120 | { 121 | this.RemoveAt(index); 122 | return true; 123 | } 124 | return false; 125 | } 126 | 127 | public bool ContainsAt(int index, T item) 128 | { 129 | return index >= 0 && index < PeakCount && this.arrayAllocation[index] && innerArray[index].Equals(item); 130 | } 131 | 132 | public T this[int index] 133 | { 134 | get 135 | { 136 | if (arrayAllocation[index] == false) 137 | throw new System.IndexOutOfRangeException(); 138 | return innerArray[index]; 139 | } 140 | 141 | set 142 | { 143 | 144 | if (arrayAllocation[index] == false) 145 | throw new System.IndexOutOfRangeException(); 146 | innerArray[index] = value; 147 | } 148 | 149 | } 150 | 151 | public void Clear() 152 | { 153 | for (int i = 0; i < Capacity; i++) 154 | { 155 | innerArray[i] = default(T); 156 | } 157 | FastClear(); 158 | } 159 | 160 | public void FastClear() 161 | { 162 | arrayAllocation.SetAll(false); 163 | OpenSlots.FastClear(); 164 | PeakCount = 0; 165 | Count = 0; 166 | } 167 | 168 | public void Enumerate(FastList output) 169 | { 170 | output.FastClear(); 171 | for (int i = 0; i < PeakCount; i++) 172 | { 173 | if (arrayAllocation[i]) 174 | { 175 | output.Add(innerArray[i]); 176 | } 177 | } 178 | } 179 | } 180 | 181 | } -------------------------------------------------------------------------------- /FastBucket.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 94ed7dae023f88d48943ad682b3e1315 3 | timeCreated: 1437006712 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /FastDictionary.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace FastCollections 5 | { 6 | /// 7 | /// Experimenting with dictionary. Not yet functional. 8 | /// 9 | public class FastDictionary 10 | { 11 | static int curDepth; 12 | static int bigIndex, smallIndex, normHash, leIndex; 13 | const int CollisionResolver = 1; 14 | const int CollisionDepth = 5; 15 | const int BucketCount = 64; 16 | const int BucketSize = 64; 17 | const int MaxItems = BucketCount * BucketSize; 18 | ulong[] bucketAllocation = new ulong[BucketCount]; 19 | TValue[] bucketValues = new TValue[BucketCount * BucketSize]; 20 | int[] bucketHashes = new int[BucketCount * BucketSize]; 21 | //TKey[] bucketKeys = new TKey[BucketCount * BucketSize]; 22 | 23 | public bool Add (TKey key, TValue value) 24 | { 25 | Prime (); 26 | return _Add (key.GetHashCode (), value); 27 | } 28 | 29 | private bool _Add (int hashCode, TValue item) 30 | { 31 | if (ForceStop) 32 | return false; 33 | GenerateIndexes (hashCode); 34 | if (Shortcuts.GetBit (bucketAllocation[bigIndex],smallIndex)) { 35 | if (bucketHashes [leIndex] == hashCode) { 36 | return false; 37 | } 38 | //Resolve collision 39 | return _Add (hashCode * CollisionResolver, item); 40 | } 41 | Shortcuts.SetBitTrue (ref bucketAllocation [bigIndex], smallIndex); 42 | bucketValues [leIndex] = item; 43 | bucketHashes [leIndex] = hashCode; 44 | return true; 45 | } 46 | 47 | public bool Remove (TKey key) 48 | { 49 | Prime (); 50 | return _Remove (key.GetHashCode ()); 51 | } 52 | 53 | private bool _Remove (int hashCode) 54 | { 55 | if (ForceStop) 56 | return false; 57 | if (ConfirmSlot (hashCode)) { 58 | Shortcuts.SetBitFalse (ref bucketAllocation [bigIndex], smallIndex); 59 | return true; 60 | } 61 | return _Remove (hashCode * CollisionResolver); 62 | } 63 | 64 | public TValue this [TKey key] { 65 | get { 66 | Prime (); 67 | return _GetValue (key.GetHashCode ()); 68 | } 69 | } 70 | 71 | private TValue _GetValue (int hashCode) 72 | { 73 | if (ForceStop) 74 | throw new System.IndexOutOfRangeException (); 75 | if (ConfirmSlot (hashCode)) 76 | { 77 | return bucketValues[leIndex]; 78 | } 79 | return _GetValue (hashCode * CollisionResolver); 80 | } 81 | 82 | public bool TryGetValue (TKey key, out TValue output) 83 | { 84 | Prime (); 85 | return _TryGetValue (key.GetHashCode (), out output); 86 | } 87 | 88 | private bool _TryGetValue (int hashCode, out TValue output) 89 | { 90 | if (ForceStop) { 91 | output = default(TValue); 92 | return false; 93 | } 94 | if (ConfirmSlot (hashCode)) { 95 | output = bucketValues [leIndex]; 96 | return true; 97 | } 98 | return _TryGetValue (hashCode * CollisionResolver, out output); 99 | } 100 | 101 | public bool ContainsKey (TKey key) 102 | { 103 | Prime (); 104 | return _ContainsKey (key.GetHashCode ()); 105 | } 106 | 107 | private bool _ContainsKey (int hashCode) 108 | { 109 | if (ForceStop) 110 | return false; 111 | GenerateIndexes (hashCode); 112 | if (ConfirmSlot (hashCode)) 113 | return true; 114 | return _ContainsKey (hashCode * CollisionResolver); 115 | } 116 | 117 | private static void Prime () 118 | { 119 | curDepth = 0; 120 | } 121 | 122 | private static bool ForceStop { 123 | get{ return (curDepth++ >= CollisionDepth);} 124 | } 125 | 126 | private static void GenerateIndexes (int hashCode) 127 | { 128 | normHash = hashCode % MaxItems; 129 | bigIndex = normHash / BucketCount; 130 | smallIndex = normHash % BucketSize; 131 | leIndex = smallIndex * BucketCount + bigIndex; 132 | } 133 | 134 | private bool ConfirmSlot (int hashCode) 135 | { 136 | GenerateIndexes (hashCode); 137 | if (Shortcuts.GetBit (bucketAllocation[bigIndex],smallIndex)) { 138 | if (bucketHashes [leIndex] == hashCode) { 139 | return true; 140 | } 141 | } 142 | return false; 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /FastDictionary.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8f514f8300cc56643b670b06cb2433f1 3 | timeCreated: 1437189479 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /FastList.cs: -------------------------------------------------------------------------------- 1 | //======================================================================= 2 | // Copyright (c) 2015 John Pan 3 | // Distributed under the MIT License. 4 | // (See accompanying file LICENSE or copy at 5 | // http://opensource.org/licenses/MIT) 6 | //======================================================================= 7 | 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using System; 11 | 12 | namespace FastCollections 13 | { 14 | public class FastList : FastEnumerable, IEnumerable 15 | { 16 | private const int DefaultCapacity = 8; 17 | public T[] innerArray; 18 | public int Count {get; private set;} //Also the index of the next element to be added 19 | public int Capacity = DefaultCapacity; 20 | public bool IsValueType { get; private set;} 21 | public FastList (FastList CopyList) 22 | { 23 | innerArray = (T[])CopyList.innerArray.Clone (); 24 | Count = innerArray.Length; 25 | Capacity = innerArray.Length; 26 | } 27 | 28 | public FastList (T[] StartArray) 29 | { 30 | innerArray = StartArray; 31 | Count = innerArray.Length; 32 | Capacity = innerArray.Length; 33 | } 34 | 35 | public FastList (int StartCapacity) 36 | { 37 | Capacity = StartCapacity; 38 | innerArray = new T[Capacity]; 39 | 40 | Initialize (); 41 | } 42 | public FastList () 43 | { 44 | innerArray = new T[Capacity]; 45 | Initialize (); 46 | } 47 | 48 | private void Initialize () 49 | { 50 | 51 | Count = 0; 52 | this.IsValueType = typeof(T).IsValueType; 53 | } 54 | 55 | public void Add (T item) 56 | { 57 | EnsureCapacity (Count + 1); 58 | innerArray [Count++] = item; 59 | 60 | } 61 | 62 | public void AddRange (FastList items) 63 | { 64 | int arrayLength = items.Count; 65 | EnsureCapacity (Count + arrayLength + 1); 66 | for (int i = 0; i < arrayLength; i++) 67 | { 68 | innerArray[Count++] = items[i]; 69 | } 70 | } 71 | 72 | public void AddRange (T[] items) 73 | { 74 | int arrayLength = items.Length; 75 | EnsureCapacity (Count + arrayLength + 1); 76 | for (int i = 0; i < arrayLength; i++) 77 | { 78 | innerArray[Count++] = items[i]; 79 | } 80 | } 81 | public void AddRange (T[] items, int startIndex, int count) 82 | { 83 | EnsureCapacity (Count + count + 1); 84 | for (int i = 0; i < count; i++) 85 | { 86 | innerArray[Count++] = items[i + startIndex]; 87 | } 88 | } 89 | 90 | public bool Remove (T item) 91 | { 92 | 93 | int index = Array.IndexOf (innerArray, item, 0, Count); 94 | if (index >= 0) { 95 | RemoveAt (index); 96 | return true; 97 | } 98 | return false; 99 | } 100 | 101 | public void RemoveAt (int index) 102 | { 103 | Count--; 104 | innerArray [index] = default(T); 105 | Array.Copy (innerArray, index + 1, innerArray, index, Count - index); 106 | 107 | } 108 | 109 | public T[] ToArray () 110 | { 111 | T[] retArray = new T[Count]; 112 | Array.Copy (innerArray,0,retArray,0,Count); 113 | return retArray; 114 | } 115 | 116 | public bool Contains (T item) 117 | { 118 | return Array.IndexOf (innerArray,item,0,Count) != -1; 119 | } 120 | 121 | public void Reverse () 122 | { 123 | //Array.Reverse (innerArray,0,Count); 124 | int highCount = Count / 2; 125 | int reverseCount = Count - 1; 126 | for (int i = 0; i < highCount; i++) 127 | { 128 | T swapItem = innerArray[i]; 129 | innerArray[i] = innerArray[reverseCount]; 130 | innerArray[reverseCount] = swapItem; 131 | 132 | reverseCount--; 133 | } 134 | } 135 | 136 | public void EnsureCapacity (int min) 137 | { 138 | if (Capacity < min) 139 | { 140 | Capacity *= 2; 141 | if (Capacity < min) { 142 | Capacity = min; 143 | } 144 | Array.Resize (ref innerArray, Capacity); 145 | } 146 | } 147 | 148 | public T this [int index] { 149 | get { 150 | return innerArray [index]; 151 | } 152 | set { 153 | innerArray [index] = value; 154 | } 155 | } 156 | 157 | public void Clear () 158 | { 159 | if (this.IsValueType) { 160 | FastClear(); 161 | } else { 162 | for (int i = 0; i < Capacity; i++) { 163 | innerArray[i] = default(T); 164 | } 165 | } 166 | Count = 0; 167 | } 168 | 169 | /// 170 | /// Marks elements for overwriting. Note: this list will still keep references to objects. 171 | /// 172 | public void FastClear () 173 | { 174 | Count = 0; 175 | } 176 | 177 | public void CopyTo (FastList target) 178 | { 179 | Array.Copy (innerArray,0,target.innerArray,0,Count); 180 | target.Count = Count; 181 | target.Capacity = Capacity; 182 | } 183 | 184 | public T[] TrimmedArray { 185 | get { 186 | T[] ret = new T[Count]; 187 | Array.Copy (innerArray, ret, Count); 188 | return ret; 189 | } 190 | } 191 | 192 | public override string ToString () 193 | { 194 | if (Count <= 0) 195 | return base.ToString (); 196 | string output = string.Empty; 197 | for (int i = 0; i < Count - 1; i++) 198 | output += innerArray [i] + ", "; 199 | 200 | return base.ToString () + ": " + output + innerArray [Count - 1]; 201 | } 202 | 203 | public IEnumerator GetEnumerator () 204 | { 205 | for (int i = 0; i < this.Count; i++) { 206 | yield return this.innerArray[i]; 207 | } 208 | } 209 | 210 | IEnumerator IEnumerable.GetEnumerator () 211 | { 212 | for (int i = 0; i < this.Count; i++) { 213 | yield return this.innerArray[i]; 214 | } 215 | } 216 | 217 | public void Enumerate (FastList output) { 218 | output.FastClear (); 219 | output.AddRange (this); 220 | } 221 | } 222 | 223 | } 224 | -------------------------------------------------------------------------------- /FastList.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: de0c0f02cdd0171469ccfb3826ed0766 3 | timeCreated: 1435878984 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /FastQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FastCollections 4 | { 5 | public class FastQueue { 6 | private T[] innerArray; 7 | private int head; 8 | private int tail; 9 | public int Count { get; private set; } 10 | public int Capacity { get; private set; } 11 | 12 | public FastQueue() 13 | : this(8) {} 14 | 15 | public FastQueue(int capacity) { 16 | Capacity = capacity; 17 | head = 0; 18 | tail = 0; 19 | Count = 0; 20 | innerArray = new T[Capacity]; 21 | } 22 | 23 | public void Add(T item) { 24 | if (tail == head) { 25 | SetCapacity(Count + 1); 26 | } 27 | 28 | innerArray[tail++] = item; 29 | if (tail == Capacity) { 30 | tail = 0; 31 | } 32 | 33 | Count++; 34 | } 35 | 36 | public T Pop() { 37 | T ret = innerArray[head]; 38 | innerArray[head] = default(T); 39 | head++; 40 | if (head == Capacity) { 41 | head = 0; 42 | } 43 | Count--; 44 | return ret; 45 | } 46 | public void Remove () { 47 | innerArray[head] = default(T); 48 | head++; 49 | if (head == Capacity) { 50 | head = 0; 51 | } 52 | Count--; 53 | } 54 | public T Peek () { 55 | return innerArray[head]; 56 | } 57 | public T PeekTail () { 58 | int tailIndex = tail - 1; 59 | if (tailIndex < 0) tailIndex = this.Capacity - 1; 60 | return innerArray[tailIndex]; 61 | } 62 | 63 | public void SetCapacity(int min) { 64 | if (Capacity < min) { 65 | int prevLength = Capacity; 66 | Capacity *= 2; 67 | if (Capacity < min) { 68 | Capacity = min; 69 | } 70 | 71 | var newArray = new T[Capacity]; 72 | if (tail > head) { // If we are not wrapped around... 73 | Array.Copy(innerArray, head, newArray, 0, Count); // ...take from head to head+Count and copy to beginning of new array 74 | } else if (Count > 0) { // Else if we are wrapped around... (tail == head is ambiguous - could be an empty buffer or a full one) 75 | Array.Copy(innerArray, head, newArray, 0, prevLength - head); // ...take head to end and copy to beginning of new array 76 | Array.Copy(innerArray, 0, newArray, prevLength - head, tail); // ...take beginning to tail and copy after previously copied elements 77 | } 78 | 79 | head = 0; 80 | tail = Count; 81 | innerArray = newArray; 82 | } 83 | } 84 | public void FullClear () { 85 | Shortcuts.ClearArray(innerArray); 86 | FastClear(); 87 | } 88 | public void FastClear() { 89 | Count = 0; 90 | tail = 0; 91 | head = 0; 92 | } 93 | 94 | public T[] ToArray() { 95 | var result = new T[Count]; 96 | if (tail > head) { 97 | Array.Copy(innerArray, head, result, 0, Count); 98 | } else if (Count > 0) { 99 | Array.Copy(innerArray, head, result, 0, Capacity - head); 100 | Array.Copy(innerArray, 0, result, Capacity - head, tail); 101 | } 102 | return result; 103 | } 104 | 105 | } 106 | } -------------------------------------------------------------------------------- /FastQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1d827fd52b2bd9744a1abedbdbf0c040 3 | timeCreated: 1439262175 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /FastSorter.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System; 4 | 5 | namespace FastCollections 6 | { 7 | /// 8 | /// List that sorts its elements, adds from the optimal end, and has O(1) popping. 9 | /// 10 | public class FastSorter 11 | { 12 | public delegate int SortCompare (CompareT source, CompareT other); 13 | private const int defaultCapacity = 4; 14 | 15 | public int Count { get; private set; } 16 | 17 | private int Capacity; 18 | private T[] innerArray; 19 | private int _offset; 20 | 21 | private int Offset { 22 | get { 23 | return _offset; 24 | } 25 | set { 26 | _offset = value; 27 | } 28 | } 29 | 30 | public SortCompare Comparer; 31 | public FastSorter () { 32 | _Init (CompareWithIComparable, defaultCapacity); 33 | } 34 | public FastSorter (SortCompare comparer) 35 | { 36 | _Init (comparer, defaultCapacity); 37 | } 38 | 39 | private void _Init (SortCompare comparer, int startCapacity) 40 | { 41 | Comparer = comparer; 42 | Capacity = startCapacity; 43 | innerArray = new T[startCapacity]; 44 | Offset = 0; 45 | } 46 | 47 | public void Add (T item) 48 | { 49 | if (Count > 0) { 50 | int min = 0; 51 | int max = Count; 52 | int split = GetSplit (min, max); 53 | 54 | while (min < max) { 55 | int compare = Comparer (item, innerArray [GetIndex (split)]); 56 | if (compare == 0) { 57 | min = split + 1; 58 | break; 59 | } else if (compare > 0) { 60 | min = split + 1; 61 | } else { 62 | max = split; 63 | } 64 | split = GetSplit (min, max); 65 | } 66 | Insert (item, min); 67 | } else { 68 | Insert (item, 0); 69 | } 70 | Count++; 71 | } 72 | 73 | public T PopMin () 74 | { 75 | int index = GetIndex (0); 76 | T ret = innerArray [index]; 77 | innerArray[index] = default(T); 78 | Offset++; 79 | Count--; 80 | if (Count == 0) 81 | Offset = 0; 82 | return ret; 83 | } 84 | 85 | public T PeekMin () { 86 | return innerArray[GetIndex(0)]; 87 | } 88 | 89 | public T PopMax () 90 | { 91 | int index = GetIndex (--Count); 92 | T ret = innerArray [index]; 93 | innerArray[index] = default(T); 94 | if (Count == 0) 95 | Offset = 0; 96 | return ret; 97 | } 98 | 99 | public T PeekMax () { 100 | return innerArray[GetIndex (Count - 1)]; 101 | } 102 | 103 | public void Clear (bool fast = true) 104 | { 105 | if (fast == false) { 106 | Array.Clear (innerArray, GetIndex(0), Count); 107 | } 108 | Count = 0; 109 | Offset = 0; 110 | 111 | } 112 | 113 | private int GetSplit (int min, int max) 114 | { 115 | return min + ((max - min) / 2); 116 | } 117 | 118 | private int GetIndex (int place) 119 | { 120 | return place + Offset; 121 | } 122 | 123 | private void Insert (T item, int place) 124 | { 125 | int index = GetIndex (place); 126 | bool capped = Count + Offset >= Capacity; 127 | bool forceLeftShift = false; 128 | if (capped) { 129 | if (forceLeftShift = Offset != 0) { 130 | 131 | } else { 132 | CheckCapacity (Count + Offset + 1); 133 | } 134 | } 135 | 136 | if (Count > 0) { 137 | int distanceToHead = Count - place; 138 | int distanceToTail = place + 1; 139 | 140 | if (forceLeftShift || Offset != 0 && (distanceToHead >= distanceToTail)) { 141 | Shift (innerArray, Offset--, index, -1); 142 | } else { 143 | Shift (innerArray, index, Count + Offset, 1); 144 | } 145 | if (Offset == 0 || (!capped && distanceToHead < distanceToTail)) { 146 | if (distanceToHead > 0) { 147 | 148 | } 149 | } else { 150 | 151 | } 152 | } 153 | 154 | innerArray [GetIndex (place)] = item; 155 | } 156 | 157 | private void CheckCapacity (int min) 158 | { 159 | if (Capacity < min) { 160 | Capacity *= 2; 161 | if (Capacity < min) 162 | Capacity = min; 163 | Array.Resize (ref innerArray, Capacity); 164 | } 165 | } 166 | 167 | 168 | private static void Shift (Array array, int min, int max, int shiftAmount) { 169 | if (shiftAmount == 0) return; 170 | Array.Copy (array, min, array, min + shiftAmount, max - min); 171 | } 172 | 173 | #region Default SortCompares 174 | public static SortCompare CompareWithIComparable = 175 | (obj, other) => {return ((IComparable)obj).CompareTo (other);}; 176 | 177 | #endregion 178 | } 179 | } -------------------------------------------------------------------------------- /FastSorter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5ace4253d0df8034f8dd88373dfebbb7 3 | timeCreated: 1444597263 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /FastStack.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System; 3 | namespace FastCollections 4 | { 5 | public class FastStack 6 | { 7 | private const int DefaultCapacity = 8; 8 | public T[] innerArray; 9 | public int Count = 0; 10 | public int Capacity; 11 | 12 | public FastStack (int StartCapacity) 13 | { 14 | Capacity = StartCapacity; 15 | Initialize (); 16 | } 17 | public FastStack () 18 | { 19 | Capacity = DefaultCapacity; 20 | Initialize (); 21 | } 22 | 23 | private void Initialize () 24 | { 25 | #if UNITY_EDITOR 26 | if (Capacity <= 0) 27 | { 28 | UnityEngine.Debug.LogError ("Initializing list with capacity less than or equal to zero isn't supported"); 29 | } 30 | #endif 31 | innerArray = new T[Capacity]; 32 | } 33 | 34 | public void Add (T item) 35 | { 36 | EnsureCapacity (); 37 | innerArray [Count++] = item; 38 | } 39 | 40 | public T Pop () 41 | { 42 | return (innerArray[--Count]); 43 | } 44 | 45 | public T Peek () 46 | { 47 | return innerArray[Count - 1]; 48 | } 49 | 50 | private void EnsureCapacity () 51 | { 52 | EnsureCapacity (Count + 1); 53 | } 54 | public void EnsureCapacity (int min) { 55 | if (Capacity < min) { 56 | Capacity *= 2; 57 | if (Capacity < min) 58 | Capacity = min; 59 | T[] newItems = new T[Capacity]; 60 | Array.Copy (innerArray, 0, newItems, 0, Count); 61 | innerArray = newItems; 62 | } 63 | } 64 | 65 | public T this [int index] { 66 | get { 67 | return innerArray [index]; 68 | } 69 | set { 70 | innerArray [index] = value; 71 | } 72 | } 73 | 74 | public void Clear () 75 | { 76 | innerArray = new T[Capacity]; 77 | } 78 | 79 | /// 80 | /// Marks elements for overwriting. Note: After using FastClear(), this list will still keep any references to objects it previously had. 81 | /// 82 | public void FastClear () 83 | { 84 | Count = 0; 85 | } 86 | 87 | public override string ToString () 88 | { 89 | if (Count <= 0) 90 | return base.ToString (); 91 | string output = string.Empty; 92 | for (int i = 0; i < Count - 1; i++) 93 | output += innerArray [i] + ", "; 94 | 95 | return base.ToString () + ": " + output + innerArray [Count - 1]; 96 | } 97 | 98 | 99 | 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /FastStack.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b6eabc6c5cf9b6e4ba4a01394c9f911c 3 | timeCreated: 1436071412 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 John Pan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c20f491243d77d9478313d28e555196e 3 | timeCreated: 1492830079 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /LSEnumerable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace FastCollections 5 | { 6 | public interface FastEnumerable 7 | { 8 | void Enumerate (FastList output); 9 | } 10 | } -------------------------------------------------------------------------------- /LSEnumerable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e7d91db705d4cbe43a0adc0a30cd9aa9 3 | timeCreated: 1442351745 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FastCollections 2 | Custom collections with optimizations at the cost of safety 3 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 84b3abe4680f31c4ab94e8f2458971bd 3 | timeCreated: 1492830079 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Shortcuts.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using System; 5 | namespace FastCollections { 6 | public static class Shortcuts { 7 | 8 | #region ArrayManipulation 9 | /// 10 | /// Shifts all items in array from index min to max by shiftamount. I.e. the item on index min will be shifted onto index min + shiftamount. 11 | /// 12 | /// 13 | /// 14 | /// 15 | /// 16 | public static void Shift(Array array, int min, int max, int shiftAmount) 17 | { 18 | if (shiftAmount == 0) return; 19 | Array.Copy(array, min, array, min + shiftAmount, max - min); 20 | 21 | } 22 | /// 23 | /// Clears all items in array. 24 | /// 25 | /// 26 | public static void ClearArray(Array array) 27 | { 28 | System.Array.Clear(array, 0, array.Length); 29 | } 30 | #endregion 31 | #region BitMask Manipulation 32 | //ulong mask 33 | /// 34 | /// Sets the value at bitIndex of a 64 bit mask to true 35 | /// 36 | /// 37 | /// 38 | public static void SetBitTrue(ref ulong mask, int bitIndex) 39 | { 40 | mask |= (ulong)1 << bitIndex; 41 | } 42 | /// 43 | /// Sets the value at bitIndex of a 64 bit mask to false 44 | /// 45 | /// 46 | /// 47 | public static void SetBitFalse(ref ulong mask, int bitIndex) 48 | { 49 | mask &= ~((ulong)1 << bitIndex); 50 | } 51 | /// 52 | /// Get the value of the bit at bitIndex 53 | /// 54 | /// 55 | /// 56 | /// 57 | public static bool GetBit(ulong mask, int bitIndex) 58 | { 59 | return (mask & ((ulong)1 << bitIndex)) != 0; 60 | } 61 | 62 | 63 | //uint mask 64 | /// 65 | /// Sets the value at bitIndex of a 32 bit mask to true 66 | /// 67 | /// 68 | /// 69 | public static void SetBitTrue(ref uint mask, int bitIndex) 70 | { 71 | mask |= (uint)1 << bitIndex; 72 | } 73 | /// 74 | /// Sets the value at bitIndex of a 32 bit mask to false 75 | /// 76 | /// 77 | /// 78 | public static void SetBitFalse(ref uint mask, int bitIndex) 79 | { 80 | mask &= ~((uint)1 << bitIndex); 81 | } 82 | /// 83 | /// Get the value of the bit at bitIndex 84 | /// 85 | /// 86 | /// 87 | /// 88 | public static bool GetBit(uint mask, int bitIndex) 89 | { 90 | return (mask & ((uint)1 << bitIndex)) != 0; 91 | } 92 | 93 | #endregion 94 | } 95 | } -------------------------------------------------------------------------------- /Shortcuts.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0feddddc62bd24944a500be807bc9aa0 3 | timeCreated: 1492830429 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Tests.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e4e9ae75b45124b46b24ec64b05c6408 3 | folderAsset: yes 4 | timeCreated: 1450911835 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Tests/Array2DTester.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using Lockstep; 4 | namespace FastCollections.Tests 5 | { 6 | public class Array2DTester : MonoBehaviour 7 | { 8 | Array2D testArray; 9 | int shiftAmount = 1; 10 | // Use this for initialization 11 | void Start() 12 | { 13 | testArray = new Array2D(4, 4); 14 | Refill(); 15 | } 16 | 17 | void Refill() 18 | { 19 | for (int i = 0; i < testArray.Width; i++) 20 | { 21 | for (int j = 0; j < testArray.Height; j++) 22 | { 23 | testArray[i, j] = new Coordinate(i, j); 24 | } 25 | } 26 | } 27 | 28 | void OnGUI() 29 | { 30 | GUILayout.BeginHorizontal(); 31 | for (int i = 0; i < testArray.Width; i++) 32 | { 33 | GUILayout.BeginVertical(); 34 | for (int j = testArray.Height - 1; j >= 0; j--) 35 | { 36 | GUILayout.Label(testArray[i, j].ToString(), GUILayout.Height(50f), GUILayout.Width(50)); 37 | } 38 | GUILayout.EndVertical(); 39 | } 40 | GUILayout.EndHorizontal(); 41 | if (GUILayout.Button("Refill")) 42 | { 43 | Refill(); 44 | } 45 | 46 | int.TryParse(GUILayout.TextField(shiftAmount.ToString()), out shiftAmount); 47 | if (GUILayout.Button("Shift Width")) 48 | { 49 | testArray.Shift(shiftAmount, 0); 50 | } 51 | if (GUILayout.Button("Shift Height")) 52 | { 53 | testArray.Shift(0, shiftAmount); 54 | } 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Tests/Array2DTester.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 53e839bc276f5b549b900ec96499cea3 3 | timeCreated: 1450911903 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | --------------------------------------------------------------------------------