示例#1
0
文件: PoolArray.cs 项目: myth326/ecs
        /// <summary>
        /// Returns an array with at least the specified length.
        /// Warning: Returned arrays may contain arbitrary data.
        /// You cannot rely on it being zeroed out.
        /// </summary>
        internal static T[] Claim(int minimumLength)
        {
            //return new T[minimumLength];

            if (minimumLength <= 0)
            {
                return(PoolArray <T> .empty);              //PoolArray<T>.ClaimWithExactLength(0);
            }

            var bucketIndex = 0;

            while (1 << bucketIndex < minimumLength && bucketIndex < 30)
            {
                ++bucketIndex;
            }

            if (bucketIndex == 30)
            {
                throw new System.ArgumentException("Too high minimum length");
            }

            if (PoolArray <T> .pool[0] == null)
            {
                PoolArray <T> .Initialize();
            }

            if (PoolArray <T> .pool[bucketIndex].TryPop(out var result) == true)
            {
                return(result);
            }

            /*lock (PoolArray<T>.pool) {
             *
             *      if (PoolArray<T>.pool[bucketIndex] == null) {
             *              PoolArray<T>.pool[bucketIndex] = new System.Collections.Generic.Stack<T[]>();
             *      }
             *
             *      if (PoolArray<T>.pool[bucketIndex].Count > 0) {
             *              var array= PoolArray<T>.pool[bucketIndex].Pop();
             *              outArrays.Add(array);
             *              return array;
             *      }
             *
             * }
             *
             * var arr = new T[1 << bucketIndex];
             * outArrays.Add(arr);
             * return arr;*/
            return(new T[1 << bucketIndex]);
        }
示例#2
0
        internal static T[] Claim(int minimumLength)
        {
            //return new T[minimumLength];

            if (minimumLength <= 0)
            {
                return(PoolArray <T> .empty);              //PoolArray<T>.ClaimWithExactLength(0);
            }

            var bucketIndex = 0;

            while (1 << bucketIndex < minimumLength && bucketIndex < 30)
            {
                ++bucketIndex;
            }

            if (bucketIndex == 30)
            {
                throw new System.ArgumentException("Too high minimum length");
            }

            if (PoolArray <T> .pool[0] == null)
            {
                PoolArray <T> .Initialize();
            }

                        #if MULTITHREAD_SUPPORT
            if (PoolArray <T> .pool[bucketIndex].TryPop(out var result) == true)
            {
                return(result);
            }
                        #else
            var pool = PoolArray <T> .pool[bucketIndex];
            if (pool.Count > 0)
            {
                var arrPooled = pool.Pop();
                                #if UNITY_EDITOR
                if (PoolArray <T> .outArrays.Contains(arrPooled) == true)
                {
                    UnityEngine.Debug.LogError("You are trying to pool array that has been already in pool");
                }
                PoolArray <T> .outArrays.Add(arrPooled);
                                #endif

                //UnityEngine.Debug.Log("Spawn array: " + arrPooled + " :: " + arrPooled.GetHashCode());
                return(arrPooled);
            }
                        #endif

            /*lock (PoolArray<T>.pool) {
             *
             *      if (PoolArray<T>.pool[bucketIndex] == null) {
             *              PoolArray<T>.pool[bucketIndex] = new System.Collections.Generic.Stack<T[]>();
             *      }
             *
             *      if (PoolArray<T>.pool[bucketIndex].Count > 0) {
             *              var array= PoolArray<T>.pool[bucketIndex].Pop();
             *              outArrays.Add(array);
             *              return array;
             *      }
             *
             * }
             *
             * var arr = new T[1 << bucketIndex];
             * outArrays.Add(arr);
             * return arr;*/
            var arr = new T[1 << bucketIndex];
                        #if UNITY_EDITOR
            PoolArray <T> .outArrays.Add(arr);
                        #endif
            //UnityEngine.Debug.Log("Spawn new array: " + arr + " :: " + arr.GetHashCode());
            return(arr);
        }