/********************************************************************
	created:	2018/05/31
	created:	31:5:2018   23:10
	filename: 	C:\X2M\x2m\mainline\dev\client\Assets\Script\X2Interface\Core\Pool\ContainerPool.cs
	file path:	C:\X2M\x2m\mainline\dev\client\Assets\Script\X2Interface\Core\Pool
	file base:	ContainerPool
	file ext:	cs
	author:		zhang qiang
	
	purpose:	containter pool
*********************************************************************/


namespace Giant.Common
{
    using System;
    using System.Collections.Generic;

    public static class ListPool<T>
    {
        // Object pool to avoid allocations.
        private static readonly ObjectPool<List<T>> s_pool = new ObjectPool< List<T> >();
         
        public static List<T> Get()
        {
            return s_pool.Get();    
        }

        public static void Release(List<T> toRelease)
        {
            toRelease.Clear();
            s_pool.Release(toRelease);
        }
    }

    public static class DictionaryPool<Key, Value>
    {
        // Object pool to avoid allocations.
        private static readonly ObjectPool<Dictionary<Key, Value>> s_pool = new ObjectPool< Dictionary<Key, Value> >();

        public static Dictionary<Key, Value> Get()
        {
            return s_pool.Get();
        }

        public static void Release(Dictionary<Key, Value> toRelease)
        {
            toRelease.Clear();
            s_pool.Release(toRelease);
        }
    }

    public static class StackPool<T>
    {
        // Object pool to avoid allocations.
        private static readonly ObjectPool< Stack<T> > s_pool = new ObjectPool< Stack<T> >();

        public static Stack<T> Get()
        {
            return s_pool.Get();
        }

        public static void Release(Stack<T> toRelease)
        {
            toRelease.Clear();
            s_pool.Release(toRelease);
        }
    }

    public static class ArrayPool<T> where T : new()
    {
        // Object pool to avoid allocations.
        static private Dictionary<int, Stack<Object>> s_arrayContainer = new Dictionary<int, Stack<object>>();
        public static T[] Get(int arrayCount)
        {
            T[] element;
            Stack<object> arrayStack = null;
            if(!s_arrayContainer.TryGetValue(arrayCount, out arrayStack))
            {
                arrayStack = new Stack<object>(10); 
                s_arrayContainer[arrayCount] =  arrayStack;
                element = new T[arrayCount];     
            }
            else
            {
                if(arrayStack.Count == 0)
                    element = new T[arrayCount];
                else
                    element = arrayStack.Pop() as T[];
            }
            return element;
        }

        public static void Release(T[] toRelease, int count)
        {
            Stack<object> arrayStack = null;
            if(s_arrayContainer.TryGetValue(count, out arrayStack))
            {
                arrayStack.Push(toRelease);
            }
            else
            {
                //LogHelper.LogWarning("array pool size error");
            }
                
        }
    }
}