diff --git a/Core/Utils/Collections/ExhaustableArray.cs b/Core/Utils/Collections/ExhaustableArray.cs new file mode 100644 index 00000000..7dc7e5b8 --- /dev/null +++ b/Core/Utils/Collections/ExhaustableArray.cs @@ -0,0 +1,71 @@ +using Core.Utils.Cloners; + +namespace Core.Utils.Collections; + +public class ExhaustableArray : IExhaustableArray +{ + private LinkedList? pool; + private RandomUtil _randomUtil; + private ICloner _cloner; + + public ExhaustableArray( + T[]? itemPool, + RandomUtil randomUtil, + ICloner cloner + ) : this(new LinkedList(itemPool ?? []), randomUtil, cloner) + {} + + public ExhaustableArray( + LinkedList? itemPool, + RandomUtil randomUtil, + ICloner cloner + ) + { + _cloner = cloner; + _randomUtil = randomUtil; + pool = cloner.Clone(itemPool ?? []); + } + + public ExhaustableArray( + ICollection? itemPool, + RandomUtil randomUtil, + ICloner cloner + ) : this(new LinkedList(itemPool ?? []), randomUtil, cloner) + {} + + public T? GetRandomValue() + { + if (pool?.Count == 0) + { + return default; + } + + var index = _randomUtil.GetInt(0, pool.Count - 1); + var element = pool.ElementAt(index); + pool.Remove(element); + return _cloner.Clone(element); + } + + public T? GetFirstValue() { + if (pool?.Count == 0) + { + return default; + } + + var element = pool.ElementAt(0); + pool.Remove(element); + return _cloner.Clone(element); + } + + public bool HasValues() + { + return pool?.Count != 0; + } +} + +public interface IExhaustableArray +{ + T? GetRandomValue(); + T? GetFirstValue(); + bool HasValues(); +}