public MMFFileSort(IMASort maSort) { this.maSort = maSort; }
private Queue <FileInfo> SortParts(FileInfo fileSource, FileInfo fileDestination, int step, IMASort maSort, CancellationToken token) { string tmpFileName = Path.GetFileNameWithoutExtension(fileDestination.Name); int bufferSize = step * sizeof(UInt16); byte[] buffer = new byte[bufferSize]; UInt16[] array = new UInt16[step]; IMemoryAcessor ma = new ArrayMemoryAccessor(array); FileStream streamSource = fileSource.OpenRead(); Queue <FileInfo> fileParts = new Queue <FileInfo>(); while (true) { token.ThrowIfCancellationRequested(); int readBytes = streamSource.Read(buffer); if (readBytes > 0) { Buffer.BlockCopy(buffer, 0, array, 0, readBytes); maSort.Sort(ma, 0, readBytes / sizeof(UInt16), token); Buffer.BlockCopy(array, 0, buffer, 0, readBytes); FileInfo filePart = new FileInfo(Path.Combine(fileDestination.DirectoryName, tmpFileName + $".partial.{fileParts.Count}" + fileDestination.Extension)); FileStream streamPart = filePart.OpenWrite(); streamPart.Write(buffer, 0, readBytes); streamPart.Close(); fileParts.Enqueue(filePart); } if (readBytes < bufferSize) { break; } } streamSource.Close(); return(fileParts); }
public MergeMASort(int subSortSize, IMASort subSort) { this.subSortSize = subSortSize; this.subSort = subSort; }
public MergeFileSort(IMASort maSort, int divider) { this.maSort = maSort; this.divider = divider; }
static void MergeSort(IMemoryAcessor ma, long start, long end, long subSortSize, IMASort subSort, CancellationToken token) { IMemoryAcessor aux = ma.CloneAUX(); for (long startInner = start; startInner < end; startInner += subSortSize) { long endInner = startInner + subSortSize; if (endInner > end) { endInner = end; } subSort.Sort(ma, end - endInner, end - startInner, token); } for (long size = subSortSize; size < end - start; size <<= 1) { for (long startInner = start; startInner < end; startInner += (size << 1)) { long midInner = startInner + size; long endInner = midInner + size; if (midInner < end) { if (endInner > end) { endInner = end; } Merge(aux, ma, end - endInner, end - midInner, end - startInner, token); } } } }