| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System.Text;
- using System.Collections.Generic;
- namespace WXB
- {
- internal class PoolData<T> where T : new()
- {
- static public List<T> bufs = new List<T>();
-
- static public T Get()
- {
- if (bufs.Count == 0)
- {
- return new T();
- }
- T t = bufs[bufs.Count - 1];
- bufs.RemoveAt(bufs.Count - 1);
- return t;
- }
- static public void Free(T t)
- {
- bufs.Add(t);
- }
- static public void FreeList(List<T> list, System.Action<T> fun)
- {
- for (int i = 0; i < list.Count; ++i)
- fun(list[i]);
- bufs.AddRange(list);
- list.Clear();
- }
- }
- internal struct PD<T> : System.IDisposable where T : new()
- {
- public PD(System.Action<T> free)
- {
- value = PoolData<T>.Get();
- this.free = free;
- }
- public T value;
- private System.Action<T> free;
- public void Dispose()
- {
- free(value);
- PoolData<T>.Free(value);
- }
- }
- interface IFactory
- {
- object create();
- }
- internal class Factory<T> : IFactory where T : new()
- {
- public Factory(System.Action<T> f)
- {
- free = f;
- }
- public System.Action<T> free;
- public object create()
- {
- return new PD<T>(free);
- }
- }
- internal static class Pool
- {
- static Factory<StringBuilder> sb_factory = null;
- public static PD<StringBuilder> GetSB()
- {
- if (sb_factory == null)
- {
- sb_factory = new Factory<StringBuilder>((StringBuilder sb) => { sb.Length = 0; });
- }
- return (PD<StringBuilder>)sb_factory.create();
- }
- }
- }
|