[DGD] Comparison of mappings

Felix A. Croes felix at dworkin.nl
Thu Feb 19 15:25:07 CET 2004


"Colin Ross" <c.ross at cantab.net> wrote:

> Is there an eas (or easier) way of comparing two mappings for
> "equivalence"?  Ie, I want to test that two given mappings
> have the same keys and that the values for those keys are 
> the same.
>
> For arrays, I can do:
>
> int equal;
> equal = (sizeof(array1)==sizeof(array2)) &&
>         !sizeof( array1 - array2 )
>
> which will give 1 if they are equal, and 0 otherwise.

This is a somewhat odd definition of equal, since it considers
({ 0, 0, 1 }) and ({ 1, 1, 0 }) to be the same.


> For mappings though, I think I have to (for example) check
> the sizes are the same, and then loop through the keys
> of one, checking that the value of that key in the other
> mapping agrees, and if they all agree, then the mappings
> are equivalent:
>
> array indices;
> int i, equal;
> equal = 0;
> if ( sizeof(mapping1)==sizeof(mapping2) )
> {
>     equal = 1;
>     indices = map_indices(mapping1);
>     for (i=0; i<sizeof(indices); i++)
>     {
>         if ( mapping1[ indices[i] ] !=
>              mapping2[ indices[i] ] )
>         {
>             equal = 0;
>             break;
>         }
>     }
> }
>
> I was just wondering if there was an easier way that was
> eluding me? 

Suppose that array_equal() works on arrays.  Then for mappings you
can use,

    int map_equal(mapping map1, mapping map2)
    {
	return array_equal(map_indices(map1), map_indices(map2)) &&
	       array_equal(map_values(map1), map_values(map2));
    }

Regards,
Dworkin
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list