12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Collections.Generic;
- namespace Pathfinding {
-
- public static class PathPool {
- static readonly Dictionary<Type, Stack<Path> > pool = new Dictionary<Type, Stack<Path> >();
- static readonly Dictionary<Type, int> totalCreated = new Dictionary<Type, int>();
-
-
-
-
- public static void Pool (Path path) {
- #if !ASTAR_NO_POOLING
- lock (pool) {
- if (((IPathInternals)path).Pooled) {
- throw new System.ArgumentException("The path is already pooled.");
- }
- Stack<Path> poolStack;
- if (!pool.TryGetValue(path.GetType(), out poolStack)) {
- poolStack = new Stack<Path>();
- pool[path.GetType()] = poolStack;
- }
- ((IPathInternals)path).Pooled = true;
- ((IPathInternals)path).OnEnterPool();
- poolStack.Push(path);
- }
- #endif
- }
-
- public static int GetTotalCreated (Type type) {
- int created;
- if (totalCreated.TryGetValue(type, out created)) {
- return created;
- } else {
- return 0;
- }
- }
-
- public static int GetSize (Type type) {
- Stack<Path> poolStack;
- if (pool.TryGetValue(type, out poolStack)) {
- return poolStack.Count;
- } else {
- return 0;
- }
- }
-
- public static T GetPath<T>() where T : Path, new() {
- #if ASTAR_NO_POOLING
- T result = new T();
- ((IPathInternals)result).Reset();
- return result;
- #else
- lock (pool) {
- T result;
- Stack<Path> poolStack;
- if (pool.TryGetValue(typeof(T), out poolStack) && poolStack.Count > 0) {
-
- result = poolStack.Pop() as T;
- } else {
- result = new T();
-
- if (!totalCreated.ContainsKey(typeof(T))) {
- totalCreated[typeof(T)] = 0;
- }
- totalCreated[typeof(T)]++;
- }
- ((IPathInternals)result).Pooled = false;
- ((IPathInternals)result).Reset();
- return result;
- }
- #endif
- }
- }
- }
|