[DGD] Comparison of mappings

Felix A. Croes felix at dworkin.nl
Thu Feb 19 16:01:05 CET 2004


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

> > Felix A. Croes wrote:
> > 
> > This is a somewhat odd definition of equal, since it considers
> > ({ 0, 0, 1 }) and ({ 1, 1, 0 }) to be the same.
> > 
>
> Ah yes, oops.  I was only thinking about arrays as sets at the
> time.  So to compare two arrays, you would need to loop round the
> elements then? 

Right.  You might want to use different equal functions, a fast one
to compare sets and a slower one that takes position in the array
into account.


> > 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));
> >     }
> > 
>
> Yup, that'd work :).  Why do I never see these things...

This "fast" version of mapping equality does assume the quick and
dirty array comparison, since I thought that was what you wanted.
You could still do a reasonably fast one with a quick and dirty
comparison of the indices (which are always sets) followed by a
slower one on the values.

It gets a little more tricky if you want to do a "deep" comparison,
on arrays of arrays and the like. :)

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



More information about the DGD mailing list