123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #if !UNITY_EDITOR
- #define ASTAR_OPTIMIZE_POOLING
- #endif
- using System;
- using System.Collections.Generic;
- namespace Pathfinding.Util {
- public interface IAstarPooledObject {
- void OnEnterPool();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static class ObjectPool<T> where T : class, IAstarPooledObject, new(){
- public static T Claim () {
- return ObjectPoolSimple<T>.Claim();
- }
- public static void Release (ref T obj) {
- obj.OnEnterPool();
- ObjectPoolSimple<T>.Release(ref obj);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static class ObjectPoolSimple<T> where T : class, new(){
-
- static List<T> pool = new List<T>();
- #if !ASTAR_NO_POOLING
- static readonly HashSet<T> inPool = new HashSet<T>();
- #endif
-
-
-
-
-
-
- public static T Claim () {
- #if ASTAR_NO_POOLING
- return new T();
- #else
- lock (pool) {
- if (pool.Count > 0) {
- T ls = pool[pool.Count-1];
- pool.RemoveAt(pool.Count-1);
- inPool.Remove(ls);
- return ls;
- } else {
- return new T();
- }
- }
- #endif
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void Release (ref T obj) {
- #if !ASTAR_NO_POOLING
- lock (pool) {
- #if !ASTAR_OPTIMIZE_POOLING
- if (!inPool.Add(obj)) {
- throw new InvalidOperationException("You are trying to pool an object twice. Please make sure that you only pool it once.");
- }
- #endif
- pool.Add(obj);
- }
- #endif
- obj = null;
- }
-
-
-
-
- public static void Clear () {
- lock (pool) {
- #if !ASTAR_OPTIMIZE_POOLING && !ASTAR_NO_POOLING
- inPool.Clear();
- #endif
- pool.Clear();
- }
- }
-
- public static int GetSize () {
- return pool.Count;
- }
- }
- }
|