/
TON618OFF
/
Unity-Project-RTU
Обзор
Документация
Войти
/
TON618OFF
/
Unity-Project-RTU
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Library/PackageCache/com.unity.visualscripting@1.9.4/Runtime/VisualScripting.Core/Pooling/HashSetPool.cs
64 строки
2 KB
TON618OFF
release
03 окт 2024, 22:20
03 окт 2024, 22:20
7c70e9f
Код
Авторство
О чём код?
using System; using System.Collections.Generic; namespace Unity.VisualScripting { public static class HashSetPool<T> { private static readonly object @lock = new object(); private static readonly Stack<HashSet<T>> free = new Stack<HashSet<T>>(); private static readonly HashSet<HashSet<T>> busy = new HashSet<HashSet<T>>(); public static HashSet<T> New() { lock (@lock) { if (free.Count == 0) { free.Push(new HashSet<T>()); } var hashSet = free.Pop(); busy.Add(hashSet); return hashSet; } } public static void Free(HashSet<T> hashSet) { lock (@lock) { if (!busy.Remove(hashSet)) { throw new ArgumentException("The hash set to free is not in use by the pool.", nameof(hashSet)); } hashSet.Clear(); free.Push(hashSet); } } } public static class XHashSetPool { public static HashSet<T> ToHashSetPooled<T>(this IEnumerable<T> source) { var hashSet = HashSetPool<T>.New(); foreach (var item in source) { hashSet.Add(item); } return hashSet; } public static void Free<T>(this HashSet<T> hashSet) { HashSetPool<T>.Free(hashSet); } } }