C# 判断某个数/某组数,是否在一定的(某组)范围内,适配多种类型

    protected bool CheckBase<T>(List<T> datas, int index, int count, int threshold, Func<bool,bool> action, int offset = 10) where T : struct, IComparable<T>
    {
        var range = datas.GetRange(index, count);
        var res = range.All(x => x.CompareTo((T)(dynamic)(threshold + offset)) <= 0 && x.CompareTo((T)(dynamic)(threshold - offset)) >= 0);
        return action(res);
    }

    protected bool CheckBase<T>(List<T> datas,  int index, int count, List<int> thresholds, Func<bool, bool> action, int offset = 10) where T : struct, IComparable<T>
    {
        var range = datas.GetRange(index, count);
        var res = range.Zip(thresholds, (data, threshold) =>
            data.CompareTo((T)(dynamic)(threshold + offset)) <= 0 &&
            data.CompareTo((T)(dynamic)(threshold - offset)) >= 0
        ).All(x => x);
        return action(res);
    }

定义了两个泛型方法 CheckBase,它们用于对列表中的数据进行校验。

第一个方法 CheckBase(List datas, int index, int count, int threshold, Func action, int offset = 10) 的功能是检查从给定索引 index 开始的指定数量 count 的数据是否在指定的阈值范围内。阈值由 threshold 指定,同时还有一个可选参数 offset 用于控制阈值的偏移量,默认值为 10。该方法使用泛型约束 where T : struct, IComparable 来确保传入的数据类型是值类型并实现了 IComparable 接口,以进行比较操作。

该方法首先通过 datas.GetRange(index, count) 从传入的 datas 列表中获取指定索引和数量的子列表 range。然后使用 LINQ 方法 All() 遍历 range 中的每个元素 x,检查其是否在阈值范围内,即是否满足 (x.CompareTo((T)(dynamic)(threshold + offset)) <= 0 && x.CompareTo((T)(dynamic)(threshold - offset)) >= 0) 的条件。最后,将结果传递给传入的 action 委托进行处理,并返回结果。

第二个方法 CheckBase(List datas, int index, int count, List thresholds, Func action, int offset = 10) 的功能与第一个方法类似,只是它使用了一个列表 thresholds 来存储多个阈值,而不是单个阈值。方法中的逻辑基本相同,不同之处在于使用 range.Zip(thresholds, ...) 将两个列表进行逐对组合,然后对每对数据进行判断是否在对应的阈值范围内。其余部分与第一个方法相似。

这两个方法可以用于校验给定数据列表中的数据是否满足指定的阈值要求,并通过提供的委托进行结果处理。
示例

// 示例 1 - 使用 CheckBase(List datas, int index, int count, int threshold, Func action, int offset = 10) 方法
List<int> dataList = new List<int> { 5, 7, 9, 11, 13, 15 };
int startIndex = 0;
int elementsCount = 4;
int threshold = 10;
int offset = 2;

bool result1 = CheckBase(dataList, startIndex, elementsCount, threshold, (res) =>
{
    Console.WriteLine($"All elements in the range [{startIndex}-{startIndex + elementsCount - 1}] are within the threshold +/-{offset}: {res}");
    return res;
}, offset);
// 输出: All elements in the range [0-3] are within the threshold +/-2: False

// 示例 2 - 使用 CheckBase(List datas, int index, int count, List thresholds, Func action, int offset = 10) 方法
List<double> dataValues = new List<double> { 1.2, 1.4, 1.5, 1.8, 2.1 };
int startIndex2 = 1;
int elementsCount2 = 3;
List<int> thresholds = new List<int> { 1, 2, 3 };
int offset2 = 0;

bool result2 = CheckBase(dataValues, startIndex2, elementsCount2, thresholds, (res) =>
{
    Console.WriteLine($"All elements in the range [{startIndex2}-{startIndex2 + elementsCount2 - 1}] are within the thresholds +/-{offset2}: {res}");
    return res;
}, offset2);
// 输出: All elements in the range [1-3] are within the thresholds +/-0: False

你可能感兴趣的:(C#,c#,开发语言)