Files
懒得勤快 8e10f5f1bd 精简代码
2023-12-08 23:49:54 +08:00

54 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
namespace Masuit.Tools.RandomSelector;
/// <summary>
/// 多选器
/// </summary>
/// <typeparam name="T"></typeparam>
internal class MultipleSelector<T> : SelectorBase<T>
{
internal MultipleSelector(WeightedSelector<T> weightedSelector) : base(weightedSelector)
{
}
internal IEnumerable<T> Select(int count)
{
Validate(ref count);
var items = new List<WeightedItem<T>>(WeightedSelector.Items);
int result = 0;
do
{
var item = WeightedSelector.Option.AllowDuplicate ? BinarySelect(items) : LinearSelect(items);
yield return item.Value;
result++;
if (!WeightedSelector.Option.AllowDuplicate)
{
items.Remove(item);
}
} while (result < count);
}
private void Validate(ref int count)
{
if (count <= 0)
{
throw new InvalidOperationException("筛选个数必须大于0");
}
var items = WeightedSelector.Items;
if (items.Count == 0)
{
count = 0;
return;
}
if (!WeightedSelector.Option.AllowDuplicate && items.Count < count)
{
count = items.Count;
}
}
}