This commit is contained in:
SoulliesOfficial
2025-10-24 09:11:22 -04:00
parent 61a397dd4c
commit 76157e3cb1
329 changed files with 8609 additions and 4549 deletions

View File

@@ -123,18 +123,4 @@ namespace SLSFramework.General
}
}
}
/// <summary>
/// 实现该接口的类可以根据优先级进行比较和排序。
/// 数字越大优先级越高。
/// </summary>
public interface IPrioritized : IComparable<IPrioritized>
{
int Priority { get; }
int IComparable<IPrioritized>.CompareTo(IPrioritized other)
{
return other.Priority.CompareTo(Priority);
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
namespace SLSFramework.General
{
/// <summary>
/// 实现该接口的类可以根据优先级进行比较和排序。
/// 数字越大优先级越高。
/// </summary>
public interface IPrioritized : IComparable<IPrioritized>
{
int Priority { get; }
int IComparable<IPrioritized>.CompareTo(IPrioritized other)
{
return other.Priority.CompareTo(Priority);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 84b1c40e60420ad47b31aef43bc98901

View File

@@ -6,7 +6,7 @@ using Random = UnityEngine.Random;
namespace SLSFramework.General
{
public static class ListExtension
public static partial class ListExtension
{
/// <summary>
/// 对列表中的每个元素执行指定的操作
@@ -116,4 +116,44 @@ namespace SLSFramework.General
return filters.All(filter => filter(item));
}
}
public static partial class ListExtension
{
/// <summary>
/// 根据优先级将新项插入到已排序的列表中,保持列表按优先级从高到低排序(二分查找)。
/// 数字越大优先级越高。
/// </summary>
public static void AddByPriority<T>(this List<T> list, T newItem) where T : IPrioritized
{
// 优化:检查是否可以直接添加到末尾
if (list.Count == 0 || newItem.Priority <= list[list.Count - 1].Priority)
{
list.Add(newItem);
return;
}
int low = 0;
int high = list.Count;
while (low < high)
{
int mid = low + (high - low) / 2;
// 规则:数字越大,优先级越高,越靠前
// 如果中间项的优先级 >= 新项的优先级,
// 说明新项应该在它后面
if (list[mid].Priority >= newItem.Priority)
{
low = mid + 1;
}
else
{
// 中间项的优先级 < 新项,说明新项应该在它前面(或就是这个位置)
high = mid;
}
}
list.Insert(low, newItem);
}
}
}