예제 #1
0
 /// <summary>
 /// Clears the terms in the current <see cref="InfiniteSequence{T}" />, leaving in place the seed terms.
 /// </summary>
 public void Reset()
 {
     lock (SyncRoot)
     {
         CalculatedTerms = new List <T>(CalculatedTerms.Take(SeedTermCount));
     }
 }
예제 #2
0
 /// <summary>
 /// Calculates the next term in the sequence.
 /// </summary>
 /// <returns>
 /// The next term in the sequence.
 /// </returns>
 public T CalculateNext()
 {
     lock (SyncRoot)
     {
         var nextTerm = CalculateNext(CalculatedTerms);
         CalculatedTerms.Add(nextTerm);
         return(nextTerm);
     }
 }
예제 #3
0
        /// <summary>
        /// Calculates the specified range of terms and returns them as an array.
        /// </summary>
        /// <param name="startIndex">
        /// The zero-based index of the first returned term.
        /// </param>
        /// <param name="count">
        /// The number of terms to return.
        /// </param>
        /// <returns>
        /// An array containing the calculated terms in the specified range.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex" /> is less than zero or <paramref name="count" /> is less than zero.
        /// </exception>
        public T[] ToArray(Int32 startIndex, Int32 count)
        {
            var indexCeiling = (startIndex.RejectIf().IsLessThan(0, nameof(startIndex)) + count.RejectIf().IsLessThan(0, nameof(count)));

            lock (SyncRoot)
            {
                while (CalculatedTerms.Count < indexCeiling)
                {
                    CalculatedTerms.Add(CalculateNext(CalculatedTerms));
                }

                return(CalculatedTerms.Skip(startIndex).Take(count).ToArray());
            }
        }
예제 #4
0
        /// <summary>
        /// Calculates the next terms in the sequence.
        /// </summary>
        /// <param name="count">
        /// The number of terms to calculate.
        /// </param>
        /// <returns>
        /// An array containing the calculated terms.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="count" /> is less than zero.
        /// </exception>
        public T[] CalculateNext(Int32 count)
        {
            lock (SyncRoot)
            {
                var nextTerms = new T[count.RejectIf().IsLessThan(0, nameof(count))];

                for (var i = 0; i < count; i++)
                {
                    var nextTerm = CalculateNext(CalculatedTerms);
                    CalculatedTerms.Add(nextTerm);
                    nextTerms[i] = nextTerm;
                }

                return(nextTerms);
            }
        }
예제 #5
0
        /// <summary>
        /// Gets the term at the specified index.
        /// </summary>
        /// <param name="index">
        /// The zero-based index of the term to get.
        /// </param>
        /// <returns>
        /// The term at the specified index.
        /// </returns>
        /// <exception cref="IndexOutOfRangeException">
        /// <paramref name="index" /> is less than zero.
        /// </exception>
        public T this[Int32 index]
        {
            get
            {
                if (index < 0)
                {
                    throw new IndexOutOfRangeException();
                }

                var indexCeiling = (index + 1);

                lock (SyncRoot)
                {
                    while (CalculatedTerms.Count < indexCeiling)
                    {
                        CalculatedTerms.Add(CalculateNext(CalculatedTerms));
                    }

                    return(CalculatedTerms[index]);
                }
            }
        }
예제 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InfiniteSequence{T}" /> class.
 /// </summary>
 /// <param name="seedTerms">
 /// The first terms in the sequence.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="seedTerms" /> is <see langword="null" />.
 /// </exception>
 protected InfiniteSequence(T[] seedTerms)
 {
     CalculatedTerms.AddRange(seedTerms);
     SeedTermCount = seedTerms.Length;
 }