Esempio n. 1
0
        private void LoadInputWindow(InputWindow newWindow)
        {
            this.InputWindow    = newWindow;
            this.PosWindowStart = newWindow.PosWindowStart;
            this.PosWindowEnd   = newWindow.PosWindowEnd;


            this.m_outputWriter.AddWindow(newWindow);
        }
Esempio n. 2
0
        private void QueueMoreWindows()
        {
            for (;;)
            {
                lock (this.m_inputWindowQueue) {
                    if (CntWindowsToQueue == this.m_inputWindowQueue.Count)
                    {
                        return;
                    }
                }

                int  cntCharsRead = this.m_readBlockDelegate.EndInvoke(this.m_readBlockResult);
                bool isLastWindow = false;

                if (cntCharsRead < WindowSize)
                {
                    isLastWindow = true;
                }

                int posNewWindowStart;

                if (this.m_lastWindowRead != null)
                {
                    this.m_windowBuilder.Length = 0;
                    this.m_windowBuilder.Append(this.m_lastWindowRead.Window, this.m_lastWindowRead.Window.Length - CntSafetyMargin, CntSafetyMargin);

                    posNewWindowStart = this.m_lastWindowRead.PosWindowEnd + 1 - (CntSafetyMargin);
                }
                else
                {
                    posNewWindowStart = 0;
                }

                this.m_idxBufferEnd += cntCharsRead;
                InputWindow newWindow = this.BuildNewWindow(posNewWindowStart, isLastWindow);
                lock (this.m_inputWindowQueue) {
                    this.m_hasReadLastWindow = isLastWindow;
                    this.m_inputWindowQueue.Enqueue(newWindow);
                    this.m_windowIsReady.Set();
                }

                this.m_lastWindowRead = newWindow;

                if (isLastWindow)
                {
                    return;
                }

                if (this.m_buffer.Length < this.m_idxBufferEnd + WindowSize + 4)
                {
                    Array.Resize(ref this.m_buffer, this.m_idxBufferEnd + WindowSize + 4);
                }

                this.m_readBlockResult = this.m_readBlockDelegate.BeginInvoke(this.m_buffer, this.m_idxBufferEnd + 1, WindowSize, null, null);
            }
        }
        private void GetNextLineEnd(InputWindow window, out int idxNextLineEnd, out int posNextLineEnd)
        {
            idxNextLineEnd = Array.BinarySearch(window.IdxLineEnds, this.m_posCurrent);

            if (idxNextLineEnd < 0)
            {
                idxNextLineEnd = ~idxNextLineEnd;
            }

            if (window.IdxLineEnds.Length <= idxNextLineEnd)
            {
                posNextLineEnd = int.MaxValue;
                return;
            }
            else
            {
                posNextLineEnd = window.IdxLineEnds[idxNextLineEnd];
            }
        }
Esempio n. 4
0
        private void LoadInputWindow(InputWindow newWindow)
        {
            this.InputWindow = newWindow;
            this.PosWindowStart = newWindow.PosWindowStart;
            this.PosWindowEnd = newWindow.PosWindowEnd;

            this.m_outputWriter.AddWindow(newWindow);
        }
Esempio n. 5
0
 internal void AddWindow(InputWindow newWindow)
 {
     lock (this.m_windows) {
         this.m_windows.Enqueue(newWindow);
     }
 }
Esempio n. 6
0
        private void GetNextLineEnd(InputWindow window, out int idxNextLineEnd, out int posNextLineEnd)
        {
            idxNextLineEnd = Array.BinarySearch(window.IdxLineEnds, this.m_posCurrent);

            if (idxNextLineEnd < 0) {
                idxNextLineEnd = ~idxNextLineEnd;
            }

            if (window.IdxLineEnds.Length <= idxNextLineEnd) {
                posNextLineEnd = int.MaxValue;
                return;
            } else {
                posNextLineEnd = window.IdxLineEnds[idxNextLineEnd];
            }
        }
 internal void AddWindow(InputWindow newWindow)
 {
     lock (this.m_windows) {
         this.m_windows.Enqueue(newWindow);
     }
 }
Esempio n. 8
0
        private void AdvanceWindow()
        {
            bool        isDone     = false;
            InputWindow nextWindow = null;

            lock (this.m_inputWindowQueue) {
                if (0 < this.m_inputWindowQueue.Count)
                {
                    nextWindow = this.m_inputWindowQueue.Dequeue();
                    isDone     = true;
                }
                else
                {
                    if (this.m_hasReadLastWindow)
                    {
                        string msg = "Abnormal condition. We have finished reading input (m_hasReadLastWindow is true), and yet "
                                     + "there are no windows available when AdvanceWindow was called. This is a bug.";
                        throw new AssertionViolationException(msg);
                    }
                }

                this.m_windowIsReady.Reset();

                if (this.m_queueMoreWindowsResult != null && this.m_queueMoreWindowsResult.IsCompleted)
                {
                    this.m_queueMoreWindows.EndInvoke(this.m_queueMoreWindowsResult);

                    if (this.m_hasReadLastWindow)
                    {
                        this.m_queueMoreWindowsResult = null;
                    }
                    else
                    {
                        this.m_queueMoreWindowsResult = this.m_queueMoreWindows.BeginInvoke(null, null);
                    }
                }
            }

            if (!isDone)
            {
                WaitHandle.WaitAny(new WaitHandle[] { this.m_windowIsReady, this.m_queueMoreWindowsResult.AsyncWaitHandle });

                lock (this.m_inputWindowQueue) {
                    // this can never happen, because whenever windowIsready OR queueMoreWindowsResult are signaled, it means
                    // that we have at least 1 window available (windowIsReady signaled) or CntWindowsToQueue available (result signaled)
                    if (0 == this.m_inputWindowQueue.Count)
                    {
                        if (this.m_queueMoreWindowsResult.IsCompleted)
                        {
                            // there was probably an exception inside queueMoreWindows. Let it bubble up
                            this.m_queueMoreWindows.EndInvoke(this.m_queueMoreWindowsResult);
                        }

                        string msg = StringExtensions.Fi("SlidingWindowReader has encountered an abnormal condition. The QueueMoreWindows() method "
                                                         + "has returned without an exception, but no windows have been queued.");
                        throw new AssertionViolationException(msg);
                    }

                    nextWindow = this.m_inputWindowQueue.Dequeue();
                }
            }

            this.m_currentWindow = nextWindow;

            if (this.OnNewInputWindow != null)
            {
                this.OnNewInputWindow(nextWindow);
            }
        }
