Esempio n. 1
0
        private static File MergePass(List <File> filesToMerge, MergeBuffer mergeBuffer)
        {
            File outFile = new File(
                nBlocks: (uint)filesToMerge.Sum(file => file.nBlocks),
                blockSize: filesToMerge[0].blockSize,
                initializer: 0);

            for (int bufferIndex = 0; bufferIndex < (int)mergeBuffer.BufferBlocks.Length; bufferIndex++)
            {
                mergeBuffer.BufferBlocks[bufferIndex]
                .assignInputFile(filesToMerge[bufferIndex]);
            }
            mergeBuffer.OutputBuffer.assignOutputFile(outFile);

            while (mergeBuffer.BufferBlocks.Any(block => block.HasNext))
            {
                var validBlocks = mergeBuffer.BufferBlocks.Where(block => block.HasNext);

                var nextBlock = validBlocks.First(
                    block => block.peekNext == validBlocks.Min(blk => blk.peekNext));

                mergeBuffer.OutputBuffer.writeNext(nextBlock.readNext());
            }
            return(outFile);
        }
Esempio n. 2
0
        private static File MergePhase(File sourceFile, uint bufferSize, List <File> runFiles)
        {
            File        sortedFile;
            MergeBuffer mergeBuffer = new MergeBuffer(
                nBlocks: bufferSize,
                blockSize: sourceFile.blockSize);

            int nMergingSources = (int)bufferSize - 1;
            int nMergeRuns      = (int)Math.Ceiling((decimal)(runFiles.Count / nMergingSources));

            while (nMergeRuns > 1)
            {
                List <File> nextPassFiles = new List <File>();
                for (int passIndex = 0; passIndex < nMergeRuns; passIndex++)
                {
                    var filesToMerge = runFiles.GetRange(
                        index: passIndex * nMergingSources,
                        count: nMergingSources
                        );
                    var outFile = MergePass(filesToMerge, mergeBuffer);
                    nextPassFiles.Add(outFile);
                }
                runFiles   = nextPassFiles;
                nMergeRuns = (int)Math.Ceiling((decimal)runFiles.Count / nMergingSources);
            }

            sortedFile = MergePass(runFiles, mergeBuffer);
            return(sortedFile);
        }