using System.Collections.Generic; namespace Cryville.Common.Buffers { public abstract class CategorizedPool where TObject : class { protected readonly Dictionary> Buckets = new Dictionary>(); public int RentedCount { get; private set; } public TObject Rent(TCategory name) { var obj = Buckets[name].Rent(); RentedCount++; return obj; } public void Return(TCategory name, TObject obj) { Buckets[name].Return(obj); --RentedCount; } } public class CategorizedPoolAccessor where TObject : class { readonly CategorizedPool _pool; static readonly SimpleObjectPool> _dictPool = new SimpleObjectPool>(1024); Dictionary _rented; public CategorizedPoolAccessor(CategorizedPool pool) { _pool = pool; } public TObject Rent(TCategory name) { var obj = _pool.Rent(name); if (_rented == null) _rented = _dictPool.Rent(); _rented.Add(obj, name); return obj; } public void Return(TObject obj) { _pool.Return(_rented[obj], obj); _rented.Remove(obj); } public void ReturnAll() { if (_rented == null) return; foreach (var obj in _rented) { _pool.Return(obj.Value, obj.Key); } _rented.Clear(); _dictPool.Return(_rented); _rented = null; } } }