示例#1
0
        private static int LastIndexOfSliceExperimental <A>(this IEnumerable <A> source, IEnumerable <A> that, int end, IEqualityComparer <A> comparer)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            if (that == null)
            {
                throw Error.ArgumentNull("that");
            }
            if (comparer == null)
            {
                comparer = EqualityComparer <A> .Default;
            }

            // ReSharper disable PossibleMultipleEnumeration
            var l          = source.Count();
            var tl         = that.Count();
            var clippedEnd = Math.Min(l - tl, end);

            if (end < 0)
            {
                return(-1);
            }
            if (tl < 1)
            {
                return(clippedEnd);
            }
            if (l < tl)
            {
                return(-1);
            }
            return(KmpSearchUtilities.KmpSearch(source, 0, clippedEnd + tl, that, 0, tl, comparer, forward: false));
            // ReSharper restore PossibleMultipleEnumeration
        }
示例#2
0
        private static int IndexOfSliceExperimental <A>(this IEnumerable <A> source, IEnumerable <A> that, int from, IEqualityComparer <A> comparer)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            if (that == null)
            {
                throw Error.ArgumentNull("that");
            }
            if (comparer == null)
            {
                comparer = EqualityComparer <A> .Default;
            }

            var sourceList = source as IList <A>;
            var thatList   = that as IList <A>;

            var clippedFrom = Math.Max(0, from);

            if (sourceList != null && thatList != null)
            {
                var l  = sourceList.Count;
                var tl = thatList.Count;
                if (from > l)
                {
                    return(-1);
                }
                if (tl < 1)
                {
                    return(clippedFrom);
                }
                if (l < tl)
                {
                    return(-1);
                }
                return(KmpSearchUtilities.KmpSearch(sourceList, clippedFrom, l, thatList, 0, tl, comparer, forward: true));
            }

            // ReSharper disable PossibleMultipleEnumeration
            if (that.IsEmpty())
            {
                var sourceLength = source.Count();
                return((clippedFrom <= sourceLength) ? clippedFrom : -1);
            }

            var index = clippedFrom;
            var seq   = source.Skip(clippedFrom);

            for (; ;)
            {
                if (seq.IsEmpty())
                {
                    return(-1);
                }
                if (seq.StartsWith(that, comparer))
                {
                    return(index);
                }
                seq = seq.Tail();
                index++;
            }
            // ReSharper restore PossibleMultipleEnumeration
        }