Comparisons.cs
author Dean
Wed, 14 Sep 2016 13:14:33 +0200
changeset 1289 9422ede79073
parent 1238 55b7869062a7
child 1290 44776fed0bd2
permissions -rw-r--r--
Add Equals overload to check unordered lists.
Dean@1238
     1
´╗┐using System.Linq;
Dean@1238
     2
using System;
Dean@1238
     3
using System.Collections.Generic;
Dean@1238
     4
Dean@1238
     5
namespace pEp
Dean@1238
     6
{
Dean@1238
     7
    public static class Comparisons
Dean@1238
     8
    {
Dean@1238
     9
        /// <summary>
Dean@1238
    10
        /// Compares the given values and checks for equality.
Dean@1238
    11
        /// Null and empty cases are handled.
Dean@1238
    12
        /// </summary>
Dean@1238
    13
        /// <param name="value1">The first value to compare.</param>
Dean@1238
    14
        /// <param name="value2">The second value to compare with.</param>
Dean@1238
    15
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1238
    16
        new public static bool Equals(object value1,
Dean@1238
    17
                                      object value2)
Dean@1238
    18
        {
Dean@1238
    19
            return (object.Equals(value1, value2));
Dean@1238
    20
        }
Dean@1238
    21
Dean@1238
    22
        /// <summary>
Dean@1238
    23
        /// Compares the given values and checks for equality.
Dean@1238
    24
        /// Null and empty cases are handled.
Dean@1238
    25
        /// </summary>
Dean@1238
    26
        /// <param name="value1">The first value to compare.</param>
Dean@1238
    27
        /// <param name="value2">The second value to compare with.</param>
Dean@1238
    28
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1238
    29
        public static bool Equals<T>(IEnumerable<T> value1,
Dean@1238
    30
                                     IEnumerable<T> value2)
Dean@1238
    31
        {
Dean@1238
    32
            if ((value1 == null) ||
Dean@1238
    33
                (value2 == null))
Dean@1238
    34
            {
Dean@1238
    35
                if ((value1 != null) ||
Dean@1238
    36
                    (value2 != null))
Dean@1238
    37
                {
Dean@1238
    38
                    // One is null while the other is not
Dean@1238
    39
                    return (false);
Dean@1238
    40
                }
Dean@1238
    41
            }
Dean@1238
    42
            else
Dean@1238
    43
            {
Dean@1238
    44
                return (Enumerable.SequenceEqual(value1, value2));
Dean@1238
    45
            }
Dean@1238
    46
Dean@1238
    47
            return (true);
Dean@1238
    48
        }
Dean@1238
    49
Dean@1289
    50
        /// <summary>
Dean@1289
    51
        /// Compares the given values and checks for equality.
Dean@1289
    52
        /// Null and empty cases are handled.
Dean@1289
    53
        /// </summary>
Dean@1289
    54
        /// <param name="value1">The first value to compare.</param>
Dean@1289
    55
        /// <param name="value2">The second value to compare with.</param>
Dean@1289
    56
        /// <param name="ignoreSequence">Whether to ignore the sequence of items in the list. If true, this
Dean@1289
    57
        /// will only check that the same value occurs an equal number of times in both lists.</param>
Dean@1289
    58
        /// <returns>True if the values are considered equal, otherwise false.</returns>
Dean@1289
    59
        public static bool Equals<T>(IEnumerable<T> value1,
Dean@1289
    60
                                     IEnumerable<T> value2,
Dean@1289
    61
                                     bool ignoreSequence)
Dean@1289
    62
        {
Dean@1289
    63
            long count;
Dean@1289
    64
            bool[] isCounted;
Dean@1289
    65
Dean@1289
    66
            if (ignoreSequence == false)
Dean@1289
    67
            {
Dean@1289
    68
                return (Comparisons.Equals(value1, value2));
Dean@1289
    69
            }
Dean@1289
    70
            else
Dean@1289
    71
            {
Dean@1289
    72
                if ((value1 == null) ||
Dean@1289
    73
                    (value2 == null))
Dean@1289
    74
                {
Dean@1289
    75
                    if ((value1 != null) ||
Dean@1289
    76
                        (value2 != null))
Dean@1289
    77
                    {
Dean@1289
    78
                        // One is null while the other is not
Dean@1289
    79
                        return (false);
Dean@1289
    80
                    }
Dean@1289
    81
                }
Dean@1289
    82
                else if (value1.Count() != value2.Count())
Dean@1289
    83
                {
Dean@1289
    84
                    return (false);
Dean@1289
    85
                }
Dean@1289
    86
                else
Dean@1289
    87
                {
Dean@1289
    88
                    count = 0;
Dean@1289
    89
                    isCounted = new bool[value2.Count()]; // filled with false
Dean@1289
    90
Dean@1289
    91
                    // Compare every element
Dean@1289
    92
                    for (int i = 0; i < value1.Count(); i++)
Dean@1289
    93
                    {
Dean@1289
    94
                        for (int j = 0; j < value2.Count(); j++)
Dean@1289
    95
                        {
Dean@1289
    96
                            if ((value1.ElementAt(i).Equals(value2.ElementAt(j))) &&
Dean@1289
    97
                                (isCounted[j] == false))
Dean@1289
    98
                            {
Dean@1289
    99
                                count++;
Dean@1289
   100
                                isCounted[j] = true;
Dean@1289
   101
                                break;
Dean@1289
   102
                            }
Dean@1289
   103
                        }
Dean@1289
   104
                    }
Dean@1289
   105
Dean@1289
   106
                    if (count != value1.Count())
Dean@1289
   107
                    {
Dean@1289
   108
                        return (false);
Dean@1289
   109
                    }
Dean@1289
   110
                }
Dean@1289
   111
            }
Dean@1289
   112
Dean@1289
   113
            return (true);
Dean@1289
   114
        }
Dean@1238
   115
    }
Dean@1238
   116
}