Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
6.9 KiB

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class FastList<T> {
  6. /// <summary>
  7. /// Comparison function should return -1 if left is less than right, 1 if left is greater than right, and 0 if they match.
  8. /// </summary>
  9. public delegate int CompareFunc(T left, T right);
  10. public T[] array = null;
  11. public int size = 0;
  12. public FastList () {
  13. }
  14. public FastList(int size) {
  15. if (size > 0) {
  16. this.size = 0;
  17. array = new T[size];
  18. }
  19. else {
  20. this.size = 0;
  21. }
  22. }
  23. public int Count {
  24. get { return size;}
  25. set { }
  26. }
  27. public T this[int i] {
  28. get { return array[i];}
  29. set { array[i] = value;}
  30. }
  31. //Add item to end of list.
  32. public void Add(T item) {
  33. if (array == null || size == array.Length) {
  34. Allocate();
  35. }
  36. array[size] = item;
  37. size++;
  38. }
  39. //Add item to end of list if it is unique.
  40. public void AddUnique( T item ) {
  41. if ( array == null || size == array.Length ) {
  42. Allocate();
  43. }
  44. if ( !Contains( item ) ) {
  45. array[size] = item;
  46. size++;
  47. }
  48. }
  49. //Add items to the end of the list
  50. public void AddRange( IEnumerable<T> items ) {
  51. foreach ( T item in items ) {
  52. Add( item );
  53. }
  54. }
  55. //Insert item at specified index
  56. public void Insert(int index, T item) {
  57. if (array == null || size == array.Length) {
  58. Allocate();
  59. }
  60. if (index < size) {
  61. //move things back 1
  62. for (int i = size; i > index; i--) {
  63. array[i] = array[i-1];
  64. }
  65. array[index] = item;
  66. size++;
  67. }
  68. else Add(item);
  69. }
  70. //Removes specified item and keeps everything else in order
  71. public bool Remove(T item) {
  72. if (array != null) {
  73. for (int i = 0; i < size; i++) {
  74. if (item.Equals(array[i])) { //found it, push everything up
  75. size--;
  76. for (int j = i; j < size; j++) {
  77. array[j] = array[j+1];
  78. }
  79. array[size] = default(T);
  80. return true;
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. //Removes item at specified index while keeping everything else in order
  87. //O(n)
  88. public void RemoveAt(int index) {
  89. if (array != null && size > 0 && index < size) {
  90. size--;
  91. for (int i = index; i < size; i++) {
  92. array[i] = array[i+1];
  93. }
  94. array[size] = default(T);
  95. }
  96. }
  97. //Removes the specified item from the list and replaces with last item. Return true if removed, false if not found.
  98. public bool RemoveFast(T item) {
  99. if (array != null) {
  100. for (int i = 0; i < size; i++) {
  101. if ( item.Equals( array[i] )) { //found
  102. //Move last item here
  103. if (i < (size - 1)) {
  104. T lastItem = array[size-1];
  105. array[size-1] = default(T);
  106. array[i] = lastItem;
  107. } else {
  108. array[i] = default(T);
  109. }
  110. size--;
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. //Removes item at specified index and replace with last item.
  118. public void RemoveAtFast(int index) {
  119. if (array != null && index < size && index >= 0) {
  120. //last element
  121. if (index == size - 1) {
  122. array[index] = default(T);
  123. }
  124. else {
  125. T lastItem = array[size - 1];
  126. array[index] = lastItem;
  127. array[size - 1] = default(T);
  128. }
  129. size--;
  130. }
  131. }
  132. //Return whether an item is contained within the list
  133. //O(n)
  134. public bool Contains(T item) {
  135. if (array == null || size <= 0 ) return false;
  136. for (int i = 0; i < size; i++) {
  137. if (array[i].Equals(item)) { return true;}
  138. }
  139. return false;
  140. }
  141. //Returns index of specified item, or -1 if not found.
  142. //O(n)
  143. public int IndexOf(T item) {
  144. if (size <= 0 || array == null) { return -1;}
  145. for (int i = 0; i < size; i++) {
  146. if (item.Equals(array[i])) { return i;}
  147. }
  148. return -1;
  149. }
  150. public T Pop() {
  151. if (array != null && size > 0) {
  152. T lastItem = array[size-1];
  153. array[size-1] = default(T);
  154. size--;
  155. return lastItem;
  156. }
  157. return default(T);
  158. }
  159. public T[] ToArray() {
  160. Trim();
  161. return array;
  162. }
  163. public void Sort (CompareFunc comparer) {
  164. int start = 0;
  165. int end = size - 1;
  166. bool changed = true;
  167. while (changed) {
  168. changed = false;
  169. for (int i = start; i < end; i++) {
  170. if (comparer(array[i], array[i + 1]) > 0) {
  171. T temp = array[i];
  172. array[i] = array[i+1];
  173. array[i+1] = temp;
  174. changed = true;
  175. }
  176. else if (!changed) {
  177. start = (i==0) ? 0 : i-1;
  178. }
  179. }
  180. }
  181. }
  182. public void InsertionSort(CompareFunc comparer) {
  183. for (int i = 1; i < size; i++) {
  184. T curr = array[i];
  185. int j = i;
  186. while (j > 0 && comparer(array[j - 1], curr) > 0) {
  187. array[j] = array[j-1];
  188. j--;
  189. }
  190. array[j] = curr;
  191. }
  192. }
  193. public IEnumerator<T> GetEnumerator() {
  194. if (array != null) {
  195. for (int i = 0; i < size; i++) {
  196. yield return array[i];
  197. }
  198. }
  199. }
  200. public T Find(Predicate<T> match) {
  201. if (match != null) {
  202. if (array != null) {
  203. for (int i = 0; i < size; i++) {
  204. if (match(array[i])) { return array[i];}
  205. }
  206. }
  207. }
  208. return default(T);
  209. }
  210. //Allocate more space to internal array.
  211. void Allocate() {
  212. T[] newArray;
  213. if (array == null) {
  214. newArray = new T[32];
  215. }
  216. else {
  217. newArray = new T[Mathf.Max(array.Length << 1, 32)];
  218. }
  219. if (array != null && size > 0) {
  220. array.CopyTo(newArray, 0);
  221. }
  222. array = newArray;
  223. }
  224. void Trim() {
  225. if (size > 0) {
  226. T[] newArray = new T[size];
  227. for (int i = 0; i < size; i++) {
  228. newArray[i] = array[i];
  229. }
  230. array = newArray;
  231. }
  232. else {
  233. array = null;
  234. }
  235. }
  236. //Set size to 0, does not delete array from memory
  237. public void Clear() {
  238. size = 0;
  239. }
  240. //Delete array from memory
  241. public void Release() {
  242. Clear();
  243. array = null;
  244. }
  245. }