[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ADT Lib (6)



At 9:18 AM +0100 6/12/98, Michael van Acken wrote:

>Instead of "obj.Hash()" or "o1.LessThan(o2)" I prefer proper
>functions, like "Hash(obj)" or "LessThan(o1, o2)".  The functions can
>be implemented as a procedure variable, or as a type-bound procedure
>of some base class.  See also Tim's posting about a "Compare" class
>some time back: http://www.uni-kl.de/OOC/ooc-list/msg00528.html
>
>The reason for this is simple: An object has no "natural" total
>ordering.  If I have a "RECORD a,b: INTEGER END", then I can sort by
>field `a' or `b' (or both), in ascending or descending order (but not
>both ;-).  Therefore a single _method_ Object.Compare is not flexible
>enough.  An external comparison _function_ gives this kind of
>flexibility, though.
>
>In other words: To insert an object in a hash table, one needs to
>have
>a hash function that can be applied to the object.  The object does
>not need to have a property "hashable".

   Okay, this might have some merit for Oberon-2 after all. However I hope
that those "functions" are then installed into the container through the
constructor procedure? Otherwise you would have to carry them around in
each call. Given that a module for AVL-trees then exports

   TYPE
     Equal = PROCEDURE (o, p: Object): BOOLEAN;
     LessThan = PROCEDURE (o, p: Object): BOOLEAN;

an implementation must ensure that it is passed objects of the "right" type:

   PROCEDURE NudelEqual (o, p: Object): BOOLEAN;
   BEGIN
     WITH o: Nudel DO
       WITH p: Nudel DO
         (* compare them *)
       END;
     END;
   END NudelEqual;

which reduces to just one WITH in the case of using an "Ordered" base
class. Note that Equal() could return FALSE anyway, eg. in the ELSE clauses
of WITH, however LessThan() would have to abort since FALSE is not a valid
default answer in this case.

   One further comment: Since procedure types use structural equivalence
anyway, it would be okay to add procedure types for common operations to
one of the basic modules, say for Equal(), LessThan(), GreaterThan(), and
HashCode().

Just my $0.02...

By(T)e...        /  Department of Information and Computer Science  \
        Peter... \ University of California - Irvine, CA 92697-3425 /