Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. //#define ASTAR_NO_POOLING //@SHOWINEDITOR Disable pooling for some reason. Could be debugging or just for measuring the difference.
  2.  
  3. using System.Collections.Generic;
  4.  
  5. namespace Pathfinding.Util {
  6.     /** Lightweight Stack Pool.
  7.      * Handy class for pooling stacks of type T.
  8.      *
  9.      * Usage:
  10.      * - Claim a new stack using \code Stack<SomeClass> foo = StackPool<SomeClass>.Claim (); \endcode
  11.      * - Use it and do stuff with it
  12.      * - Release it with \code StackPool<SomeClass>.Release (foo); \endcode
  13.      *
  14.      * You do not need to clear the stack before releasing it.
  15.      * After you have released a stack, you should never use it again.
  16.      *
  17.      * \warning This class is not thread safe
  18.      *
  19.      * \since Version 3.2
  20.      * \see Pathfinding.Util.ListPool
  21.      */
  22.     public static class StackPool<T>{
  23.         /** Internal pool */
  24.         static readonly List<Stack<T> > pool;
  25.  
  26.         /** Static constructor */
  27.         static StackPool () {
  28.             pool = new List<Stack<T> >();
  29.         }
  30.  
  31.         /** Claim a stack.
  32.          * Returns a pooled stack if any are in the pool.
  33.          * Otherwise it creates a new one.
  34.          * After usage, this stack should be released using the Release function (though not strictly necessary).
  35.          */
  36.         public static Stack<T> Claim () {
  37.             lock(pool) {
  38.                 if (pool.Count > 0) {
  39.                     Stack<T> ls = pool[pool.Count-1];
  40.                     pool.RemoveAt(pool.Count-1);
  41.                     return ls;
  42.                 }
  43.             }
  44.  
  45.             return new Stack<T>();
  46.         }
  47.  
  48.         /** Makes sure the pool contains at least \a count pooled items.
  49.          * This is good if you want to do all allocations at start.
  50.          */
  51.         public static void Warmup (int count) {
  52.             var tmp = new Stack<T>[count];
  53.  
  54.             for (int i = 0; i < count; i++) tmp[i] = Claim();
  55.             for (int i = 0; i < count; i++) Release(tmp[i]);
  56.         }
  57.  
  58.         /** Releases a stack.
  59.          * After the stack has been released it should not be used anymore.
  60.          * Releasing a stack twice will cause an error.
  61.          */
  62.         public static void Release (Stack<T> stack) {
  63.             stack.Clear();
  64.             lock(pool) {
  65.                 for (int i = 0; i < pool.Count; i++)
  66.                     if (pool[i] == stack) UnityEngine.Debug.LogError("The Stack is released even though it is inside the pool");
  67.  
  68.                 pool.Add(stack);
  69.             }
  70.         }
  71.  
  72.         /** Clears all pooled stacks of this type.
  73.          * This is an O(n) operation, where n is the number of pooled stacks
  74.          */
  75.         public static void Clear () {
  76.             lock(pool) {
  77.                 pool.Clear();
  78.             }
  79.         }
  80.  
  81.         /** Number of stacks of this type in the pool */
  82.         public static int GetSize () {
  83.             return pool.Count;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement