/// <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); } }
/// <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()); } }
/// <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); } }
/// <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]); } } }