Comparisons.cs
author Dean
Thu, 15 Sep 2016 12:29:45 +0200
changeset 1290 44776fed0bd2
parent 1289 9422ede79073
child 1638 501e8e24a249
permissions -rw-r--r--
Handle IEnumerable in Comparisons.
     1 ´╗┐using System.Collections;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 
     5 namespace pEp
     6 {
     7     public static class Comparisons
     8     {
     9         /// <summary>
    10         /// Compares the given values and checks for equality.
    11         /// Null and empty cases are handled.
    12         /// </summary>
    13         /// <param name="value1">The first value to compare.</param>
    14         /// <param name="value2">The second value to compare with.</param>
    15         /// <returns>True if the values are considered equal, otherwise false.</returns>
    16         new public static bool Equals(object value1,
    17                                       object value2)
    18         {
    19             if ((value1 is IEnumerable) &&
    20                 (value2 is IEnumerable))
    21             {
    22                 return (Comparisons.Equals((IEnumerable)value1, (IEnumerable)value2));
    23             }
    24             else
    25             {
    26                 return (object.Equals(value1, value2));
    27             }
    28         }
    29 
    30         /// <summary>
    31         /// Compares the given values and checks for equality.
    32         /// Null and empty cases are handled.
    33         /// </summary>
    34         /// <param name="value1">The first value to compare.</param>
    35         /// <param name="value2">The second value to compare with.</param>
    36         /// <returns>True if the values are considered equal, otherwise false.</returns>
    37         public static bool Equals(IEnumerable value1,
    38                                   IEnumerable value2)
    39         {
    40             bool exists1;
    41             bool exists2;
    42             IEnumerator enumerator1;
    43             IEnumerator enumerator2;
    44 
    45             if ((value1 == null) ||
    46                 (value2 == null))
    47             {
    48                 if ((value1 != null) ||
    49                     (value2 != null))
    50                 {
    51                     // One is null while the other is not
    52                     return (false);
    53                 }
    54             }
    55             else
    56             {
    57                 enumerator1 = value1.GetEnumerator();
    58                 enumerator2 = value2.GetEnumerator();
    59 
    60                 try
    61                 {
    62                     exists1 = enumerator1.MoveNext();
    63                     exists2 = enumerator2.MoveNext();
    64 
    65                     while (exists1 && exists2)
    66                     {
    67                         if (Comparisons.Equals(enumerator1.Current, enumerator2.Current) == false)
    68                         {
    69                             return (false);
    70                         }
    71 
    72                         exists1 = enumerator1.MoveNext();
    73                         exists2 = enumerator2.MoveNext();
    74                     }
    75 
    76                     // If one of the enumerators still has a value, the lengths don't match
    77                     if (exists1 || exists2)
    78                     {
    79                         return (false);
    80                     }
    81                 }
    82                 catch
    83                 {
    84                     return (false);
    85                 }
    86             }
    87 
    88             return (true);
    89         }
    90 
    91         /// <summary>
    92         /// Compares the given values and checks for equality.
    93         /// Null and empty cases are handled.
    94         /// </summary>
    95         /// <param name="value1">The first value to compare.</param>
    96         /// <param name="value2">The second value to compare with.</param>
    97         /// <returns>True if the values are considered equal, otherwise false.</returns>
    98         public static bool Equals<T>(IEnumerable<T> value1,
    99                                      IEnumerable<T> value2)
   100         {
   101             if ((value1 == null) ||
   102                 (value2 == null))
   103             {
   104                 if ((value1 != null) ||
   105                     (value2 != null))
   106                 {
   107                     // One is null while the other is not
   108                     return (false);
   109                 }
   110             }
   111             else
   112             {
   113                 return (Enumerable.SequenceEqual(value1, value2));
   114             }
   115 
   116             return (true);
   117         }
   118 
   119         /// <summary>
   120         /// Compares the given values and checks for equality.
   121         /// Null and empty cases are handled.
   122         /// </summary>
   123         /// <param name="value1">The first value to compare.</param>
   124         /// <param name="value2">The second value to compare with.</param>
   125         /// <param name="ignoreSequence">Whether to ignore the sequence of items in the list. If true, this
   126         /// will only check that the same value occurs an equal number of times in both lists.</param>
   127         /// <returns>True if the values are considered equal, otherwise false.</returns>
   128         public static bool Equals<T>(IEnumerable<T> value1,
   129                                      IEnumerable<T> value2,
   130                                      bool ignoreSequence)
   131         {
   132             long count;
   133             bool[] isCounted;
   134 
   135             if (ignoreSequence == false)
   136             {
   137                 return (Comparisons.Equals(value1, value2));
   138             }
   139             else
   140             {
   141                 if ((value1 == null) ||
   142                     (value2 == null))
   143                 {
   144                     if ((value1 != null) ||
   145                         (value2 != null))
   146                     {
   147                         // One is null while the other is not
   148                         return (false);
   149                     }
   150                 }
   151                 else if (value1.Count() != value2.Count())
   152                 {
   153                     return (false);
   154                 }
   155                 else
   156                 {
   157                     count = 0;
   158                     isCounted = new bool[value2.Count()]; // filled with false
   159 
   160                     // Compare every element
   161                     for (int i = 0; i < value1.Count(); i++)
   162                     {
   163                         for (int j = 0; j < value2.Count(); j++)
   164                         {
   165                             if ((value1.ElementAt(i).Equals(value2.ElementAt(j))) &&
   166                                 (isCounted[j] == false))
   167                             {
   168                                 count++;
   169                                 isCounted[j] = true;
   170                                 break;
   171                             }
   172                         }
   173                     }
   174 
   175                     if (count != value1.Count())
   176                     {
   177                         return (false);
   178                     }
   179                 }
   180             }
   181 
   182             return (true);
   183         }
   184     }
   185 }