Re: Array comparison

From: Nicolai Hansen (nic_at_aub.dk)
Date: 10/07/04

  • Next message: Skybuck Flying: "Re: Array comparison"
    Date: 7 Oct 2004 01:50:06 -0700
    
    

    > Apply the "=" operator to each of the array's elements.

    And if the element is an object, you compare addresses, but if its an
    integer you compare values?

    My reasoning is as follows:

    *You cannot compare arrays. "Array of Byte" and "Array of Byte" can
    not be compared in Pascal; you need to declare them as type.
    *You could then compare your types, but how on earth could you make
    something logical that can compare two types if they can compare only
    array types, and not record types?
    *Next step would be to compare any instance type with another instance
    of the same type.
     -All strings can be compared to eachother (compares the contents of
    the string, but not the reference counts)
     -All TObjects can be compared (shallow comparison, compares only the
    address of the objects)
    *What to do with all other types? Deep or shallow comparison? Deep and
    recursive comparison (fear the cross references!)? Who is to tell me
    how my records should be compared?

    > I think I have to side with Skybuck on this one, and I don't need to
    > tell you how much it pains me to do that.

    :) I must then prove you wrong! ;)

    > > It would then also need to give you with a method of comparing any
    > > kinds of records, or classes. And how do you compare two classes? What
    > > if they contain pointers? What if they contain _untyped_ pointers?
    >
    > The only valid point in that is records. The compiler already knows how
    > to compare everything else, although some of the comparisons (e.g.,
    > objects, pointers) aren't always the most useful. But that the compiler
    > doesn't know how to provide an intelligible comparison of two objects
    > does not explain why it can't compare two like-typed arrays.

    You cannot allow comparisons of certain user-defined types, but not of
    others.
    The fun part here is that IIRC two identical types can be compared
    according to the Pascal ISO.

    > > and not even that can count for untyped pointers.
    >
    > Untyped pointers are very easy to compare. People do it all the time.
    >
    > var
    > a, b: Pointer;
    >
    > if a = b then ...

    Yes, it is easy to compare the value of the pointers. IE compare them
    as if they were integers... But this might not be what was wanted.

    > > The only way in Pascal is to write your own procedures (you actually
    > > do that in C++ also), like function IsEqual(comp1, comp2: TSameType):
    > > boolean - you can overload this as much as you want to make it compare
    > > any kind of types; make an IsEqual(comp1, comp2: Pointer) or
    > > IsEqual(comp1, comp2: TObject) to cover for all objects where you only
    > > want addresses compared...
    >
    > Writing generic procedures to compare static arrays and records was a
    > cool little exercise. Tomorrow I'll tackle how to do a deep comparison
    > on dynamic arrays.
    >
    > Whatever the reasons for disallowing "=" on static arrays and records,
    > technical difficulty is not one of them -- not nowadays, anyway. I'm
    > more inclined to say the reason is lack of customer demand for the feature.

    I never said it was a technical difficulty in comparing arrays; I
    stated that it was plain easy to write your own compare methods. My
    point was that I see reason in it NOT being allowed, due to confusion
    about which types can and can not be compared.


  • Next message: Skybuck Flying: "Re: Array comparison"

    Relevant Pages

    • Re: Array comparison
      ... And how do you compare two classes? ... does not explain why it can't compare two like-typed arrays. ... Free Pascal supports operator overloading. ... more inclined to say the reason is lack of customer demand for the feature. ...
      (alt.comp.lang.borland-delphi)
    • Re: Option Compare Statement
      ... both arrays are always the same type. ... compare text,. ... Your first post regarding this in the vb.controls newsgroup was, ... strings, let's say A and B. Then I want to compare their values. ...
      (microsoft.public.vb.general.discussion)
    • Re: Why cant I compare addresses of template classes?
      ... >compare them. ... For some reason I assumed the OP didn't want to have to ... cast, ... and we have pointers to different sub-parts ...
      (comp.lang.cpp)
    • Re: Similarity searching
      ... a factor of N*log, if the arrays are big you won't in general have to compare to the last element so I don't see the factor of L**2, and I don't see where that C**2 factor is coming from at all. ... There are also sorting algorithms which don't compare each item to every other item, yet still generate a sorted result. ... If electricity comes from electrons, ...
      (comp.lang.fortran)
    • Re: Hashtable Contains() - byte arrays as keys -
      ... > But that would again mean he will have to iterate and compare each entries ... Assuming I understand his problem (that he needs the byte arrays to ... entries of the arrays. ... The hashcode might be cacheable if the byte arrays are not changed once ...
      (microsoft.public.dotnet.languages.csharp)