﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Len.Framework.Extensions
{
    public static partial class EnumerableExtensions
    {
        public static bool SequenceEqual<T1, T2>(this IEnumerable<T1> @this, IEnumerable<T2> other, Func<T1, T2, bool> comparer)
        {
            using (IEnumerator<T1> leftEnumerator = @this.GetEnumerator())
            {
                using (IEnumerator<T2> rightEnumerator = other.GetEnumerator())
                {
                    bool leftNext = leftEnumerator.MoveNext(), rightNext = rightEnumerator.MoveNext();

                    while (leftNext && rightNext)
                    {
                        // 两个相对应的元素不匹配时，返回false
                        if (!comparer(leftEnumerator.Current, rightEnumerator.Current))
                            return false;

                        leftNext = leftEnumerator.MoveNext();
                        rightNext = rightEnumerator.MoveNext();
                    }

                    // 有一个集合的元素较多时，返回false
                    if (leftNext || rightNext)
                        return false;
                }
            }

            return true;
        }
    }
}
