123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #if !UNITY_EDITOR
- #define ASTAR_OPTIMIZE_POOLING
- #endif
- using System;
- using System.Collections.Generic;
- namespace Pathfinding.Util {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static class ArrayPool<T> {
- #if !ASTAR_NO_POOLING
-
-
-
-
- const int MaximumExactArrayLength = 256;
-
-
-
-
- static readonly Stack<T[]>[] pool = new Stack<T[]>[31];
- static readonly Stack<T[]>[] exactPool = new Stack<T[]>[MaximumExactArrayLength+1];
- #if !ASTAR_OPTIMIZE_POOLING
- static readonly HashSet<T[]> inPool = new HashSet<T[]>();
- #endif
- #endif
-
-
-
-
-
-
-
- public static T[] Claim (int minimumLength) {
- if (minimumLength <= 0) {
- return ClaimWithExactLength(0);
- }
- int bucketIndex = 0;
- while ((1 << bucketIndex) < minimumLength && bucketIndex < 30) {
- bucketIndex++;
- }
- if (bucketIndex == 30)
- throw new System.ArgumentException("Too high minimum length");
- #if !ASTAR_NO_POOLING
- lock (pool) {
- if (pool[bucketIndex] == null) {
- pool[bucketIndex] = new Stack<T[]>();
- }
- if (pool[bucketIndex].Count > 0) {
- var array = pool[bucketIndex].Pop();
- #if !ASTAR_OPTIMIZE_POOLING
- inPool.Remove(array);
- #endif
- return array;
- }
- }
- #endif
- return new T[1 << bucketIndex];
- }
-
-
-
-
-
-
-
-
-
-
- public static T[] ClaimWithExactLength (int length) {
- #if !ASTAR_NO_POOLING
- bool isPowerOfTwo = length != 0 && (length & (length - 1)) == 0;
- if (isPowerOfTwo) {
-
- return Claim(length);
- }
- if (length <= MaximumExactArrayLength) {
- lock (pool) {
- Stack<T[]> stack = exactPool[length];
- if (stack != null && stack.Count > 0) {
- var array = stack.Pop();
- #if !ASTAR_OPTIMIZE_POOLING
- inPool.Remove(array);
- #endif
- return array;
- }
- }
- }
- #endif
- return new T[length];
- }
-
-
-
-
-
- public static void Release (ref T[] array, bool allowNonPowerOfTwo = false) {
- if (array == null) return;
- if (array.GetType() != typeof(T[])) {
- throw new System.ArgumentException("Expected array type " + typeof(T[]).Name + " but found " + array.GetType().Name + "\nAre you using the correct generic class?\n");
- }
- #if !ASTAR_NO_POOLING
- bool isPowerOfTwo = array.Length != 0 && (array.Length & (array.Length - 1)) == 0;
- if (!isPowerOfTwo && !allowNonPowerOfTwo && array.Length != 0) throw new System.ArgumentException("Length is not a power of 2");
- lock (pool) {
- #if !ASTAR_OPTIMIZE_POOLING
- if (!inPool.Add(array)) {
- throw new InvalidOperationException("You are trying to pool an array twice. Please make sure that you only pool it once.");
- }
- #endif
- if (isPowerOfTwo) {
- int bucketIndex = 0;
- while ((1 << bucketIndex) < array.Length && bucketIndex < 30) {
- bucketIndex++;
- }
- if (pool[bucketIndex] == null) {
- pool[bucketIndex] = new Stack<T[]>();
- }
- pool[bucketIndex].Push(array);
- } else if (array.Length <= MaximumExactArrayLength) {
- Stack<T[]> stack = exactPool[array.Length];
- if (stack == null) stack = exactPool[array.Length] = new Stack<T[]>();
- stack.Push(array);
- }
- }
- #endif
- array = null;
- }
- }
-
- public static class ListExtensions {
-
-
-
-
-
-
- public static T[] ToArrayFromPool<T>(this List<T> list) {
- var arr = ArrayPool<T>.ClaimWithExactLength(list.Count);
- for (int i = 0; i < arr.Length; i++) {
- arr[i] = list[i];
- }
- return arr;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static void ClearFast<T>(this List<T> list) {
- if (list.Count*2 < list.Capacity) {
- list.RemoveRange(0, list.Count);
- } else {
- list.Clear();
- }
- }
- }
- }
|