本文介绍: 主要为迭代器的拓展方法, 为了方便使用
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

using UnityEngine;

/// <summary>迭代器扩展方法</summary>
[DebuggerStepThrough]
public static class EnumerableExtension
{
    /// <summary> 遍历操作 </summary>
    public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
    {
        foreach (T ele in values) 
            action(ele);
    }
    /// <summary> 遍历操作 </summary>
    public static void ForEach(this IEnumerable values, Action<object> action)
    {
        foreach (object ele in values)
            action(ele);
    }
    /// <summary> 计数</summary>
    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;
    }
    /// <summary> 计数</summary>
    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;
    }

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

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

    /// <summary> 查找下标</summary>
    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;
    }
    /// <summary> 查找下标</summary>
    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;
    }
    /// <summary> 查找下标</summary>
    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;
    }
    /// <summary>查找全部</summary>
    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;
    }

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

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

    /// <summary> 转化为目标数组</summary>
    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;
    }

    /// <summary> 转化为目标数组</summary>
    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;
    }
}

原文地址:https://blog.csdn.net/KamikazePilot/article/details/136025642

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_66637.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注