|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Open.Disposable |
| 6 | +{ |
| 7 | + public static class ListPool<T> |
| 8 | + { |
| 9 | + /// <summary> |
| 10 | + /// A shared object pool for use with lists. |
| 11 | + /// The list is cleared after being returned. |
| 12 | + /// The max size of the pool is 128 which should suffice for most use cases. |
| 13 | + /// </summary> |
| 14 | + public static readonly ConcurrentQueueObjectPool<List<T>> Shared = Create(); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Creates an object pool for use with lists. |
| 18 | + /// The list is cleared after being returned. |
| 19 | + /// </summary> |
| 20 | + public static ConcurrentQueueObjectPool<List<T>> Create(int capacity = Constants.DEFAULT_CAPACITY) => new(() => new List<T>(), h => |
| 21 | + { |
| 22 | + h.Clear(); |
| 23 | + if (h.Capacity > 16) h.Capacity = 16; |
| 24 | + }, null, capacity); |
| 25 | + } |
| 26 | + |
| 27 | + public static class HashSetPool<T> |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// A shared object pool for use with hash-sets. |
| 31 | + /// The hash-set is cleared after being returned. |
| 32 | + /// The max size of the pool is 128 which should suffice for most use cases. |
| 33 | + /// </summary> |
| 34 | + public static readonly ConcurrentQueueObjectPool<HashSet<T>> Shared = Create(); |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates an object pool for use with hash-sets. |
| 38 | + /// The hash-set is cleared after being returned. |
| 39 | + /// </summary> |
| 40 | + public static ConcurrentQueueObjectPool<HashSet<T>> Create(int capacity = Constants.DEFAULT_CAPACITY) => new(() => new HashSet<T>(), h => h.Clear(), null, capacity); |
| 41 | + } |
| 42 | + |
| 43 | + public static class DictionaryPool<TKey, TValue> |
| 44 | + { |
| 45 | + /// <summary> |
| 46 | + /// A shared object pool for use with dictionaries. |
| 47 | + /// The dictionary is cleared after being returned. |
| 48 | + /// The max size of the pool is 128 which should suffice for most use cases. |
| 49 | + /// </summary> |
| 50 | + public static readonly ConcurrentQueueObjectPool<Dictionary<TKey, TValue>> Shared = Create(); |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Creates an object pooll for use with dictionaries. |
| 54 | + /// The dictionary is cleared after being returned. |
| 55 | + /// </summary> |
| 56 | + public static ConcurrentQueueObjectPool<Dictionary<TKey, TValue>> Create(int capacity = Constants.DEFAULT_CAPACITY) => new(() => new Dictionary<TKey, TValue>(), h => h.Clear(), null, capacity); |
| 57 | + |
| 58 | + } |
| 59 | +} |
0 commit comments