-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInfiniteSequence.cs
More file actions
224 lines (202 loc) · 7.79 KB
/
Copy pathInfiniteSequence.cs
File metadata and controls
224 lines (202 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace RapidField.SolidInstruments.Collections
{
/// <summary>
/// Represents a thread-safe, infinite sequence of calculated values.
/// </summary>
/// <remarks>
/// <see cref="InfiniteSequence{T}" /> is the default implementation of <see cref="IInfiniteSequence{T}" />.
/// </remarks>
/// <typeparam name="T">
/// The element type of the sequence.
/// </typeparam>
public abstract class InfiniteSequence<T> : IInfiniteSequence<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="InfiniteSequence{T}" /> class.
/// </summary>
protected InfiniteSequence()
: this(Array.Empty<T>())
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="InfiniteSequence{T}" /> class.
/// </summary>
/// <param name="seedTerm">
/// The first term in the sequence.
/// </param>
protected InfiniteSequence(T seedTerm)
: this(new T[] { seedTerm })
{
return;
}
/// <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;
}
/// <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];
}
}
}
/// <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 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>
/// 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));
}
}
/// <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>
/// Converts the value of the current <see cref="InfiniteSequence{T}" /> to its equivalent string representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="InfiniteSequence{T}" />.
/// </returns>
public override String ToString() => $"{{ \"{nameof(CalculatedTermCount)}\": {CalculatedTermCount} }}";
/// <summary>
/// Calculates the next term in the sequence.
/// </summary>
/// <param name="calculatedTerms">
/// The ordered, calculated terms for the current <see cref="InfiniteSequence{T}" />.
/// </param>
/// <returns>
/// The next term in the sequence.
/// </returns>
protected abstract T CalculateNext(IEnumerable<T> calculatedTerms);
/// <summary>
/// Gets the number of terms that have been calculated.
/// </summary>
public Int32 CalculatedTermCount => CalculatedTerms.Count;
/// <summary>
/// Represents the number of seed terms that were provided during initialization of the current
/// <see cref="InfiniteSequence{T}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Int32 SeedTermCount;
/// <summary>
/// Represents an object that can be used to synchronize access to the current <see cref="InfiniteSequence{T}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Object SyncRoot = new Object();
/// <summary>
/// Represents the ordered, calculated terms for the current <see cref="InfiniteSequence{T}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private List<T> CalculatedTerms = new List<T>();
}
}