Beispiel #1
0
        /// <summary>
        /// Factory method for generic streams
        /// </summary>
        /// <param name="sequence"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        public static IStream <T> Create(IEnumerable <T> sequence, bool buffered)
        {
            IList <T> list = sequence as IList <T>;

            if (list != null)
            {
                return(new ListStream <T>(list));
            }
            else if (buffered)
            {
                list = new SequenceBuffer <T>(sequence);
                return(new ListStream <T>(list));
            }

            return(new EnumerableStream <T>(sequence));
        }
Beispiel #2
0
        /// <summary>
        /// Deferred execution of iterator
        /// </summary>
        private void EnsureReady()
        {
            // only execute when requested
            if (this.isReady || this.isCompleted)
            {
                return;
            }
            this.isReady = true;

            // store the current item or null if complete
            int next = this.index + 1;

            SequenceBuffer <T> sBuffer = this.Buffer as SequenceBuffer <T>;

            if (sBuffer != null)
            {
                // avoid using SequenceBuffer<T>.Count as will need to enumerate entire sequence
                if (sBuffer.TryAdvance(next))
                {
                    this.current = this.Buffer[next];
                }
                else
                {
                    this.isCompleted = true;
                    this.current     = default(T);
                }
            }
            else
            {
                if (next < this.Buffer.Count)
                {
                    this.current = this.Buffer[next];
                }
                else
                {
                    this.isCompleted = true;
                    this.current     = default(T);
                }
            }
        }
Beispiel #3
0
        public void WrapResult_LongStringTwice_ReturnsSameSequence()
        {
            const string input = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

            var sequence = new SequenceBuffer <char>(this.GetSequence(new StringStream(input)));

            var buffer = new StringBuilder();

            foreach (char ch in sequence)
            {
                buffer.Append(ch);
            }

            Assert.Equal(input, buffer.ToString());

            buffer.Clear();
            foreach (char ch in sequence)
            {
                buffer.Append(ch);
            }

            // wrapped version iterates multiple times
            Assert.Equal(input, buffer.ToString());
        }
Beispiel #4
0
        public void WrapInput_NullString_ReturnsEmptySequence()
        {
            var buffer = new SequenceBuffer <char>(null);

            Assert.Equal(new char[0], buffer.ToArray());
        }
Beispiel #5
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="sequence"></param>
 public Enumerator(SequenceBuffer <T> sequence)
 {
     this.Sequence = sequence;
     this.Index    = -1;
 }