Exemple #1
0
        public bool TryReadLine(out ProblemString problemString)
        {
            if (IsEmpty)
            {
                return(ReturnEmpty(out problemString));
            }

            if (_buffer.Count > 0)
            {
                return(ReturnFromBuffer(out problemString));
            }

            SpinWait.SpinUntil(() => Completion.IsCompleted || _buffer.Count > 0);

            if (IsEmpty)
            {
                return(ReturnEmpty(out problemString));
            }

            if (_buffer.Count > 0)
            {
                return(ReturnFromBuffer(out problemString));
            }

            throw new Exception("Logic error");
        }
Exemple #2
0
        public bool TryReadLine(out ProblemString problemString)
        {
            if (_buffer.Count > 0)
            {
                return(ReturnFromBuffer(out problemString));
            }
            else
            {
                if (EOF)
                {
                    return(ReturnEmpty(out problemString));
                }

                Load();

                return(_buffer.Count > 0
                    ? ReturnFromBuffer(out problemString)
                    : ReturnEmpty(out problemString));
            }
        }
Exemple #3
0
        /// <summary>
        /// Проверить, что файл отсортирован
        /// </summary>
        static Task CheckSort(CheckSortOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.FileName))
            {
                throw new ArgumentException("Empty file name");
            }

            using var file = File.OpenText(options.FileName);
            if (file.EndOfStream)
            {
                throw new ArgumentException("Empty file");
            }

            Console.WriteLine($"Check file is sort for file '{options.FileName}'");

            long linesCount = 0L;

            var prevLine = new ProblemString(file.ReadLine());

            linesCount++;

            while (!file.EndOfStream)
            {
                var line = new ProblemString(file.ReadLine());
                linesCount++;

                if (ProblemStringComparer.Default.Compare(prevLine, line) > 0)
                {
                    Console.WriteLine("Wrong order found:");
                    Console.WriteLine($"[{linesCount - 1}] {prevLine.RawString}");
                    Console.WriteLine($"[{linesCount}] {line.RawString}");
                    break;
                }

                prevLine = line;
            }

            Console.WriteLine($"Check file is sort DONE, {linesCount} lines");

            return(Task.CompletedTask);
        }
Exemple #4
0
        /// <summary>
        /// Чтение строк из файла;
        /// Наверное стоило не допускать многопоточного чтения, но я не увидел особой разницы в сравнении с однопоточным чтением;
        /// </summary>
        void Load()
        {
            if (EOF)
            {
                return;
            }

            long readed = 0L;

            while (readed < BufferSize)
            {
                var lineRaw = _chunk.ReadLine();
                if (lineRaw == null)
                {
                    _chunk.Dispose();
                    _chunk = null;
                    break;
                }

                var line = new ProblemString(lineRaw);
                readed += Consts.GetLineSize(line.RawString);
                _buffer.Enqueue(line);
            }
        }
Exemple #5
0
 bool ReturnEmpty(out ProblemString problemString)
 {
     problemString = ProblemString.Empty;
     return(false);
 }
Exemple #6
0
 bool ReturnFromBuffer(out ProblemString problemString)
 {
     _buffer.TryDequeue(out problemString);
     return(true);
 }