Exemplo n.º 1
0
        // TODO: Check Suffix array get matches method
        public IEnumerable <Match> FindMatchesAtPosition(byte[] input, int position)
        {
            var maxSize         = FindLimitations.MaxLength <= 0 ? input.Length : FindLimitations.MaxLength;
            var maxDisplacement = FindLimitations.MaxDisplacement <= 0 ? input.Length : FindLimitations.MaxDisplacement;

            if (!_array.IsBuilt)
            {
                _array.Build(input, 0, FindLimitations.MinLength);
            }

            if (input.Length - position < FindLimitations.MinLength)
            {
                yield break;
            }

            var cappedSize       = Math.Min(maxSize, input.Length - position);
            var longestMatchSize = FindLimitations.MinLength - 1;
            var displacement     = -1;
            var offsets          = _array.GetOffsets(position, FindLimitations.MinLength, FindLimitations.MinDisplacement, maxDisplacement, (int)FindOptions.UnitSize);

            foreach (var offset in offsets.OrderByDescending(x => x))
            {
                var matchLength = FindLimitations.MinLength;
                while (matchLength < cappedSize && input[offset + matchLength] == input[position + matchLength])
                {
                    matchLength++;
                }

                if (matchLength > longestMatchSize)
                {
                    longestMatchSize = matchLength;
                    displacement     = position - offset;
                    if (longestMatchSize == cappedSize)
                    {
                        break;
                    }
                }
            }

            if (displacement > -1)
            {
                yield return(new Match(position, displacement, longestMatchSize));
            }
        }