C# 列表IList, 集合ICollection, 可迭代容器IEnumerable 扩展

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

using UnityEngine;

/// 迭代器扩展方法
[DebuggerStepThrough]
public static class EnumerableExtension
{
    ///  遍历操作 
    public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
    {
        foreach (T ele in values) 
            action(ele);
    }
    ///  遍历操作 
    public static void ForEach(this IEnumerable values, Action<object> action)
    {
        foreach (object ele in values)
            action(ele);
    }
    ///  计数
    public static int CountOf<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        int ret = 0;
        foreach (T ele in values)
            ret += match(ele) ? 1 : 0;
        return ret;
    }
    ///  计数
    public static int CountOf(this IEnumerable values, Predicate<object> match)
    {
        int ret = 0;
        foreach (object ele in values)
            ret += match(ele) ? 1 : 0;
        return ret;
    }

    ///  查找
    public static T Find<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        foreach (var ele in values)
        {
            if (match(ele)) return ele;
        }
        return default;
    }
    ///  查找
    public static object Find(this IEnumerable values, Predicate<object> match)
    {
        foreach (object ele in values)
        {
            if (match(ele)) return ele;
        }
        return default;
    }

    ///  查找
    public static T Find<T>(this IEnumerable<T> values, int index)
    {
        foreach(var ele in values)
        {
            if (index-- == 0)
                return ele;
        }
        return default;
    }

    ///  查找下标
    public static int FindIndex<T>(this IEnumerable<T> values, T data)
    {
        int i = 0;
        foreach (var ele in values)
        {
            if (ele.Equals(data))
                return i;
            ++i;
        }
        return -1;
    }
    ///  查找下标
    public static int FindIndex<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        int i = 0;
        foreach (T ele in values)
        {
            if (match(ele))
                return i;
            ++i;
        }
        return -1;
    }
    ///  查找下标
    public static int FindIndex(this IEnumerable values, Predicate<object> match)
    {
        int i = 0;
        foreach (object ele in values)
        {
            if (match(ele))
                return i;
            ++i;
        }
        return -1;
    }
    /// 查找全部
    public static List<T> FindAll<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        List<T> ret = new List<T>();
        foreach (T ele in values)
            if (match(ele))
                ret.Add(ele);
        return ret;
    }

    /// 是否存在
    public static bool Exist<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        return values.FindIndex(match) != -1;
    }

    ///  是否为空
    public static bool Empty(this IEnumerable values) => !values.GetEnumerator().MoveNext();

    ///  转化为目标数组
    public static List<object[]> ToObjectArr(this IEnumerable values)
    {
        List<object[]> ret = new List<object[]>(values is ICollection c ? c.Count : 4);
        foreach (object ele in values)
            ret.Add(new object[] { ele });
        return ret;
    }

    ///  转化为目标数组
    public static List<object[]> ToObjectArr<T, TOut>(this IEnumerable<T> values, Func<T, TOut> func)
    {
        List<object[]> ret = new List<object[]>(values is ICollection c ? c.Count : 4);
        foreach (T ele in values)
            ret.Add(new object[] { ele, func.Invoke(ele) });
        return ret;
    }
}

你可能感兴趣的:(Unity,C#,c#,算法)