using StgSharp.Graphics.OpenGL; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; namespace StgSharp.Commom.Collections { public interface IPoolable { public int ID { get; } } public class CacheIndexPool : IEnumerable where T : IPoolable { private ConcurrentDictionary _cache; private Func _defaultCreater; public CacheIndexPool(Func defaultCreater) { _cache = new ConcurrentDictionary(); _defaultCreater = defaultCreater; } public T this[int index] { get { if (_cache.TryGetValue(index, out T value)) { return value; } T newValue = _defaultCreater(); _cache.TryAdd(index, newValue); return newValue; } } public IEnumerator GetEnumerator() { return _cache.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public T CreateObject() { T _new = _defaultCreater(); _cache.AddOrUpdate(1, _new,static (_,origin)=>origin); throw new NotImplementedException(); return _new; } } }