/Collections/Utilities/Extensions.cs |
@@ -0,0 +1,58 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System.Collections.Generic; |
using System.Linq; |
|
namespace wasSharp.Collections.Utilities |
{ |
public static class Extensions |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Returns true of an enumerable contains more than one element. |
/// </summary> |
/// <typeparam name="T">the type of the enumeration</typeparam> |
/// <param name="e">the enumeration</param> |
/// <returns>true if enumeration contains more than one element</returns> |
/// <remarks>O(2) worst case</remarks> |
public static bool Some<T>(this IEnumerable<T> e) |
{ |
var i = 0; |
using (var iter = e.GetEnumerator()) |
{ |
while (iter.MoveNext()) |
{ |
if (++i > 1) |
return true; |
} |
return false; |
} |
} |
|
/// <summary> |
/// Compares two dictionaries for equality. |
/// </summary> |
/// <typeparam name="TKey">key type</typeparam> |
/// <typeparam name="TValue">value type</typeparam> |
/// <param name="dictionary">dictionary to compare</param> |
/// <param name="otherDictionary">dictionary to compare to</param> |
/// <returns>true if the dictionaries contain the same elements</returns> |
public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, |
IDictionary<TKey, TValue> otherDictionary) |
{ |
return |
(dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals( |
(otherDictionary ?? new Dictionary<TKey, TValue>()).Count) && |
(otherDictionary ?? new Dictionary<TKey, TValue>()) |
.OrderBy(kvp => kvp.Key) |
.SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>()) |
.OrderBy(kvp => kvp.Key)); |
} |
} |
} |