Esempio n. 9
0
        public SlidingWindowReader(string s)
        {
            ArgumentValidator.ThrowIfNull(s, "s");

            this.m_windowBuilder = new StringBuilder(s.Length + 2);
            this.m_lineEnds = new List<int>(s.Length / 40);

            this.m_buffer = new char[s.Length + 1];
            s.CopyTo(0, this.m_buffer, 0, s.Length);
            this.m_idxBufferEnd = s.Length-1;

            this.m_currentWindow = this.BuildNewWindow(0, true);
        }
Esempio n. 10
0
        private void QueueMoreWindows()
        {
            for (;;) {
                lock (this.m_inputWindowQueue) {
                    if (CntWindowsToQueue == this.m_inputWindowQueue.Count) {
                        return;
                    }
                }

                int cntCharsRead = this.m_readBlockDelegate.EndInvoke(this.m_readBlockResult);
                bool isLastWindow = false;

                if (cntCharsRead < WindowSize) {
                    isLastWindow = true;
                }

                int posNewWindowStart;

                if (this.m_lastWindowRead != null) {
                    this.m_windowBuilder.Length = 0;
                    this.m_windowBuilder.Append(this.m_lastWindowRead.Window, this.m_lastWindowRead.Window.Length - CntSafetyMargin, CntSafetyMargin);

                    posNewWindowStart = this.m_lastWindowRead.PosWindowEnd + 1 - (CntSafetyMargin);
                } else {
                    posNewWindowStart = 0;
                }

                this.m_idxBufferEnd += cntCharsRead;
                InputWindow newWindow = this.BuildNewWindow(posNewWindowStart, isLastWindow);
                lock (this.m_inputWindowQueue) {
                    this.m_hasReadLastWindow = isLastWindow;
                    this.m_inputWindowQueue.Enqueue(newWindow);
                    this.m_windowIsReady.Set();
                }

                this.m_lastWindowRead = newWindow;

                if (isLastWindow) {
                    return;
                }

                if (this.m_buffer.Length < this.m_idxBufferEnd + WindowSize + 4) {
                    Array.Resize(ref this.m_buffer, this.m_idxBufferEnd + WindowSize + 4);
                }

                this.m_readBlockResult = this.m_readBlockDelegate.BeginInvoke(this.m_buffer, this.m_idxBufferEnd + 1, WindowSize, null, null);
            }
        }
Esempio n. 11
0
        private void AdvanceWindow()
        {
            bool isDone = false;
            InputWindow nextWindow = null;

            lock (this.m_inputWindowQueue) {
                if (0 < this.m_inputWindowQueue.Count) {
                    nextWindow = this.m_inputWindowQueue.Dequeue();
                    isDone = true;
                } else {
                    if (this.m_hasReadLastWindow) {
                        string msg = "Abnormal condition. We have finished reading input (m_hasReadLastWindow is true), and yet "
                            + "there are no windows available when AdvanceWindow was called. This is a bug.";
                        throw new AssertionViolationException(msg);
                    }
                }

                this.m_windowIsReady.Reset();

                if (this.m_queueMoreWindowsResult != null && this.m_queueMoreWindowsResult.IsCompleted) {
                    this.m_queueMoreWindows.EndInvoke(this.m_queueMoreWindowsResult);

                    if (this.m_hasReadLastWindow) {
                        this.m_queueMoreWindowsResult = null;
                    } else {
                        this.m_queueMoreWindowsResult = this.m_queueMoreWindows.BeginInvoke(null, null);
                    }
                }
            }

            if (!isDone) {
                WaitHandle.WaitAny(new WaitHandle[] {this.m_windowIsReady, this.m_queueMoreWindowsResult.AsyncWaitHandle});

                lock (this.m_inputWindowQueue) {
                    // this can never happen, because whenever windowIsready OR queueMoreWindowsResult are signaled, it means
                    // that we have at least 1 window available (windowIsReady signaled) or CntWindowsToQueue available (result signaled)
                    if (0 == this.m_inputWindowQueue.Count) {
                        if (this.m_queueMoreWindowsResult.IsCompleted) {
                            // there was probably an exception inside queueMoreWindows. Let it bubble up
                            this.m_queueMoreWindows.EndInvoke(this.m_queueMoreWindowsResult);
                        }

                        string msg = StringExtensions.Fi("SlidingWindowReader has encountered an abnormal condition. The QueueMoreWindows() method "
                            + "has returned without an exception, but no windows have been queued.");
                        throw new AssertionViolationException(msg);
                    }

                    nextWindow = this.m_inputWindowQueue.Dequeue();
                }
            }

            this.m_currentWindow = nextWindow;

            if (this.OnNewInputWindow != null) {
                this.OnNewInputWindow(nextWindow);
            }
        }