11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics . CodeAnalysis ;
4+ using System . Diagnostics . Contracts ;
45using System . Linq ;
56using System . Runtime . InteropServices ;
67
@@ -13,7 +14,7 @@ public static class Extensions
1314 /// </summary>
1415 public static void EnsureLength < T > ( ref T [ ] array , int length )
1516 {
16- if ( array . Length > length )
17+ if ( array . Length >= length )
1718 return ;
1819
1920 Array . Resize ( ref array , length ) ;
@@ -40,20 +41,13 @@ public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
4041 /// <returns>A new list with the same elements as <paramref name="list" />.</returns>
4142 public static List < T > ShallowClone < T > ( this List < T > self )
4243 {
43- var list = new List < T > ( self . Count ) ;
44- list . AddRange ( self ) ;
45- return list ;
44+ return new List < T > ( self ) ;
4645 }
4746
4847 public static Dictionary < TKey , TValue > ShallowClone < TKey , TValue > ( this Dictionary < TKey , TValue > self )
4948 where TKey : notnull
5049 {
51- var dict = new Dictionary < TKey , TValue > ( self . Count ) ;
52- foreach ( var item in self )
53- {
54- dict [ item . Key ] = item . Value ;
55- }
56- return dict ;
50+ return new Dictionary < TKey , TValue > ( self , self . Comparer ) ;
5751 }
5852
5953 /// <summary>
@@ -72,13 +66,12 @@ public static bool DictionaryEquals<TKey, TValue>(
7266 IReadOnlyDictionary < TKey , TValue > other )
7367 where TKey : notnull
7468 {
69+ if ( ReferenceEquals ( self , other ) )
70+ return true ;
71+
7572 if ( self . Count != other . Count )
7673 return false ;
7774
78- // Checking itself.
79- if ( self . Equals ( other ) )
80- return true ;
81-
8275 var valueComparer = EqualityComparer < TValue > . Default ;
8376 foreach ( var ( key , value ) in self )
8477 {
@@ -219,7 +212,8 @@ public static bool TryFirstOrNull<TSource>(this IEnumerable<TSource> source, Fun
219212 /// <paramref name="source" /> is <see langword="null" />.</exception>
220213 public static bool TryFirstOrNull < TSource > ( this IEnumerable < TSource > source , [ NotNullWhen ( true ) ] out TSource ? element ) where TSource : struct
221214 {
222- return TryFirstOrNull ( source , _ => true , out element ) ;
215+ element = source . FirstOrNull ( ) ;
216+ return element != null ;
223217 }
224218
225219 /// <summary>
@@ -247,7 +241,8 @@ public static bool TryFirstOrDefault<TSource>(this IEnumerable<TSource> source,
247241 /// <paramref name="source" /> is <see langword="null" />.</exception>
248242 public static bool TryFirstOrDefault < TSource > ( this IEnumerable < TSource > source , [ NotNullWhen ( true ) ] out TSource ? element ) where TSource : class
249243 {
250- return TryFirstOrDefault ( source , _ => true , out element ) ;
244+ element = source . FirstOrDefault ( ) ;
245+ return element != null ;
251246 }
252247
253248 public static TValue GetOrNew < TKey , TValue > ( this IDictionary < TKey , TValue > dict , TKey key ) where TValue : new ( )
@@ -284,20 +279,13 @@ public static TValue GetOrNew<TKey, TValue>(this Dictionary<TKey, TValue> dict,
284279 return entry ! ;
285280 }
286281
287- // More efficient than LINQ.
282+ [ Pure ]
288283 public static KeyValuePair < TKey , TValue > [ ] ToArray < TKey , TValue > ( this Dictionary < TKey , TValue > dict )
289284 where TKey : notnull
290285 {
291- var array = new KeyValuePair < TKey , TValue > [ dict . Count ] ;
292-
293- var i = 0 ;
294- foreach ( var kvPair in dict )
295- {
296- array [ i ] = kvPair ;
297- i += 1 ;
298- }
299-
300- return array ;
286+ // Faster than the old custom implementation
287+ // 0 count faster, 1 count slower, any higher faster (on my 7800X3D).
288+ return Enumerable . ToArray ( dict ) ;
301289 }
302290
303291 /// <summary>
0 commit comments