Bluehunter

 

    Hi,

    I need to compare multi-dimensional arrays in a function which takes 2 multi-dim arrays which can be any type(int, double, bool...etc). Comparison is done by Object.Equals() of each value in each index of the arrays.

As a result, Generic and  Multi-Dimensional is what I need to combine. Any idea I compare 1 Dimensional-arrays with the below code of mine:

public static bool AreArraysEqualByVal<T>(IList<T> src, IList<T> dest)

{

if (src == null || dest == null)

throw new ArgumentNullException("source or destination array is null.");

if (src.Count != dest.Count)

{

Debug.WriteLine("Array lengths were not equal.");

return false;

}

for (int i = 0; i < src.Count; i++)

{

T itemSrc = src [ i ];

T itemDest = dest [ i ];

if (!((object)itemSrc).Equals((object)itemDest))

{

Debug.WriteLine("Source item at index " + i + " differs from the destination item.");

Debug.WriteLine("Source item: " + itemSrc.ToString() + ", destination item: " + itemDest.ToString());

return false;

}

}

return true;

}




Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

Jumpin JP

They always say "Divide and Conquer"

So to me all a mulidimensional array is a list of lists. (using your example).

You probably see where I'm going with this already...

One would have to assume that the Z order for each of the lists would be comparable.

Example we have a multidimensional list of lists of cars

MODEL, MAKE, MANUFACTURER

X,Y,Z

MODEL, and MAKE are passed in for the same MANUFACTURER for each src and dst list.

The MANUFACTURER must be the same for each comparison of a set of src and dst lists passed to your routine above.

Iterate through all MANUFACTURERS (Z order) and you've got it.

Just a thought.





Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

micvos

Try this code:

 

static bool CompareArrays(IList l1, IList l2)
        {
            if ((l1 == null) | (l2 == null) | !l1.GetType().Equals(l2.GetType()) | l1.Count != l2.Count)
                return false;
            for (int i = 0; i < l1.Count; ++i)
            {
                if (!l1[i ].GetType().Equals(l2[i ].GetType()))
                    return false;
                if (l1[i ] is IList)
                {
                    if (!CompareArrays((IList)l1[i ], (IList)l2[i ]))
                        return false;
                }
                else if (!l1[i ].Equals(l2[i ]))
                    return false;
            }
            return true;
        } 

You can pass any type of multidimensional array becuase the all implement IList. The method uses recusrsion so the number of dimensions doesn't really matter.






Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

Bluehunter

Your code did not work friend. I sent your method these arrays:

bool result = CompareArrays(new double[ , ] { { 1, 2 }, { 3, 4 }, { 5, 6 } }, new double[ , ] { { 7, 8 }, { 9, 10 }, { 11, 12 } });

and I got this error:

System.ArgumentException : Array was not a one-dimensional array.






Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

Bluehunter

Send a code friend if you can because I may write something wrong related to what you've explained and what I understood from it. Also, other people can see your codeand use it if it works. Thanks for your time and for each word you spent time on writing.






Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

micvos

Then you can change the [] operator with an enumerator:

static bool CompareArrays(IList l1, IList l2)
{
if ((l1 == null) | (l2 == null) | !l1.GetType().Equals(l2.GetType()) | l1.Count != l2.Count)
return false;
IEnumerator enu1 = l1.GetEnumerator();
IEnumerator enu2 = l2.GetEnumerator();
while (enu1.MoveNext()& enu2.MoveNext())
{
if (!enu1.Current.GetType().Equals(enu2.Current.GetType()))
return false;
if (enu1.Current is IList)
{
if (!CompareArrays((IList)enu1.Current, (IList)enu2.Current))
return false;
}
else if (!enu1.Current.Equals(enu2.Current))
return false;
}
return true;
}






Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

Bluehunter

It works, thanks but do you know the reason why it did not work "without" using enumerators I will feel better if I can find it out :) Thanks for your time and good work friend.

Regards,

Bluehunter






Re: Visual C# General Generic Multi-Dimensional Arrays Comparison

micvos

If you define an array as multi dimentional, then the compiler expects multiple values within the [] operator. So if you don't know the number of dimensions in advance, you don't know how to use the operator. There is no generic way to use it. Enumerators on the other hand always work. No matter how much dimensions there are, each item will be returned by the enumerator once and you have a single and simple way of calling each item.