示例#1
0
 public MMFFileSort(IMASort maSort)
 {
     this.maSort = maSort;
 }
示例#2
0
        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);
        }
示例#3
0
 public MergeMASort(int subSortSize, IMASort subSort)
 {
     this.subSortSize = subSortSize;
     this.subSort     = subSort;
 }
示例#4
0
 public MergeFileSort(IMASort maSort, int divider)
 {
     this.maSort  = maSort;
     this.divider = divider;
 }
示例#5
0
        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);
                    }
                }
            }
        }