summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/EnumerableCacheExtensions.cs
blob: 0886ef26a50cbbe31e31ed4d707fb55cb98c735c (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//-----------------------------------------------------------------------
// <copyright file="EnumerableCacheExtensions.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// This code is released under the Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging {
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using Validation;

	/// <summary>
	/// Extension methods for <see cref="IEnumerable&lt;T&gt;"/> types.
	/// </summary>
	public static class EnumerableCacheExtensions {
		/// <summary>
		/// Caches the results of enumerating over a given object so that subsequence enumerations
		/// don't require interacting with the object a second time.
		/// </summary>
		/// <typeparam name="T">The type of element found in the enumeration.</typeparam>
		/// <param name="sequence">The enumerable object.</param>
		/// <returns>
		/// Either a new enumerable object that caches enumerated results, or the original, <paramref name="sequence"/>
		/// object if no caching is necessary to avoid additional CPU work.
		/// </returns>
		/// <remarks>
		///   <para>This is designed for use on the results of generator methods (the ones with <c>yield return</c> in them)
		/// so that only those elements in the sequence that are needed are ever generated, while not requiring
		/// regeneration of elements that are enumerated over multiple times.</para>
		///   <para>This can be a huge performance gain if enumerating multiple times over an expensive generator method.</para>
		///   <para>Some enumerable types such as collections, lists, and already-cached generators do not require
		/// any (additional) caching, and this method will simply return those objects rather than caching them
		/// to avoid double-caching.</para>
		/// </remarks>
		public static IEnumerable<T> CacheGeneratedResults<T>(this IEnumerable<T> sequence) {
			Requires.NotNull(sequence, "sequence");

			// Don't create a cache for types that don't need it.
			if (sequence is IList<T> ||
			  sequence is ICollection<T> ||
			  sequence is Array ||
			  sequence is EnumerableCache<T>) {
				return sequence;
			}

			return new EnumerableCache<T>(sequence);
		}

		/// <summary>
		/// A wrapper for <see cref="IEnumerable&lt;T&gt;"/> types and returns a caching <see cref="IEnumerator&lt;T&gt;"/>
		/// from its <see cref="IEnumerable&lt;T&gt;.GetEnumerator"/> method.
		/// </summary>
		/// <typeparam name="T">The type of element in the sequence.</typeparam>
		private class EnumerableCache<T> : IEnumerable<T> {
			/// <summary>
			/// The results from enumeration of the live object that have been collected thus far.
			/// </summary>
			private List<T> cache;

			/// <summary>
			/// The original generator method or other enumerable object whose contents should only be enumerated once.
			/// </summary>
			private IEnumerable<T> generator;

			/// <summary>
			/// The enumerator we're using over the generator method's results.
			/// </summary>
			private IEnumerator<T> generatorEnumerator;

			/// <summary>
			/// The sync object our caching enumerators use when adding a new live generator method result to the cache.
			/// </summary>
			/// <remarks>
			/// Although individual enumerators are not thread-safe, this <see cref="IEnumerable&lt;T&gt;"/> should be
			/// thread safe so that multiple enumerators can be created from it and used from different threads.
			/// </remarks>
			private object generatorLock = new object();

			/// <summary>
			/// Initializes a new instance of the EnumerableCache class.
			/// </summary>
			/// <param name="generator">The generator.</param>
			internal EnumerableCache(IEnumerable<T> generator) {
				Requires.NotNull(generator, "generator");

				this.generator = generator;
			}

			#region IEnumerable<T> Members

			/// <summary>
			/// Returns an enumerator that iterates through the collection.
			/// </summary>
			/// <returns>
			/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
			/// </returns>
			public IEnumerator<T> GetEnumerator() {
				if (this.generatorEnumerator == null) {
					this.cache = new List<T>();
					this.generatorEnumerator = this.generator.GetEnumerator();
				}

				return new EnumeratorCache(this);
			}

			#endregion

			#region IEnumerable Members

			/// <summary>
			/// Returns an enumerator that iterates through a collection.
			/// </summary>
			/// <returns>
			/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
			/// </returns>
			System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
				return this.GetEnumerator();
			}

			#endregion

			/// <summary>
			/// An enumerator that uses cached enumeration results whenever they are available,
			/// and caches whatever results it has to pull from the original <see cref="IEnumerable&lt;T&gt;"/> object.
			/// </summary>
			private class EnumeratorCache : IEnumerator<T> {
				/// <summary>
				/// The parent enumeration wrapper class that stores the cached results.
				/// </summary>
				private EnumerableCache<T> parent;

				/// <summary>
				/// The position of this enumerator in the cached list.
				/// </summary>
				private int cachePosition = -1;

				/// <summary>
				/// Initializes a new instance of the EnumeratorCache class.
				/// </summary>
				/// <param name="parent">The parent cached enumerable whose GetEnumerator method is calling this constructor.</param>
				internal EnumeratorCache(EnumerableCache<T> parent) {
					Requires.NotNull(parent, "parent");

					this.parent = parent;
				}

				#region IEnumerator<T> Members

				/// <summary>
				/// Gets the element in the collection at the current position of the enumerator.
				/// </summary>
				/// <returns>
				/// The element in the collection at the current position of the enumerator.
				/// </returns>
				public T Current {
					get {
						if (this.cachePosition < 0 || this.cachePosition >= this.parent.cache.Count) {
							throw new InvalidOperationException();
						}

						return this.parent.cache[this.cachePosition];
					}
				}

				#endregion

				#region IEnumerator Properties

				/// <summary>
				/// Gets the element in the collection at the current position of the enumerator.
				/// </summary>
				/// <returns>
				/// The element in the collection at the current position of the enumerator.
				/// </returns>
				object System.Collections.IEnumerator.Current {
					get { return this.Current; }
				}

				#endregion

				#region IDisposable Members

				/// <summary>
				/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
				/// </summary>
				public void Dispose() {
					this.Dispose(true);
					GC.SuppressFinalize(this);
				}

				#endregion

				#region IEnumerator Methods

				/// <summary>
				/// Advances the enumerator to the next element of the collection.
				/// </summary>
				/// <returns>
				/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
				/// </returns>
				/// <exception cref="T:System.InvalidOperationException">
				/// The collection was modified after the enumerator was created.
				/// </exception>
				public bool MoveNext() {
					this.cachePosition++;
					if (this.cachePosition >= this.parent.cache.Count) {
						lock (this.parent.generatorLock) {
							if (this.parent.generatorEnumerator.MoveNext()) {
								this.parent.cache.Add(this.parent.generatorEnumerator.Current);
							} else {
								return false;
							}
						}
					}

					return true;
				}

				/// <summary>
				/// Sets the enumerator to its initial position, which is before the first element in the collection.
				/// </summary>
				/// <exception cref="T:System.InvalidOperationException">
				/// The collection was modified after the enumerator was created.
				/// </exception>
				public void Reset() {
					this.cachePosition = -1;
				}

				#endregion

				/// <summary>
				/// Releases unmanaged and - optionally - managed resources
				/// </summary>
				/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
				protected virtual void Dispose(bool disposing) {
					// Nothing to do here.
				}
			}
		}
	}
}