Exemplo n.º 1
0
        private GroupedItem <T>[] MergeBlocks(
            IFileWriter fileWriter,
            int blockSize,
            GroupedItem <T>[] left,
            GroupedItem <T>[] right
            )
        {
            _Status = SortStatus.Merging;

            if (left.Length == 0)
            {
                return(right);
            }

            if (right.Length == 0)
            {
                return(left);
            }

            if (
                _comparer.Compare(left[0], left[left.Length - 1]) == 0 &&
                _comparer.Compare(right[0], right[right.Length - 1]) == 0
                )
            {
                if (_comparer.Compare(left[left.Length - 1], right[0]) <= 0)
                {
                    Write(fileWriter, right);

                    _BlocksMerged += 2;

                    return(left);
                }

                Write(fileWriter, left);

                _BlocksMerged += 2;

                return(right);
            }

            left = _sortStrategy.Merge(left, right);

            _BlocksMerged += 2;

            Write(fileWriter, left.Skip(blockSize).ToArray());

            return(left.Take(blockSize).ToArray());
        }