Lean  $LEAN_TAG$
LinqExtensions.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Collections.Immutable;
19 using System.Collections.ObjectModel;
20 using System.Linq;
21 
22 namespace QuantConnect.Util
23 {
24  /// <summary>
25  /// Provides more extension methods for the enumerable types
26  /// </summary>
27  public static class LinqExtensions
28  {
29  /// <summary>
30  /// Creates a new read-only dictionary from the key value pairs
31  /// </summary>
32  /// <typeparam name="K">The key type</typeparam>
33  /// <typeparam name="V">The value type</typeparam>
34  /// <param name="enumerable">The IEnumerable of KeyValuePair instances to convert to a dictionary</param>
35  /// <returns>A read-only dictionary holding the same data as the enumerable</returns>
36  public static IReadOnlyDictionary<K, V> ToReadOnlyDictionary<K, V>(this IEnumerable<KeyValuePair<K, V>> enumerable)
37  {
38  return new ReadOnlyDictionary<K, V>(enumerable.ToDictionary());
39  }
40 
41  /// <summary>
42  /// Creates a new <see cref="HashSet{T}"/> from the elements in the specified enumerable
43  /// </summary>
44  /// <typeparam name="T">The item type of the source enumerable</typeparam>
45  /// <typeparam name="TResult">The type of the items in the output <see cref="HashSet{T}"/></typeparam>
46  /// <param name="enumerable">The items to be placed into the enumerable</param>
47  /// <param name="selector">Selects items from the enumerable to be placed into the <see cref="HashSet{T}"/></param>
48  /// <returns>A new <see cref="HashSet{T}"/> containing the items in the enumerable</returns>
49  public static HashSet<TResult> ToHashSet<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> selector)
50  {
51  return new HashSet<TResult>(enumerable.Select(selector));
52  }
53 
54  /// <summary>
55  /// Creates a new <see cref="IList{T}"/> from the projected elements in the specified enumerable
56  /// </summary>
57  /// <typeparam name="T">The item type of the source enumerable</typeparam>
58  /// <typeparam name="TResult">The type of the items in the output <see cref="List{T}"/></typeparam>
59  /// <param name="enumerable">The items to be placed into the list</param>
60  /// <param name="selector">Selects items from the enumerable to be placed into the <see cref="List{T}"/></param>
61  /// <returns>A new <see cref="List{T}"/> containing the items in the enumerable</returns>
62  public static List<TResult> ToList<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> selector)
63  {
64  return enumerable.Select(selector).ToList();
65  }
66 
67  /// <summary>
68  /// Creates a new array from the projected elements in the specified enumerable
69  /// </summary>
70  /// <typeparam name="T">The item type of the source enumerable</typeparam>
71  /// <typeparam name="TResult">The type of the items in the output array</typeparam>
72  /// <param name="enumerable">The items to be placed into the array</param>
73  /// <param name="selector">Selects items from the enumerable to be placed into the array</param>
74  /// <returns>A new array containing the items in the enumerable</returns>
75  public static TResult[] ToArray<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> selector)
76  {
77  return enumerable.Select(selector).ToArray();
78  }
79 
80  /// <summary>
81  /// Creates a new immutable array from the projected elements in the specified enumerable
82  /// </summary>
83  /// <typeparam name="T">The item type of the source enumerable</typeparam>
84  /// <typeparam name="TResult">The type of the items in the output array</typeparam>
85  /// <param name="enumerable">The items to be placed into the array</param>
86  /// <param name="selector">Selects items from the enumerable to be placed into the array</param>
87  /// <returns>A new array containing the items in the enumerable</returns>
88  public static ImmutableArray<TResult> ToImmutableArray<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> selector)
89  {
90  return enumerable.Select(selector).ToImmutableArray();
91  }
92 
93  /// <summary>
94  /// Returns true if the specified enumerable is null or has no elements
95  /// </summary>
96  /// <typeparam name="T">The enumerable's item type</typeparam>
97  /// <param name="enumerable">The enumerable to check for a value</param>
98  /// <returns>True if the enumerable has elements, false otherwise</returns>
99  public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
100  {
101  return enumerable == null || !enumerable.Any();
102  }
103 
104  /// <summary>
105  /// Gets the median value in the collection
106  /// </summary>
107  /// <typeparam name="T">The item type in the collection</typeparam>
108  /// <param name="enumerable">The enumerable of items to search</param>
109  /// <returns>The median value, throws InvalidOperationException if no items are present</returns>
110  public static T Median<T>(this IEnumerable<T> enumerable)
111  {
112  var collection = enumerable.ToList();
113  return collection.OrderBy(x => x).Skip(collection.Count/2).First();
114  }
115 
116  /// <summary>
117  /// Gets the median value in the collection
118  /// </summary>
119  /// <typeparam name="T">The item type in the collection</typeparam>
120  /// <typeparam name="TProperty">The type of the value selected</typeparam>
121  /// <param name="collection">The collection of items to search</param>
122  /// <param name="selector">Function used to select a value from collection items</param>
123  /// <returns>The median value, throws InvalidOperationException if no items are present</returns>
124  public static TProperty Median<T, TProperty>(this IEnumerable<T> collection, Func<T, TProperty> selector)
125  {
126  return collection.Select(selector).Median();
127  }
128 
129  /// <summary>
130  /// Performs a binary search on the specified collection.
131  /// </summary>
132  /// <typeparam name="TItem">The type of the item.</typeparam>
133  /// <typeparam name="TSearch">The type of the searched item.</typeparam>
134  /// <param name="list">The list to be searched.</param>
135  /// <param name="value">The value to search for.</param>
136  /// <param name="comparer">The comparer that is used to compare the value with the list items.</param>
137  /// <returns>The index of the item if found, otherwise the bitwise complement where the value should be per MSDN specs</returns>
138  public static int BinarySearch<TItem, TSearch>(this IList<TItem> list, TSearch value, Func<TSearch, TItem, int> comparer)
139  {
140  if (list == null)
141  {
142  throw new ArgumentNullException(nameof(list));
143  }
144  if (comparer == null)
145  {
146  throw new ArgumentNullException(nameof(comparer));
147  }
148 
149  var lower = 0;
150  var upper = list.Count - 1;
151 
152  while (lower <= upper)
153  {
154  var middle = lower + (upper - lower) / 2;
155  var comparisonResult = comparer(value, list[middle]);
156  if (comparisonResult < 0)
157  {
158  upper = middle - 1;
159  }
160  else if (comparisonResult > 0)
161  {
162  lower = middle + 1;
163  }
164  else
165  {
166  return middle;
167  }
168  }
169 
170  return ~lower;
171  }
172 
173  /// <summary>
174  /// Performs a binary search on the specified collection.
175  /// </summary>
176  /// <typeparam name="TItem">The type of the item.</typeparam>
177  /// <param name="list">The list to be searched.</param>
178  /// <param name="value">The value to search for.</param>
179  /// <returns>The index of the item if found, otherwise the bitwise complement where the value should be per MSDN specs</returns>
180  public static int BinarySearch<TItem>(this IList<TItem> list, TItem value)
181  {
182  return BinarySearch(list, value, Comparer<TItem>.Default);
183  }
184 
185  /// <summary>
186  /// Performs a binary search on the specified collection.
187  /// </summary>
188  /// <typeparam name="TItem">The type of the item.</typeparam>
189  /// <param name="list">The list to be searched.</param>
190  /// <param name="value">The value to search for.</param>
191  /// <param name="comparer">The comparer that is used to compare the value with the list items.</param>
192  /// <returns>The index of the item if found, otherwise the bitwise complement where the value should be per MSDN specs</returns>
193  public static int BinarySearch<TItem>(this IList<TItem> list, TItem value, IComparer<TItem> comparer)
194  {
195  return list.BinarySearch(value, comparer.Compare);
196  }
197 
198  /// <summary>
199  /// Wraps the specified enumerable such that it will only be enumerated once
200  /// </summary>
201  /// <typeparam name="T">The enumerable's element type</typeparam>
202  /// <param name="enumerable">The source enumerable to be wrapped</param>
203  /// <returns>A new enumerable that can be enumerated multiple times without re-enumerating the source enumerable</returns>
204  public static IEnumerable<T> Memoize<T>(this IEnumerable<T> enumerable)
205  {
206  if (enumerable is MemoizingEnumerable<T>) return enumerable;
207  return new MemoizingEnumerable<T>(enumerable);
208  }
209 
210  /// <summary>
211  /// Produces the an enumerable of the range of values between start and end using the specified
212  /// incrementing function
213  /// </summary>
214  /// <typeparam name="T">The enumerable item type</typeparam>
215  /// <param name="start">The start of the range</param>
216  /// <param name="end">The end of the range, non-inclusive by default</param>
217  /// <param name="incrementer">The incrementing function, with argument of the current item</param>
218  /// <param name="includeEndPoint">True to emit the end point, false otherwise</param>
219  /// <returns>An enumerable of the range of items between start and end</returns>
220  public static IEnumerable<T> Range<T>(T start, T end, Func<T, T> incrementer, bool includeEndPoint = false)
221  where T : IComparable
222  {
223  var current = start;
224  if (includeEndPoint)
225  {
226  while (current.CompareTo(end) <= 0)
227  {
228  yield return current;
229  current = incrementer(current);
230  }
231  }
232  else
233  {
234  while (current.CompareTo(end) < 0)
235  {
236  yield return current;
237  current = incrementer(current);
238  }
239  }
240  }
241 
242  /// <summary>
243  /// Groups adjacent elements of the enumerale using the specified grouper function
244  /// </summary>
245  /// <typeparam name="T">The enumerable item type</typeparam>
246  /// <param name="enumerable">The source enumerable to be grouped</param>
247  /// <param name="grouper">A function that accepts the previous value and the next value and returns
248  /// true if the next value belongs in the same group as the previous value, otherwise returns false</param>
249  /// <returns>A new enumerable of the groups defined by grouper. These groups don't have a key
250  /// and are only grouped by being emitted separately from this enumerable</returns>
251  public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(this IEnumerable<T> enumerable, Func<T, T, bool> grouper)
252  {
253  using (var e = enumerable.GetEnumerator())
254  {
255  if (e.MoveNext())
256  {
257  var list = new List<T> {e.Current};
258  var pred = e.Current;
259  while (e.MoveNext())
260  {
261  if (grouper(pred, e.Current))
262  {
263  list.Add(e.Current);
264  }
265  else
266  {
267  yield return list;
268  list = new List<T> {e.Current};
269  }
270  pred = e.Current;
271  }
272  yield return list;
273  }
274  }
275  }
276 
277  /// <summary>
278  /// Determines if there are any differences between the left and right collections.
279  /// This method uses sets to improve performance and also uses lazy evaluation so if a
280  /// difference is found, true is immediately returned and evaluation is halted.
281  /// </summary>
282  /// <typeparam name="T">The item type</typeparam>
283  /// <param name="left">The left set</param>
284  /// <param name="right">The right set</param>
285  /// <returns>True if there are any differences between the two sets, false otherwise</returns>
286  public static bool AreDifferent<T>(this ISet<T> left, ISet<T> right)
287  {
288  if(ReferenceEquals(left, right))
289  {
290  return false;
291  }
292  return !left.SetEquals(right);
293  }
294 
295  /// <summary>
296  /// Converts an <see cref="IEnumerator{T}"/> to an <see cref="IEnumerable{T}"/>
297  /// </summary>
298  /// <typeparam name="T">Collection element type</typeparam>
299  /// <param name="enumerator">The enumerator to convert to an enumerable</param>
300  /// <returns>An enumerable wrapping the specified enumerator</returns>
301  public static IEnumerable<T> AsEnumerable<T>(this IEnumerator<T> enumerator)
302  {
303  using (enumerator)
304  {
305  while (enumerator.MoveNext())
306  {
307  yield return enumerator.Current;
308  }
309  }
310  }
311 
312  /// <summary>
313  /// Gets the value associated with the specified key or provided default value if key is not found.
314  /// </summary>
315  /// <typeparam name="K">The key type</typeparam>
316  /// <typeparam name="V">The value type</typeparam>
317  /// <param name="dictionary">The dictionary instance</param>
318  /// <param name="key">Lookup key</param>
319  /// <param name="defaultValue">Default value</param>
320  /// <returns>Value associated with the specified key or default value</returns>
321  public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dictionary, K key, V defaultValue = default(V))
322  {
323  V obj;
324  return dictionary.TryGetValue(key, out obj) ? obj : defaultValue;
325  }
326 
327  /// <summary>
328  /// Performs an action for each element in collection source
329  /// </summary>
330  /// <typeparam name="T"></typeparam>
331  /// <param name="source">Collection source</param>
332  /// <param name="action">An action to perform</param>
333  public static void DoForEach<T>(this IEnumerable<T> source, Action<T> action)
334  {
335  foreach (var element in source)
336  {
337  action(element);
338  }
339  }
340  }
341 }