Exemple #1
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);
                    }
                }
            }
        }
Exemple #2
0
        public void Sort(FileInfo fileSource, FileInfo fileDestination, CancellationToken token)
        {
            File.Copy(fileSource.FullName, fileDestination.FullName);
            fileDestination.Refresh();

            IMemoryAcessor ma = new FileMemoryAccessor(fileDestination);

            maSort.Sort(ma, 0, fileDestination.Length / sizeof(UInt16), token);
        }
        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);
        }